blob: 3c209440b0cfa9e0c8d20c1b1dc5c7b01d1dce25 [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()))
Fariborz Jahaniand7f00252009-05-08 21:10:00 +000029 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
Fariborz Jahanian80ccaa92009-05-08 20:20:55 +000030 if (result != Compatible) {
31 Diag(Loc, diag::warn_accessor_property_type_mismatch)
32 << property->getDeclName()
33 << GetterMethod->getSelector();
34 Diag(GetterMethod->getLocation(), diag::note_declared_at);
35 return true;
36 }
37 }
38 return false;
39}
40
Steve Naroff8df46022009-02-28 16:59:13 +000041/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000042/// and user declared, in the method definition's AST.
Chris Lattner5261d0c2009-03-28 19:18:32 +000043void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000044 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner5261d0c2009-03-28 19:18:32 +000045 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff3ac43f92008-07-25 17:57:26 +000046
47 // If we don't have a valid method decl, simply return.
48 if (!MDecl)
49 return;
Steve Narofffe9eb6a2007-12-18 01:30:32 +000050
Chris Lattnerc92920c2009-04-19 05:21:20 +000051 CurFunctionNeedsScopeChecking = false;
52
Steve Narofffe9eb6a2007-12-18 01:30:32 +000053 // Allow the rest of sema to find private method decl implementations.
Douglas Gregor5d764842009-01-09 17:18:27 +000054 if (MDecl->isInstanceMethod())
Steve Narofffe9eb6a2007-12-18 01:30:32 +000055 AddInstanceMethodToGlobalPool(MDecl);
56 else
57 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000058
59 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor8acb7272008-12-11 16:49:14 +000060 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000061
62 // Create Decl objects for each parameter, entrring them in the scope for
63 // binding to their use.
Chris Lattner855e51f2007-12-12 07:09:47 +000064
65 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +000066 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +000067
Daniel Dunbareaf91c32008-08-26 06:07:48 +000068 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
69 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000070
Chris Lattner97316c02008-04-10 02:22:51 +000071 // Introduce all of the other parameters into this scope.
Chris Lattner5c6b2c62009-02-20 18:43:26 +000072 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
73 E = MDecl->param_end(); PI != E; ++PI)
74 if ((*PI)->getIdentifier())
75 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000076}
77
Chris Lattner5261d0c2009-03-28 19:18:32 +000078Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +000079ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
80 IdentifierInfo *ClassName, SourceLocation ClassLoc,
81 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +000082 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000083 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000084 assert(ClassName && "Missing class identifier");
85
86 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +000087 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +000088 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +000089 // Maybe we will complain about the shadowed template parameter.
90 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
91 // Just pretend that we didn't see the previous declaration.
92 PrevDecl = 0;
93 }
94
Ted Kremenek42730c52008-01-07 19:49:32 +000095 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000096 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +000097 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +000098 }
99
Ted Kremenek42730c52008-01-07 19:49:32 +0000100 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000101 if (IDecl) {
102 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +0000103 if (!IDecl->isForwardDecl()) {
Chris Lattner9513cfd2009-02-23 22:00:08 +0000104 IDecl->setInvalidDecl();
Chris Lattner271d4c22008-11-24 05:29:24 +0000105 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattner5b250652008-11-23 22:46:27 +0000106 Diag(IDecl->getLocation(), diag::note_previous_definition);
107
Steve Naroff1422a622008-11-18 19:15:30 +0000108 // Return the previous class interface.
109 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +0000110 return DeclPtrTy::make(IDecl);
Steve Naroff1422a622008-11-18 19:15:30 +0000111 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +0000112 IDecl->setLocation(AtInterfaceLoc);
113 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +0000114 }
Chris Lattner5cece462008-07-21 07:06:49 +0000115 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000116 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +0000117 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000118 if (AttrList)
119 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000120
Steve Naroff104956f2009-04-23 15:15:40 +0000121 PushOnScopeChains(IDecl, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000122 }
123
124 if (SuperName) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000125 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000126 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner65cae292008-11-19 08:23:25 +0000127
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000128 ObjCInterfaceDecl *SuperClassDecl =
129 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner7699a532009-02-16 21:33:09 +0000130
131 // Diagnose classes that inherit from deprecated classes.
132 if (SuperClassDecl)
Douglas Gregoraa57e862009-02-18 21:56:37 +0000133 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattner7699a532009-02-16 21:33:09 +0000134
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000135 if (PrevDecl && SuperClassDecl == 0) {
136 // The previous declaration was not a class decl. Check if we have a
137 // typedef. If we do, get the underlying class type.
138 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
139 QualType T = TDecl->getUnderlyingType();
140 if (T->isObjCInterfaceType()) {
141 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
142 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
143 }
144 }
Chris Lattner7699a532009-02-16 21:33:09 +0000145
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000146 // This handles the following case:
147 //
148 // typedef int SuperClass;
149 // @interface MyClass : SuperClass {} @end
150 //
151 if (!SuperClassDecl) {
152 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
153 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
154 }
155 }
Chris Lattner7699a532009-02-16 21:33:09 +0000156
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000157 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
158 if (!SuperClassDecl)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000159 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner65cae292008-11-19 08:23:25 +0000160 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000161 else if (SuperClassDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000162 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000163 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner65cae292008-11-19 08:23:25 +0000164 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000165 }
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000166 IDecl->setSuperClass(SuperClassDecl);
Steve Naroff7c371742008-04-11 19:35:35 +0000167 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000168 IDecl->setLocEnd(SuperLoc);
169 } else { // we have a root class.
170 IDecl->setLocEnd(ClassLoc);
171 }
172
Steve Naroff1422a622008-11-18 19:15:30 +0000173 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000174 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000175 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
176 Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000177 IDecl->setLocEnd(EndProtoLoc);
178 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000179
180 CheckObjCDeclScope(IDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000181 return DeclPtrTy::make(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000182}
183
184/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000185/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000186Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
187 IdentifierInfo *AliasName,
188 SourceLocation AliasLocation,
189 IdentifierInfo *ClassName,
190 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000191 // Look for previous declaration of alias name
Douglas Gregor09be81b2009-02-04 17:27:36 +0000192 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000193 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000194 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000195 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000196 else
Chris Lattner65cae292008-11-19 08:23:25 +0000197 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000198 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000199 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000200 }
201 // Check for class declaration
Douglas Gregor09be81b2009-02-04 17:27:36 +0000202 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000203 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
204 QualType T = TDecl->getUnderlyingType();
205 if (T->isObjCInterfaceType()) {
206 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
207 ClassName = IDecl->getIdentifier();
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000208 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000209 }
210 }
211 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000212 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
213 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000214 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000215 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000216 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000217 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000218 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000219
220 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000221 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000222 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe57c21a2008-04-01 23:04:06 +0000223
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000224 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor54713b62009-04-24 02:57:34 +0000225 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000226
Chris Lattner5261d0c2009-03-28 19:18:32 +0000227 return DeclPtrTy::make(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000228}
229
Steve Narofffe56a632009-03-05 15:22:01 +0000230void Sema::CheckForwardProtocolDeclarationForCircularDependency(
231 IdentifierInfo *PName,
232 SourceLocation &Ploc, SourceLocation PrevLoc,
233 const ObjCList<ObjCProtocolDecl> &PList)
234{
235 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
236 E = PList.end(); I != E; ++I) {
237
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000238 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Narofffe56a632009-03-05 15:22:01 +0000239 if (PDecl->getIdentifier() == PName) {
240 Diag(Ploc, diag::err_protocol_has_circular_dependency);
241 Diag(PrevLoc, diag::note_previous_definition);
242 }
243 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
244 PDecl->getLocation(), PDecl->getReferencedProtocols());
245 }
246 }
247}
248
Chris Lattner5261d0c2009-03-28 19:18:32 +0000249Sema::DeclPtrTy
Chris Lattner2bdedd62008-07-26 04:03:38 +0000250Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
251 IdentifierInfo *ProtocolName,
252 SourceLocation ProtocolLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000253 const DeclPtrTy *ProtoRefs,
Chris Lattner2bdedd62008-07-26 04:03:38 +0000254 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000255 SourceLocation EndProtoLoc,
256 AttributeList *AttrList) {
257 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000258 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000259 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000260 if (PDecl) {
261 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000262 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian68199552009-04-06 23:43:32 +0000263 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000264 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-03-16 01:25:17 +0000265 // Just return the protocol we already had.
266 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +0000267 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000268 }
Steve Narofffe56a632009-03-05 15:22:01 +0000269 ObjCList<ObjCProtocolDecl> PList;
270 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
271 CheckForwardProtocolDeclarationForCircularDependency(
272 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
273 PList.Destroy(Context);
274
Steve Naroff9a9e5212008-08-13 16:39:22 +0000275 // Make sure the cached decl gets a valid start location.
276 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000277 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000278 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000279 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
280 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000281 PushOnScopeChains(PDecl, TUScope);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000282 PDecl->setForwardDecl(false);
Chris Lattner180f7e22008-03-16 01:23:04 +0000283 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000284 if (AttrList)
285 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000286 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000287 /// Check then save referenced protocols.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000288 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000289 PDecl->setLocEnd(EndProtoLoc);
290 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000291
292 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000293 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000294}
295
296/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000297/// issues an error if they are not declared. It returns list of
298/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000299void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000300Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000301 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000302 unsigned NumProtocols,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000303 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000304 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000305 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattner17d50a92008-07-26 03:47:43 +0000306 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000307 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000308 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000309 continue;
310 }
Chris Lattnerfaaad132009-02-14 08:22:25 +0000311
Douglas Gregoraa57e862009-02-18 21:56:37 +0000312 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner17d50a92008-07-26 03:47:43 +0000313
314 // If this is a forward declaration and we are supposed to warn in this
315 // case, do it.
316 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000317 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000318 << ProtocolId[i].first;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000319 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000320 }
321}
322
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000323/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000324/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000325///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000326void
327Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
328 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000329 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000330 ObjCPropertyDecl::PropertyAttributeKind CAttr =
331 Property->getPropertyAttributes();
332 ObjCPropertyDecl::PropertyAttributeKind SAttr =
333 SuperProperty->getPropertyAttributes();
334 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
335 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000336 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000337 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000338 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
339 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000340 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000341 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000342 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
343 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000344 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000345 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000346
347 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
348 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000349 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000350 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000351 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000352 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000353 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000354 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000355 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000356 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff2567c9e2009-03-03 15:43:24 +0000357
358 QualType LHSType =
359 Context.getCanonicalType(SuperProperty->getType());
360 QualType RHSType =
361 Context.getCanonicalType(Property->getType());
362
363 if (!Context.typesAreCompatible(LHSType, RHSType)) {
364 // FIXME: Incorporate this test with typesAreCompatible.
365 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
366 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
367 return;
368 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
369 << Property->getType() << SuperProperty->getType() << inheritedName;
370 }
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000371}
372
373/// ComparePropertiesInBaseAndSuper - This routine compares property
374/// declarations in base and its super class, if any, and issues
375/// diagnostics in a variety of inconsistant situations.
376///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000377void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000378 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
379 if (!SDecl)
380 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000381 // FIXME: O(N^2)
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000382 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
383 E = SDecl->prop_end(Context); S != E; ++S) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000384 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000385 // Does property in super class has declaration in current class?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000386 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
387 E = IDecl->prop_end(Context); I != E; ++I) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000388 ObjCPropertyDecl *PDecl = (*I);
389 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000390 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000391 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000392 }
393 }
394}
395
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000396/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
397/// of properties declared in a protocol and adds them to the list
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000398/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000399void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000400Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000401 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000402 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
403 if (!IDecl) {
404 // Category
405 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
406 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000407 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
408 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000409 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000410 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000411 // Is this property already in category's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000412 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000413 CP != CE; ++CP)
414 if ((*CP)->getIdentifier() == Pr->getIdentifier())
415 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000416 if (CP != CE)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000417 // Property protocol already exist in class. Diagnose any mismatch.
418 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
419 }
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000420 return;
421 }
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000422 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
423 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000424 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000425 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000426 // Is this property already in class's list of properties?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000427 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000428 CP != CE; ++CP)
429 if ((*CP)->getIdentifier() == Pr->getIdentifier())
430 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000431 if (CP != CE)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000432 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000433 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000434 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000435}
436
437/// MergeProtocolPropertiesIntoClass - This routine merges properties
438/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000439/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000440///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000441void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000442 DeclPtrTy MergeItsProtocols) {
443 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000444 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
445
446 if (!IDecl) {
447 // Category
448 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
449 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
450 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
451 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
452 E = MDecl->protocol_end(); P != E; ++P)
453 // Merge properties of category (*P) into IDECL's
454 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
455
456 // Go thru the list of protocols for this category and recursively merge
457 // their properties into this class as well.
458 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
459 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000460 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000461 } else {
462 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
463 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
464 E = MD->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000465 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000466 }
467 return;
468 }
469
Chris Lattner5cece462008-07-21 07:06:49 +0000470 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000471 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
472 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000473 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000474 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
475
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000476 // Go thru the list of protocols for this class and recursively merge
477 // their properties into this class as well.
478 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
479 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000480 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000481 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000482 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
483 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
484 E = MD->protocol_end(); P != E; ++P)
Chris Lattner5261d0c2009-03-28 19:18:32 +0000485 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattner5cece462008-07-21 07:06:49 +0000486 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000487}
488
Fariborz Jahanian38a1acc2009-03-02 19:06:08 +0000489/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000490/// a class method in its extension.
491///
492void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
493 ObjCInterfaceDecl *ID) {
494 if (!ID)
495 return; // Possibly due to previous error
496
497 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000498 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
499 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000500 ObjCMethodDecl *MD = *i;
501 MethodMap[MD->getSelector()] = MD;
502 }
503
504 if (MethodMap.empty())
505 return;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000506 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
507 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000508 ObjCMethodDecl *Method = *i;
509 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
510 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
511 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
512 << Method->getDeclName();
513 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
514 }
515 }
516}
517
Chris Lattnere6dcfc22009-04-12 08:43:13 +0000518/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000519Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +0000520Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000521 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000522 unsigned NumElts,
523 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000524 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000525
526 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000527 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000528 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000529 if (PDecl == 0) { // Not already seen?
530 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
531 IdentList[i].second, Ident);
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000532 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000533 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000534 if (attrList)
535 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000536 Protocols.push_back(PDecl);
537 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000538
539 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000540 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000541 &Protocols[0], Protocols.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000542 CurContext->addDecl(Context, PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000543 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000544 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000545}
546
Chris Lattner5261d0c2009-03-28 19:18:32 +0000547Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +0000548ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
549 IdentifierInfo *ClassName, SourceLocation ClassLoc,
550 IdentifierInfo *CategoryName,
551 SourceLocation CategoryLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000552 const DeclPtrTy *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000553 unsigned NumProtoRefs,
554 SourceLocation EndProtoLoc) {
Chris Lattnere29dc832008-03-16 20:34:23 +0000555 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000556 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
557 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000558 CurContext->addDecl(Context, CDecl);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000559
560 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000561 /// Check that class of this category is already completely declared.
Chris Lattner1fa7f622009-02-16 21:26:43 +0000562 if (!IDecl || IDecl->isForwardDecl()) {
563 CDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000564 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000565 return DeclPtrTy::make(CDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000566 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000567
Chris Lattner1fa7f622009-02-16 21:26:43 +0000568 CDecl->setClassInterface(IDecl);
Chris Lattnerde3fea82009-02-16 21:30:01 +0000569
570 // If the interface is deprecated, warn about it.
Douglas Gregoraa57e862009-02-18 21:56:37 +0000571 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000572
573 /// Check for duplicate interface declaration for this category
574 ObjCCategoryDecl *CDeclChain;
575 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
576 CDeclChain = CDeclChain->getNextClassCategory()) {
577 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
578 Diag(CategoryLoc, diag::warn_dup_category_def)
579 << ClassName << CategoryName;
580 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
581 break;
582 }
583 }
584 if (!CDeclChain)
585 CDecl->insertNextClassCategory();
586
Chris Lattner855e51f2007-12-12 07:09:47 +0000587 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000588 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner45142b92008-07-26 04:07:02 +0000589 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000590 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000591
592 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000593 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000594}
595
596/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000597/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000598/// object.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000599Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000600 SourceLocation AtCatImplLoc,
601 IdentifierInfo *ClassName, SourceLocation ClassLoc,
602 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000603 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000604 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000605 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
606 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000607 /// Check that class of this category is already completely declared.
608 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000609 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000610
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000611 // FIXME: PushOnScopeChains?
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000612 CurContext->addDecl(Context, CDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000613
Chris Lattner855e51f2007-12-12 07:09:47 +0000614 /// TODO: Check that CatName, category name, is not used in another
615 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000616 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000617
618 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000619 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000620}
621
Chris Lattner5261d0c2009-03-28 19:18:32 +0000622Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000623 SourceLocation AtClassImplLoc,
624 IdentifierInfo *ClassName, SourceLocation ClassLoc,
625 IdentifierInfo *SuperClassname,
626 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000627 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000628 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000629 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000630 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000631 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000632 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner9513cfd2009-02-23 22:00:08 +0000633 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +0000634 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000635 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanianf7f09ab2009-04-23 21:49:04 +0000636 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000637 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanianf7f09ab2009-04-23 21:49:04 +0000638 IDecl = 0;
639 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000640 }
641
642 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000643 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000644 if (SuperClassname) {
645 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000646 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000647 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000648 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
649 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000650 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000651 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000652 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000653 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000654 Diag(SuperClassLoc, diag::err_undef_superclass)
655 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000656 else if (IDecl && IDecl->getSuperClass() != SDecl) {
657 // This implementation and its interface do not have the same
658 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000659 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000660 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000661 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000662 }
663 }
664 }
665
666 if (!IDecl) {
667 // Legacy case of @implementation with no corresponding @interface.
668 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000669
670 // FIXME: Do we support attributes on the @implementation? If so
671 // we should copy them over.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000672 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
673 ClassName, ClassLoc, false, true);
Chris Lattner855e51f2007-12-12 07:09:47 +0000674 IDecl->setSuperClass(SDecl);
675 IDecl->setLocEnd(ClassLoc);
Douglas Gregor453e11d2009-04-24 00:16:12 +0000676
677 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbardbc8af72009-04-21 21:41:56 +0000678 } else {
679 // Mark the interface as being completed, even if it was just as
680 // @class ....;
681 // declaration; the user cannot reopen it.
682 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +0000683 }
684
Ted Kremenek42730c52008-01-07 19:49:32 +0000685 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000686 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000687 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000688
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000689 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000690 return DeclPtrTy::make(IMPDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000691
Chris Lattner855e51f2007-12-12 07:09:47 +0000692 // Check that there is no duplicate implementation of this class.
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000693 if (LookupObjCImplementation(ClassName))
Chris Lattner1b6de332008-03-16 20:53:07 +0000694 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000695 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000696 else // add it to the list.
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000697 PushOnScopeChains(IMPDecl, TUScope);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000698 return DeclPtrTy::make(IMPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000699}
700
Ted Kremenek42730c52008-01-07 19:49:32 +0000701void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
702 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000703 SourceLocation RBrace) {
704 assert(ImpDecl && "missing implementation decl");
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000705 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000706 if (!IDecl)
707 return;
708 /// Check case of non-existing @interface decl.
709 /// (legacy objective-c @implementation decl without an @interface decl).
710 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff7333b492009-04-20 20:09:33 +0000711 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000712 IDecl->setIVarList(ivars, numIvars, Context);
713 IDecl->setLocEnd(RBrace);
Chris Lattner855e51f2007-12-12 07:09:47 +0000714 return;
715 }
716 // If implementation has empty ivar list, just return.
717 if (numIvars == 0)
718 return;
719
720 assert(ivars && "missing @implementation ivars");
721
722 // Check interface's Ivar list against those in the implementation.
723 // names and types must match.
724 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000725 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000726 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000727 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
728 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000729 ObjCIvarDecl* ImplIvar = ivars[j++];
730 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000731 assert (ImplIvar && "missing implementation ivar");
732 assert (ClsIvar && "missing class ivar");
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000733
734 // First, make sure the types match.
Chris Lattnerb5457862008-07-27 00:05:05 +0000735 if (Context.getCanonicalType(ImplIvar->getType()) !=
736 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000737 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000738 << ImplIvar->getIdentifier()
739 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000740 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000741 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
742 Expr *ImplBitWidth = ImplIvar->getBitWidth();
743 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman5255e7a2009-04-26 19:19:15 +0000744 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
745 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000746 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
747 << ImplIvar->getIdentifier();
748 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
749 }
750 }
751 // Make sure the names are identical.
752 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000753 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000754 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000755 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000756 }
757 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000758 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000759
760 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000761 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000762 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000763 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000764}
765
Steve Naroffb4f48512008-02-10 21:38:56 +0000766void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
767 bool &IncompleteImpl) {
768 if (!IncompleteImpl) {
769 Diag(ImpLoc, diag::warn_incomplete_impl);
770 IncompleteImpl = true;
771 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000772 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000773}
774
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000775void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
776 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnerb95aea02009-04-11 18:01:59 +0000777 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian6e83c922009-05-14 23:52:54 +0000778 ImpMethodDecl->getResultType()) &&
779 !QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
780 ImpMethodDecl->getResultType())) {
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000781 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
782 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
783 << ImpMethodDecl->getResultType();
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000784 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
785 }
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000786
787 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
788 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
789 IM != EM; ++IM, ++IF) {
Fariborz Jahanian6e83c922009-05-14 23:52:54 +0000790 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()) ||
791 QualifiedIdConformsQualifiedId((*IF)->getType(), (*IM)->getType()))
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000792 continue;
793
794 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
795 << ImpMethodDecl->getDeclName() << (*IF)->getType()
796 << (*IM)->getType();
Chris Lattner6e48be22009-04-11 20:14:49 +0000797 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000798 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000799}
800
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000801/// isPropertyReadonly - Return true if property is readonly, by searching
802/// for the property in the class and in its categories and implementations
803///
804bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000805 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000806 // by far the most common case.
807 if (!PDecl->isReadOnly())
808 return false;
809 // Even if property is ready only, if interface has a user defined setter,
810 // it is not considered read only.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000811 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000812 return false;
813
814 // Main class has the property as 'readonly'. Must search
815 // through the category list to see if the property's
816 // attribute has been over-ridden to 'readwrite'.
817 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
818 Category; Category = Category->getNextClassCategory()) {
819 // Even if property is ready only, if a category has a user defined setter,
820 // it is not considered read only.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000821 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000822 return false;
823 ObjCPropertyDecl *P =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000824 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000825 if (P && !P->isReadOnly())
826 return false;
827 }
828
829 // Also, check for definition of a setter method in the implementation if
830 // all else failed.
831 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
832 if (ObjCImplementationDecl *IMD =
833 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000834 if (IMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000835 return false;
836 }
837 else if (ObjCCategoryImplDecl *CIMD =
838 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Douglas Gregorcd19b572009-04-23 01:02:12 +0000839 if (CIMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000840 return false;
841 }
842 }
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000843 // Lastly, look through the implementation (if one is in scope).
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000844 if (ObjCImplementationDecl *ImpDecl
845 = LookupObjCImplementation(IDecl->getIdentifier()))
Douglas Gregorcd19b572009-04-23 01:02:12 +0000846 if (ImpDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000847 return false;
Fariborz Jahanianbfdf9db2009-04-06 16:59:10 +0000848 // If all fails, look at the super class.
849 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
850 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000851 return true;
852}
853
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000854/// FIXME: Type hierarchies in Objective-C can be deep. We could most
855/// likely improve the efficiency of selector lookups and type
856/// checking by associating with each protocol / interface / category
857/// the flattened instance tables. If we used an immutable set to keep
858/// the table then it wouldn't add significant memory cost and it
859/// would be handy for lookups.
860
Steve Naroffb268d2a2008-02-08 22:06:17 +0000861/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000862/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000863void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
864 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000865 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000866 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000867 const llvm::DenseSet<Selector> &ClsMap,
868 ObjCInterfaceDecl *IDecl) {
869 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
870
871 // If a method lookup fails locally we still need to look and see if
872 // the method was implemented by a base class or an inherited
873 // protocol. This lookup is slow, but occurs rarely in correct code
874 // and otherwise would terminate in a warning.
875
Chris Lattner855e51f2007-12-12 07:09:47 +0000876 // check unimplemented instance methods.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000877 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
878 E = PDecl->instmeth_end(Context); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000879 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000880 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000881 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000882 (!Super ||
883 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000884 // Ugly, but necessary. Method declared in protcol might have
885 // have been synthesized due to a property declared in the class which
886 // uses the protocol.
887 ObjCMethodDecl *MethodInClass =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000888 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanian30ecfb72009-04-03 21:51:32 +0000889 if (!MethodInClass || !MethodInClass->isSynthesized())
890 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
891 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000892 }
893 // check unimplemented class methods
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000894 for (ObjCProtocolDecl::classmeth_iterator
895 I = PDecl->classmeth_begin(Context),
896 E = PDecl->classmeth_end(Context);
897 I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000898 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000899 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
900 !ClsMap.count(method->getSelector()) &&
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000901 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000902 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000903 }
Chris Lattner0be08822008-07-21 21:32:27 +0000904 // Check on this protocols's referenced protocols, recursively.
905 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
906 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000907 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000908}
909
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +0000910/// MatchAllMethodDeclarations - Check methods declaraed in interface or
911/// or protocol against those declared in their implementations.
912///
913void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
914 const llvm::DenseSet<Selector> &ClsMap,
915 llvm::DenseSet<Selector> &InsMapSeen,
916 llvm::DenseSet<Selector> &ClsMapSeen,
917 ObjCImplDecl* IMPDecl,
918 ObjCContainerDecl* CDecl,
919 bool &IncompleteImpl,
920 bool ImmediateClass)
921{
922 // Check and see if instance methods in class interface have been
923 // implemented in the implementation class. If so, their types match.
924 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
925 E = CDecl->instmeth_end(Context); I != E; ++I) {
926 if (InsMapSeen.count((*I)->getSelector()))
927 continue;
928 InsMapSeen.insert((*I)->getSelector());
929 if (!(*I)->isSynthesized() &&
930 !InsMap.count((*I)->getSelector())) {
931 if (ImmediateClass)
932 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
933 continue;
934 }
935 else {
936 ObjCMethodDecl *ImpMethodDecl =
937 IMPDecl->getInstanceMethod(Context, (*I)->getSelector());
938 ObjCMethodDecl *IntfMethodDecl =
939 CDecl->getInstanceMethod(Context, (*I)->getSelector());
940 assert(IntfMethodDecl &&
941 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
942 // ImpMethodDecl may be null as in a @dynamic property.
943 if (ImpMethodDecl)
944 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
945 }
946 }
947
948 // Check and see if class methods in class interface have been
949 // implemented in the implementation class. If so, their types match.
950 for (ObjCInterfaceDecl::classmeth_iterator
951 I = CDecl->classmeth_begin(Context),
952 E = CDecl->classmeth_end(Context);
953 I != E; ++I) {
954 if (ClsMapSeen.count((*I)->getSelector()))
955 continue;
956 ClsMapSeen.insert((*I)->getSelector());
957 if (!ClsMap.count((*I)->getSelector())) {
958 if (ImmediateClass)
959 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
960 }
961 else {
962 ObjCMethodDecl *ImpMethodDecl =
963 IMPDecl->getClassMethod(Context, (*I)->getSelector());
964 ObjCMethodDecl *IntfMethodDecl =
965 CDecl->getClassMethod(Context, (*I)->getSelector());
966 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
967 }
968 }
969 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
970 // Check for any implementation of a methods declared in protocol.
971 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
972 E = I->protocol_end(); PI != E; ++PI)
973 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
974 IMPDecl,
975 (*PI), IncompleteImpl, false);
976 if (I->getSuperClass())
977 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
978 IMPDecl,
979 I->getSuperClass(), IncompleteImpl, false);
980 }
981}
982
Chris Lattner7cc13bf2009-03-01 00:56:52 +0000983void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
984 ObjCContainerDecl* CDecl,
985 bool IncompleteImpl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000986 llvm::DenseSet<Selector> InsMap;
987 // Check and see if instance methods in class interface have been
988 // implemented in the implementation class.
Douglas Gregorcd19b572009-04-23 01:02:12 +0000989 for (ObjCImplementationDecl::instmeth_iterator
990 I = IMPDecl->instmeth_begin(Context),
991 E = IMPDecl->instmeth_end(Context); I != E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +0000992 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000993
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +0000994 // Check and see if properties declared in the interface have either 1)
995 // an implementation or 2) there is a @synthesize/@dynamic implementation
996 // of the property in the @implementation.
997 if (isa<ObjCInterfaceDecl>(CDecl))
998 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context),
999 E = CDecl->prop_end(Context); P != E; ++P) {
1000 ObjCPropertyDecl *Prop = (*P);
1001 if (Prop->isInvalidDecl())
1002 continue;
1003 ObjCPropertyImplDecl *PI = 0;
1004 // Is there a matching propery synthesize/dynamic?
Douglas Gregorcd19b572009-04-23 01:02:12 +00001005 for (ObjCImplDecl::propimpl_iterator
1006 I = IMPDecl->propimpl_begin(Context),
1007 EI = IMPDecl->propimpl_end(Context); I != EI; ++I)
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001008 if ((*I)->getPropertyDecl() == Prop) {
1009 PI = (*I);
1010 break;
1011 }
1012 if (PI)
1013 continue;
1014 if (!InsMap.count(Prop->getGetterName())) {
1015 Diag(Prop->getLocation(),
1016 diag::warn_setter_getter_impl_required)
1017 << Prop->getDeclName() << Prop->getGetterName();
1018 Diag(IMPDecl->getLocation(),
1019 diag::note_property_impl_required);
1020 }
1021
1022 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1023 Diag(Prop->getLocation(),
1024 diag::warn_setter_getter_impl_required)
1025 << Prop->getDeclName() << Prop->getSetterName();
1026 Diag(IMPDecl->getLocation(),
1027 diag::note_property_impl_required);
1028 }
1029 }
1030
Chris Lattner855e51f2007-12-12 07:09:47 +00001031 llvm::DenseSet<Selector> ClsMap;
Douglas Gregorcd19b572009-04-23 01:02:12 +00001032 for (ObjCImplementationDecl::classmeth_iterator
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001033 I = IMPDecl->classmeth_begin(Context),
1034 E = IMPDecl->classmeth_end(Context); I != E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +00001035 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +00001036
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001037 // Check for type conflict of methods declared in a class/protocol and
1038 // its implementation; if any.
1039 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1040 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1041 IMPDecl, CDecl,
1042 IncompleteImpl, true);
Chris Lattner855e51f2007-12-12 07:09:47 +00001043
1044 // Check the protocol list for unimplemented methods in the @implementation
1045 // class.
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001046 // Check and see if class methods in class interface have been
1047 // implemented in the implementation class.
1048
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001049 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001050 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001051 E = I->protocol_end(); PI != E; ++PI)
1052 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1053 InsMap, ClsMap, I);
1054 // Check class extensions (unnamed categories)
1055 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1056 Categories; Categories = Categories->getNextClassCategory()) {
1057 if (!Categories->getIdentifier()) {
1058 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1059 break;
1060 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +00001061 }
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001062 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1063 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1064 E = C->protocol_end(); PI != E; ++PI)
1065 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1066 InsMap, ClsMap, C->getClassInterface());
1067 } else
1068 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner855e51f2007-12-12 07:09:47 +00001069}
1070
1071/// ActOnForwardClassDeclaration -
Chris Lattner5261d0c2009-03-28 19:18:32 +00001072Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +00001073Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner28400082009-02-16 19:25:52 +00001074 IdentifierInfo **IdentList,
1075 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001076 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +00001077
1078 for (unsigned i = 0; i != NumElts; ++i) {
1079 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +00001080 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +00001081 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +00001082 // Maybe we will complain about the shadowed template parameter.
1083 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1084 // Just pretend that we didn't see the previous declaration.
1085 PrevDecl = 0;
1086 }
1087
Ted Kremenek42730c52008-01-07 19:49:32 +00001088 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +00001089 // GCC apparently allows the following idiom:
1090 //
1091 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1092 // @class XCElementToggler;
1093 //
1094 // FIXME: Make an extension?
1095 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1096 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +00001097 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +00001098 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +00001099 }
Fariborz Jahanian4c16d762009-05-07 21:49:26 +00001100 else if (TDD) {
1101 // a forward class declaration matching a typedef name of a class
1102 // refers to the underlying class.
1103 if (ObjCInterfaceType * OI =
1104 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1105 PrevDecl = OI->getDecl();
1106 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001107 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001108 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001109 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001110 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1111 IdentList[i], SourceLocation(), true);
Steve Naroff74b916a2009-04-23 16:00:56 +00001112 PushOnScopeChains(IDecl, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +00001113 }
1114
1115 Interfaces.push_back(IDecl);
1116 }
1117
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001118 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001119 &Interfaces[0],
1120 Interfaces.size());
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001121 CurContext->addDecl(Context, CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001122 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001123 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001124}
1125
1126
1127/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1128/// returns true, or false, accordingly.
1129/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +00001130bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +00001131 const ObjCMethodDecl *PrevMethod,
1132 bool matchBasedOnSizeAndAlignment) {
1133 QualType T1 = Context.getCanonicalType(Method->getResultType());
1134 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1135
1136 if (T1 != T2) {
1137 // The result types are different.
1138 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +00001139 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +00001140 // Incomplete types don't have a size and alignment.
1141 if (T1->isIncompleteType() || T2->isIncompleteType())
1142 return false;
1143 // Check is based on size and alignment.
1144 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1145 return false;
1146 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001147
1148 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1149 E = Method->param_end();
1150 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1151
1152 for (; ParamI != E; ++ParamI, ++PrevI) {
1153 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1154 T1 = Context.getCanonicalType((*ParamI)->getType());
1155 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +00001156 if (T1 != T2) {
1157 // The result types are different.
1158 if (!matchBasedOnSizeAndAlignment)
1159 return false;
1160 // Incomplete types don't have a size and alignment.
1161 if (T1->isIncompleteType() || T2->isIncompleteType())
1162 return false;
1163 // Check is based on size and alignment.
1164 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1165 return false;
1166 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001167 }
1168 return true;
1169}
1170
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001171/// \brief Read the contents of the instance and factory method pools
1172/// for a given selector from external storage.
1173///
1174/// This routine should only be called once, when neither the instance
1175/// nor the factory method pool has an entry for this selector.
1176Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
1177 bool isInstance) {
1178 assert(ExternalSource && "We need an external AST source");
1179 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1180 "Selector data already loaded into the instance method pool");
1181 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1182 "Selector data already loaded into the factory method pool");
1183
1184 // Read the method list from the external source.
1185 std::pair<ObjCMethodList, ObjCMethodList> Methods
1186 = ExternalSource->ReadMethodPool(Sel);
1187
1188 if (isInstance) {
1189 if (Methods.second.Method)
1190 FactoryMethodPool[Sel] = Methods.second;
1191 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
1192 }
1193
1194 if (Methods.first.Method)
1195 InstanceMethodPool[Sel] = Methods.first;
1196
1197 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1198}
1199
Ted Kremenek42730c52008-01-07 19:49:32 +00001200void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001201 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1202 = InstanceMethodPool.find(Method->getSelector());
1203 if (Pos == InstanceMethodPool.end()) {
1204 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1205 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1206 else
1207 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1208 ObjCMethodList())).first;
1209 }
1210
1211 ObjCMethodList &Entry = Pos->second;
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001212 if (Entry.Method == 0) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001213 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001214 Entry.Method = Method;
1215 Entry.Next = 0;
1216 return;
Chris Lattner855e51f2007-12-12 07:09:47 +00001217 }
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001218
1219 // We've seen a method with this name, see if we have already seen this type
1220 // signature.
1221 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1222 if (MatchTwoMethodDeclarations(Method, List->Method))
1223 return;
1224
1225 // We have a new signature for an existing method - add it.
1226 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1227 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001228}
1229
Steve Naroffec7e0882008-10-21 10:50:19 +00001230// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001231ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1232 SourceRange R) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001233 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1234 = InstanceMethodPool.find(Sel);
Douglas Gregor89f2c8b2009-04-24 22:23:41 +00001235 if (Pos == InstanceMethodPool.end()) {
1236 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001237 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1238 else
1239 return 0;
1240 }
1241
1242 ObjCMethodList &MethList = Pos->second;
Steve Naroffb91afca2008-10-21 10:37:50 +00001243 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001244
1245 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001246 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1247 // This checks if the methods differ by size & alignment.
1248 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1249 issueWarning = true;
1250 }
1251 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001252 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001253 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001254 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001255 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001256 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001257 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001258 }
1259 return MethList.Method;
1260}
1261
Ted Kremenek42730c52008-01-07 19:49:32 +00001262void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001263 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1264 = FactoryMethodPool.find(Method->getSelector());
1265 if (Pos == FactoryMethodPool.end()) {
1266 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1267 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1268 else
1269 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1270 ObjCMethodList())).first;
1271 }
1272
1273 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner855e51f2007-12-12 07:09:47 +00001274 if (!FirstMethod.Method) {
1275 // Haven't seen a method with this selector name yet - add it.
1276 FirstMethod.Method = Method;
1277 FirstMethod.Next = 0;
1278 } else {
1279 // We've seen a method with this name, now check the type signature(s).
1280 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1281
Ted Kremenek42730c52008-01-07 19:49:32 +00001282 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001283 Next = Next->Next)
1284 match = MatchTwoMethodDeclarations(Method, Next->Method);
1285
1286 if (!match) {
1287 // We have a new signature for an existing method - add it.
1288 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001289 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001290 FirstMethod.Next = OMI;
1291 }
1292 }
1293}
1294
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001295ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
1296 SourceRange R) {
1297 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1298 = FactoryMethodPool.find(Sel);
1299 if (Pos == FactoryMethodPool.end()) {
1300 if (ExternalSource && !InstanceMethodPool.count(Sel))
1301 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1302 else
1303 return 0;
1304 }
1305
1306 ObjCMethodList &MethList = Pos->second;
1307 bool issueWarning = false;
1308
1309 if (MethList.Method && MethList.Next) {
1310 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1311 // This checks if the methods differ by size & alignment.
1312 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1313 issueWarning = true;
1314 }
1315 if (issueWarning && (MethList.Method && MethList.Next)) {
1316 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1317 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1318 << MethList.Method->getSourceRange();
1319 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1320 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1321 << Next->Method->getSourceRange();
1322 }
1323 return MethList.Method;
1324}
1325
Steve Naroffab63fd62009-01-08 17:28:14 +00001326/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1327/// have the property type and issue diagnostics if they don't.
1328/// Also synthesize a getter/setter method if none exist (and update the
1329/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1330/// methods is the "right" thing to do.
1331void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1332 ObjCContainerDecl *CD) {
1333 ObjCMethodDecl *GetterMethod, *SetterMethod;
1334
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001335 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1336 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Fariborz Jahanian80ccaa92009-05-08 20:20:55 +00001337 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1338 property->getLocation());
1339
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001340 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001341 if (Context.getCanonicalType(SetterMethod->getResultType())
1342 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001343 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001344 if (SetterMethod->param_size() != 1 ||
1345 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001346 Diag(property->getLocation(),
Fariborz Jahaniana996bb02009-05-08 19:36:34 +00001347 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001348 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001349 << SetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001350 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1351 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001352 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001353
1354 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001355 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001356 // FIXME: The synthesized property we set here is misleading. We
1357 // almost always synthesize these methods unless the user explicitly
1358 // provided prototypes (which is odd, but allowed). Sema should be
1359 // typechecking that the declarations jive in that situation (which
1360 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001361 if (!GetterMethod) {
1362 // No instance method of same name as property getter name was found.
1363 // Declare a getter method and add it to the list of methods
1364 // for this class.
1365 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1366 property->getLocation(), property->getGetterName(),
1367 property->getType(), CD, true, false, true,
1368 (property->getPropertyImplementation() ==
1369 ObjCPropertyDecl::Optional) ?
1370 ObjCMethodDecl::Optional :
1371 ObjCMethodDecl::Required);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001372 CD->addDecl(Context, GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001373 } else
1374 // A user declared getter will be synthesize when @synthesize of
1375 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001376 GetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001377 property->setGetterMethodDecl(GetterMethod);
1378
1379 // Skip setter if property is read-only.
1380 if (!property->isReadOnly()) {
1381 // Find the default setter and if one not found, add one.
1382 if (!SetterMethod) {
1383 // No instance method of same name as property setter name was found.
1384 // Declare a setter method and add it to the list of methods
1385 // for this class.
1386 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1387 property->getLocation(),
1388 property->getSetterName(),
1389 Context.VoidTy, CD, true, false, true,
1390 (property->getPropertyImplementation() ==
1391 ObjCPropertyDecl::Optional) ?
1392 ObjCMethodDecl::Optional :
1393 ObjCMethodDecl::Required);
1394 // Invent the arguments for the setter. We don't bother making a
1395 // nice name for the argument.
1396 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattner6e48be22009-04-11 20:14:49 +00001397 property->getLocation(),
Steve Naroff5492c1b2009-01-08 20:15:03 +00001398 property->getIdentifier(),
1399 property->getType(),
1400 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001401 0);
Steve Naroff79ea0e02009-04-20 15:06:07 +00001402 SetterMethod->setMethodParams(Context, &Argument, 1);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001403 CD->addDecl(Context, SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001404 } else
1405 // A user declared setter will be synthesize when @synthesize of
1406 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001407 SetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001408 property->setSetterMethodDecl(SetterMethod);
1409 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001410 // Add any synthesized methods to the global pool. This allows us to
1411 // handle the following, which is supported by GCC (and part of the design).
1412 //
1413 // @interface Foo
1414 // @property double bar;
1415 // @end
1416 //
1417 // void thisIsUnfortunate() {
1418 // id foo;
1419 // double bar = [foo bar];
1420 // }
1421 //
Douglas Gregord1675382009-01-09 19:42:16 +00001422 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001423 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001424 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001425 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001426}
1427
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001428// Note: For class/category implemenations, allMethods/allProperties is
1429// always null.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001430void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1431 DeclPtrTy *allMethods, unsigned allNum,
1432 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattnera17991f2009-03-29 16:50:03 +00001433 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001434 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner855e51f2007-12-12 07:09:47 +00001435
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001436 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1437 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001438 // should be true.
1439 if (!ClassDecl)
1440 return;
1441
Chris Lattner855e51f2007-12-12 07:09:47 +00001442 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001443 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1444 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001445 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001446
Steve Naroffab63fd62009-01-08 17:28:14 +00001447 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001448
1449 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1450 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1451 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1452
Chris Lattner855e51f2007-12-12 07:09:47 +00001453 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001454 ObjCMethodDecl *Method =
Chris Lattner5261d0c2009-03-28 19:18:32 +00001455 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner855e51f2007-12-12 07:09:47 +00001456
1457 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001458 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001459 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001460 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001461 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1462 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001463 if ((isInterfaceDeclKind && PrevMethod && !match)
1464 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001465 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001466 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001467 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001468 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001469 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001470 InsMap[Method->getSelector()] = Method;
1471 /// The following allows us to typecheck messages to "id".
1472 AddInstanceMethodToGlobalPool(Method);
1473 }
1474 }
1475 else {
1476 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001477 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001478 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1479 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001480 if ((isInterfaceDeclKind && PrevMethod && !match)
1481 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001482 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001483 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001484 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001485 } else {
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001486 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001487 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001488 /// The following allows us to typecheck messages to "Class".
1489 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001490 }
1491 }
1492 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001493 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001494 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001495 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001496 ComparePropertiesInBaseAndSuper(I);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001497 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff451f83c2009-01-09 15:36:25 +00001498 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001499 // Categories are used to extend the class by declaring new methods.
1500 // By the same token, they are also used to add new properties. No
1501 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001502
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001503 // Merge protocol properties into category
Chris Lattner5261d0c2009-03-28 19:18:32 +00001504 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianffb68822009-03-02 19:05:07 +00001505 if (C->getIdentifier() == 0)
1506 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +00001507 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001508 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1509 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1510 // user-defined setter/getter. It also synthesizes setter/getter methods
1511 // and adds them to the DeclContext and global method pools.
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001512 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1513 E = CDecl->prop_end(Context);
1514 I != E; ++I)
Chris Lattner22063402009-02-16 18:32:47 +00001515 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001516 CDecl->setAtEndLoc(AtEndLoc);
1517 }
1518 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001519 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001520 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001521 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001522 } else if (ObjCCategoryImplDecl* CatImplClass =
1523 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001524 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001525
Chris Lattner855e51f2007-12-12 07:09:47 +00001526 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001527 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001528 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001529 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001530 Categories; Categories = Categories->getNextClassCategory()) {
1531 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001532 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner855e51f2007-12-12 07:09:47 +00001533 break;
1534 }
1535 }
1536 }
1537 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001538 if (isInterfaceDeclKind) {
1539 // Reject invalid vardecls.
1540 for (unsigned i = 0; i != tuvNum; i++) {
1541 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1542 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1543 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar644c15e2009-04-14 02:25:56 +00001544 if (!VDecl->hasExternalStorage())
Steve Naroff03d55572009-04-13 17:58:46 +00001545 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianca277322009-03-21 18:06:45 +00001546 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001547 }
Fariborz Jahanian2c7de6d2009-03-18 22:33:24 +00001548 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001549}
1550
1551
1552/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1553/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001554static Decl::ObjCDeclQualifier
1555CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1556 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1557 if (PQTVal & ObjCDeclSpec::DQ_In)
1558 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1559 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1560 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1561 if (PQTVal & ObjCDeclSpec::DQ_Out)
1562 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1563 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1564 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1565 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1566 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1567 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1568 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001569
1570 return ret;
1571}
1572
Chris Lattner5261d0c2009-03-28 19:18:32 +00001573Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner855e51f2007-12-12 07:09:47 +00001574 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001575 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001576 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001577 Selector Sel,
1578 // optional arguments. The number of types/arguments is obtained
1579 // from the Sel.getNumArgs().
Chris Lattner612a2f72009-04-11 18:57:04 +00001580 ObjCArgInfo *ArgInfo,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001581 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001582 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1583 bool isVariadic) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001584 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff70f16242008-02-29 21:48:07 +00001585
1586 // Make sure we can establish a context for the method.
1587 if (!ClassDecl) {
1588 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001589 return DeclPtrTy();
Steve Naroff70f16242008-02-29 21:48:07 +00001590 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001591 QualType resultDeclType;
1592
Steve Naroffa442ad92009-02-20 22:59:16 +00001593 if (ReturnType) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001594 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffa442ad92009-02-20 22:59:16 +00001595
1596 // Methods cannot return interface types. All ObjC objects are
1597 // passed by reference.
1598 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner76210732009-04-11 19:08:56 +00001599 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1600 << 0 << resultDeclType;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001601 return DeclPtrTy();
Steve Naroffa442ad92009-02-20 22:59:16 +00001602 }
1603 } else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001604 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001605
Chris Lattner114add62008-03-16 00:49:28 +00001606 ObjCMethodDecl* ObjCMethod =
1607 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner3afb2df2009-03-29 04:30:19 +00001608 cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001609 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001610 false,
Chris Lattner114add62008-03-16 00:49:28 +00001611 MethodDeclKind == tok::objc_optional ?
1612 ObjCMethodDecl::Optional :
1613 ObjCMethodDecl::Required);
1614
Chris Lattnereee57c02008-04-04 06:12:32 +00001615 llvm::SmallVector<ParmVarDecl*, 16> Params;
1616
Chris Lattner4f0496b2009-04-11 19:42:43 +00001617 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner76210732009-04-11 19:08:56 +00001618 QualType ArgType, UnpromotedArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001619
Chris Lattner612a2f72009-04-11 18:57:04 +00001620 if (ArgInfo[i].Type == 0) {
Chris Lattner76210732009-04-11 19:08:56 +00001621 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattner612a2f72009-04-11 18:57:04 +00001622 } else {
Chris Lattner76210732009-04-11 19:08:56 +00001623 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001624 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner761b72f2009-04-11 19:34:56 +00001625 ArgType = adjustParameterType(ArgType);
Chris Lattner612a2f72009-04-11 18:57:04 +00001626 }
1627
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001628 ParmVarDecl* Param;
Chris Lattner76210732009-04-11 19:08:56 +00001629 if (ArgType == UnpromotedArgType)
Chris Lattner4f0496b2009-04-11 19:42:43 +00001630 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001631 ArgInfo[i].Name, ArgType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001632 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001633 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001634 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner4f0496b2009-04-11 19:42:43 +00001635 ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001636 ArgInfo[i].Name, ArgType,
1637 UnpromotedArgType,
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001638 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001639
Chris Lattner761b72f2009-04-11 19:34:56 +00001640 if (ArgType->isObjCInterfaceType()) {
1641 Diag(ArgInfo[i].NameLoc,
1642 diag::err_object_cannot_be_passed_returned_by_value)
1643 << 1 << ArgType;
1644 Param->setInvalidDecl();
1645 }
1646
Chris Lattnereee57c02008-04-04 06:12:32 +00001647 Param->setObjCDeclQualifier(
Chris Lattner612a2f72009-04-11 18:57:04 +00001648 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattner761b72f2009-04-11 19:34:56 +00001649
1650 // Apply the attributes to the parameter.
1651 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1652
Chris Lattnereee57c02008-04-04 06:12:32 +00001653 Params.push_back(Param);
1654 }
1655
Steve Naroff79ea0e02009-04-20 15:06:07 +00001656 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs());
Ted Kremenek42730c52008-01-07 19:49:32 +00001657 ObjCMethod->setObjCDeclQualifier(
1658 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1659 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001660
1661 if (AttrList)
1662 ProcessDeclAttributeList(ObjCMethod, AttrList);
Ted Kremenek2b0e6da2009-04-30 18:41:06 +00001663
Chris Lattner855e51f2007-12-12 07:09:47 +00001664 // For implementations (which can be very "coarse grain"), we add the
1665 // method now. This allows the AST to implement lookup methods that work
1666 // incrementally (without waiting until we parse the @end). It also allows
1667 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001668 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001669 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001670 if (MethodType == tok::minus) {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001671 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel);
1672 ImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001673 } else {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001674 PrevMethod = ImpDecl->getClassMethod(Context, Sel);
1675 ImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001676 }
Fariborz Jahanian060f7072009-05-12 21:36:23 +00001677 if (AttrList)
1678 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner855e51f2007-12-12 07:09:47 +00001679 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001680 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001681 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001682 if (MethodType == tok::minus) {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001683 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel);
1684 CatImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001685 } else {
Douglas Gregorcd19b572009-04-23 01:02:12 +00001686 PrevMethod = CatImpDecl->getClassMethod(Context, Sel);
1687 CatImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001688 }
Fariborz Jahanian060f7072009-05-12 21:36:23 +00001689 if (AttrList)
1690 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner855e51f2007-12-12 07:09:47 +00001691 }
1692 if (PrevMethod) {
1693 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001694 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001695 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001696 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001697 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001698 return DeclPtrTy::make(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001699}
1700
Daniel Dunbar540ff472008-09-23 21:53:23 +00001701void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1702 SourceLocation Loc,
1703 unsigned &Attributes) {
1704 // FIXME: Improve the reported location.
1705
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001706 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001707 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001708 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1709 ObjCDeclSpec::DQ_PR_assign |
1710 ObjCDeclSpec::DQ_PR_copy |
1711 ObjCDeclSpec::DQ_PR_retain))) {
1712 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1713 "readwrite" :
1714 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1715 "assign" :
1716 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1717 "copy" : "retain";
1718
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001719 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001720 diag::err_objc_property_attr_mutually_exclusive :
1721 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001722 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001723 }
1724
1725 // Check for copy or retain on non-object types.
1726 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1727 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001728 Diag(Loc, diag::err_objc_property_requires_object)
1729 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001730 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1731 }
1732
1733 // Check for more than one of { assign, copy, retain }.
1734 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1735 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001736 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1737 << "assign" << "copy";
1738 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001739 }
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 << "assign" << "retain";
1743 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001744 }
1745 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1746 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001747 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1748 << "copy" << "retain";
1749 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001750 }
1751 }
1752
1753 // Warn if user supplied no assignment attribute, property is
1754 // readwrite, and this is an object type.
1755 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1756 ObjCDeclSpec::DQ_PR_retain)) &&
1757 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1758 Context.isObjCObjectPointerType(PropertyTy)) {
1759 // Skip this warning in gc-only mode.
1760 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1761 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1762
1763 // If non-gc code warn that this is likely inappropriate.
1764 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1765 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1766
1767 // FIXME: Implement warning dependent on NSCopying being
1768 // implemented. See also:
1769 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1770 // (please trim this list while you are at it).
1771 }
Mike Stump2f567c62009-05-07 23:06:50 +00001772
1773 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1774 && getLangOptions().getGCMode() == LangOptions::GCOnly
1775 && PropertyTy->isBlockPointerType())
1776 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001777}
1778
Chris Lattner5261d0c2009-03-28 19:18:32 +00001779Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1780 FieldDeclarator &FD,
1781 ObjCDeclSpec &ODS,
1782 Selector GetterSel,
1783 Selector SetterSel,
1784 DeclPtrTy ClassCategory,
1785 bool *isOverridingProperty,
1786 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001787 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001788 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1789 // default is readwrite!
1790 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1791 // property is defaulted to 'assign' if it is readwrite and is
1792 // not retain or copy
1793 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1794 (isReadWrite &&
1795 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1796 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1797 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001798 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001799 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar540ff472008-09-23 21:53:23 +00001800 // May modify Attributes.
1801 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001802 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1803 if (!CDecl->getIdentifier()) {
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001804 // This is a continuation class. property requires special
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001805 // handling.
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001806 if ((CCPrimary = CDecl->getClassInterface())) {
1807 // Find the property in continuation class's primary class only.
1808 ObjCPropertyDecl *PIDecl = 0;
1809 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001810 for (ObjCInterfaceDecl::prop_iterator
1811 I = CCPrimary->prop_begin(Context),
1812 E = CCPrimary->prop_end(Context);
1813 I != E; ++I)
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001814 if ((*I)->getIdentifier() == PropertyId) {
1815 PIDecl = *I;
1816 break;
1817 }
1818
1819 if (PIDecl) {
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001820 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001821 // with continuation class's readwrite property attribute!
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001822 unsigned PIkind = PIDecl->getPropertyAttributes();
1823 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001824 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001825 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1826 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001827 PIDecl->makeitReadWriteAttribute();
1828 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1829 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1830 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1831 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1832 PIDecl->setSetterName(SetterSel);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001833 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001834 else
1835 Diag(AtLoc, diag::err_use_continuation_class)
1836 << CCPrimary->getDeclName();
1837 *isOverridingProperty = true;
Fariborz Jahaniane6faa992009-04-15 19:19:03 +00001838 // Make sure setter decl is synthesized, and added to primary
1839 // class's list.
1840 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001841 return DeclPtrTy();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001842 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001843 // No matching property found in the primary class. Just fall thru
1844 // and add property to continuation class's primary class.
1845 ClassDecl = CCPrimary;
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001846 } else {
Chris Lattnera5d47712009-02-20 21:38:52 +00001847 Diag(CDecl->getLocation(), diag::err_continuation_class);
1848 *isOverridingProperty = true;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001849 return DeclPtrTy();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001850 }
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001851 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001852
Steve Naroffdcf1e842009-01-11 12:47:58 +00001853 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1854 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattner6e48be22009-04-11 20:14:49 +00001855 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1856 FD.D.getIdentifierLoc(),
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001857 FD.D.getIdentifier(), T);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001858 DC->addDecl(Context, PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001859
Chris Lattner6e48be22009-04-11 20:14:49 +00001860 if (T->isArrayType() || T->isFunctionType()) {
1861 Diag(AtLoc, diag::err_property_type) << T;
1862 PDecl->setInvalidDecl();
1863 }
1864
Chris Lattner22063402009-02-16 18:32:47 +00001865 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001866
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001867 // Regardless of setter/getter attribute, we save the default getter/setter
1868 // selector names in anticipation of declaration of setter/getter methods.
1869 PDecl->setGetterName(GetterSel);
1870 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001871
Daniel Dunbar540ff472008-09-23 21:53:23 +00001872 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001873 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001874
Daniel Dunbar540ff472008-09-23 21:53:23 +00001875 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001876 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001877
Daniel Dunbar540ff472008-09-23 21:53:23 +00001878 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001879 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001880
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001881 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001882 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001883
Daniel Dunbar540ff472008-09-23 21:53:23 +00001884 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001885 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001886
Daniel Dunbar540ff472008-09-23 21:53:23 +00001887 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001888 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001889
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001890 if (isAssign)
1891 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1892
Daniel Dunbar540ff472008-09-23 21:53:23 +00001893 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001894 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001895
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001896 if (MethodImplKind == tok::objc_required)
1897 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1898 else if (MethodImplKind == tok::objc_optional)
1899 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001900 // A case of continuation class adding a new property in the class. This
1901 // is not what it was meant for. However, gcc supports it and so should we.
1902 // Make sure setter/getters are declared here.
1903 if (CCPrimary)
1904 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001905
Chris Lattner5261d0c2009-03-28 19:18:32 +00001906 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001907}
1908
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001909/// ActOnPropertyImplDecl - This routine performs semantic checks and
1910/// builds the AST node for a property implementation declaration; declared
1911/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001912///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001913Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1914 SourceLocation PropertyLoc,
1915 bool Synthesize,
1916 DeclPtrTy ClassCatImpDecl,
1917 IdentifierInfo *PropertyId,
1918 IdentifierInfo *PropertyIvar) {
1919 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001920 // Make sure we have a context for the property implementation declaration.
1921 if (!ClassImpDecl) {
1922 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001923 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001924 }
1925 ObjCPropertyDecl *property = 0;
1926 ObjCInterfaceDecl* IDecl = 0;
1927 // Find the class or category class where this property must have
1928 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001929 ObjCImplementationDecl *IC = 0;
1930 ObjCCategoryImplDecl* CatImplClass = 0;
1931 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001932 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001933 // We always synthesize an interface for an implementation
1934 // without an interface decl. So, IDecl is always non-zero.
1935 assert(IDecl &&
1936 "ActOnPropertyImplDecl - @implementation without @interface");
1937
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001938 // Look for this property declaration in the @implementation's @interface
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001939 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001940 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001941 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001942 return DeclPtrTy();
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001943 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001944 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001945 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001946 if (Synthesize) {
1947 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001948 return DeclPtrTy();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001949 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001950 IDecl = CatImplClass->getClassInterface();
1951 if (!IDecl) {
1952 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001953 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001954 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001955 ObjCCategoryDecl *Category =
1956 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1957
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001958 // If category for this implementation not found, it is an error which
1959 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001960 if (!Category)
Chris Lattner5261d0c2009-03-28 19:18:32 +00001961 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001962 // Look for this property declaration in @implementation's category
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001963 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001964 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001965 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001966 << Category->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001967 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001968 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001969 } else {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001970 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001971 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001972 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001973 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001974 // Check that we have a valid, previously declared ivar for @synthesize
1975 if (Synthesize) {
1976 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001977 if (!PropertyIvar)
1978 PropertyIvar = PropertyId;
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00001979 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001980 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001981 ObjCInterfaceDecl *ClassDeclared;
1982 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001983 if (!Ivar) {
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001984 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1985 PropertyIvar, PropType,
1986 ObjCIvarDecl::Public,
1987 (Expr *)0);
1988 property->setPropertyIvarDecl(Ivar);
1989 if (!getLangOptions().ObjCNonFragileABI)
Steve Naroff92aeef32009-03-03 22:09:41 +00001990 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001991 // Note! I deliberately want it to fall thru so, we have a
1992 // a property implementation and to avoid future warnings.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001993 }
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001994 else if (getLangOptions().ObjCNonFragileABI &&
Fariborz Jahanian76389e42009-04-29 21:45:02 +00001995 ClassDeclared != IDecl) {
Fariborz Jahanianc181f5a2009-04-30 21:39:24 +00001996 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00001997 << property->getDeclName() << Ivar->getDeclName()
1998 << ClassDeclared->getDeclName();
1999 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2000 << Ivar << Ivar->getNameAsCString();
2001 // Note! I deliberately want it to fall thru so more errors are caught.
2002 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00002003 QualType IvarType = Context.getCanonicalType(Ivar->getType());
2004
Steve Naroff631e3922008-09-30 00:24:17 +00002005 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00002006 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00002007 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00002008 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00002009 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002010 // Note! I deliberately want it to fall thru so, we have a
2011 // a property implementation and to avoid future warnings.
Steve Naroffc32e45c2008-09-30 10:07:56 +00002012 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00002013
2014 // FIXME! Rules for properties are somewhat different that those
2015 // for assignments. Use a new routine to consolidate all cases;
2016 // specifically for property redeclarations as well as for ivars.
2017 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2018 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
2019 if (lhsType != rhsType &&
2020 lhsType->isArithmeticType()) {
2021 Diag(PropertyLoc, diag::error_property_ivar_type)
2022 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002023 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00002024 }
2025 // __weak is explicit. So it works on Canonical type.
Fariborz Jahaniandc150fb2009-04-07 21:25:06 +00002026 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2027 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002028 Diag(PropertyLoc, diag::error_weak_property)
2029 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002030 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00002031 }
2032 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian18411912009-04-10 22:42:54 +00002033 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2034 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002035 Diag(PropertyLoc, diag::error_strong_property)
2036 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002037 // Fall thru - see previous comment
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00002038 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00002039 }
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002040 } else if (PropertyIvar)
2041 // @dynamic
2042 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002043 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002044 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00002045 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2046 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002047 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00002048 ObjCPropertyImplDecl::Synthesize
2049 : ObjCPropertyImplDecl::Dynamic),
2050 Ivar);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002051 if (IC) {
2052 if (Synthesize)
2053 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00002054 IC->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002055 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2056 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2057 << PropertyIvar;
2058 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2059 }
2060
Douglas Gregorcd19b572009-04-23 01:02:12 +00002061 if (ObjCPropertyImplDecl *PPIDecl
2062 = IC->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002063 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2064 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002065 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00002066 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00002067 IC->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002068 }
2069 else {
2070 if (Synthesize)
2071 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00002072 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002073 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2074 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2075 << PropertyIvar;
2076 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2077 }
2078
2079 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregorcd19b572009-04-23 01:02:12 +00002080 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002081 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2082 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002083 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00002084 }
Douglas Gregorcd19b572009-04-23 01:02:12 +00002085 CatImplClass->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002086 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002087
Chris Lattner5261d0c2009-03-28 19:18:32 +00002088 return DeclPtrTy::make(PIDecl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002089}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00002090
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002091bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00002092 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00002093 return false;
2094
2095 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2096 D->setInvalidDecl();
2097
2098 return true;
2099}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002100
2101/// Collect the instance variables declared in an Objective-C object. Used in
2102/// the creation of structures from objects using the @defs directive.
2103/// FIXME: This should be consolidated with CollectObjCIvars as it is also
2104/// part of the AST generation logic of @defs.
2105static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
2106 ASTContext& Ctx,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002107 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002108 if (Class->getSuperClass())
2109 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
2110
2111 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002112 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
2113 E = Class->ivar_end(); I != E; ++I) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002114 ObjCIvarDecl* ID = *I;
Chris Lattner5261d0c2009-03-28 19:18:32 +00002115 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
2116 ID->getIdentifier(), ID->getType(),
2117 ID->getBitWidth());
2118 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002119 }
2120}
2121
2122/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2123/// instance variables of ClassName into Decls.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002124void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002125 IdentifierInfo *ClassName,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002126 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002127 // Check that ClassName is a valid class
2128 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2129 if (!Class) {
2130 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2131 return;
2132 }
Fariborz Jahanian51ba3b82009-04-21 20:28:41 +00002133 if (LangOpts.ObjCNonFragileABI) {
2134 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2135 return;
2136 }
2137
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002138 // Collect the instance variables
Chris Lattner5261d0c2009-03-28 19:18:32 +00002139 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002140
2141 // Introduce all of these fields into the appropriate scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002142 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002143 D != Decls.end(); ++D) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002144 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002145 if (getLangOptions().CPlusPlus)
2146 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002147 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002148 Record->addDecl(Context, FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002149 }
2150}
2151