blob: 001190513fa52e3b74b9f311c3370b789e0f7248 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Lattner4d391482007-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 Gregorf0aaf7a2009-04-24 21:10:55 +000015#include "clang/Sema/ExternalSemaSource.h"
Steve Naroffca331292009-03-03 14:49:36 +000016#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000020using namespace clang;
21
Fariborz Jahanianc001e892009-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 Narofff4954562009-07-16 15:41:00 +000028 if (property->getType()->isObjCObjectPointerType())
Fariborz Jahanian7aaa4092009-05-08 21:10:00 +000029 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
Fariborz Jahanianc001e892009-05-08 20:20:55 +000030 if (result != Compatible) {
31 Diag(Loc, diag::warn_accessor_property_type_mismatch)
32 << property->getDeclName()
33 << GetterMethod->getSelector();
34 Diag(GetterMethod->getLocation(), diag::note_declared_at);
35 return true;
36 }
37 }
38 return false;
39}
40
Steve Naroffebf64432009-02-28 16:59:13 +000041/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000042/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000043void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000044 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000045 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff394f3f42008-07-25 17:57:26 +000046
47 // If we don't have a valid method decl, simply return.
48 if (!MDecl)
49 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000050
Chris Lattner38c5ebd2009-04-19 05:21:20 +000051 CurFunctionNeedsScopeChecking = false;
52
Steve Naroffa56f6162007-12-18 01:30:32 +000053 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000054 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000055 AddInstanceMethodToGlobalPool(MDecl);
56 else
57 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000058
59 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000060 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-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 Lattner4d391482007-12-12 07:09:47 +000064
65 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000066 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000067
Daniel Dunbar451318c2008-08-26 06:07:48 +000068 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
69 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000070
Chris Lattner8123a952008-04-10 02:22:51 +000071 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-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 Lattner4d391482007-12-12 07:09:47 +000076}
77
Chris Lattnerb28317a2009-03-28 19:18:32 +000078Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000079ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
80 IdentifierInfo *ClassName, SourceLocation ClassLoc,
81 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000082 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000083 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000084 assert(ClassName && "Missing class identifier");
85
86 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000087 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000088 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-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 Kremeneka526c5c2008-01-07 19:49:32 +000095 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000096 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000097 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000098 }
99
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000100 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000101 if (IDecl) {
102 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000103 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000104 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000105 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000106 Diag(IDecl->getLocation(), diag::note_previous_definition);
107
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000108 // Return the previous class interface.
109 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000110 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000111 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000112 IDecl->setLocation(AtInterfaceLoc);
113 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000114 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000115 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000116 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000117 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000118 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000119 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000120
Steve Naroffa7503a72009-04-23 15:15:40 +0000121 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000122 }
123
124 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000125 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000126 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000127 if (PrevDecl == IDecl) {
128 Diag(SuperLoc, diag::err_recursive_superclass)
129 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
130 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000131 } else {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000132 ObjCInterfaceDecl *SuperClassDecl =
133 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000134
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000135 // Diagnose classes that inherit from deprecated classes.
136 if (SuperClassDecl)
137 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000138
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000139 if (PrevDecl && SuperClassDecl == 0) {
140 // The previous declaration was not a class decl. Check if we have a
141 // typedef. If we do, get the underlying class type.
142 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
143 QualType T = TDecl->getUnderlyingType();
144 if (T->isObjCInterfaceType()) {
145 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
146 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
147 }
148 }
149
150 // This handles the following case:
151 //
152 // typedef int SuperClass;
153 // @interface MyClass : SuperClass {} @end
154 //
155 if (!SuperClassDecl) {
156 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
157 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000158 }
159 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000160
161 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
162 if (!SuperClassDecl)
163 Diag(SuperLoc, diag::err_undef_superclass)
164 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
165 else if (SuperClassDecl->isForwardDecl())
166 Diag(SuperLoc, diag::err_undef_superclass)
167 << SuperClassDecl->getDeclName() << ClassName
168 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000169 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000170 IDecl->setSuperClass(SuperClassDecl);
171 IDecl->setSuperClassLoc(SuperLoc);
172 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000173 }
Chris Lattner4d391482007-12-12 07:09:47 +0000174 } else { // we have a root class.
175 IDecl->setLocEnd(ClassLoc);
176 }
177
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000178 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000179 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000180 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
181 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000182 IDecl->setLocEnd(EndProtoLoc);
183 }
Anders Carlsson15281452008-11-04 16:57:32 +0000184
185 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000186 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000187}
188
189/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000190/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000191Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
192 IdentifierInfo *AliasName,
193 SourceLocation AliasLocation,
194 IdentifierInfo *ClassName,
195 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000196 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000197 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000198 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000199 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000200 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000201 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000202 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000203 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000204 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000205 }
206 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000207 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000208 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
209 QualType T = TDecl->getUnderlyingType();
210 if (T->isObjCInterfaceType()) {
211 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
212 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000213 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000214 }
215 }
216 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000217 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
218 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000219 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000220 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000221 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000222 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000223 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000224
225 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000226 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000227 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000228
Anders Carlsson15281452008-11-04 16:57:32 +0000229 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000230 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000231
Chris Lattnerb28317a2009-03-28 19:18:32 +0000232 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000233}
234
Steve Naroff61d68522009-03-05 15:22:01 +0000235void Sema::CheckForwardProtocolDeclarationForCircularDependency(
236 IdentifierInfo *PName,
237 SourceLocation &Ploc, SourceLocation PrevLoc,
238 const ObjCList<ObjCProtocolDecl> &PList)
239{
240 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
241 E = PList.end(); I != E; ++I) {
242
Douglas Gregor6e378de2009-04-23 23:18:26 +0000243 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-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 }
248 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
249 PDecl->getLocation(), PDecl->getReferencedProtocols());
250 }
251 }
252}
253
Chris Lattnerb28317a2009-03-28 19:18:32 +0000254Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000255Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
256 IdentifierInfo *ProtocolName,
257 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000258 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000259 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000260 SourceLocation EndProtoLoc,
261 AttributeList *AttrList) {
262 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000263 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000264 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000265 if (PDecl) {
266 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000267 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000268 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000269 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000270 // Just return the protocol we already had.
271 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000272 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000273 }
Steve Naroff61d68522009-03-05 15:22:01 +0000274 ObjCList<ObjCProtocolDecl> PList;
275 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
276 CheckForwardProtocolDeclarationForCircularDependency(
277 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
278 PList.Destroy(Context);
279
Steve Narofff11b5082008-08-13 16:39:22 +0000280 // Make sure the cached decl gets a valid start location.
281 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000282 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000283 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000284 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
285 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000286 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000287 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000288 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000289 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000290 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000291 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000292 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000293 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000294 PDecl->setLocEnd(EndProtoLoc);
295 }
Anders Carlsson15281452008-11-04 16:57:32 +0000296
297 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000298 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000299}
300
301/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-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 Lattner4d391482007-12-12 07:09:47 +0000304void
Chris Lattnere13b9592008-07-26 04:03:38 +0000305Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000306 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000307 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000308 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000309 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000310 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000311 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000312 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000313 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000314 continue;
315 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000316
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000317 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-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 Lattnerfa25bbb2008-11-19 05:08:23 +0000322 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000323 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000324 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000325 }
326}
327
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000328/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000329/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000330///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000331void
332Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
333 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000334 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000335 ObjCPropertyDecl::PropertyAttributeKind CAttr =
336 Property->getPropertyAttributes();
337 ObjCPropertyDecl::PropertyAttributeKind SAttr =
338 SuperProperty->getPropertyAttributes();
339 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
340 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000341 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000342 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000343 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
344 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000345 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000346 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000347 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
348 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000349 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000350 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000351
352 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
353 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000354 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000355 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000356 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000357 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000358 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000359 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000360 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000361 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000362
363 QualType LHSType =
364 Context.getCanonicalType(SuperProperty->getType());
365 QualType RHSType =
366 Context.getCanonicalType(Property->getType());
367
368 if (!Context.typesAreCompatible(LHSType, RHSType)) {
369 // FIXME: Incorporate this test with typesAreCompatible.
370 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff4084c302009-07-23 01:01:38 +0000371 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff15edf0d2009-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 Jahanianb5e02242008-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 Lattner70f19542009-02-16 21:26:43 +0000382void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000383 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
384 if (!SDecl)
385 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000386 // FIXME: O(N^2)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000387 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
388 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000389 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000390 // Does property in super class has declaration in current class?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000391 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
392 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000393 ObjCPropertyDecl *PDecl = (*I);
394 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000395 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000396 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000397 }
398 }
399}
400
Fariborz Jahanianaebf0cb2008-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 Jahanian1ac2bc42008-12-06 23:03:39 +0000403/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000404void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000405Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000406 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-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 Kyrtzidis17945a02009-06-30 02:36:12 +0000412 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
413 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000414 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000415 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000416 // Is this property already in category's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000417 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000418 if ((*CP)->getIdentifier() == Pr->getIdentifier())
419 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000420 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000421 // Property protocol already exist in class. Diagnose any mismatch.
422 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
423 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000424 return;
425 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000426 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
427 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000428 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000429 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000430 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000431 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000432 if ((*CP)->getIdentifier() == Pr->getIdentifier())
433 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000434 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000435 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000436 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000437 }
Fariborz Jahanianaebf0cb2008-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 Jahanian1ac2bc42008-12-06 23:03:39 +0000442/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000443///
Chris Lattner70f19542009-02-16 21:26:43 +0000444void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000445 DeclPtrTy MergeItsProtocols) {
446 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-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);
458
459 // 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 Lattnerb28317a2009-03-28 19:18:32 +0000463 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-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 Lattnerb28317a2009-03-28 19:18:32 +0000468 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000469 }
470 return;
471 }
472
Chris Lattnerb752f282008-07-21 07:06:49 +0000473 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000474 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
475 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000476 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000477 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
478
Fariborz Jahanianaebf0cb2008-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 Lattnerb28317a2009-03-28 19:18:32 +0000483 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000484 } else {
Argyrios Kyrtzidise8f0d302008-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 Lattnerb28317a2009-03-28 19:18:32 +0000488 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000489 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000490}
491
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000492/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000493/// a class method in its extension.
494///
495void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
496 ObjCInterfaceDecl *ID) {
497 if (!ID)
498 return; // Possibly due to previous error
499
500 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000501 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
502 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000503 ObjCMethodDecl *MD = *i;
504 MethodMap[MD->getSelector()] = MD;
505 }
506
507 if (MethodMap.empty())
508 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000509 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
510 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-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 Lattner58fe03b2009-04-12 08:43:13 +0000521/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000522Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000523Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000524 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000525 unsigned NumElts,
526 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000527 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000528
529 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000530 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000531 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000532 if (PDecl == 0) { // Not already seen?
533 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
534 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000535 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000536 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000537 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000538 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000539 Protocols.push_back(PDecl);
540 }
Anders Carlsson15281452008-11-04 16:57:32 +0000541
542 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000543 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000544 &Protocols[0], Protocols.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000545 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000546 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000547 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000548}
549
Chris Lattnerb28317a2009-03-28 19:18:32 +0000550Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000551ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
552 IdentifierInfo *ClassName, SourceLocation ClassLoc,
553 IdentifierInfo *CategoryName,
554 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000555 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000556 unsigned NumProtoRefs,
557 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000558 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000559 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
560 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000561 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000562
563 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000564 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000565 if (!IDecl || IDecl->isForwardDecl()) {
566 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000567 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000568 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000569 }
Chris Lattner4d391482007-12-12 07:09:47 +0000570
Chris Lattner70f19542009-02-16 21:26:43 +0000571 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000572
573 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000574 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-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 Lattner4d391482007-12-12 07:09:47 +0000590 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000591 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000592 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000593 }
Anders Carlsson15281452008-11-04 16:57:32 +0000594
595 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000596 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000597}
598
599/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000600/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000601/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000602Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000603 SourceLocation AtCatImplLoc,
604 IdentifierInfo *ClassName, SourceLocation ClassLoc,
605 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000606 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000607 ObjCCategoryDecl *CatIDecl = 0;
608 if (IDecl) {
609 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
610 if (!CatIDecl) {
611 // Category @implementation with no corresponding @interface.
612 // Create and install one.
613 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
614 CatName);
615 CatIDecl->setClassInterface(IDecl);
616 CatIDecl->insertNextClassCategory();
617 }
618 }
619
Chris Lattner75c9cae2008-03-16 20:53:07 +0000620 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000621 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
622 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000623 /// Check that class of this category is already completely declared.
624 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000625 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000626
Douglas Gregord0434102009-01-09 00:49:46 +0000627 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000628 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000629
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000630 /// Check that CatName, category name, is not used in another implementation.
631 if (CatIDecl) {
632 if (CatIDecl->getImplementation()) {
633 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
634 << CatName;
635 Diag(CatIDecl->getImplementation()->getLocation(),
636 diag::note_previous_definition);
637 } else
638 CatIDecl->setImplementation(CDecl);
639 }
Anders Carlsson15281452008-11-04 16:57:32 +0000640
641 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000642 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000643}
644
Chris Lattnerb28317a2009-03-28 19:18:32 +0000645Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000646 SourceLocation AtClassImplLoc,
647 IdentifierInfo *ClassName, SourceLocation ClassLoc,
648 IdentifierInfo *SuperClassname,
649 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000650 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000651 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000652 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000653 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000654 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000655 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000656 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000657 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000658 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000659 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000660 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000661 IDecl = 0;
662 }
Chris Lattner4d391482007-12-12 07:09:47 +0000663 }
664
665 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000666 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000667 if (SuperClassname) {
668 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000669 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000670 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000671 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
672 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000673 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000674 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000675 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000676 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000677 Diag(SuperClassLoc, diag::err_undef_superclass)
678 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000679 else if (IDecl && IDecl->getSuperClass() != SDecl) {
680 // This implementation and its interface do not have the same
681 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000682 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000683 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000684 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000685 }
686 }
687 }
688
689 if (!IDecl) {
690 // Legacy case of @implementation with no corresponding @interface.
691 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000692
Mike Stump390b4cc2009-05-16 07:39:55 +0000693 // FIXME: Do we support attributes on the @implementation? If so we should
694 // copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000695 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
696 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000697 IDecl->setSuperClass(SDecl);
698 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000699
700 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000701 } else {
702 // Mark the interface as being completed, even if it was just as
703 // @class ....;
704 // declaration; the user cannot reopen it.
705 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000706 }
707
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000708 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000709 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000710 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000711
Anders Carlsson15281452008-11-04 16:57:32 +0000712 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000713 return DeclPtrTy::make(IMPDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000714
Chris Lattner4d391482007-12-12 07:09:47 +0000715 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000716 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000717 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000718 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000719 Diag(IDecl->getImplementation()->getLocation(),
720 diag::note_previous_definition);
721 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000722 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000723 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000724 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000725 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000726}
727
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000728void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
729 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000730 SourceLocation RBrace) {
731 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000732 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000733 if (!IDecl)
734 return;
735 /// Check case of non-existing @interface decl.
736 /// (legacy objective-c @implementation decl without an @interface decl).
737 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000738 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000739 IDecl->setIVarList(ivars, numIvars, Context);
740 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000741 return;
742 }
743 // If implementation has empty ivar list, just return.
744 if (numIvars == 0)
745 return;
746
747 assert(ivars && "missing @implementation ivars");
748
749 // Check interface's Ivar list against those in the implementation.
750 // names and types must match.
751 //
Chris Lattner4d391482007-12-12 07:09:47 +0000752 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000753 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000754 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
755 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000756 ObjCIvarDecl* ImplIvar = ivars[j++];
757 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000758 assert (ImplIvar && "missing implementation ivar");
759 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000760
761 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000762 if (Context.getCanonicalType(ImplIvar->getType()) !=
763 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000764 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000765 << ImplIvar->getIdentifier()
766 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000767 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000768 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
769 Expr *ImplBitWidth = ImplIvar->getBitWidth();
770 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000771 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
772 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000773 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
774 << ImplIvar->getIdentifier();
775 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
776 }
777 }
778 // Make sure the names are identical.
779 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000780 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000781 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000782 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000783 }
784 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000785 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000786
787 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000788 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000789 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000790 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000791}
792
Steve Naroff3c2eb662008-02-10 21:38:56 +0000793void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
794 bool &IncompleteImpl) {
795 if (!IncompleteImpl) {
796 Diag(ImpLoc, diag::warn_incomplete_impl);
797 IncompleteImpl = true;
798 }
Chris Lattner08631c52008-11-23 21:45:46 +0000799 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000800}
801
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000802void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
803 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000804 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000805 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000806 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
807 ImpMethodDecl->getResultType())) {
Chris Lattner3aff9192009-04-11 19:58:42 +0000808 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
809 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
810 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000811 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
812 }
Chris Lattner3aff9192009-04-11 19:58:42 +0000813
814 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
815 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
816 IM != EM; ++IM, ++IF) {
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000817 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()) ||
Steve Naroff4084c302009-07-23 01:01:38 +0000818 Context.QualifiedIdConformsQualifiedId((*IF)->getType(),
819 (*IM)->getType()))
Chris Lattner3aff9192009-04-11 19:58:42 +0000820 continue;
821
822 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
823 << ImpMethodDecl->getDeclName() << (*IF)->getType()
824 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000825 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000826 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000827}
828
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000829/// isPropertyReadonly - Return true if property is readonly, by searching
830/// for the property in the class and in its categories and implementations
831///
832bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000833 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000834 // by far the most common case.
835 if (!PDecl->isReadOnly())
836 return false;
837 // Even if property is ready only, if interface has a user defined setter,
838 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000839 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000840 return false;
841
842 // Main class has the property as 'readonly'. Must search
843 // through the category list to see if the property's
844 // attribute has been over-ridden to 'readwrite'.
845 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
846 Category; Category = Category->getNextClassCategory()) {
847 // Even if property is ready only, if a category has a user defined setter,
848 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000849 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000850 return false;
851 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000852 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000853 if (P && !P->isReadOnly())
854 return false;
855 }
856
857 // Also, check for definition of a setter method in the implementation if
858 // all else failed.
859 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
860 if (ObjCImplementationDecl *IMD =
861 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000862 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000863 return false;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000864 } else if (ObjCCategoryImplDecl *CIMD =
865 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000866 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000867 return false;
868 }
869 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000870 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000871 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000872 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000873 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000874 // If all fails, look at the super class.
875 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
876 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000877 return true;
878}
879
Mike Stump390b4cc2009-05-16 07:39:55 +0000880/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
881/// improve the efficiency of selector lookups and type checking by associating
882/// with each protocol / interface / category the flattened instance tables. If
883/// we used an immutable set to keep the table then it wouldn't add significant
884/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000885
Steve Naroffefe7f362008-02-08 22:06:17 +0000886/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000887/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000888void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
889 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000890 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000891 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000892 const llvm::DenseSet<Selector> &ClsMap,
893 ObjCInterfaceDecl *IDecl) {
894 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000895 ObjCInterfaceDecl *NSIDecl = 0;
896 if (getLangOptions().NeXTRuntime) {
897 // check to see if class implements forwardInvocation method and objects
898 // of this class are derived from 'NSProxy' so that to forward requests
899 // from one object to another.
900 // Under such conditions, which means that every method possible is
901 // implemented in the class, we should not issue "Method definition not
902 // found" warnings.
903 // FIXME: Use a general GetUnarySelector method for this.
904 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
905 Selector fISelector = Context.Selectors.getSelector(1, &II);
906 if (InsMap.count(fISelector))
907 // Is IDecl derived from 'NSProxy'? If so, no instance methods
908 // need be implemented in the implementation.
909 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
910 }
911
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000912 // If a method lookup fails locally we still need to look and see if
913 // the method was implemented by a base class or an inherited
914 // protocol. This lookup is slow, but occurs rarely in correct code
915 // and otherwise would terminate in a warning.
916
Chris Lattner4d391482007-12-12 07:09:47 +0000917 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000918 if (!NSIDecl)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000919 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
920 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000921 ObjCMethodDecl *method = *I;
922 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
923 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
924 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000925 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000926 // Ugly, but necessary. Method declared in protcol might have
927 // have been synthesized due to a property declared in the class which
928 // uses the protocol.
929 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000930 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000931 if (!MethodInClass || !MethodInClass->isSynthesized())
932 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
933 }
934 }
Chris Lattner4d391482007-12-12 07:09:47 +0000935 // check unimplemented class methods
Douglas Gregor6ab35242009-04-09 21:40:53 +0000936 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000937 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000938 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000939 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000940 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
941 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000942 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000943 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000944 }
Chris Lattner780f3292008-07-21 21:32:27 +0000945 // Check on this protocols's referenced protocols, recursively.
946 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
947 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000948 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000949}
950
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000951/// MatchAllMethodDeclarations - Check methods declaraed in interface or
952/// or protocol against those declared in their implementations.
953///
954void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
955 const llvm::DenseSet<Selector> &ClsMap,
956 llvm::DenseSet<Selector> &InsMapSeen,
957 llvm::DenseSet<Selector> &ClsMapSeen,
958 ObjCImplDecl* IMPDecl,
959 ObjCContainerDecl* CDecl,
960 bool &IncompleteImpl,
961 bool ImmediateClass)
962{
963 // Check and see if instance methods in class interface have been
964 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000965 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
966 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000967 if (InsMapSeen.count((*I)->getSelector()))
968 continue;
969 InsMapSeen.insert((*I)->getSelector());
970 if (!(*I)->isSynthesized() &&
971 !InsMap.count((*I)->getSelector())) {
972 if (ImmediateClass)
973 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
974 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000975 } else {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000976 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000977 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000978 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000979 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000980 assert(IntfMethodDecl &&
981 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
982 // ImpMethodDecl may be null as in a @dynamic property.
983 if (ImpMethodDecl)
984 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
985 }
986 }
987
988 // Check and see if class methods in class interface have been
989 // implemented in the implementation class. If so, their types match.
990 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000991 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000992 if (ClsMapSeen.count((*I)->getSelector()))
993 continue;
994 ClsMapSeen.insert((*I)->getSelector());
995 if (!ClsMap.count((*I)->getSelector())) {
996 if (ImmediateClass)
997 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000998 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000999 ObjCMethodDecl *ImpMethodDecl =
1000 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001001 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001002 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001003 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1004 }
1005 }
1006 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1007 // Check for any implementation of a methods declared in protocol.
1008 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1009 E = I->protocol_end(); PI != E; ++PI)
1010 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1011 IMPDecl,
1012 (*PI), IncompleteImpl, false);
1013 if (I->getSuperClass())
1014 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1015 IMPDecl,
1016 I->getSuperClass(), IncompleteImpl, false);
1017 }
1018}
1019
Chris Lattnercddc8882009-03-01 00:56:52 +00001020void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1021 ObjCContainerDecl* CDecl,
1022 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001023 llvm::DenseSet<Selector> InsMap;
1024 // Check and see if instance methods in class interface have been
1025 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001026 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001027 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001028 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +00001029
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001030 // Check and see if properties declared in the interface have either 1)
1031 // an implementation or 2) there is a @synthesize/@dynamic implementation
1032 // of the property in the @implementation.
1033 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001034 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1035 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001036 ObjCPropertyDecl *Prop = (*P);
1037 if (Prop->isInvalidDecl())
1038 continue;
1039 ObjCPropertyImplDecl *PI = 0;
1040 // Is there a matching propery synthesize/dynamic?
Douglas Gregor653f1b12009-04-23 01:02:12 +00001041 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001042 I = IMPDecl->propimpl_begin(),
1043 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001044 if ((*I)->getPropertyDecl() == Prop) {
1045 PI = (*I);
1046 break;
1047 }
1048 if (PI)
1049 continue;
1050 if (!InsMap.count(Prop->getGetterName())) {
1051 Diag(Prop->getLocation(),
1052 diag::warn_setter_getter_impl_required)
1053 << Prop->getDeclName() << Prop->getGetterName();
1054 Diag(IMPDecl->getLocation(),
1055 diag::note_property_impl_required);
1056 }
1057
1058 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1059 Diag(Prop->getLocation(),
1060 diag::warn_setter_getter_impl_required)
1061 << Prop->getDeclName() << Prop->getSetterName();
1062 Diag(IMPDecl->getLocation(),
1063 diag::note_property_impl_required);
1064 }
1065 }
1066
Chris Lattner4d391482007-12-12 07:09:47 +00001067 llvm::DenseSet<Selector> ClsMap;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001068 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001069 I = IMPDecl->classmeth_begin(),
1070 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001071 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +00001072
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001073 // Check for type conflict of methods declared in a class/protocol and
1074 // its implementation; if any.
1075 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1076 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1077 IMPDecl, CDecl,
1078 IncompleteImpl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001079
1080 // Check the protocol list for unimplemented methods in the @implementation
1081 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001082 // Check and see if class methods in class interface have been
1083 // implemented in the implementation class.
1084
Chris Lattnercddc8882009-03-01 00:56:52 +00001085 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001086 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001087 E = I->protocol_end(); PI != E; ++PI)
1088 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1089 InsMap, ClsMap, I);
1090 // Check class extensions (unnamed categories)
1091 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1092 Categories; Categories = Categories->getNextClassCategory()) {
1093 if (!Categories->getIdentifier()) {
1094 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1095 break;
1096 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001097 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001098 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1099 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1100 E = C->protocol_end(); PI != E; ++PI)
1101 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1102 InsMap, ClsMap, C->getClassInterface());
1103 } else
1104 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001105}
1106
1107/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001108Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001109Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001110 IdentifierInfo **IdentList,
1111 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001112 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +00001113
1114 for (unsigned i = 0; i != NumElts; ++i) {
1115 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001116 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001117 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001118 // Maybe we will complain about the shadowed template parameter.
1119 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1120 // Just pretend that we didn't see the previous declaration.
1121 PrevDecl = 0;
1122 }
1123
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001124 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001125 // GCC apparently allows the following idiom:
1126 //
1127 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1128 // @class XCElementToggler;
1129 //
1130 // FIXME: Make an extension?
1131 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1132 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001133 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001134 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001135 } else if (TDD) {
1136 // a forward class declaration matching a typedef name of a class refers
1137 // to the underlying class.
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001138 if (ObjCInterfaceType * OI =
1139 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1140 PrevDecl = OI->getDecl();
1141 }
Chris Lattner4d391482007-12-12 07:09:47 +00001142 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001143 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001144 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +00001145 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1146 IdentList[i], SourceLocation(), true);
Steve Naroff8f06f842009-04-23 16:00:56 +00001147 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +00001148 }
1149
1150 Interfaces.push_back(IDecl);
1151 }
1152
Douglas Gregord0434102009-01-09 00:49:46 +00001153 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001154 &Interfaces[0],
1155 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001156 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001157 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001158 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001159}
1160
1161
1162/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1163/// returns true, or false, accordingly.
1164/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001165bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001166 const ObjCMethodDecl *PrevMethod,
1167 bool matchBasedOnSizeAndAlignment) {
1168 QualType T1 = Context.getCanonicalType(Method->getResultType());
1169 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1170
1171 if (T1 != T2) {
1172 // The result types are different.
1173 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001174 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001175 // Incomplete types don't have a size and alignment.
1176 if (T1->isIncompleteType() || T2->isIncompleteType())
1177 return false;
1178 // Check is based on size and alignment.
1179 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1180 return false;
1181 }
Chris Lattner89951a82009-02-20 18:43:26 +00001182
1183 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1184 E = Method->param_end();
1185 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1186
1187 for (; ParamI != E; ++ParamI, ++PrevI) {
1188 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1189 T1 = Context.getCanonicalType((*ParamI)->getType());
1190 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001191 if (T1 != T2) {
1192 // The result types are different.
1193 if (!matchBasedOnSizeAndAlignment)
1194 return false;
1195 // Incomplete types don't have a size and alignment.
1196 if (T1->isIncompleteType() || T2->isIncompleteType())
1197 return false;
1198 // Check is based on size and alignment.
1199 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1200 return false;
1201 }
Chris Lattner4d391482007-12-12 07:09:47 +00001202 }
1203 return true;
1204}
1205
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001206/// \brief Read the contents of the instance and factory method pools
1207/// for a given selector from external storage.
1208///
1209/// This routine should only be called once, when neither the instance
1210/// nor the factory method pool has an entry for this selector.
1211Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
1212 bool isInstance) {
1213 assert(ExternalSource && "We need an external AST source");
1214 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1215 "Selector data already loaded into the instance method pool");
1216 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1217 "Selector data already loaded into the factory method pool");
1218
1219 // Read the method list from the external source.
1220 std::pair<ObjCMethodList, ObjCMethodList> Methods
1221 = ExternalSource->ReadMethodPool(Sel);
1222
1223 if (isInstance) {
1224 if (Methods.second.Method)
1225 FactoryMethodPool[Sel] = Methods.second;
1226 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
1227 }
1228
1229 if (Methods.first.Method)
1230 InstanceMethodPool[Sel] = Methods.first;
1231
1232 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1233}
1234
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001235void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001236 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1237 = InstanceMethodPool.find(Method->getSelector());
1238 if (Pos == InstanceMethodPool.end()) {
1239 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1240 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1241 else
1242 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1243 ObjCMethodList())).first;
1244 }
1245
1246 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001247 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001248 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001249 Entry.Method = Method;
1250 Entry.Next = 0;
1251 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001252 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001253
1254 // We've seen a method with this name, see if we have already seen this type
1255 // signature.
1256 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1257 if (MatchTwoMethodDeclarations(Method, List->Method))
1258 return;
1259
1260 // We have a new signature for an existing method - add it.
1261 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1262 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001263}
1264
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001265// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001266ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1267 SourceRange R) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001268 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1269 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001270 if (Pos == InstanceMethodPool.end()) {
1271 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001272 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1273 else
1274 return 0;
1275 }
1276
1277 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001278 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001279
1280 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001281 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1282 // This checks if the methods differ by size & alignment.
1283 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1284 issueWarning = true;
1285 }
1286 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001287 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001288 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001289 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001290 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001291 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001292 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001293 }
1294 return MethList.Method;
1295}
1296
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001297void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001298 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1299 = FactoryMethodPool.find(Method->getSelector());
1300 if (Pos == FactoryMethodPool.end()) {
1301 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1302 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1303 else
1304 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1305 ObjCMethodList())).first;
1306 }
1307
1308 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001309 if (!FirstMethod.Method) {
1310 // Haven't seen a method with this selector name yet - add it.
1311 FirstMethod.Method = Method;
1312 FirstMethod.Next = 0;
1313 } else {
1314 // We've seen a method with this name, now check the type signature(s).
1315 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1316
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001317 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001318 Next = Next->Next)
1319 match = MatchTwoMethodDeclarations(Method, Next->Method);
1320
1321 if (!match) {
1322 // We have a new signature for an existing method - add it.
1323 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001324 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001325 FirstMethod.Next = OMI;
1326 }
1327 }
1328}
1329
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001330ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
1331 SourceRange R) {
1332 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1333 = FactoryMethodPool.find(Sel);
1334 if (Pos == FactoryMethodPool.end()) {
1335 if (ExternalSource && !InstanceMethodPool.count(Sel))
1336 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1337 else
1338 return 0;
1339 }
1340
1341 ObjCMethodList &MethList = Pos->second;
1342 bool issueWarning = false;
1343
1344 if (MethList.Method && MethList.Next) {
1345 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1346 // This checks if the methods differ by size & alignment.
1347 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1348 issueWarning = true;
1349 }
1350 if (issueWarning && (MethList.Method && MethList.Next)) {
1351 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1352 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1353 << MethList.Method->getSourceRange();
1354 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1355 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1356 << Next->Method->getSourceRange();
1357 }
1358 return MethList.Method;
1359}
1360
Steve Naroff0701bbb2009-01-08 17:28:14 +00001361/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1362/// have the property type and issue diagnostics if they don't.
1363/// Also synthesize a getter/setter method if none exist (and update the
1364/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1365/// methods is the "right" thing to do.
1366void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1367 ObjCContainerDecl *CD) {
1368 ObjCMethodDecl *GetterMethod, *SetterMethod;
1369
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001370 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1371 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001372 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1373 property->getLocation());
1374
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001375 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001376 if (Context.getCanonicalType(SetterMethod->getResultType())
1377 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001378 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001379 if (SetterMethod->param_size() != 1 ||
1380 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001381 Diag(property->getLocation(),
Fariborz Jahanian4c2743f2009-05-08 19:36:34 +00001382 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001383 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001384 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001385 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1386 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001387 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001388
1389 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001390 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001391 // FIXME: The synthesized property we set here is misleading. We almost always
1392 // synthesize these methods unless the user explicitly provided prototypes
1393 // (which is odd, but allowed). Sema should be typechecking that the
1394 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001395 if (!GetterMethod) {
1396 // No instance method of same name as property getter name was found.
1397 // Declare a getter method and add it to the list of methods
1398 // for this class.
1399 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1400 property->getLocation(), property->getGetterName(),
1401 property->getType(), CD, true, false, true,
1402 (property->getPropertyImplementation() ==
1403 ObjCPropertyDecl::Optional) ?
1404 ObjCMethodDecl::Optional :
1405 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001406 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001407 } else
1408 // A user declared getter will be synthesize when @synthesize of
1409 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001410 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001411 property->setGetterMethodDecl(GetterMethod);
1412
1413 // Skip setter if property is read-only.
1414 if (!property->isReadOnly()) {
1415 // Find the default setter and if one not found, add one.
1416 if (!SetterMethod) {
1417 // No instance method of same name as property setter name was found.
1418 // Declare a setter method and add it to the list of methods
1419 // for this class.
1420 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1421 property->getLocation(),
1422 property->getSetterName(),
1423 Context.VoidTy, CD, true, false, true,
1424 (property->getPropertyImplementation() ==
1425 ObjCPropertyDecl::Optional) ?
1426 ObjCMethodDecl::Optional :
1427 ObjCMethodDecl::Required);
1428 // Invent the arguments for the setter. We don't bother making a
1429 // nice name for the argument.
1430 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001431 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001432 property->getIdentifier(),
1433 property->getType(),
1434 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001435 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001436 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001437 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001438 } else
1439 // A user declared setter will be synthesize when @synthesize of
1440 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001441 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001442 property->setSetterMethodDecl(SetterMethod);
1443 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001444 // Add any synthesized methods to the global pool. This allows us to
1445 // handle the following, which is supported by GCC (and part of the design).
1446 //
1447 // @interface Foo
1448 // @property double bar;
1449 // @end
1450 //
1451 // void thisIsUnfortunate() {
1452 // id foo;
1453 // double bar = [foo bar];
1454 // }
1455 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001456 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001457 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001458 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001459 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001460}
1461
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001462/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1463/// identical selector names in current and its super classes and issues
1464/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001465void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1466 ObjCMethodDecl *Method,
1467 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001468 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1469 if (ID == 0) return;
1470
1471 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
1472 ObjCMethodDecl *SuperMethodDecl =
1473 SD->lookupMethod(Method->getSelector(), IsInstance);
1474 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001475 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001476 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001477 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001478 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1479 E = Method->param_end();
1480 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1481 for (; ParamI != E; ++ParamI, ++PrevI) {
1482 // Number of parameters are the same and is guaranteed by selector match.
1483 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1484 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1485 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1486 // If type of arguement of method in this class does not match its
1487 // respective argument type in the super class method, issue warning;
1488 if (!Context.typesAreCompatible(T1, T2)) {
1489 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
1490 << T1 << T2;
1491 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1492 return;
1493 }
1494 }
1495 ID = SD;
1496 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001497}
1498
Steve Naroffa56f6162007-12-18 01:30:32 +00001499// Note: For class/category implemenations, allMethods/allProperties is
1500// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001501void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1502 DeclPtrTy *allMethods, unsigned allNum,
1503 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001504 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001505 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001506
Steve Naroffa56f6162007-12-18 01:30:32 +00001507 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1508 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001509 // should be true.
1510 if (!ClassDecl)
1511 return;
1512
Chris Lattner4d391482007-12-12 07:09:47 +00001513 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001514 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1515 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001516 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001517
Steve Naroff0701bbb2009-01-08 17:28:14 +00001518 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001519
1520 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1521 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1522 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1523
Chris Lattner4d391482007-12-12 07:09:47 +00001524 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001525 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001526 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001527
1528 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001529 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001530 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001531 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001532 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1533 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001534 if ((isInterfaceDeclKind && PrevMethod && !match)
1535 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001536 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001537 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001538 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001539 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001540 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001541 InsMap[Method->getSelector()] = Method;
1542 /// The following allows us to typecheck messages to "id".
1543 AddInstanceMethodToGlobalPool(Method);
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001544 // verify that the instance method conforms to the same definition of
1545 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001546 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001547 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001548 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001549 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001550 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001551 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1552 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001553 if ((isInterfaceDeclKind && PrevMethod && !match)
1554 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001555 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001556 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001557 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001558 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001559 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001560 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001561 /// The following allows us to typecheck messages to "Class".
1562 AddFactoryMethodToGlobalPool(Method);
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001563 // verify that the class method conforms to the same definition of
1564 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001565 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001566 }
1567 }
1568 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001569 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001570 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001571 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001572 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001573 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001574 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001575 // Categories are used to extend the class by declaring new methods.
1576 // By the same token, they are also used to add new properties. No
1577 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001578
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001579 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001580 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001581 if (C->getIdentifier() == 0)
1582 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001583 }
Steve Naroff09c47192009-01-09 15:36:25 +00001584 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1585 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1586 // user-defined setter/getter. It also synthesizes setter/getter methods
1587 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001588 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1589 E = CDecl->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001590 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001591 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001592 CDecl->setAtEndLoc(AtEndLoc);
1593 }
1594 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001595 IC->setAtEndLoc(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001596 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001597 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001598 } else if (ObjCCategoryImplDecl* CatImplClass =
1599 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001600 CatImplClass->setAtEndLoc(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001601
Chris Lattner4d391482007-12-12 07:09:47 +00001602 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001603 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001604 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001605 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001606 Categories; Categories = Categories->getNextClassCategory()) {
1607 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001608 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001609 break;
1610 }
1611 }
1612 }
1613 }
Chris Lattner682bf922009-03-29 16:50:03 +00001614 if (isInterfaceDeclKind) {
1615 // Reject invalid vardecls.
1616 for (unsigned i = 0; i != tuvNum; i++) {
1617 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1618 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1619 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001620 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001621 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001622 }
Chris Lattner682bf922009-03-29 16:50:03 +00001623 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001624 }
Chris Lattner4d391482007-12-12 07:09:47 +00001625}
1626
1627
1628/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1629/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001630static Decl::ObjCDeclQualifier
1631CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1632 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1633 if (PQTVal & ObjCDeclSpec::DQ_In)
1634 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1635 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1636 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1637 if (PQTVal & ObjCDeclSpec::DQ_Out)
1638 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1639 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1640 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1641 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1642 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1643 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1644 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001645
1646 return ret;
1647}
1648
Chris Lattnerb28317a2009-03-28 19:18:32 +00001649Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001650 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001651 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001652 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001653 Selector Sel,
1654 // optional arguments. The number of types/arguments is obtained
1655 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001656 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001657 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001658 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1659 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001660 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001661
1662 // Make sure we can establish a context for the method.
1663 if (!ClassDecl) {
1664 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001665 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001666 }
Chris Lattner4d391482007-12-12 07:09:47 +00001667 QualType resultDeclType;
1668
Steve Naroffccef3712009-02-20 22:59:16 +00001669 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001670 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001671
1672 // Methods cannot return interface types. All ObjC objects are
1673 // passed by reference.
1674 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001675 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1676 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001677 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001678 }
1679 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001680 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001681
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001682 ObjCMethodDecl* ObjCMethod =
1683 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner32d3f9c2009-03-29 04:30:19 +00001684 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001685 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001686 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001687 MethodDeclKind == tok::objc_optional ?
1688 ObjCMethodDecl::Optional :
1689 ObjCMethodDecl::Required);
1690
Chris Lattner0ed844b2008-04-04 06:12:32 +00001691 llvm::SmallVector<ParmVarDecl*, 16> Params;
1692
Chris Lattner7db638d2009-04-11 19:42:43 +00001693 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001694 QualType ArgType, UnpromotedArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001695
Chris Lattnere294d3f2009-04-11 18:57:04 +00001696 if (ArgInfo[i].Type == 0) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001697 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001698 } else {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001699 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff6082c622008-12-09 19:36:17 +00001700 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001701 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001702 }
1703
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001704 ParmVarDecl* Param;
Chris Lattner2dd979f2009-04-11 19:08:56 +00001705 if (ArgType == UnpromotedArgType)
Chris Lattner7db638d2009-04-11 19:42:43 +00001706 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001707 ArgInfo[i].Name, ArgType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001708 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001709 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001710 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner7db638d2009-04-11 19:42:43 +00001711 ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001712 ArgInfo[i].Name, ArgType,
1713 UnpromotedArgType,
Douglas Gregor64650af2009-02-02 23:39:07 +00001714 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001715
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001716 if (ArgType->isObjCInterfaceType()) {
1717 Diag(ArgInfo[i].NameLoc,
1718 diag::err_object_cannot_be_passed_returned_by_value)
1719 << 1 << ArgType;
1720 Param->setInvalidDecl();
1721 }
1722
Chris Lattner0ed844b2008-04-04 06:12:32 +00001723 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001724 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001725
1726 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001727 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001728
Chris Lattner0ed844b2008-04-04 06:12:32 +00001729 Params.push_back(Param);
1730 }
1731
Jay Foadbeaaccd2009-05-21 09:52:38 +00001732 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001733 ObjCMethod->setObjCDeclQualifier(
1734 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1735 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001736
1737 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001738 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Ted Kremenekb27d1172009-04-30 18:41:06 +00001739
Chris Lattner4d391482007-12-12 07:09:47 +00001740 // For implementations (which can be very "coarse grain"), we add the
1741 // method now. This allows the AST to implement lookup methods that work
1742 // incrementally (without waiting until we parse the @end). It also allows
1743 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001744 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001745 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001746 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001747 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1748 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001749 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001750 PrevMethod = ImpDecl->getClassMethod(Sel);
1751 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001752 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001753 if (AttrList)
1754 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001755 } else if (ObjCCategoryImplDecl *CatImpDecl =
1756 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001757 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001758 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1759 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001760 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001761 PrevMethod = CatImpDecl->getClassMethod(Sel);
1762 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001763 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001764 if (AttrList)
1765 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001766 }
1767 if (PrevMethod) {
1768 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001769 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001770 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001771 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001772 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001773 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001774}
1775
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001776void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1777 SourceLocation Loc,
1778 unsigned &Attributes) {
1779 // FIXME: Improve the reported location.
1780
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001781 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001782 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001783 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1784 ObjCDeclSpec::DQ_PR_assign |
1785 ObjCDeclSpec::DQ_PR_copy |
1786 ObjCDeclSpec::DQ_PR_retain))) {
1787 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1788 "readwrite" :
1789 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1790 "assign" :
1791 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1792 "copy" : "retain";
1793
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001794 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001795 diag::err_objc_property_attr_mutually_exclusive :
1796 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001797 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001798 }
1799
1800 // Check for copy or retain on non-object types.
1801 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Steve Narofff4954562009-07-16 15:41:00 +00001802 !PropertyTy->isObjCObjectPointerType() &&
1803 !PropertyTy->isBlockPointerType() &&
1804 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001805 Diag(Loc, diag::err_objc_property_requires_object)
1806 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001807 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1808 }
1809
1810 // Check for more than one of { assign, copy, retain }.
1811 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1812 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001813 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1814 << "assign" << "copy";
1815 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001816 }
1817 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001818 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1819 << "assign" << "retain";
1820 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001821 }
1822 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1823 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001824 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1825 << "copy" << "retain";
1826 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001827 }
1828 }
1829
1830 // Warn if user supplied no assignment attribute, property is
1831 // readwrite, and this is an object type.
1832 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1833 ObjCDeclSpec::DQ_PR_retain)) &&
1834 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00001835 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001836 // Skip this warning in gc-only mode.
1837 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1838 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1839
1840 // If non-gc code warn that this is likely inappropriate.
1841 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1842 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1843
1844 // FIXME: Implement warning dependent on NSCopying being
1845 // implemented. See also:
1846 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1847 // (please trim this list while you are at it).
1848 }
Mike Stump046efd92009-05-07 23:06:50 +00001849
1850 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1851 && getLangOptions().getGCMode() == LangOptions::GCOnly
1852 && PropertyTy->isBlockPointerType())
1853 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001854}
1855
Chris Lattnerb28317a2009-03-28 19:18:32 +00001856Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1857 FieldDeclarator &FD,
1858 ObjCDeclSpec &ODS,
1859 Selector GetterSel,
1860 Selector SetterSel,
1861 DeclPtrTy ClassCategory,
1862 bool *isOverridingProperty,
1863 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001864 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001865 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1866 // default is readwrite!
1867 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1868 // property is defaulted to 'assign' if it is readwrite and is
1869 // not retain or copy
1870 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1871 (isReadWrite &&
1872 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1873 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1874 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001875 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001876 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001877 // May modify Attributes.
1878 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001879 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1880 if (!CDecl->getIdentifier()) {
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001881 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001882 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001883 if ((CCPrimary = CDecl->getClassInterface())) {
1884 // Find the property in continuation class's primary class only.
1885 ObjCPropertyDecl *PIDecl = 0;
1886 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001887 for (ObjCInterfaceDecl::prop_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001888 I = CCPrimary->prop_begin(), E = CCPrimary->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001889 I != E; ++I)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001890 if ((*I)->getIdentifier() == PropertyId) {
1891 PIDecl = *I;
1892 break;
1893 }
1894
1895 if (PIDecl) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001896 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001897 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001898 unsigned PIkind = PIDecl->getPropertyAttributes();
1899 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001900 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001901 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1902 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001903 PIDecl->makeitReadWriteAttribute();
1904 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1905 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1906 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1907 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1908 PIDecl->setSetterName(SetterSel);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001909 } else
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001910 Diag(AtLoc, diag::err_use_continuation_class)
1911 << CCPrimary->getDeclName();
1912 *isOverridingProperty = true;
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001913 // Make sure setter decl is synthesized, and added to primary
1914 // class's list.
1915 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001916 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001917 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001918 // No matching property found in the primary class. Just fall thru
1919 // and add property to continuation class's primary class.
1920 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001921 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001922 Diag(CDecl->getLocation(), diag::err_continuation_class);
1923 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001924 return DeclPtrTy();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001925 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001926 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001927
Steve Naroff93983f82009-01-11 12:47:58 +00001928 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1929 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001930 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1931 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001932 FD.D.getIdentifier(), T);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001933 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001934
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001935 if (T->isArrayType() || T->isFunctionType()) {
1936 Diag(AtLoc, diag::err_property_type) << T;
1937 PDecl->setInvalidDecl();
1938 }
1939
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001940 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001941
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001942 // Regardless of setter/getter attribute, we save the default getter/setter
1943 // selector names in anticipation of declaration of setter/getter methods.
1944 PDecl->setGetterName(GetterSel);
1945 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001946
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001947 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001948 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001949
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001950 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001951 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001952
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001953 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001954 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001955
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001956 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001957 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001958
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001959 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001960 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001961
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001962 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001963 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001964
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001965 if (isAssign)
1966 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1967
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001968 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001969 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001970
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001971 if (MethodImplKind == tok::objc_required)
1972 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1973 else if (MethodImplKind == tok::objc_optional)
1974 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001975 // A case of continuation class adding a new property in the class. This
1976 // is not what it was meant for. However, gcc supports it and so should we.
1977 // Make sure setter/getters are declared here.
1978 if (CCPrimary)
1979 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001980
Chris Lattnerb28317a2009-03-28 19:18:32 +00001981 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001982}
1983
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001984/// ActOnPropertyImplDecl - This routine performs semantic checks and
1985/// builds the AST node for a property implementation declaration; declared
1986/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001987///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001988Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1989 SourceLocation PropertyLoc,
1990 bool Synthesize,
1991 DeclPtrTy ClassCatImpDecl,
1992 IdentifierInfo *PropertyId,
1993 IdentifierInfo *PropertyIvar) {
1994 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001995 // Make sure we have a context for the property implementation declaration.
1996 if (!ClassImpDecl) {
1997 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001998 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001999 }
2000 ObjCPropertyDecl *property = 0;
2001 ObjCInterfaceDecl* IDecl = 0;
2002 // Find the class or category class where this property must have
2003 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002004 ObjCImplementationDecl *IC = 0;
2005 ObjCCategoryImplDecl* CatImplClass = 0;
2006 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002007 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002008 // We always synthesize an interface for an implementation
2009 // without an interface decl. So, IDecl is always non-zero.
2010 assert(IDecl &&
2011 "ActOnPropertyImplDecl - @implementation without @interface");
2012
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002013 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002014 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002015 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002016 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002017 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002018 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002019 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002020 if (Synthesize) {
2021 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002022 return DeclPtrTy();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002023 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002024 IDecl = CatImplClass->getClassInterface();
2025 if (!IDecl) {
2026 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002027 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002028 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002029 ObjCCategoryDecl *Category =
2030 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
2031
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002032 // If category for this implementation not found, it is an error which
2033 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002034 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002035 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002036 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002037 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002038 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002039 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002040 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002041 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002042 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002043 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002044 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002045 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002046 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002047 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002048 // Check that we have a valid, previously declared ivar for @synthesize
2049 if (Synthesize) {
2050 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002051 if (!PropertyIvar)
2052 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002053 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002054 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002055 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002056 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002057 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002058 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
2059 assert(EnclosingContext &&
2060 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
2061 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002062 PropertyIvar, PropType,
2063 ObjCIvarDecl::Public,
2064 (Expr *)0);
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002065 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002066 IDecl->addDecl(Ivar);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002067 property->setPropertyIvarDecl(Ivar);
2068 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002069 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002070 // Note! I deliberately want it to fall thru so, we have a
2071 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002072 } else if (getLangOptions().ObjCNonFragileABI &&
2073 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002074 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002075 << property->getDeclName() << Ivar->getDeclName()
2076 << ClassDeclared->getDeclName();
2077 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2078 << Ivar << Ivar->getNameAsCString();
2079 // Note! I deliberately want it to fall thru so more errors are caught.
2080 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002081 QualType IvarType = Context.getCanonicalType(Ivar->getType());
2082
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002083 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002084 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002085 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002086 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002087 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002088 // Note! I deliberately want it to fall thru so, we have a
2089 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002090 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002091
2092 // FIXME! Rules for properties are somewhat different that those
2093 // for assignments. Use a new routine to consolidate all cases;
2094 // specifically for property redeclarations as well as for ivars.
2095 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2096 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
2097 if (lhsType != rhsType &&
2098 lhsType->isArithmeticType()) {
2099 Diag(PropertyLoc, diag::error_property_ivar_type)
2100 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002101 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002102 }
2103 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002104 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2105 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002106 Diag(PropertyLoc, diag::error_weak_property)
2107 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002108 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002109 }
Steve Naroff14108da2009-07-10 23:34:53 +00002110 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002111 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2112 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002113 Diag(PropertyLoc, diag::error_strong_property)
2114 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002115 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002116 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002117 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002118 } else if (PropertyIvar)
2119 // @dynamic
2120 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002121 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002122 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00002123 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2124 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002125 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002126 ObjCPropertyImplDecl::Synthesize
2127 : ObjCPropertyImplDecl::Dynamic),
2128 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002129 if (IC) {
2130 if (Synthesize)
2131 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002132 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002133 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2134 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2135 << PropertyIvar;
2136 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2137 }
2138
Douglas Gregor653f1b12009-04-23 01:02:12 +00002139 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002140 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002141 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2142 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002143 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002144 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002145 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002146 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002147 if (Synthesize)
2148 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002149 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002150 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2151 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2152 << PropertyIvar;
2153 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2154 }
2155
2156 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002157 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002158 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2159 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002160 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002161 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002162 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002163 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002164
Chris Lattnerb28317a2009-03-28 19:18:32 +00002165 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002166}
Anders Carlsson15281452008-11-04 16:57:32 +00002167
Chris Lattnercc98eac2008-12-17 07:13:27 +00002168bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002169 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002170 return false;
2171
2172 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2173 D->setInvalidDecl();
2174
2175 return true;
2176}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002177
Chris Lattnercc98eac2008-12-17 07:13:27 +00002178/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2179/// instance variables of ClassName into Decls.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002180void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002181 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002182 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002183 // Check that ClassName is a valid class
2184 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2185 if (!Class) {
2186 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2187 return;
2188 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002189 if (LangOpts.ObjCNonFragileABI) {
2190 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2191 return;
2192 }
2193
Chris Lattnercc98eac2008-12-17 07:13:27 +00002194 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002195 llvm::SmallVector<FieldDecl*, 32> RecFields;
2196 Context.CollectObjCIvars(Class, RecFields);
2197 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2198 for (unsigned i = 0; i < RecFields.size(); i++) {
2199 FieldDecl* ID = RecFields[i];
2200 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2201 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2202 ID->getIdentifier(), ID->getType(),
2203 ID->getBitWidth());
2204 Decls.push_back(Sema::DeclPtrTy::make(FD));
2205 }
Chris Lattnercc98eac2008-12-17 07:13:27 +00002206
2207 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002208 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002209 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002210 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002211 if (getLangOptions().CPlusPlus)
2212 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002213 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002214 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002215 }
2216}
2217