blob: 51651ab4bba4c439b05c8d6170906975f7aebcd8 [file] [log] [blame]
Chris Lattnerda463fe2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerda463fe2007-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 Gregorc78d3462009-04-24 21:10:55 +000015#include "clang/Sema/ExternalSemaSource.h"
Steve Naroff157599f2009-03-03 14:49:36 +000016#include "clang/AST/Expr.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclObjC.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000020using namespace clang;
21
Fariborz Jahanianfe9e3942009-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;
Steve Naroff79d12152009-07-16 15:41:00 +000028 if (property->getType()->isObjCObjectPointerType())
Fariborz Jahanian3e68a1f2009-05-08 21:10:00 +000029 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +000030 if (result != Compatible) {
Mike Stump11289f42009-09-09 15:08:12 +000031 Diag(Loc, diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +000032 << property->getDeclName()
33 << GetterMethod->getSelector();
34 Diag(GetterMethod->getLocation(), diag::note_declared_at);
35 return true;
36 }
37 }
38 return false;
39}
40
Steve Naroff70f41d62009-02-28 16:59:13 +000041/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattnerda463fe2007-12-12 07:09:47 +000042/// and user declared, in the method definition's AST.
Chris Lattner83f095c2009-03-28 19:18:32 +000043void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis853fbea2008-06-28 06:07:14 +000044 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner83f095c2009-03-28 19:18:32 +000045 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump11289f42009-09-09 15:08:12 +000046
Steve Naroff542cd5d2008-07-25 17:57:26 +000047 // If we don't have a valid method decl, simply return.
48 if (!MDecl)
49 return;
Steve Naroff1d2538c2007-12-18 01:30:32 +000050
Chris Lattner9fecd742009-04-19 05:21:20 +000051 CurFunctionNeedsScopeChecking = false;
52
Steve Naroff1d2538c2007-12-18 01:30:32 +000053 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorffca3a22009-01-09 17:18:27 +000054 if (MDecl->isInstanceMethod())
Steve Naroff1d2538c2007-12-18 01:30:32 +000055 AddInstanceMethodToGlobalPool(MDecl);
56 else
57 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump11289f42009-09-09 15:08:12 +000058
Chris Lattnerda463fe2007-12-12 07:09:47 +000059 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor91f84212008-12-11 16:49:14 +000060 PushDeclContext(FnBodyScope, MDecl);
Chris Lattnerda463fe2007-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 Lattnerda463fe2007-12-12 07:09:47 +000064
65 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +000066 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump11289f42009-09-09 15:08:12 +000067
Daniel Dunbar279d1cc2008-08-26 06:07:48 +000068 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
69 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +000070
Chris Lattner58258242008-04-10 02:22:51 +000071 // Introduce all of the other parameters into this scope.
Chris Lattnera4997152009-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 Lattnerda463fe2007-12-12 07:09:47 +000076}
77
Chris Lattner83f095c2009-03-28 19:18:32 +000078Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +000079ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
80 IdentifierInfo *ClassName, SourceLocation ClassLoc,
81 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +000082 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +000083 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattnerda463fe2007-12-12 07:09:47 +000084 assert(ClassName && "Missing class identifier");
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattnerda463fe2007-12-12 07:09:47 +000086 // Check for another declaration kind with the same name.
Douglas Gregor2ada0482009-02-04 17:27:36 +000087 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor5daeee22008-12-08 18:40:42 +000088 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-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 Kremenek1b0ea822008-01-07 19:49:32 +000095 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +000096 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +000097 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +000098 }
Mike Stump11289f42009-09-09 15:08:12 +000099
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000100 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000101 if (IDecl) {
102 // Class already seen. Is it a forward declaration?
Steve Naroff119f60e2008-11-18 19:15:30 +0000103 if (!IDecl->isForwardDecl()) {
Chris Lattnerd13b8b52009-02-23 22:00:08 +0000104 IDecl->setInvalidDecl();
Chris Lattnerf3d3fae2008-11-24 05:29:24 +0000105 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnere6447ef2008-11-23 22:46:27 +0000106 Diag(IDecl->getLocation(), diag::note_previous_definition);
107
Steve Naroff119f60e2008-11-18 19:15:30 +0000108 // Return the previous class interface.
109 // FIXME: don't leak the objects passed in!
Chris Lattner83f095c2009-03-28 19:18:32 +0000110 return DeclPtrTy::make(IDecl);
Steve Naroff119f60e2008-11-18 19:15:30 +0000111 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000112 IDecl->setLocation(AtInterfaceLoc);
113 IDecl->setForwardDecl(false);
Steve Naroffe0064d22009-09-11 00:12:01 +0000114 IDecl->setClassLoc(ClassLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000115 }
Chris Lattnerca1e8482008-07-21 07:06:49 +0000116 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000117 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff5a4611c2008-04-11 19:35:35 +0000118 ClassName, ClassLoc);
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000119 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000120 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +0000121
Steve Naroff3c301dc2009-04-23 15:15:40 +0000122 PushOnScopeChains(IDecl, TUScope);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000123 }
Mike Stump11289f42009-09-09 15:08:12 +0000124
Chris Lattnerda463fe2007-12-12 07:09:47 +0000125 if (SuperName) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000126 // Check if a different kind of symbol declared in this scope.
Douglas Gregored8f2882009-01-30 01:04:22 +0000127 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000128 if (PrevDecl == IDecl) {
129 Diag(SuperLoc, diag::err_recursive_superclass)
130 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
131 IDecl->setLocEnd(ClassLoc);
Mike Stump12b8ce12009-08-04 21:02:39 +0000132 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000133 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000134 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000135
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000136 // Diagnose classes that inherit from deprecated classes.
137 if (SuperClassDecl)
138 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000139
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000140 if (PrevDecl && SuperClassDecl == 0) {
141 // The previous declaration was not a class decl. Check if we have a
142 // typedef. If we do, get the underlying class type.
143 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
144 QualType T = TDecl->getUnderlyingType();
145 if (T->isObjCInterfaceType()) {
John McCall9dd450b2009-09-21 23:43:11 +0000146 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000147 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
148 }
149 }
Mike Stump11289f42009-09-09 15:08:12 +0000150
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000151 // This handles the following case:
152 //
153 // typedef int SuperClass;
154 // @interface MyClass : SuperClass {} @end
155 //
156 if (!SuperClassDecl) {
157 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
158 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff189d41f2009-02-04 17:14:05 +0000159 }
160 }
Mike Stump11289f42009-09-09 15:08:12 +0000161
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000162 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
163 if (!SuperClassDecl)
164 Diag(SuperLoc, diag::err_undef_superclass)
165 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
166 else if (SuperClassDecl->isForwardDecl())
167 Diag(SuperLoc, diag::err_undef_superclass)
168 << SuperClassDecl->getDeclName() << ClassName
169 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000170 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000171 IDecl->setSuperClass(SuperClassDecl);
172 IDecl->setSuperClassLoc(SuperLoc);
173 IDecl->setLocEnd(SuperLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000174 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000175 } else { // we have a root class.
176 IDecl->setLocEnd(ClassLoc);
177 }
Mike Stump11289f42009-09-09 15:08:12 +0000178
Steve Naroff119f60e2008-11-18 19:15:30 +0000179 /// Check then save referenced protocols.
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000180 if (NumProtoRefs) {
Chris Lattner22298722009-02-20 21:35:13 +0000181 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
182 Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000183 IDecl->setLocEnd(EndProtoLoc);
184 }
Mike Stump11289f42009-09-09 15:08:12 +0000185
Anders Carlssona6b508a2008-11-04 16:57:32 +0000186 CheckObjCDeclScope(IDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000187 return DeclPtrTy::make(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000188}
189
190/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000191/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner83f095c2009-03-28 19:18:32 +0000192Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000193 IdentifierInfo *AliasName,
Chris Lattner83f095c2009-03-28 19:18:32 +0000194 SourceLocation AliasLocation,
195 IdentifierInfo *ClassName,
196 SourceLocation ClassLocation) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000197 // Look for previous declaration of alias name
Douglas Gregor2ada0482009-02-04 17:27:36 +0000198 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000199 if (ADecl) {
Chris Lattnerd0685032008-11-23 23:20:13 +0000200 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattnerda463fe2007-12-12 07:09:47 +0000201 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerd0685032008-11-23 23:20:13 +0000202 else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000203 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerd0685032008-11-23 23:20:13 +0000204 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000205 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000206 }
207 // Check for class declaration
Douglas Gregor2ada0482009-02-04 17:27:36 +0000208 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000209 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
210 QualType T = TDecl->getUnderlyingType();
211 if (T->isObjCInterfaceType()) {
John McCall9dd450b2009-09-21 23:43:11 +0000212 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000213 ClassName = IDecl->getIdentifier();
Douglas Gregored8f2882009-01-30 01:04:22 +0000214 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000215 }
216 }
217 }
Chris Lattner219b3e92008-03-16 21:17:37 +0000218 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
219 if (CDecl == 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000220 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner219b3e92008-03-16 21:17:37 +0000221 if (CDeclU)
Chris Lattnerd0685032008-11-23 23:20:13 +0000222 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000223 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000224 }
Mike Stump11289f42009-09-09 15:08:12 +0000225
Chris Lattner219b3e92008-03-16 21:17:37 +0000226 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump11289f42009-09-09 15:08:12 +0000227 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000228 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000229
Anders Carlssona6b508a2008-11-04 16:57:32 +0000230 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor38feed82009-04-24 02:57:34 +0000231 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000232
Chris Lattner83f095c2009-03-28 19:18:32 +0000233 return DeclPtrTy::make(AliasDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000234}
235
Steve Naroff41d09ad2009-03-05 15:22:01 +0000236void Sema::CheckForwardProtocolDeclarationForCircularDependency(
237 IdentifierInfo *PName,
238 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000239 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000240 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
241 E = PList.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000242
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000243 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000244 if (PDecl->getIdentifier() == PName) {
245 Diag(Ploc, diag::err_protocol_has_circular_dependency);
246 Diag(PrevLoc, diag::note_previous_definition);
247 }
Mike Stump11289f42009-09-09 15:08:12 +0000248 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff41d09ad2009-03-05 15:22:01 +0000249 PDecl->getLocation(), PDecl->getReferencedProtocols());
250 }
251 }
252}
253
Chris Lattner83f095c2009-03-28 19:18:32 +0000254Sema::DeclPtrTy
Chris Lattner3bbae002008-07-26 04:03:38 +0000255Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
256 IdentifierInfo *ProtocolName,
257 SourceLocation ProtocolLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000258 const DeclPtrTy *ProtoRefs,
Chris Lattner3bbae002008-07-26 04:03:38 +0000259 unsigned NumProtoRefs,
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000260 SourceLocation EndProtoLoc,
261 AttributeList *AttrList) {
262 // FIXME: Deal with AttrList.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000263 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000264 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000265 if (PDecl) {
266 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner5074f8f2008-03-16 01:25:17 +0000267 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian54d569c2009-04-06 23:43:32 +0000268 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnere6447ef2008-11-23 22:46:27 +0000269 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000270 // Just return the protocol we already had.
271 // FIXME: don't leak the objects passed in!
Chris Lattner83f095c2009-03-28 19:18:32 +0000272 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000273 }
Steve Naroff41d09ad2009-03-05 15:22:01 +0000274 ObjCList<ObjCProtocolDecl> PList;
Mike Stump11289f42009-09-09 15:08:12 +0000275 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff41d09ad2009-03-05 15:22:01 +0000276 CheckForwardProtocolDeclarationForCircularDependency(
277 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
278 PList.Destroy(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000279
Steve Naroffeb03dac2008-08-13 16:39:22 +0000280 // Make sure the cached decl gets a valid start location.
281 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000282 PDecl->setForwardDecl(false);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000283 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000284 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000285 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000286 PushOnScopeChains(PDecl, TUScope);
Chris Lattneracc04a92008-03-16 20:19:15 +0000287 PDecl->setForwardDecl(false);
Chris Lattnerf87ca0a2008-03-16 01:23:04 +0000288 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000289 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000290 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000291 if (NumProtoRefs) {
Chris Lattneracc04a92008-03-16 20:19:15 +0000292 /// Check then save referenced protocols.
Chris Lattner22298722009-02-20 21:35:13 +0000293 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000294 PDecl->setLocEnd(EndProtoLoc);
295 }
Mike Stump11289f42009-09-09 15:08:12 +0000296
297 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000298 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000299}
300
301/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000302/// issues an error if they are not declared. It returns list of
303/// protocol declarations in its 'Protocols' argument.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000304void
Chris Lattner3bbae002008-07-26 04:03:38 +0000305Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000306 const IdentifierLocPair *ProtocolId,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000307 unsigned NumProtocols,
Chris Lattner83f095c2009-03-28 19:18:32 +0000308 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000309 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000310 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000311 if (!PDecl) {
Chris Lattner3b054132008-11-19 05:08:23 +0000312 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000313 << ProtocolId[i].first;
Chris Lattner9c1842b2008-07-26 03:47:43 +0000314 continue;
315 }
Mike Stump11289f42009-09-09 15:08:12 +0000316
Douglas Gregor171c45a2009-02-18 21:56:37 +0000317 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000318
319 // If this is a forward declaration and we are supposed to warn in this
320 // case, do it.
321 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner3b054132008-11-19 05:08:23 +0000322 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000323 << ProtocolId[i].first;
Chris Lattner83f095c2009-03-28 19:18:32 +0000324 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattnerda463fe2007-12-12 07:09:47 +0000325 }
326}
327
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000328/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar4684f372008-08-27 05:40:03 +0000329/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000330///
Fariborz Jahanian7cf18862008-05-01 00:03:38 +0000331void
Mike Stump11289f42009-09-09 15:08:12 +0000332Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian7cf18862008-05-01 00:03:38 +0000333 ObjCPropertyDecl *SuperProperty,
Chris Lattner86d7d912008-11-24 03:54:41 +0000334 const IdentifierInfo *inheritedName) {
Mike Stump11289f42009-09-09 15:08:12 +0000335 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000336 Property->getPropertyAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000337 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000338 SuperProperty->getPropertyAttributes();
339 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
340 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner3b054132008-11-19 05:08:23 +0000341 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner86d7d912008-11-24 03:54:41 +0000342 << Property->getDeclName() << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000343 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
344 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner377d1f82008-11-18 22:52:51 +0000345 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000346 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000347 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
348 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner377d1f82008-11-18 22:52:51 +0000349 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000350 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump11289f42009-09-09 15:08:12 +0000351
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000352 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
353 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner377d1f82008-11-18 22:52:51 +0000354 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000355 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000356 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner377d1f82008-11-18 22:52:51 +0000357 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump11289f42009-09-09 15:08:12 +0000358 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000359 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner377d1f82008-11-18 22:52:51 +0000360 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000361 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff3e1181e2009-03-03 15:43:24 +0000362
Mike Stump11289f42009-09-09 15:08:12 +0000363 QualType LHSType =
Steve Naroff3e1181e2009-03-03 15:43:24 +0000364 Context.getCanonicalType(SuperProperty->getType());
Mike Stump11289f42009-09-09 15:08:12 +0000365 QualType RHSType =
Steve Naroff3e1181e2009-03-03 15:43:24 +0000366 Context.getCanonicalType(Property->getType());
Mike Stump11289f42009-09-09 15:08:12 +0000367
Steve Naroff3e1181e2009-03-03 15:43:24 +0000368 if (!Context.typesAreCompatible(LHSType, RHSType)) {
369 // FIXME: Incorporate this test with typesAreCompatible.
370 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff8e6aee52009-07-23 01:01:38 +0000371 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff3e1181e2009-03-03 15:43:24 +0000372 return;
373 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
374 << Property->getType() << SuperProperty->getType() << inheritedName;
375 }
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000376}
377
378/// ComparePropertiesInBaseAndSuper - This routine compares property
379/// declarations in base and its super class, if any, and issues
380/// diagnostics in a variety of inconsistant situations.
381///
Chris Lattner9018ca82009-02-16 21:26:43 +0000382void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000383 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
384 if (!SDecl)
385 return;
Daniel Dunbar4684f372008-08-27 05:40:03 +0000386 // FIXME: O(N^2)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000387 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
388 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian7cf18862008-05-01 00:03:38 +0000389 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000390 // Does property in super class has declaration in current class?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000391 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
392 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000393 ObjCPropertyDecl *PDecl = (*I);
394 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump11289f42009-09-09 15:08:12 +0000395 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner86d7d912008-11-24 03:54:41 +0000396 SDecl->getIdentifier());
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000397 }
398 }
399}
400
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000401/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
402/// of properties declared in a protocol and adds them to the list
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000403/// of properties for current class/category if it is not there already.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000404void
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000405Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner86d7d912008-11-24 03:54:41 +0000406 ObjCProtocolDecl *PDecl) {
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000407 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
408 if (!IDecl) {
409 // Category
410 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
411 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000412 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
413 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000414 ObjCPropertyDecl *Pr = (*P);
Steve Naroffb3a87982009-01-09 15:36:25 +0000415 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000416 // Is this property already in category's list of properties?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000417 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000418 if ((*CP)->getIdentifier() == Pr->getIdentifier())
419 break;
Fariborz Jahanian519976c2009-01-09 21:04:52 +0000420 if (CP != CE)
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000421 // Property protocol already exist in class. Diagnose any mismatch.
422 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
423 }
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000424 return;
425 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000426 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
427 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000428 ObjCPropertyDecl *Pr = (*P);
Steve Naroffb3a87982009-01-09 15:36:25 +0000429 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000430 // Is this property already in class's list of properties?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000431 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000432 if ((*CP)->getIdentifier() == Pr->getIdentifier())
433 break;
Fariborz Jahanian519976c2009-01-09 21:04:52 +0000434 if (CP != CE)
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000435 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner86d7d912008-11-24 03:54:41 +0000436 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000437 }
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000438}
439
440/// MergeProtocolPropertiesIntoClass - This routine merges properties
441/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000442/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000443///
Chris Lattner9018ca82009-02-16 21:26:43 +0000444void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000445 DeclPtrTy MergeItsProtocols) {
446 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000447 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
448
449 if (!IDecl) {
450 // Category
451 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
452 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
453 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
454 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
455 E = MDecl->protocol_end(); P != E; ++P)
456 // Merge properties of category (*P) into IDECL's
457 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Mike Stump11289f42009-09-09 15:08:12 +0000458
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000459 // Go thru the list of protocols for this category and recursively merge
460 // their properties into this class as well.
461 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
462 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattner83f095c2009-03-28 19:18:32 +0000463 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000464 } else {
465 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
466 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
467 E = MD->protocol_end(); P != E; ++P)
Chris Lattner83f095c2009-03-28 19:18:32 +0000468 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000469 }
470 return;
471 }
472
Chris Lattnerca1e8482008-07-21 07:06:49 +0000473 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000474 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
475 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000476 // Merge properties of class (*P) into IDECL's
Chris Lattnerca1e8482008-07-21 07:06:49 +0000477 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Mike Stump11289f42009-09-09 15:08:12 +0000478
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000479 // Go thru the list of protocols for this class and recursively merge
480 // their properties into this class as well.
481 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
482 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner83f095c2009-03-28 19:18:32 +0000483 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerca1e8482008-07-21 07:06:49 +0000484 } else {
Argyrios Kyrtzidisb3fa8632008-07-21 09:18:38 +0000485 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
486 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
487 E = MD->protocol_end(); P != E; ++P)
Chris Lattner83f095c2009-03-28 19:18:32 +0000488 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerca1e8482008-07-21 07:06:49 +0000489 }
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000490}
491
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000492/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000493/// a class method in its extension.
494///
Mike Stump11289f42009-09-09 15:08:12 +0000495void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000496 ObjCInterfaceDecl *ID) {
497 if (!ID)
498 return; // Possibly due to previous error
499
500 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000501 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
502 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000503 ObjCMethodDecl *MD = *i;
504 MethodMap[MD->getSelector()] = MD;
505 }
506
507 if (MethodMap.empty())
508 return;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000509 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
510 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000511 ObjCMethodDecl *Method = *i;
512 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
513 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
514 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
515 << Method->getDeclName();
516 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
517 }
518 }
519}
520
Chris Lattnerdac168d2009-04-12 08:43:13 +0000521/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner83f095c2009-03-28 19:18:32 +0000522Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000523Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000524 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000525 unsigned NumElts,
526 AttributeList *attrList) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000527 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Mike Stump11289f42009-09-09 15:08:12 +0000528
Chris Lattnerda463fe2007-12-12 07:09:47 +0000529 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000530 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000531 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000532 if (PDecl == 0) { // Not already seen?
Mike Stump11289f42009-09-09 15:08:12 +0000533 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000534 IdentList[i].second, Ident);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000535 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000536 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000537 if (attrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000538 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000539 Protocols.push_back(PDecl);
540 }
Mike Stump11289f42009-09-09 15:08:12 +0000541
542 ObjCForwardProtocolDecl *PDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000543 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlssona6b508a2008-11-04 16:57:32 +0000544 &Protocols[0], Protocols.size());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000545 CurContext->addDecl(PDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +0000546 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000547 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000548}
549
Chris Lattner83f095c2009-03-28 19:18:32 +0000550Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000551ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
552 IdentifierInfo *ClassName, SourceLocation ClassLoc,
553 IdentifierInfo *CategoryName,
554 SourceLocation CategoryLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000555 const DeclPtrTy *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000556 unsigned NumProtoRefs,
557 SourceLocation EndProtoLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000558 ObjCCategoryDecl *CDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000559 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
560 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000561 CurContext->addDecl(CDecl);
Chris Lattner9018ca82009-02-16 21:26:43 +0000562
563 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian9a5124a2008-01-17 20:33:24 +0000564 /// Check that class of this category is already completely declared.
Chris Lattner9018ca82009-02-16 21:26:43 +0000565 if (!IDecl || IDecl->isForwardDecl()) {
566 CDecl->setInvalidDecl();
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000567 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner83f095c2009-03-28 19:18:32 +0000568 return DeclPtrTy::make(CDecl);
Fariborz Jahanian9a5124a2008-01-17 20:33:24 +0000569 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000570
Chris Lattner9018ca82009-02-16 21:26:43 +0000571 CDecl->setClassInterface(IDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000572
Chris Lattnerced903b2009-02-16 21:30:01 +0000573 // If the interface is deprecated, warn about it.
Douglas Gregor171c45a2009-02-18 21:56:37 +0000574 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner9018ca82009-02-16 21:26:43 +0000575
576 /// Check for duplicate interface declaration for this category
577 ObjCCategoryDecl *CDeclChain;
578 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
579 CDeclChain = CDeclChain->getNextClassCategory()) {
580 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
581 Diag(CategoryLoc, diag::warn_dup_category_def)
582 << ClassName << CategoryName;
583 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
584 break;
585 }
586 }
587 if (!CDeclChain)
588 CDecl->insertNextClassCategory();
589
Chris Lattnerda463fe2007-12-12 07:09:47 +0000590 if (NumProtoRefs) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000591 // Protocols in the class extension belong to the class.
592 if (!CDecl->getIdentifier())
593 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
594 NumProtoRefs,Context);
595 else {
596 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
597 Context);
598 CDecl->setLocEnd(EndProtoLoc);
599 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000600 }
Mike Stump11289f42009-09-09 15:08:12 +0000601
Anders Carlssona6b508a2008-11-04 16:57:32 +0000602 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000603 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000604}
605
606/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000607/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000608/// object.
Chris Lattner83f095c2009-03-28 19:18:32 +0000609Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000610 SourceLocation AtCatImplLoc,
611 IdentifierInfo *ClassName, SourceLocation ClassLoc,
612 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000613 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000614 ObjCCategoryDecl *CatIDecl = 0;
615 if (IDecl) {
616 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
617 if (!CatIDecl) {
618 // Category @implementation with no corresponding @interface.
619 // Create and install one.
620 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
621 CatName);
622 CatIDecl->setClassInterface(IDecl);
623 CatIDecl->insertNextClassCategory();
624 }
625 }
626
Mike Stump11289f42009-09-09 15:08:12 +0000627 ObjCCategoryImplDecl *CDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000628 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
629 IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000630 /// Check that class of this category is already completely declared.
631 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000632 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000633
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000634 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000635 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000636
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000637 /// Check that CatName, category name, is not used in another implementation.
638 if (CatIDecl) {
639 if (CatIDecl->getImplementation()) {
640 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
641 << CatName;
642 Diag(CatIDecl->getImplementation()->getLocation(),
643 diag::note_previous_definition);
644 } else
645 CatIDecl->setImplementation(CDecl);
646 }
Mike Stump11289f42009-09-09 15:08:12 +0000647
Anders Carlssona6b508a2008-11-04 16:57:32 +0000648 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000649 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000650}
651
Chris Lattner83f095c2009-03-28 19:18:32 +0000652Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000653 SourceLocation AtClassImplLoc,
654 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000655 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000656 SourceLocation SuperClassLoc) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000657 ObjCInterfaceDecl* IDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000658 // Check for another declaration kind with the same name.
Douglas Gregor2ada0482009-02-04 17:27:36 +0000659 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000660 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000661 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000662 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerd13b8b52009-02-23 22:00:08 +0000663 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000664 // Is there an interface declaration of this class; if not, warn!
Mike Stump11289f42009-09-09 15:08:12 +0000665 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian6f0f25b2009-04-23 21:49:04 +0000666 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000667 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian6f0f25b2009-04-23 21:49:04 +0000668 IDecl = 0;
669 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000670 }
Mike Stump11289f42009-09-09 15:08:12 +0000671
Chris Lattnerda463fe2007-12-12 07:09:47 +0000672 // Check that super class name is valid class name
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000673 ObjCInterfaceDecl* SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000674 if (SuperClassname) {
675 // Check if a different kind of symbol declared in this scope.
Douglas Gregored8f2882009-01-30 01:04:22 +0000676 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000677 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000678 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
679 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +0000680 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000681 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000682 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000683 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000684 Diag(SuperClassLoc, diag::err_undef_superclass)
685 << SuperClassname << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000686 else if (IDecl && IDecl->getSuperClass() != SDecl) {
687 // This implementation and its interface do not have the same
688 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000689 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000690 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +0000691 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000692 }
693 }
694 }
Mike Stump11289f42009-09-09 15:08:12 +0000695
Chris Lattnerda463fe2007-12-12 07:09:47 +0000696 if (!IDecl) {
697 // Legacy case of @implementation with no corresponding @interface.
698 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000699
Mike Stump87c57ac2009-05-16 07:39:55 +0000700 // FIXME: Do we support attributes on the @implementation? If so we should
701 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +0000702 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000703 ClassName, ClassLoc, false, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000704 IDecl->setSuperClass(SDecl);
705 IDecl->setLocEnd(ClassLoc);
Douglas Gregorac345a32009-04-24 00:16:12 +0000706
707 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbard1148a72009-04-21 21:41:56 +0000708 } else {
709 // Mark the interface as being completed, even if it was just as
710 // @class ....;
711 // declaration; the user cannot reopen it.
712 IDecl->setForwardDecl(false);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000713 }
Mike Stump11289f42009-09-09 15:08:12 +0000714
715 ObjCImplementationDecl* IMPDecl =
716 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000717 IDecl, SDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000718
Anders Carlssona6b508a2008-11-04 16:57:32 +0000719 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner83f095c2009-03-28 19:18:32 +0000720 return DeclPtrTy::make(IMPDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000721
Chris Lattnerda463fe2007-12-12 07:09:47 +0000722 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000723 if (IDecl->getImplementation()) {
Chris Lattner36ac1ca2008-03-16 20:53:07 +0000724 // FIXME: Don't leak everything!
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000725 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000726 Diag(IDecl->getImplementation()->getLocation(),
727 diag::note_previous_definition);
728 } else { // add it to the list.
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000729 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +0000730 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000731 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000732 return DeclPtrTy::make(IMPDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000733}
734
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000735void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
736 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000737 SourceLocation RBrace) {
738 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000739 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000740 if (!IDecl)
741 return;
742 /// Check case of non-existing @interface decl.
743 /// (legacy objective-c @implementation decl without an @interface decl).
744 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +0000745 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner22298722009-02-20 21:35:13 +0000746 IDecl->setIVarList(ivars, numIvars, Context);
747 IDecl->setLocEnd(RBrace);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000748 return;
749 }
750 // If implementation has empty ivar list, just return.
751 if (numIvars == 0)
752 return;
Mike Stump11289f42009-09-09 15:08:12 +0000753
Chris Lattnerda463fe2007-12-12 07:09:47 +0000754 assert(ivars && "missing @implementation ivars");
Mike Stump11289f42009-09-09 15:08:12 +0000755
Chris Lattnerda463fe2007-12-12 07:09:47 +0000756 // Check interface's Ivar list against those in the implementation.
757 // names and types must match.
758 //
Chris Lattnerda463fe2007-12-12 07:09:47 +0000759 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000760 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +0000761 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
762 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000763 ObjCIvarDecl* ImplIvar = ivars[j++];
764 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000765 assert (ImplIvar && "missing implementation ivar");
766 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +0000767
Steve Naroff157599f2009-03-03 14:49:36 +0000768 // First, make sure the types match.
Chris Lattner35ffe332008-07-27 00:05:05 +0000769 if (Context.getCanonicalType(ImplIvar->getType()) !=
770 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +0000771 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000772 << ImplIvar->getIdentifier()
773 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +0000774 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff157599f2009-03-03 14:49:36 +0000775 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
776 Expr *ImplBitWidth = ImplIvar->getBitWidth();
777 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000778 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
779 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff157599f2009-03-03 14:49:36 +0000780 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
781 << ImplIvar->getIdentifier();
782 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
783 }
Mike Stump11289f42009-09-09 15:08:12 +0000784 }
Steve Naroff157599f2009-03-03 14:49:36 +0000785 // Make sure the names are identical.
786 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000787 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000788 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +0000789 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000790 }
791 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000792 }
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chris Lattner0f29d982007-12-12 18:11:49 +0000794 if (numIvars > 0)
Chris Lattner83021e92007-12-12 18:19:52 +0000795 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +0000796 else if (IVI != IVE)
Chris Lattner83021e92007-12-12 18:19:52 +0000797 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000798}
799
Steve Naroff15833ed2008-02-10 21:38:56 +0000800void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
801 bool &IncompleteImpl) {
802 if (!IncompleteImpl) {
803 Diag(ImpLoc, diag::warn_incomplete_impl);
804 IncompleteImpl = true;
805 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000806 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff15833ed2008-02-10 21:38:56 +0000807}
808
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000809void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
810 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnera9d0ffe2009-04-11 18:01:59 +0000811 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian0c20bdd2009-05-14 23:52:54 +0000812 ImpMethodDecl->getResultType()) &&
Steve Naroff8e6aee52009-07-23 01:01:38 +0000813 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
814 ImpMethodDecl->getResultType())) {
Mike Stump11289f42009-09-09 15:08:12 +0000815 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000816 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
817 << ImpMethodDecl->getResultType();
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000818 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
819 }
Mike Stump11289f42009-09-09 15:08:12 +0000820
Chris Lattner67f35b02009-04-11 19:58:42 +0000821 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
822 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
823 IM != EM; ++IM, ++IF) {
Fariborz Jahanian0c20bdd2009-05-14 23:52:54 +0000824 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()) ||
Mike Stump11289f42009-09-09 15:08:12 +0000825 Context.QualifiedIdConformsQualifiedId((*IF)->getType(),
Steve Naroff8e6aee52009-07-23 01:01:38 +0000826 (*IM)->getType()))
Chris Lattner67f35b02009-04-11 19:58:42 +0000827 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000828
829 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000830 << ImpMethodDecl->getDeclName() << (*IF)->getType()
831 << (*IM)->getType();
Chris Lattner5300acc2009-04-11 20:14:49 +0000832 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner67f35b02009-04-11 19:58:42 +0000833 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000834}
835
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000836/// isPropertyReadonly - Return true if property is readonly, by searching
837/// for the property in the class and in its categories and implementations
838///
839bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffde680012009-02-26 19:11:32 +0000840 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000841 // by far the most common case.
842 if (!PDecl->isReadOnly())
843 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000844 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000845 // it is not considered read only.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000846 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000847 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000848
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000849 // Main class has the property as 'readonly'. Must search
Mike Stump11289f42009-09-09 15:08:12 +0000850 // through the category list to see if the property's
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000851 // attribute has been over-ridden to 'readwrite'.
852 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
853 Category; Category = Category->getNextClassCategory()) {
Mike Stump11289f42009-09-09 15:08:12 +0000854 // Even if property is ready only, if a category has a user defined setter,
855 // it is not considered read only.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000856 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000857 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000858 ObjCPropertyDecl *P =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000859 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000860 if (P && !P->isReadOnly())
861 return false;
862 }
Mike Stump11289f42009-09-09 15:08:12 +0000863
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000864 // Also, check for definition of a setter method in the implementation if
865 // all else failed.
866 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump11289f42009-09-09 15:08:12 +0000867 if (ObjCImplementationDecl *IMD =
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000868 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000869 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000870 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000871 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stump12b8ce12009-08-04 21:02:39 +0000872 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000873 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000874 return false;
875 }
876 }
Steve Naroffde680012009-02-26 19:11:32 +0000877 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000878 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000879 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroffde680012009-02-26 19:11:32 +0000880 return false;
Fariborz Jahanian15e3a5c2009-04-06 16:59:10 +0000881 // If all fails, look at the super class.
882 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
883 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000884 return true;
885}
886
Mike Stump87c57ac2009-05-16 07:39:55 +0000887/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
888/// improve the efficiency of selector lookups and type checking by associating
889/// with each protocol / interface / category the flattened instance tables. If
890/// we used an immutable set to keep the table then it wouldn't add significant
891/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +0000892
Steve Naroffa36992242008-02-08 22:06:17 +0000893/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +0000894/// Declared in protocol, and those referenced by it.
Steve Naroffa36992242008-02-08 22:06:17 +0000895void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
896 ObjCProtocolDecl *PDecl,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000897 bool& IncompleteImpl,
Steve Naroffa36992242008-02-08 22:06:17 +0000898 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000899 const llvm::DenseSet<Selector> &ClsMap,
900 ObjCInterfaceDecl *IDecl) {
901 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000902 ObjCInterfaceDecl *NSIDecl = 0;
903 if (getLangOptions().NeXTRuntime) {
Mike Stump11289f42009-09-09 15:08:12 +0000904 // check to see if class implements forwardInvocation method and objects
905 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000906 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +0000907 // Under such conditions, which means that every method possible is
908 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000909 // found" warnings.
910 // FIXME: Use a general GetUnarySelector method for this.
911 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
912 Selector fISelector = Context.Selectors.getSelector(1, &II);
913 if (InsMap.count(fISelector))
914 // Is IDecl derived from 'NSProxy'? If so, no instance methods
915 // need be implemented in the implementation.
916 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
917 }
Mike Stump11289f42009-09-09 15:08:12 +0000918
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000919 // If a method lookup fails locally we still need to look and see if
920 // the method was implemented by a base class or an inherited
921 // protocol. This lookup is slow, but occurs rarely in correct code
922 // and otherwise would terminate in a warning.
923
Chris Lattnerda463fe2007-12-12 07:09:47 +0000924 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000925 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +0000926 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000927 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000928 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000929 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000930 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000931 (!Super ||
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000932 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000933 // Ugly, but necessary. Method declared in protcol might have
934 // have been synthesized due to a property declared in the class which
935 // uses the protocol.
Mike Stump11289f42009-09-09 15:08:12 +0000936 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000937 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000938 if (!MethodInClass || !MethodInClass->isSynthesized())
939 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
940 }
941 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000942 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +0000943 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000944 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000945 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000946 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000947 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
948 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000949 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff15833ed2008-02-10 21:38:56 +0000950 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff3ce37a62007-12-14 23:37:57 +0000951 }
Chris Lattner390d39a2008-07-21 21:32:27 +0000952 // Check on this protocols's referenced protocols, recursively.
953 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
954 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000955 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000956}
957
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000958/// MatchAllMethodDeclarations - Check methods declaraed in interface or
959/// or protocol against those declared in their implementations.
960///
961void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
962 const llvm::DenseSet<Selector> &ClsMap,
963 llvm::DenseSet<Selector> &InsMapSeen,
964 llvm::DenseSet<Selector> &ClsMapSeen,
965 ObjCImplDecl* IMPDecl,
966 ObjCContainerDecl* CDecl,
967 bool &IncompleteImpl,
Mike Stump11289f42009-09-09 15:08:12 +0000968 bool ImmediateClass) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000969 // Check and see if instance methods in class interface have been
970 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000971 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
972 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000973 if (InsMapSeen.count((*I)->getSelector()))
974 continue;
975 InsMapSeen.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000976 if (!(*I)->isSynthesized() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000977 !InsMap.count((*I)->getSelector())) {
978 if (ImmediateClass)
979 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
980 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +0000981 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000982 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000983 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000984 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000985 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000986 assert(IntfMethodDecl &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000987 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
988 // ImpMethodDecl may be null as in a @dynamic property.
989 if (ImpMethodDecl)
990 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
991 }
992 }
Mike Stump11289f42009-09-09 15:08:12 +0000993
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000994 // Check and see if class methods in class interface have been
995 // implemented in the implementation class. If so, their types match.
Mike Stump11289f42009-09-09 15:08:12 +0000996 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000997 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000998 if (ClsMapSeen.count((*I)->getSelector()))
999 continue;
1000 ClsMapSeen.insert((*I)->getSelector());
1001 if (!ClsMap.count((*I)->getSelector())) {
1002 if (ImmediateClass)
1003 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stump12b8ce12009-08-04 21:02:39 +00001004 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001005 ObjCMethodDecl *ImpMethodDecl =
1006 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001007 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001008 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001009 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1010 }
1011 }
1012 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1013 // Check for any implementation of a methods declared in protocol.
1014 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1015 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001016 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1017 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001018 (*PI), IncompleteImpl, false);
1019 if (I->getSuperClass())
1020 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +00001021 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001022 I->getSuperClass(), IncompleteImpl, false);
1023 }
1024}
1025
Mike Stump11289f42009-09-09 15:08:12 +00001026void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1027 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001028 bool IncompleteImpl) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001029 llvm::DenseSet<Selector> InsMap;
1030 // Check and see if instance methods in class interface have been
1031 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001032 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001033 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001034 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001035
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001036 // Check and see if properties declared in the interface have either 1)
1037 // an implementation or 2) there is a @synthesize/@dynamic implementation
1038 // of the property in the @implementation.
1039 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001040 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1041 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001042 ObjCPropertyDecl *Prop = (*P);
1043 if (Prop->isInvalidDecl())
1044 continue;
1045 ObjCPropertyImplDecl *PI = 0;
1046 // Is there a matching propery synthesize/dynamic?
Mike Stump11289f42009-09-09 15:08:12 +00001047 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001048 I = IMPDecl->propimpl_begin(),
1049 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001050 if ((*I)->getPropertyDecl() == Prop) {
1051 PI = (*I);
1052 break;
1053 }
1054 if (PI)
1055 continue;
1056 if (!InsMap.count(Prop->getGetterName())) {
Mike Stump11289f42009-09-09 15:08:12 +00001057 Diag(Prop->getLocation(),
1058 diag::warn_setter_getter_impl_required)
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001059 << Prop->getDeclName() << Prop->getGetterName();
1060 Diag(IMPDecl->getLocation(),
1061 diag::note_property_impl_required);
1062 }
Mike Stump11289f42009-09-09 15:08:12 +00001063
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001064 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Mike Stump11289f42009-09-09 15:08:12 +00001065 Diag(Prop->getLocation(),
1066 diag::warn_setter_getter_impl_required)
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001067 << Prop->getDeclName() << Prop->getSetterName();
1068 Diag(IMPDecl->getLocation(),
1069 diag::note_property_impl_required);
1070 }
1071 }
Mike Stump11289f42009-09-09 15:08:12 +00001072
Chris Lattnerda463fe2007-12-12 07:09:47 +00001073 llvm::DenseSet<Selector> ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +00001074 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001075 I = IMPDecl->classmeth_begin(),
1076 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001077 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001078
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001079 // Check for type conflict of methods declared in a class/protocol and
1080 // its implementation; if any.
1081 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +00001082 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1083 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001084 IncompleteImpl, true);
Mike Stump11289f42009-09-09 15:08:12 +00001085
Chris Lattnerda463fe2007-12-12 07:09:47 +00001086 // Check the protocol list for unimplemented methods in the @implementation
1087 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001088 // Check and see if class methods in class interface have been
1089 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001090
Chris Lattner9ef10f42009-03-01 00:56:52 +00001091 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001092 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner9ef10f42009-03-01 00:56:52 +00001093 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001094 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001095 InsMap, ClsMap, I);
1096 // Check class extensions (unnamed categories)
1097 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1098 Categories; Categories = Categories->getNextClassCategory()) {
1099 if (!Categories->getIdentifier()) {
1100 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1101 break;
1102 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +00001103 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001104 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1105 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1106 E = C->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001107 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001108 InsMap, ClsMap, C->getClassInterface());
1109 } else
1110 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00001111}
1112
Mike Stump11289f42009-09-09 15:08:12 +00001113/// ActOnForwardClassDeclaration -
Chris Lattner83f095c2009-03-28 19:18:32 +00001114Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00001115Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00001116 IdentifierInfo **IdentList,
1117 unsigned NumElts) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001118 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump11289f42009-09-09 15:08:12 +00001119
Chris Lattnerda463fe2007-12-12 07:09:47 +00001120 for (unsigned i = 0; i != NumElts; ++i) {
1121 // Check for another declaration kind with the same name.
Douglas Gregor2ada0482009-02-04 17:27:36 +00001122 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor5daeee22008-12-08 18:40:42 +00001123 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001124 // Maybe we will complain about the shadowed template parameter.
1125 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1126 // Just pretend that we didn't see the previous declaration.
1127 PrevDecl = 0;
1128 }
1129
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001130 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00001131 // GCC apparently allows the following idiom:
1132 //
1133 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1134 // @class XCElementToggler;
1135 //
Mike Stump11289f42009-09-09 15:08:12 +00001136 // FIXME: Make an extension?
Steve Naroff946166f2008-06-05 22:57:10 +00001137 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1138 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001139 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00001140 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stump12b8ce12009-08-04 21:02:39 +00001141 } else if (TDD) {
1142 // a forward class declaration matching a typedef name of a class refers
1143 // to the underlying class.
Mike Stump11289f42009-09-09 15:08:12 +00001144 if (ObjCInterfaceType * OI =
Fariborz Jahanian0d451812009-05-07 21:49:26 +00001145 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1146 PrevDecl = OI->getDecl();
1147 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001148 }
Mike Stump11289f42009-09-09 15:08:12 +00001149 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001150 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump11289f42009-09-09 15:08:12 +00001151 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Steve Naroffe0064d22009-09-11 00:12:01 +00001152 IdentList[i],
1153 // FIXME: need to get the 'real'
1154 // identifier loc from the parser.
1155 AtClassLoc, true);
Steve Naroff0cbba992009-04-23 16:00:56 +00001156 PushOnScopeChains(IDecl, TUScope);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001157 }
1158
1159 Interfaces.push_back(IDecl);
1160 }
Mike Stump11289f42009-09-09 15:08:12 +00001161
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001162 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlssona6b508a2008-11-04 16:57:32 +00001163 &Interfaces[0],
1164 Interfaces.size());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001165 CurContext->addDecl(CDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +00001166 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001167 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001168}
1169
1170
1171/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1172/// returns true, or false, accordingly.
1173/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump11289f42009-09-09 15:08:12 +00001174bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffa0ed1652008-10-21 10:37:50 +00001175 const ObjCMethodDecl *PrevMethod,
1176 bool matchBasedOnSizeAndAlignment) {
1177 QualType T1 = Context.getCanonicalType(Method->getResultType());
1178 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump11289f42009-09-09 15:08:12 +00001179
Steve Naroffa0ed1652008-10-21 10:37:50 +00001180 if (T1 != T2) {
1181 // The result types are different.
1182 if (!matchBasedOnSizeAndAlignment)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001183 return false;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001184 // Incomplete types don't have a size and alignment.
1185 if (T1->isIncompleteType() || T2->isIncompleteType())
1186 return false;
1187 // Check is based on size and alignment.
1188 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1189 return false;
1190 }
Mike Stump11289f42009-09-09 15:08:12 +00001191
Chris Lattnera4997152009-02-20 18:43:26 +00001192 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1193 E = Method->param_end();
1194 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001195
Chris Lattnera4997152009-02-20 18:43:26 +00001196 for (; ParamI != E; ++ParamI, ++PrevI) {
1197 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1198 T1 = Context.getCanonicalType((*ParamI)->getType());
1199 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffa0ed1652008-10-21 10:37:50 +00001200 if (T1 != T2) {
1201 // The result types are different.
1202 if (!matchBasedOnSizeAndAlignment)
1203 return false;
1204 // Incomplete types don't have a size and alignment.
1205 if (T1->isIncompleteType() || T2->isIncompleteType())
1206 return false;
1207 // Check is based on size and alignment.
1208 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1209 return false;
1210 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001211 }
1212 return true;
1213}
1214
Douglas Gregorc78d3462009-04-24 21:10:55 +00001215/// \brief Read the contents of the instance and factory method pools
1216/// for a given selector from external storage.
1217///
1218/// This routine should only be called once, when neither the instance
1219/// nor the factory method pool has an entry for this selector.
Mike Stump11289f42009-09-09 15:08:12 +00001220Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001221 bool isInstance) {
1222 assert(ExternalSource && "We need an external AST source");
1223 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1224 "Selector data already loaded into the instance method pool");
1225 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1226 "Selector data already loaded into the factory method pool");
1227
1228 // Read the method list from the external source.
1229 std::pair<ObjCMethodList, ObjCMethodList> Methods
1230 = ExternalSource->ReadMethodPool(Sel);
Mike Stump11289f42009-09-09 15:08:12 +00001231
Douglas Gregorc78d3462009-04-24 21:10:55 +00001232 if (isInstance) {
1233 if (Methods.second.Method)
1234 FactoryMethodPool[Sel] = Methods.second;
1235 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump11289f42009-09-09 15:08:12 +00001236 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001237
1238 if (Methods.first.Method)
1239 InstanceMethodPool[Sel] = Methods.first;
1240
1241 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1242}
1243
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001244void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001245 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1246 = InstanceMethodPool.find(Method->getSelector());
1247 if (Pos == InstanceMethodPool.end()) {
1248 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1249 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1250 else
1251 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1252 ObjCMethodList())).first;
1253 }
1254
1255 ObjCMethodList &Entry = Pos->second;
Chris Lattner7b26b292009-03-04 05:16:45 +00001256 if (Entry.Method == 0) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001257 // Haven't seen a method with this selector name yet - add it.
Chris Lattner7b26b292009-03-04 05:16:45 +00001258 Entry.Method = Method;
1259 Entry.Next = 0;
1260 return;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001261 }
Mike Stump11289f42009-09-09 15:08:12 +00001262
Chris Lattner7b26b292009-03-04 05:16:45 +00001263 // We've seen a method with this name, see if we have already seen this type
1264 // signature.
1265 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1266 if (MatchTwoMethodDeclarations(Method, List->Method))
1267 return;
Mike Stump11289f42009-09-09 15:08:12 +00001268
Chris Lattner7b26b292009-03-04 05:16:45 +00001269 // We have a new signature for an existing method - add it.
1270 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1271 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001272}
1273
Steve Naroff9ebc0502008-10-21 10:50:19 +00001274// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump11289f42009-09-09 15:08:12 +00001275ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001276 SourceRange R,
1277 bool warn) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001278 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1279 = InstanceMethodPool.find(Sel);
Douglas Gregor9a1899b2009-04-24 22:23:41 +00001280 if (Pos == InstanceMethodPool.end()) {
1281 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001282 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1283 else
1284 return 0;
1285 }
1286
1287 ObjCMethodList &MethList = Pos->second;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001288 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001289
Steve Naroff4a82d812008-09-30 14:38:43 +00001290 if (MethList.Method && MethList.Next) {
Steve Naroffa0ed1652008-10-21 10:37:50 +00001291 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1292 // This checks if the methods differ by size & alignment.
1293 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001294 issueWarning = warn;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001295 }
1296 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattnere4b95692008-11-24 03:33:13 +00001297 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner12f7c5a2008-11-23 23:26:13 +00001298 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerf490e152008-11-19 05:27:50 +00001299 << MethList.Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001300 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner12f7c5a2008-11-23 23:26:13 +00001301 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerf490e152008-11-19 05:27:50 +00001302 << Next->Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001303 }
1304 return MethList.Method;
1305}
1306
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001307void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001308 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1309 = FactoryMethodPool.find(Method->getSelector());
1310 if (Pos == FactoryMethodPool.end()) {
1311 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1312 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1313 else
1314 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1315 ObjCMethodList())).first;
1316 }
1317
1318 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001319 if (!FirstMethod.Method) {
1320 // Haven't seen a method with this selector name yet - add it.
1321 FirstMethod.Method = Method;
1322 FirstMethod.Next = 0;
1323 } else {
1324 // We've seen a method with this name, now check the type signature(s).
1325 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump11289f42009-09-09 15:08:12 +00001326
1327 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001328 Next = Next->Next)
1329 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump11289f42009-09-09 15:08:12 +00001330
Chris Lattnerda463fe2007-12-12 07:09:47 +00001331 if (!match) {
1332 // We have a new signature for an existing method - add it.
1333 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001334 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001335 FirstMethod.Next = OMI;
1336 }
1337 }
1338}
1339
Mike Stump11289f42009-09-09 15:08:12 +00001340ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001341 SourceRange R) {
1342 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1343 = FactoryMethodPool.find(Sel);
1344 if (Pos == FactoryMethodPool.end()) {
Mike Stump11289f42009-09-09 15:08:12 +00001345 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001346 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1347 else
1348 return 0;
1349 }
1350
1351 ObjCMethodList &MethList = Pos->second;
1352 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001353
Douglas Gregorc78d3462009-04-24 21:10:55 +00001354 if (MethList.Method && MethList.Next) {
1355 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1356 // This checks if the methods differ by size & alignment.
1357 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1358 issueWarning = true;
1359 }
1360 if (issueWarning && (MethList.Method && MethList.Next)) {
1361 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1362 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1363 << MethList.Method->getSourceRange();
1364 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1365 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1366 << Next->Method->getSourceRange();
1367 }
1368 return MethList.Method;
1369}
1370
Mike Stump11289f42009-09-09 15:08:12 +00001371/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff35c62ae2009-01-08 17:28:14 +00001372/// have the property type and issue diagnostics if they don't.
1373/// Also synthesize a getter/setter method if none exist (and update the
1374/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1375/// methods is the "right" thing to do.
Mike Stump11289f42009-09-09 15:08:12 +00001376void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff35c62ae2009-01-08 17:28:14 +00001377 ObjCContainerDecl *CD) {
1378 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump11289f42009-09-09 15:08:12 +00001379
1380 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001381 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump11289f42009-09-09 15:08:12 +00001382 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +00001383 property->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001384
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001385 if (SetterMethod) {
Mike Stump11289f42009-09-09 15:08:12 +00001386 if (Context.getCanonicalType(SetterMethod->getResultType())
Fariborz Jahanian06d0dd62008-12-06 23:12:49 +00001387 != Context.VoidTy)
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001388 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattnera4997152009-02-20 18:43:26 +00001389 if (SetterMethod->param_size() != 1 ||
1390 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump11289f42009-09-09 15:08:12 +00001391 Diag(property->getLocation(),
1392 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001393 << property->getDeclName()
Ted Kremenek9a468042009-03-14 00:20:08 +00001394 << SetterMethod->getSelector();
Fariborz Jahanianff839982008-12-06 21:48:16 +00001395 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1396 }
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001397 }
Steve Naroff35c62ae2009-01-08 17:28:14 +00001398
1399 // Synthesize getter/setter methods if none exist.
Steve Naroffe538c5f2009-01-08 20:15:03 +00001400 // Find the default getter and if one not found, add one.
Mike Stump87c57ac2009-05-16 07:39:55 +00001401 // FIXME: The synthesized property we set here is misleading. We almost always
1402 // synthesize these methods unless the user explicitly provided prototypes
1403 // (which is odd, but allowed). Sema should be typechecking that the
1404 // declarations jive in that situation (which it is not currently).
Steve Naroffe538c5f2009-01-08 20:15:03 +00001405 if (!GetterMethod) {
1406 // No instance method of same name as property getter name was found.
Mike Stump11289f42009-09-09 15:08:12 +00001407 // Declare a getter method and add it to the list of methods
Steve Naroffe538c5f2009-01-08 20:15:03 +00001408 // for this class.
Mike Stump11289f42009-09-09 15:08:12 +00001409 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1410 property->getLocation(), property->getGetterName(),
1411 property->getType(), CD, true, false, true,
1412 (property->getPropertyImplementation() ==
1413 ObjCPropertyDecl::Optional) ?
1414 ObjCMethodDecl::Optional :
Steve Naroffe538c5f2009-01-08 20:15:03 +00001415 ObjCMethodDecl::Required);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001416 CD->addDecl(GetterMethod);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001417 } else
1418 // A user declared getter will be synthesize when @synthesize of
1419 // the property with the same name is seen in the @implementation
Steve Naroff04f2d142009-04-20 15:06:07 +00001420 GetterMethod->setSynthesized(true);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001421 property->setGetterMethodDecl(GetterMethod);
1422
1423 // Skip setter if property is read-only.
1424 if (!property->isReadOnly()) {
1425 // Find the default setter and if one not found, add one.
1426 if (!SetterMethod) {
1427 // No instance method of same name as property setter name was found.
Mike Stump11289f42009-09-09 15:08:12 +00001428 // Declare a setter method and add it to the list of methods
Steve Naroffe538c5f2009-01-08 20:15:03 +00001429 // for this class.
Mike Stump11289f42009-09-09 15:08:12 +00001430 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1431 property->getLocation(),
1432 property->getSetterName(),
Steve Naroffe538c5f2009-01-08 20:15:03 +00001433 Context.VoidTy, CD, true, false, true,
Mike Stump11289f42009-09-09 15:08:12 +00001434 (property->getPropertyImplementation() ==
1435 ObjCPropertyDecl::Optional) ?
1436 ObjCMethodDecl::Optional :
Steve Naroffe538c5f2009-01-08 20:15:03 +00001437 ObjCMethodDecl::Required);
1438 // Invent the arguments for the setter. We don't bother making a
1439 // nice name for the argument.
1440 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump11289f42009-09-09 15:08:12 +00001441 property->getLocation(),
Steve Naroffe538c5f2009-01-08 20:15:03 +00001442 property->getIdentifier(),
1443 property->getType(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001444 /*DInfo=*/0,
Steve Naroffe538c5f2009-01-08 20:15:03 +00001445 VarDecl::None,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001446 0);
Steve Naroff04f2d142009-04-20 15:06:07 +00001447 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001448 CD->addDecl(SetterMethod);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001449 } else
1450 // A user declared setter will be synthesize when @synthesize of
1451 // the property with the same name is seen in the @implementation
Steve Naroff04f2d142009-04-20 15:06:07 +00001452 SetterMethod->setSynthesized(true);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001453 property->setSetterMethodDecl(SetterMethod);
1454 }
Mike Stump11289f42009-09-09 15:08:12 +00001455 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff35c62ae2009-01-08 17:28:14 +00001456 // handle the following, which is supported by GCC (and part of the design).
1457 //
1458 // @interface Foo
1459 // @property double bar;
1460 // @end
1461 //
1462 // void thisIsUnfortunate() {
1463 // id foo;
1464 // double bar = [foo bar];
1465 // }
1466 //
Douglas Gregor020713e2009-01-09 19:42:16 +00001467 if (GetterMethod)
Mike Stump11289f42009-09-09 15:08:12 +00001468 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor020713e2009-01-09 19:42:16 +00001469 if (SetterMethod)
Mike Stump11289f42009-09-09 15:08:12 +00001470 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001471}
1472
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001473/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1474/// identical selector names in current and its super classes and issues
1475/// a warning if any of their argument types are incompatible.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001476void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1477 ObjCMethodDecl *Method,
1478 bool IsInstance) {
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001479 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1480 if (ID == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +00001481
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001482 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump11289f42009-09-09 15:08:12 +00001483 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001484 SD->lookupMethod(Method->getSelector(), IsInstance);
1485 if (SuperMethodDecl == 0) {
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001486 ID = SD;
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001487 continue;
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001488 }
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001489 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1490 E = Method->param_end();
1491 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1492 for (; ParamI != E; ++ParamI, ++PrevI) {
1493 // Number of parameters are the same and is guaranteed by selector match.
1494 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1495 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1496 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1497 // If type of arguement of method in this class does not match its
1498 // respective argument type in the super class method, issue warning;
1499 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump11289f42009-09-09 15:08:12 +00001500 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001501 << T1 << T2;
1502 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1503 return;
1504 }
1505 }
1506 ID = SD;
1507 }
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001508}
1509
Steve Naroff1d2538c2007-12-18 01:30:32 +00001510// Note: For class/category implemenations, allMethods/allProperties is
1511// always null.
Chris Lattner83f095c2009-03-28 19:18:32 +00001512void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1513 DeclPtrTy *allMethods, unsigned allNum,
1514 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001515 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001516 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001517
Steve Naroff1d2538c2007-12-18 01:30:32 +00001518 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1519 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattnerda463fe2007-12-12 07:09:47 +00001520 // should be true.
1521 if (!ClassDecl)
1522 return;
Mike Stump11289f42009-09-09 15:08:12 +00001523
1524 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00001525 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1526 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001527 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00001528
Steve Naroff35c62ae2009-01-08 17:28:14 +00001529 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff35c62ae2009-01-08 17:28:14 +00001530
1531 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1532 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1533 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1534
Chris Lattnerda463fe2007-12-12 07:09:47 +00001535 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001536 ObjCMethodDecl *Method =
Chris Lattner83f095c2009-03-28 19:18:32 +00001537 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001538
1539 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00001540 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001541 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001542 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001543 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001544 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001545 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001546 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001547 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001548 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001549 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001550 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001551 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001552 InsMap[Method->getSelector()] = Method;
1553 /// The following allows us to typecheck messages to "id".
1554 AddInstanceMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001555 // verify that the instance method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001556 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001557 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001558 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001559 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001560 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001561 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001562 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001563 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001564 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001565 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001566 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001567 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001568 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001569 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001570 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001571 ClsMap[Method->getSelector()] = Method;
Steve Naroff1d2538c2007-12-18 01:30:32 +00001572 /// The following allows us to typecheck messages to "Class".
1573 AddFactoryMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001574 // verify that the class method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001575 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001576 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001577 }
1578 }
1579 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001580 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump11289f42009-09-09 15:08:12 +00001581 // Compares properties declared in this class to those of its
Fariborz Jahanian7cf18862008-05-01 00:03:38 +00001582 // super class.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +00001583 ComparePropertiesInBaseAndSuper(I);
Chris Lattner83f095c2009-03-28 19:18:32 +00001584 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroffb3a87982009-01-09 15:36:25 +00001585 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001586 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00001587 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001588 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001589
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +00001590 // Merge protocol properties into category
Chris Lattner83f095c2009-03-28 19:18:32 +00001591 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanian33afd772009-03-02 19:05:07 +00001592 if (C->getIdentifier() == 0)
1593 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001594 }
Steve Naroffb3a87982009-01-09 15:36:25 +00001595 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1596 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1597 // user-defined setter/getter. It also synthesizes setter/getter methods
1598 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001599 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1600 E = CDecl->prop_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001601 I != E; ++I)
Chris Lattner41fd42e2009-02-16 18:32:47 +00001602 ProcessPropertyDecl(*I, CDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00001603 CDecl->setAtEndLoc(AtEndLoc);
1604 }
1605 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001606 IC->setAtEndLoc(AtEndLoc);
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001607 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattnerda463fe2007-12-12 07:09:47 +00001608 ImplMethodsVsClassMethods(IC, IDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001609 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00001610 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001611 CatImplClass->setAtEndLoc(AtEndLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001612
Chris Lattnerda463fe2007-12-12 07:09:47 +00001613 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00001614 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00001615 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001616 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001617 Categories; Categories = Categories->getNextClassCategory()) {
1618 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner9ef10f42009-03-01 00:56:52 +00001619 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001620 break;
1621 }
1622 }
1623 }
1624 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001625 if (isInterfaceDeclKind) {
1626 // Reject invalid vardecls.
1627 for (unsigned i = 0; i != tuvNum; i++) {
1628 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1629 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1630 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00001631 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00001632 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00001633 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001634 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00001635 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001636}
1637
1638
1639/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1640/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00001641static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001642CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1643 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1644 if (PQTVal & ObjCDeclSpec::DQ_In)
1645 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1646 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1647 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1648 if (PQTVal & ObjCDeclSpec::DQ_Out)
1649 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1650 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1651 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1652 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1653 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1654 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1655 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001656
1657 return ret;
1658}
1659
Chris Lattner83f095c2009-03-28 19:18:32 +00001660Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattnerda463fe2007-12-12 07:09:47 +00001661 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001662 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001663 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001664 Selector Sel,
1665 // optional arguments. The number of types/arguments is obtained
1666 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001667 ObjCArgInfo *ArgInfo,
Fariborz Jahaniane84858c2009-01-09 00:38:19 +00001668 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001669 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1670 bool isVariadic) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001671 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff83777fe2008-02-29 21:48:07 +00001672
1673 // Make sure we can establish a context for the method.
1674 if (!ClassDecl) {
1675 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanianb1771e42009-08-28 17:52:37 +00001676 FunctionLabelMap.clear();
Chris Lattner83f095c2009-03-28 19:18:32 +00001677 return DeclPtrTy();
Steve Naroff83777fe2008-02-29 21:48:07 +00001678 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001679 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00001680
Steve Naroff32606412009-02-20 22:59:16 +00001681 if (ReturnType) {
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +00001682 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump11289f42009-09-09 15:08:12 +00001683
Steve Naroff32606412009-02-20 22:59:16 +00001684 // Methods cannot return interface types. All ObjC objects are
1685 // passed by reference.
1686 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattnerde5a5312009-04-11 19:08:56 +00001687 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1688 << 0 << resultDeclType;
Chris Lattner83f095c2009-03-28 19:18:32 +00001689 return DeclPtrTy();
Steve Naroff32606412009-02-20 22:59:16 +00001690 }
1691 } else // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001692 resultDeclType = Context.getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +00001693
1694 ObjCMethodDecl* ObjCMethod =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001695 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump11289f42009-09-09 15:08:12 +00001696 cast<DeclContext>(ClassDecl),
Chris Lattner8d8829e2008-03-16 00:49:28 +00001697 MethodType == tok::minus, isVariadic,
Fariborz Jahanian8983f172008-05-07 20:53:44 +00001698 false,
Mike Stump11289f42009-09-09 15:08:12 +00001699 MethodDeclKind == tok::objc_optional ?
1700 ObjCMethodDecl::Optional :
Chris Lattner8d8829e2008-03-16 00:49:28 +00001701 ObjCMethodDecl::Required);
Mike Stump11289f42009-09-09 15:08:12 +00001702
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001703 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00001704
Chris Lattner23b0faf2009-04-11 19:42:43 +00001705 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattnerde5a5312009-04-11 19:08:56 +00001706 QualType ArgType, UnpromotedArgType;
Mike Stump11289f42009-09-09 15:08:12 +00001707
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001708 if (ArgInfo[i].Type == 0) {
Chris Lattnerde5a5312009-04-11 19:08:56 +00001709 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001710 } else {
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +00001711 UnpromotedArgType = ArgType = GetTypeFromParser(ArgInfo[i].Type);
Steve Naroffb0498ee2008-12-09 19:36:17 +00001712 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner9713a1c2009-04-11 19:34:56 +00001713 ArgType = adjustParameterType(ArgType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001714 }
Mike Stump11289f42009-09-09 15:08:12 +00001715
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00001716 ParmVarDecl* Param;
Chris Lattnerde5a5312009-04-11 19:08:56 +00001717 if (ArgType == UnpromotedArgType)
Chris Lattner23b0faf2009-04-11 19:42:43 +00001718 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattnerde5a5312009-04-11 19:08:56 +00001719 ArgInfo[i].Name, ArgType,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001720 /*DInfo=*/0, //FIXME: Pass info here.
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001721 VarDecl::None, 0);
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00001722 else
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +00001723 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner23b0faf2009-04-11 19:42:43 +00001724 ArgInfo[i].NameLoc,
Chris Lattnerde5a5312009-04-11 19:08:56 +00001725 ArgInfo[i].Name, ArgType,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001726 /*DInfo=*/0, //FIXME: Pass info here.
Chris Lattnerde5a5312009-04-11 19:08:56 +00001727 UnpromotedArgType,
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +00001728 VarDecl::None, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001729
Chris Lattner9713a1c2009-04-11 19:34:56 +00001730 if (ArgType->isObjCInterfaceType()) {
1731 Diag(ArgInfo[i].NameLoc,
1732 diag::err_object_cannot_be_passed_returned_by_value)
1733 << 1 << ArgType;
1734 Param->setInvalidDecl();
1735 }
Mike Stump11289f42009-09-09 15:08:12 +00001736
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001737 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001738 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00001739
Chris Lattner9713a1c2009-04-11 19:34:56 +00001740 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00001741 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001742
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001743 Params.push_back(Param);
1744 }
1745
Jay Foad7d0479f2009-05-21 09:52:38 +00001746 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001747 ObjCMethod->setObjCDeclQualifier(
1748 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1749 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001750
1751 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00001752 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00001753
1754 // For implementations (which can be very "coarse grain"), we add the
1755 // method now. This allows the AST to implement lookup methods that work
1756 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattnerda463fe2007-12-12 07:09:47 +00001757 // us to flag multiple declaration errors as they occur.
Mike Stump11289f42009-09-09 15:08:12 +00001758 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001759 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001760 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001761 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1762 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001763 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001764 PrevMethod = ImpDecl->getClassMethod(Sel);
1765 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001766 }
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001767 if (AttrList)
1768 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump11289f42009-09-09 15:08:12 +00001769 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stump12b8ce12009-08-04 21:02:39 +00001770 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001771 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001772 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1773 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001774 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001775 PrevMethod = CatImpDecl->getClassMethod(Sel);
1776 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001777 }
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001778 if (AttrList)
1779 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001780 }
1781 if (PrevMethod) {
1782 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00001783 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001784 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001785 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump11289f42009-09-09 15:08:12 +00001786 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001787 return DeclPtrTy::make(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001788}
1789
Mike Stump11289f42009-09-09 15:08:12 +00001790void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001791 SourceLocation Loc,
1792 unsigned &Attributes) {
1793 // FIXME: Improve the reported location.
1794
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00001795 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001796 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00001797 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1798 ObjCDeclSpec::DQ_PR_assign |
1799 ObjCDeclSpec::DQ_PR_copy |
1800 ObjCDeclSpec::DQ_PR_retain))) {
1801 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1802 "readwrite" :
1803 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1804 "assign" :
1805 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1806 "copy" : "retain";
Mike Stump11289f42009-09-09 15:08:12 +00001807
1808 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner409f5552009-01-29 18:49:48 +00001809 diag::err_objc_property_attr_mutually_exclusive :
1810 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00001811 << "readonly" << which;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001812 }
1813
1814 // Check for copy or retain on non-object types.
1815 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump11289f42009-09-09 15:08:12 +00001816 !PropertyTy->isObjCObjectPointerType() &&
1817 !PropertyTy->isBlockPointerType() &&
Steve Naroff79d12152009-07-16 15:41:00 +00001818 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001819 Diag(Loc, diag::err_objc_property_requires_object)
1820 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001821 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1822 }
1823
1824 // Check for more than one of { assign, copy, retain }.
1825 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1826 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001827 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1828 << "assign" << "copy";
1829 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump11289f42009-09-09 15:08:12 +00001830 }
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001831 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001832 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1833 << "assign" << "retain";
1834 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001835 }
1836 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1837 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001838 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1839 << "copy" << "retain";
1840 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001841 }
1842 }
1843
1844 // Warn if user supplied no assignment attribute, property is
1845 // readwrite, and this is an object type.
1846 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1847 ObjCDeclSpec::DQ_PR_retain)) &&
1848 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Naroff79d12152009-07-16 15:41:00 +00001849 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001850 // Skip this warning in gc-only mode.
Mike Stump11289f42009-09-09 15:08:12 +00001851 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001852 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1853
1854 // If non-gc code warn that this is likely inappropriate.
1855 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1856 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump11289f42009-09-09 15:08:12 +00001857
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001858 // FIXME: Implement warning dependent on NSCopying being
1859 // implemented. See also:
1860 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1861 // (please trim this list while you are at it).
1862 }
Mike Stump5580bdc2009-05-07 23:06:50 +00001863
1864 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1865 && getLangOptions().getGCMode() == LangOptions::GCOnly
1866 && PropertyTy->isBlockPointerType())
1867 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001868}
1869
Mike Stump11289f42009-09-09 15:08:12 +00001870Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001871 FieldDeclarator &FD,
1872 ObjCDeclSpec &ODS,
1873 Selector GetterSel,
1874 Selector SetterSel,
1875 DeclPtrTy ClassCategory,
1876 bool *isOverridingProperty,
1877 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001878 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001879 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1880 // default is readwrite!
1881 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump11289f42009-09-09 15:08:12 +00001882 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001883 // not retain or copy
1884 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump11289f42009-09-09 15:08:12 +00001885 (isReadWrite &&
1886 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001887 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1888 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattner83f095c2009-03-28 19:18:32 +00001889 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001890 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001891 // May modify Attributes.
1892 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001893 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1894 if (!CDecl->getIdentifier()) {
Mike Stump11289f42009-09-09 15:08:12 +00001895 // This is a continuation class. property requires special
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001896 // handling.
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001897 if ((CCPrimary = CDecl->getClassInterface())) {
1898 // Find the property in continuation class's primary class only.
1899 ObjCPropertyDecl *PIDecl = 0;
1900 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00001901 for (ObjCInterfaceDecl::prop_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001902 I = CCPrimary->prop_begin(), E = CCPrimary->prop_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001903 I != E; ++I)
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001904 if ((*I)->getIdentifier() == PropertyId) {
1905 PIDecl = *I;
1906 break;
1907 }
Mike Stump11289f42009-09-09 15:08:12 +00001908
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001909 if (PIDecl) {
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001910 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahaniane4fd6402009-04-01 23:23:53 +00001911 // with continuation class's readwrite property attribute!
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001912 unsigned PIkind = PIDecl->getPropertyAttributes();
1913 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian36859952008-12-08 18:47:29 +00001914 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001915 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1916 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001917 PIDecl->makeitReadWriteAttribute();
1918 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1919 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1920 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1921 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1922 PIDecl->setSetterName(SetterSel);
Mike Stump12b8ce12009-08-04 21:02:39 +00001923 } else
Mike Stump11289f42009-09-09 15:08:12 +00001924 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001925 << CCPrimary->getDeclName();
1926 *isOverridingProperty = true;
Mike Stump11289f42009-09-09 15:08:12 +00001927 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian47b21082009-04-15 19:19:03 +00001928 // class's list.
1929 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001930 return DeclPtrTy();
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001931 }
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001932 // No matching property found in the primary class. Just fall thru
1933 // and add property to continuation class's primary class.
1934 ClassDecl = CCPrimary;
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001935 } else {
Chris Lattner68d42c72009-02-20 21:38:52 +00001936 Diag(CDecl->getLocation(), diag::err_continuation_class);
1937 *isOverridingProperty = true;
Chris Lattner83f095c2009-03-28 19:18:32 +00001938 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001939 }
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001940 }
Mike Stump11289f42009-09-09 15:08:12 +00001941
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001942 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump11289f42009-09-09 15:08:12 +00001943 // gc'able conforms to NSCopying protocol
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001944 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
1945 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
1946 if (T->isObjCObjectPointerType()) {
1947 QualType InterfaceTy = T->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +00001948 if (const ObjCInterfaceType *OIT =
John McCall9dd450b2009-09-21 23:43:11 +00001949 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian83b000c2009-08-14 18:06:25 +00001950 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001951 if (IDecl)
Mike Stump11289f42009-09-09 15:08:12 +00001952 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001953 LookupProtocol(&Context.Idents.get("NSCopying")))
1954 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump11289f42009-09-09 15:08:12 +00001955 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001956 << FD.D.getIdentifier();
Fariborz Jahanian83b000c2009-08-14 18:06:25 +00001957 }
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001958 }
Fariborz Jahanian887cd6a2009-08-12 18:17:53 +00001959 if (T->isObjCInterfaceType())
1960 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump11289f42009-09-09 15:08:12 +00001961
Steve Naroffba3dc382009-01-11 12:47:58 +00001962 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1963 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattner5300acc2009-04-11 20:14:49 +00001964 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump11289f42009-09-09 15:08:12 +00001965 FD.D.getIdentifierLoc(),
Fariborz Jahanian0152a1a2008-04-14 23:36:35 +00001966 FD.D.getIdentifier(), T);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001967 DC->addDecl(PDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001968
Chris Lattner5300acc2009-04-11 20:14:49 +00001969 if (T->isArrayType() || T->isFunctionType()) {
1970 Diag(AtLoc, diag::err_property_type) << T;
1971 PDecl->setInvalidDecl();
1972 }
Mike Stump11289f42009-09-09 15:08:12 +00001973
Douglas Gregor758a8692009-06-17 21:51:59 +00001974 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001975
Fariborz Jahanianec6e4c82008-05-07 17:43:59 +00001976 // Regardless of setter/getter attribute, we save the default getter/setter
1977 // selector names in anticipation of declaration of setter/getter methods.
1978 PDecl->setGetterName(GetterSel);
1979 PDecl->setSetterName(SetterSel);
Mike Stump11289f42009-09-09 15:08:12 +00001980
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001981 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001982 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump11289f42009-09-09 15:08:12 +00001983
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001984 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001985 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump11289f42009-09-09 15:08:12 +00001986
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001987 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001988 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump11289f42009-09-09 15:08:12 +00001989
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001990 if (isReadWrite)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001991 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump11289f42009-09-09 15:08:12 +00001992
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001993 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001994 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump11289f42009-09-09 15:08:12 +00001995
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001996 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001997 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump11289f42009-09-09 15:08:12 +00001998
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001999 if (isAssign)
2000 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump11289f42009-09-09 15:08:12 +00002001
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002002 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002003 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump11289f42009-09-09 15:08:12 +00002004
Fariborz Jahanian8d916862008-05-05 18:51:55 +00002005 if (MethodImplKind == tok::objc_required)
2006 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2007 else if (MethodImplKind == tok::objc_optional)
2008 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002009 // A case of continuation class adding a new property in the class. This
2010 // is not what it was meant for. However, gcc supports it and so should we.
2011 // Make sure setter/getters are declared here.
2012 if (CCPrimary)
2013 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump11289f42009-09-09 15:08:12 +00002014
Chris Lattner83f095c2009-03-28 19:18:32 +00002015 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002016}
2017
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002018/// ActOnPropertyImplDecl - This routine performs semantic checks and
2019/// builds the AST node for a property implementation declaration; declared
2020/// as @synthesize or @dynamic.
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002021///
Mike Stump11289f42009-09-09 15:08:12 +00002022Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00002023 SourceLocation PropertyLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002024 bool Synthesize,
Chris Lattner83f095c2009-03-28 19:18:32 +00002025 DeclPtrTy ClassCatImpDecl,
2026 IdentifierInfo *PropertyId,
2027 IdentifierInfo *PropertyIvar) {
2028 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002029 // Make sure we have a context for the property implementation declaration.
2030 if (!ClassImpDecl) {
2031 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner83f095c2009-03-28 19:18:32 +00002032 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002033 }
2034 ObjCPropertyDecl *property = 0;
2035 ObjCInterfaceDecl* IDecl = 0;
2036 // Find the class or category class where this property must have
2037 // a declaration.
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002038 ObjCImplementationDecl *IC = 0;
2039 ObjCCategoryImplDecl* CatImplClass = 0;
2040 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002041 IDecl = IC->getClassInterface();
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002042 // We always synthesize an interface for an implementation
2043 // without an interface decl. So, IDecl is always non-zero.
Mike Stump11289f42009-09-09 15:08:12 +00002044 assert(IDecl &&
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002045 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump11289f42009-09-09 15:08:12 +00002046
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002047 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002048 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002049 if (!property) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002050 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner83f095c2009-03-28 19:18:32 +00002051 return DeclPtrTy();
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002052 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002053 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002054 if (Synthesize) {
2055 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner83f095c2009-03-28 19:18:32 +00002056 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002057 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002058 IDecl = CatImplClass->getClassInterface();
2059 if (!IDecl) {
2060 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner83f095c2009-03-28 19:18:32 +00002061 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002062 }
Mike Stump11289f42009-09-09 15:08:12 +00002063 ObjCCategoryDecl *Category =
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002064 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump11289f42009-09-09 15:08:12 +00002065
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002066 // If category for this implementation not found, it is an error which
2067 // has already been reported eralier.
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002068 if (!Category)
Chris Lattner83f095c2009-03-28 19:18:32 +00002069 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002070 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002071 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002072 if (!property) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002073 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002074 << Category->getDeclName();
Chris Lattner83f095c2009-03-28 19:18:32 +00002075 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002076 }
Chris Lattner83f095c2009-03-28 19:18:32 +00002077 } else {
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002078 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner83f095c2009-03-28 19:18:32 +00002079 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002080 }
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002081 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002082 // Check that we have a valid, previously declared ivar for @synthesize
2083 if (Synthesize) {
2084 // @synthesize
Fariborz Jahanianc6bec7b2008-04-21 21:57:36 +00002085 if (!PropertyIvar)
2086 PropertyIvar = PropertyId;
Fariborz Jahanianb35b4a92009-03-31 00:06:29 +00002087 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002088 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanianb6d5b542009-04-13 19:30:37 +00002089 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002090 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002091 if (!Ivar) {
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002092 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002093 assert(EnclosingContext &&
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002094 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump11289f42009-09-09 15:08:12 +00002095 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2096 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002097 ObjCIvarDecl::Public,
2098 (Expr *)0);
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002099 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002100 IDecl->addDecl(Ivar);
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002101 property->setPropertyIvarDecl(Ivar);
2102 if (!getLangOptions().ObjCNonFragileABI)
Steve Naroffc03f6b92009-03-03 22:09:41 +00002103 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump11289f42009-09-09 15:08:12 +00002104 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002105 // a property implementation and to avoid future warnings.
Mike Stump12b8ce12009-08-04 21:02:39 +00002106 } else if (getLangOptions().ObjCNonFragileABI &&
2107 ClassDeclared != IDecl) {
Fariborz Jahanian68592fc2009-04-30 21:39:24 +00002108 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump11289f42009-09-09 15:08:12 +00002109 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanianb6d5b542009-04-13 19:30:37 +00002110 << ClassDeclared->getDeclName();
2111 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2112 << Ivar << Ivar->getNameAsCString();
2113 // Note! I deliberately want it to fall thru so more errors are caught.
2114 }
Steve Naroff9f9774c2008-09-30 10:07:56 +00002115 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002116
Steve Naroff506e7172008-09-30 00:24:17 +00002117 // Check that type of property and its ivar are type compatible.
Steve Naroff9f9774c2008-09-30 10:07:56 +00002118 if (PropType != IvarType) {
Steve Naroff9546eee2008-10-16 14:59:30 +00002119 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002120 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002121 << property->getDeclName() << Ivar->getDeclName();
Mike Stump11289f42009-09-09 15:08:12 +00002122 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002123 // a property implementation and to avoid future warnings.
Steve Naroff9f9774c2008-09-30 10:07:56 +00002124 }
Mike Stump11289f42009-09-09 15:08:12 +00002125
Chris Lattner83f095c2009-03-28 19:18:32 +00002126 // FIXME! Rules for properties are somewhat different that those
2127 // for assignments. Use a new routine to consolidate all cases;
2128 // specifically for property redeclarations as well as for ivars.
2129 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2130 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump11289f42009-09-09 15:08:12 +00002131 if (lhsType != rhsType &&
Chris Lattner83f095c2009-03-28 19:18:32 +00002132 lhsType->isArithmeticType()) {
2133 Diag(PropertyLoc, diag::error_property_ivar_type)
2134 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002135 // Fall thru - see previous comment
Chris Lattner83f095c2009-03-28 19:18:32 +00002136 }
2137 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanian9ecb84b2009-04-07 21:25:06 +00002138 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2139 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002140 Diag(PropertyLoc, diag::error_weak_property)
2141 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002142 // Fall thru - see previous comment
Chris Lattner83f095c2009-03-28 19:18:32 +00002143 }
Mike Stump11289f42009-09-09 15:08:12 +00002144 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian60331be2009-04-10 22:42:54 +00002145 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2146 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002147 Diag(PropertyLoc, diag::error_strong_property)
2148 << property->getDeclName() << Ivar->getDeclName();
Mike Stump11289f42009-09-09 15:08:12 +00002149 // Fall thru - see previous comment
Fariborz Jahanian54fa4182009-01-19 20:13:47 +00002150 }
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002151 }
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002152 } else if (PropertyIvar)
2153 // @dynamic
2154 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002155 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump11289f42009-09-09 15:08:12 +00002156 ObjCPropertyImplDecl *PIDecl =
2157 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2158 property,
2159 (Synthesize ?
2160 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002161 : ObjCPropertyImplDecl::Dynamic),
2162 Ivar);
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002163 if (IC) {
2164 if (Synthesize)
Mike Stump11289f42009-09-09 15:08:12 +00002165 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002166 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump11289f42009-09-09 15:08:12 +00002167 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2168 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002169 << PropertyIvar;
2170 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2171 }
Mike Stump11289f42009-09-09 15:08:12 +00002172
2173 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002174 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002175 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2176 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +00002177 return DeclPtrTy();
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002178 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002179 IC->addPropertyImplementation(PIDecl);
Mike Stump12b8ce12009-08-04 21:02:39 +00002180 } else {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002181 if (Synthesize)
Mike Stump11289f42009-09-09 15:08:12 +00002182 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002183 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump11289f42009-09-09 15:08:12 +00002184 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2185 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002186 << PropertyIvar;
2187 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2188 }
Mike Stump11289f42009-09-09 15:08:12 +00002189
2190 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002191 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002192 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2193 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +00002194 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002195 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002196 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002197 }
Mike Stump11289f42009-09-09 15:08:12 +00002198
Chris Lattner83f095c2009-03-28 19:18:32 +00002199 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002200}
Anders Carlssona6b508a2008-11-04 16:57:32 +00002201
Chris Lattner438e5012008-12-17 07:13:27 +00002202bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00002203 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlssona6b508a2008-11-04 16:57:32 +00002204 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002205
Anders Carlssona6b508a2008-11-04 16:57:32 +00002206 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2207 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00002208
Anders Carlssona6b508a2008-11-04 16:57:32 +00002209 return true;
2210}
Chris Lattner438e5012008-12-17 07:13:27 +00002211
Chris Lattner438e5012008-12-17 07:13:27 +00002212/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2213/// instance variables of ClassName into Decls.
Mike Stump11289f42009-09-09 15:08:12 +00002214void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00002215 IdentifierInfo *ClassName,
Chris Lattner83f095c2009-03-28 19:18:32 +00002216 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00002217 // Check that ClassName is a valid class
2218 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2219 if (!Class) {
2220 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2221 return;
2222 }
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00002223 if (LangOpts.ObjCNonFragileABI) {
2224 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2225 return;
2226 }
Mike Stump11289f42009-09-09 15:08:12 +00002227
Chris Lattner438e5012008-12-17 07:13:27 +00002228 // Collect the instance variables
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00002229 llvm::SmallVector<FieldDecl*, 32> RecFields;
2230 Context.CollectObjCIvars(Class, RecFields);
2231 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2232 for (unsigned i = 0; i < RecFields.size(); i++) {
2233 FieldDecl* ID = RecFields[i];
2234 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2235 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2236 ID->getIdentifier(), ID->getType(),
2237 ID->getBitWidth());
2238 Decls.push_back(Sema::DeclPtrTy::make(FD));
2239 }
Mike Stump11289f42009-09-09 15:08:12 +00002240
Chris Lattner438e5012008-12-17 07:13:27 +00002241 // Introduce all of these fields into the appropriate scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002242 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00002243 D != Decls.end(); ++D) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002244 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner438e5012008-12-17 07:13:27 +00002245 if (getLangOptions().CPlusPlus)
2246 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner83f095c2009-03-28 19:18:32 +00002247 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002248 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00002249 }
2250}
2251