blob: 299f101bdbf8ba2d18ef0c55b9abb5b07be14127 [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"
Steve Naroffca331292009-03-03 14:49:36 +000015#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000019using namespace clang;
20
Steve Naroffebf64432009-02-28 16:59:13 +000021/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000023void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000025 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff394f3f42008-07-25 17:57:26 +000026
27 // If we don't have a valid method decl, simply return.
28 if (!MDecl)
29 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000030
31 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000032 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000033 AddInstanceMethodToGlobalPool(MDecl);
34 else
35 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000036
37 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000038 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000039
40 // Create Decl objects for each parameter, entrring them in the scope for
41 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000042
43 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000044 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000045
Daniel Dunbar451318c2008-08-26 06:07:48 +000046 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
47 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000048
Chris Lattner8123a952008-04-10 02:22:51 +000049 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000050 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
51 E = MDecl->param_end(); PI != E; ++PI)
52 if ((*PI)->getIdentifier())
53 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000054}
55
Chris Lattnerb28317a2009-03-28 19:18:32 +000056Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000057ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
58 IdentifierInfo *ClassName, SourceLocation ClassLoc,
59 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000060 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000061 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000062 assert(ClassName && "Missing class identifier");
63
64 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000065 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000066 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000067 // Maybe we will complain about the shadowed template parameter.
68 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
69 // Just pretend that we didn't see the previous declaration.
70 PrevDecl = 0;
71 }
72
Ted Kremeneka526c5c2008-01-07 19:49:32 +000073 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000074 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000075 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000076 }
77
Ted Kremeneka526c5c2008-01-07 19:49:32 +000078 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000079 if (IDecl) {
80 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000081 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +000082 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000083 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000084 Diag(IDecl->getLocation(), diag::note_previous_definition);
85
Steve Naroffcfe8bf32008-11-18 19:15:30 +000086 // Return the previous class interface.
87 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +000088 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +000089 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000090 IDecl->setLocation(AtInterfaceLoc);
91 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000092 }
Chris Lattnerb752f282008-07-21 07:06:49 +000093 } else {
Douglas Gregord0434102009-01-09 00:49:46 +000094 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000095 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000096 if (AttrList)
97 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +000098
Steve Naroff31102512008-04-02 18:30:49 +000099 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000100 // FIXME: PushOnScopeChains
Douglas Gregor482b77d2009-01-12 23:27:07 +0000101 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000102 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000103 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000104 }
105
106 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000107 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000108 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000109
Steve Naroff818cb9e2009-02-04 17:14:05 +0000110 ObjCInterfaceDecl *SuperClassDecl =
111 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000112
113 // Diagnose classes that inherit from deprecated classes.
114 if (SuperClassDecl)
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000115 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000116
Steve Naroff818cb9e2009-02-04 17:14:05 +0000117 if (PrevDecl && SuperClassDecl == 0) {
118 // The previous declaration was not a class decl. Check if we have a
119 // typedef. If we do, get the underlying class type.
120 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
121 QualType T = TDecl->getUnderlyingType();
122 if (T->isObjCInterfaceType()) {
123 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
124 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
125 }
126 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000127
Steve Naroff818cb9e2009-02-04 17:14:05 +0000128 // This handles the following case:
129 //
130 // typedef int SuperClass;
131 // @interface MyClass : SuperClass {} @end
132 //
133 if (!SuperClassDecl) {
134 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
135 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
136 }
137 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000138
Steve Naroff818cb9e2009-02-04 17:14:05 +0000139 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
140 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000141 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000142 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000143 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000144 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000145 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000146 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000147 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000148 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000149 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000150 IDecl->setLocEnd(SuperLoc);
151 } else { // we have a root class.
152 IDecl->setLocEnd(ClassLoc);
153 }
154
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000155 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000156 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000157 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
158 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000159 IDecl->setLocEnd(EndProtoLoc);
160 }
Anders Carlsson15281452008-11-04 16:57:32 +0000161
162 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000163 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000164}
165
166/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000167/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000168Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
169 IdentifierInfo *AliasName,
170 SourceLocation AliasLocation,
171 IdentifierInfo *ClassName,
172 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000173 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000174 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000175 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000176 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000177 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000178 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000179 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000180 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000181 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000182 }
183 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000184 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000185 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
186 QualType T = TDecl->getUnderlyingType();
187 if (T->isObjCInterfaceType()) {
188 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
189 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000190 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000191 }
192 }
193 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000194 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
195 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000196 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000197 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000198 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000199 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000200 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000201
202 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000203 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000204 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000205
206 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000207
208 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000209 CurContext->addDecl(AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000210 if (!CheckObjCDeclScope(AliasDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000211 TUScope->AddDecl(DeclPtrTy::make(AliasDecl));
Douglas Gregord0434102009-01-09 00:49:46 +0000212
Chris Lattnerb28317a2009-03-28 19:18:32 +0000213 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000214}
215
Steve Naroff61d68522009-03-05 15:22:01 +0000216void Sema::CheckForwardProtocolDeclarationForCircularDependency(
217 IdentifierInfo *PName,
218 SourceLocation &Ploc, SourceLocation PrevLoc,
219 const ObjCList<ObjCProtocolDecl> &PList)
220{
221 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
222 E = PList.end(); I != E; ++I) {
223
224 if (ObjCProtocolDecl *PDecl = ObjCProtocols[(*I)->getIdentifier()]) {
225 if (PDecl->getIdentifier() == PName) {
226 Diag(Ploc, diag::err_protocol_has_circular_dependency);
227 Diag(PrevLoc, diag::note_previous_definition);
228 }
229 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
230 PDecl->getLocation(), PDecl->getReferencedProtocols());
231 }
232 }
233}
234
Chris Lattnerb28317a2009-03-28 19:18:32 +0000235Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000236Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
237 IdentifierInfo *ProtocolName,
238 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000239 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000240 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000241 SourceLocation EndProtoLoc,
242 AttributeList *AttrList) {
243 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000244 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000245 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000246 if (PDecl) {
247 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000248 if (!PDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000249 PDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000250 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000251 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000252 // Just return the protocol we already had.
253 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000254 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000255 }
Steve Naroff61d68522009-03-05 15:22:01 +0000256 ObjCList<ObjCProtocolDecl> PList;
257 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
258 CheckForwardProtocolDeclarationForCircularDependency(
259 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
260 PList.Destroy(Context);
261
Steve Narofff11b5082008-08-13 16:39:22 +0000262 // Make sure the cached decl gets a valid start location.
263 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000264 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000265 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000266 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
267 AtProtoInterfaceLoc,ProtocolName);
268 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000269 CurContext->addDecl(PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000270 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000271 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000272 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000273 if (AttrList)
274 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000275 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000276 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000277 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000278 PDecl->setLocEnd(EndProtoLoc);
279 }
Anders Carlsson15281452008-11-04 16:57:32 +0000280
281 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000282 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000283}
284
285/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000286/// issues an error if they are not declared. It returns list of
287/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000288void
Chris Lattnere13b9592008-07-26 04:03:38 +0000289Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000290 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000291 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000292 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000293 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000294 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
295 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000296 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000297 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000298 continue;
299 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000300
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000301 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000302
303 // If this is a forward declaration and we are supposed to warn in this
304 // case, do it.
305 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000306 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000307 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000308 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000309 }
310}
311
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000312/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000313/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000314///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000315void
316Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
317 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000318 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000319 ObjCPropertyDecl::PropertyAttributeKind CAttr =
320 Property->getPropertyAttributes();
321 ObjCPropertyDecl::PropertyAttributeKind SAttr =
322 SuperProperty->getPropertyAttributes();
323 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
324 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000325 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000326 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000327 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
328 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000329 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000330 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000331 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
332 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000333 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000334 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000335
336 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
337 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000338 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000339 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000340 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000341 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000342 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000343 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000344 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000345 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000346
347 QualType LHSType =
348 Context.getCanonicalType(SuperProperty->getType());
349 QualType RHSType =
350 Context.getCanonicalType(Property->getType());
351
352 if (!Context.typesAreCompatible(LHSType, RHSType)) {
353 // FIXME: Incorporate this test with typesAreCompatible.
354 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
355 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
356 return;
357 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
358 << Property->getType() << SuperProperty->getType() << inheritedName;
359 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000360}
361
362/// ComparePropertiesInBaseAndSuper - This routine compares property
363/// declarations in base and its super class, if any, and issues
364/// diagnostics in a variety of inconsistant situations.
365///
Chris Lattner70f19542009-02-16 21:26:43 +0000366void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000367 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
368 if (!SDecl)
369 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000370 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000371 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
372 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000373 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000374 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000375 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
376 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000377 ObjCPropertyDecl *PDecl = (*I);
378 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000379 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000380 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000381 }
382 }
383}
384
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000385/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
386/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000387/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000388void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000389Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000390 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000391 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
392 if (!IDecl) {
393 // Category
394 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
395 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff09c47192009-01-09 15:36:25 +0000396 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
397 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000398 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000399 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000400 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000401 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000402 CP != CE; ++CP)
403 if ((*CP)->getIdentifier() == Pr->getIdentifier())
404 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000405 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000406 // Property protocol already exist in class. Diagnose any mismatch.
407 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
408 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000409 return;
410 }
Steve Naroff09c47192009-01-09 15:36:25 +0000411 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
412 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000413 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000414 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000415 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000416 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000417 CP != CE; ++CP)
418 if ((*CP)->getIdentifier() == Pr->getIdentifier())
419 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000420 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000421 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000422 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000423 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000424}
425
426/// MergeProtocolPropertiesIntoClass - This routine merges properties
427/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000428/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000429///
Chris Lattner70f19542009-02-16 21:26:43 +0000430void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000431 DeclPtrTy MergeItsProtocols) {
432 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000433 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
434
435 if (!IDecl) {
436 // Category
437 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
438 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
439 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
440 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
441 E = MDecl->protocol_end(); P != E; ++P)
442 // Merge properties of category (*P) into IDECL's
443 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
444
445 // Go thru the list of protocols for this category and recursively merge
446 // their properties into this class as well.
447 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
448 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000449 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000450 } else {
451 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
452 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
453 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000454 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000455 }
456 return;
457 }
458
Chris Lattnerb752f282008-07-21 07:06:49 +0000459 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000460 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
461 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000462 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000463 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
464
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000465 // Go thru the list of protocols for this class and recursively merge
466 // their properties into this class as well.
467 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
468 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000469 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000470 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000471 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
472 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
473 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000474 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000475 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000476}
477
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000478/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000479/// a class method in its extension.
480///
481void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
482 ObjCInterfaceDecl *ID) {
483 if (!ID)
484 return; // Possibly due to previous error
485
486 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
487 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
488 e = ID->meth_end(); i != e; ++i) {
489 ObjCMethodDecl *MD = *i;
490 MethodMap[MD->getSelector()] = MD;
491 }
492
493 if (MethodMap.empty())
494 return;
495 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
496 e = CAT->meth_end(); i != e; ++i) {
497 ObjCMethodDecl *Method = *i;
498 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
499 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
500 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
501 << Method->getDeclName();
502 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
503 }
504 }
505}
506
Chris Lattner4d391482007-12-12 07:09:47 +0000507/// ActOnForwardProtocolDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +0000508Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000509Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000510 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000511 unsigned NumElts,
512 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000513 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000514
515 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000516 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000517 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000518 if (PDecl == 0) { // Not already seen?
519 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
520 IdentList[i].second, Ident);
521 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000522 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000523 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000524 if (attrList)
525 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000526 Protocols.push_back(PDecl);
527 }
Anders Carlsson15281452008-11-04 16:57:32 +0000528
529 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000530 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000531 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000532 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000533 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000534 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000535}
536
Chris Lattnerb28317a2009-03-28 19:18:32 +0000537Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000538ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
539 IdentifierInfo *ClassName, SourceLocation ClassLoc,
540 IdentifierInfo *CategoryName,
541 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000542 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000543 unsigned NumProtoRefs,
544 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000545 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000546 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
547 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000548 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000549
550 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000551 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000552 if (!IDecl || IDecl->isForwardDecl()) {
553 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000554 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000555 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000556 }
Chris Lattner4d391482007-12-12 07:09:47 +0000557
Chris Lattner70f19542009-02-16 21:26:43 +0000558 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000559
560 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000561 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000562
563 /// Check for duplicate interface declaration for this category
564 ObjCCategoryDecl *CDeclChain;
565 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
566 CDeclChain = CDeclChain->getNextClassCategory()) {
567 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
568 Diag(CategoryLoc, diag::warn_dup_category_def)
569 << ClassName << CategoryName;
570 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
571 break;
572 }
573 }
574 if (!CDeclChain)
575 CDecl->insertNextClassCategory();
576
Chris Lattner4d391482007-12-12 07:09:47 +0000577 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000578 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000579 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000580 }
Anders Carlsson15281452008-11-04 16:57:32 +0000581
582 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000583 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000584}
585
586/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000587/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000588/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000589Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000590 SourceLocation AtCatImplLoc,
591 IdentifierInfo *ClassName, SourceLocation ClassLoc,
592 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000593 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000594 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000595 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
596 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000597 /// Check that class of this category is already completely declared.
598 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000599 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000600
Douglas Gregord0434102009-01-09 00:49:46 +0000601 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000602 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000603
Chris Lattner4d391482007-12-12 07:09:47 +0000604 /// TODO: Check that CatName, category name, is not used in another
605 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000606 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000607
608 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000609 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000610}
611
Chris Lattnerb28317a2009-03-28 19:18:32 +0000612Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000613 SourceLocation AtClassImplLoc,
614 IdentifierInfo *ClassName, SourceLocation ClassLoc,
615 IdentifierInfo *SuperClassname,
616 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000618 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000619 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000620 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000621 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000622 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000623 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000624 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000625 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000626 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000627 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000628 }
629
630 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000631 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000632 if (SuperClassname) {
633 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000634 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000635 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000636 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
637 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000638 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000639 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000640 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000641 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000642 Diag(SuperClassLoc, diag::err_undef_superclass)
643 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000644 else if (IDecl && IDecl->getSuperClass() != SDecl) {
645 // This implementation and its interface do not have the same
646 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000647 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000648 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000649 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000650 }
651 }
652 }
653
654 if (!IDecl) {
655 // Legacy case of @implementation with no corresponding @interface.
656 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000657
658 // FIXME: Do we support attributes on the @implementation? If so
659 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000660 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
661 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000662 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000663 IDecl->setSuperClass(SDecl);
664 IDecl->setLocEnd(ClassLoc);
665
Douglas Gregord0434102009-01-09 00:49:46 +0000666 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000667 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000668 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000669 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000670 }
671
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000672 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000673 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000674 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000675
Douglas Gregord0434102009-01-09 00:49:46 +0000676 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000677 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000678
Anders Carlsson15281452008-11-04 16:57:32 +0000679 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000680 return DeclPtrTy::make(IMPDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000681
Chris Lattner4d391482007-12-12 07:09:47 +0000682 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000683 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000684 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000685 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000686 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000687 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000688 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000689}
690
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000691void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
692 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000693 SourceLocation RBrace) {
694 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000695 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000696 if (!IDecl)
697 return;
698 /// Check case of non-existing @interface decl.
699 /// (legacy objective-c @implementation decl without an @interface decl).
700 /// Add implementations's ivar to the synthesize class's ivar list.
701 if (IDecl->ImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000702 IDecl->setIVarList(ivars, numIvars, Context);
703 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000704 return;
705 }
706 // If implementation has empty ivar list, just return.
707 if (numIvars == 0)
708 return;
709
710 assert(ivars && "missing @implementation ivars");
711
712 // Check interface's Ivar list against those in the implementation.
713 // names and types must match.
714 //
Chris Lattner4d391482007-12-12 07:09:47 +0000715 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000716 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000717 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
718 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000719 ObjCIvarDecl* ImplIvar = ivars[j++];
720 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000721 assert (ImplIvar && "missing implementation ivar");
722 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000723
724 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000725 if (Context.getCanonicalType(ImplIvar->getType()) !=
726 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000727 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000728 << ImplIvar->getIdentifier()
729 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000730 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000731 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
732 Expr *ImplBitWidth = ImplIvar->getBitWidth();
733 Expr *ClsBitWidth = ClsIvar->getBitWidth();
734 if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() !=
735 ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) {
736 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
737 << ImplIvar->getIdentifier();
738 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
739 }
740 }
741 // Make sure the names are identical.
742 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000743 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000744 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000745 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000746 }
747 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000748 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000749
750 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000751 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000752 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000753 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000754}
755
Steve Naroff3c2eb662008-02-10 21:38:56 +0000756void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
757 bool &IncompleteImpl) {
758 if (!IncompleteImpl) {
759 Diag(ImpLoc, diag::warn_incomplete_impl);
760 IncompleteImpl = true;
761 }
Chris Lattner08631c52008-11-23 21:45:46 +0000762 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000763}
764
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000765void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
766 ObjCMethodDecl *IntfMethodDecl) {
767 bool err = false;
768 QualType ImpMethodQType =
769 Context.getCanonicalType(ImpMethodDecl->getResultType());
770 QualType IntfMethodQType =
771 Context.getCanonicalType(IntfMethodDecl->getResultType());
772 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
773 err = true;
774 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
775 IF=IntfMethodDecl->param_begin(),
776 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
777 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
778 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
779 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
780 err = true;
781 break;
782 }
783 }
784 if (err) {
785 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
786 << ImpMethodDecl->getDeclName();
787 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
788 }
789}
790
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000791/// isPropertyReadonly - Return true if property is readonly, by searching
792/// for the property in the class and in its categories and implementations
793///
794bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000795 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000796 // by far the most common case.
797 if (!PDecl->isReadOnly())
798 return false;
799 // Even if property is ready only, if interface has a user defined setter,
800 // it is not considered read only.
801 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
802 return false;
803
804 // Main class has the property as 'readonly'. Must search
805 // through the category list to see if the property's
806 // attribute has been over-ridden to 'readwrite'.
807 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
808 Category; Category = Category->getNextClassCategory()) {
809 // Even if property is ready only, if a category has a user defined setter,
810 // it is not considered read only.
811 if (Category->getInstanceMethod(PDecl->getSetterName()))
812 return false;
813 ObjCPropertyDecl *P =
814 Category->FindPropertyDeclaration(PDecl->getIdentifier());
815 if (P && !P->isReadOnly())
816 return false;
817 }
818
819 // Also, check for definition of a setter method in the implementation if
820 // all else failed.
821 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
822 if (ObjCImplementationDecl *IMD =
823 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
824 if (IMD->getInstanceMethod(PDecl->getSetterName()))
825 return false;
826 }
827 else if (ObjCCategoryImplDecl *CIMD =
828 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
829 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
830 return false;
831 }
832 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000833 // Lastly, look through the implementation (if one is in scope).
834 if (ObjCImplementationDecl *ImpDecl =
835 ObjCImplementations[IDecl->getIdentifier()])
836 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
837 return false;
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000838 return true;
839}
840
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000841/// FIXME: Type hierarchies in Objective-C can be deep. We could most
842/// likely improve the efficiency of selector lookups and type
843/// checking by associating with each protocol / interface / category
844/// the flattened instance tables. If we used an immutable set to keep
845/// the table then it wouldn't add significant memory cost and it
846/// would be handy for lookups.
847
Steve Naroffefe7f362008-02-08 22:06:17 +0000848/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000849/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000850void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
851 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000852 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000853 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000854 const llvm::DenseSet<Selector> &ClsMap,
855 ObjCInterfaceDecl *IDecl) {
856 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
857
858 // If a method lookup fails locally we still need to look and see if
859 // the method was implemented by a base class or an inherited
860 // protocol. This lookup is slow, but occurs rarely in correct code
861 // and otherwise would terminate in a warning.
862
Chris Lattner4d391482007-12-12 07:09:47 +0000863 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000864 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000865 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000866 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000867 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000868 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000869 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
870 // Ugly, but necessary. Method declared in protcol might have
871 // have been synthesized due to a property declared in the class which
872 // uses the protocol.
873 ObjCMethodDecl *MethodInClass =
874 IDecl->lookupInstanceMethod(method->getSelector());
875 if (!MethodInClass || !MethodInClass->isSynthesized())
876 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
877 }
Chris Lattner4d391482007-12-12 07:09:47 +0000878 }
879 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000880 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000881 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000882 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000883 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
884 !ClsMap.count(method->getSelector()) &&
885 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000886 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000887 }
Chris Lattner780f3292008-07-21 21:32:27 +0000888 // Check on this protocols's referenced protocols, recursively.
889 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
890 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000891 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000892}
893
Chris Lattnercddc8882009-03-01 00:56:52 +0000894void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
895 ObjCContainerDecl* CDecl,
896 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000897 llvm::DenseSet<Selector> InsMap;
898 // Check and see if instance methods in class interface have been
899 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000900 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000901 E = IMPDecl->instmeth_end(); I != E; ++I)
902 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000903
Chris Lattnercddc8882009-03-01 00:56:52 +0000904 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
905 E = CDecl->instmeth_end(); I != E; ++I) {
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000906 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000907 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000908 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000909 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000910
911 ObjCMethodDecl *ImpMethodDecl =
912 IMPDecl->getInstanceMethod((*I)->getSelector());
913 ObjCMethodDecl *IntfMethodDecl =
Chris Lattnercddc8882009-03-01 00:56:52 +0000914 CDecl->getInstanceMethod((*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000915 assert(IntfMethodDecl &&
916 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
917 // ImpMethodDecl may be null as in a @dynamic property.
918 if (ImpMethodDecl)
919 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
920 }
Chris Lattner4c525092007-12-12 17:58:05 +0000921
Chris Lattner4d391482007-12-12 07:09:47 +0000922 llvm::DenseSet<Selector> ClsMap;
923 // Check and see if class methods in class interface have been
924 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000925 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000926 E = IMPDecl->classmeth_end(); I != E; ++I)
927 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000928
Chris Lattnercddc8882009-03-01 00:56:52 +0000929 for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(),
930 E = CDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000931 if (!ClsMap.count((*I)->getSelector()))
932 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000933 else {
934 ObjCMethodDecl *ImpMethodDecl =
935 IMPDecl->getClassMethod((*I)->getSelector());
936 ObjCMethodDecl *IntfMethodDecl =
Chris Lattnercddc8882009-03-01 00:56:52 +0000937 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000938 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
939 }
940
Chris Lattner4d391482007-12-12 07:09:47 +0000941
942 // Check the protocol list for unimplemented methods in the @implementation
943 // class.
Chris Lattnercddc8882009-03-01 00:56:52 +0000944 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
945 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
946 E = I->protocol_end(); PI != E; ++PI)
947 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
948 InsMap, ClsMap, I);
949 // Check class extensions (unnamed categories)
950 for (ObjCCategoryDecl *Categories = I->getCategoryList();
951 Categories; Categories = Categories->getNextClassCategory()) {
952 if (!Categories->getIdentifier()) {
953 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
954 break;
955 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000956 }
Chris Lattnercddc8882009-03-01 00:56:52 +0000957 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
958 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
959 E = C->protocol_end(); PI != E; ++PI)
960 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
961 InsMap, ClsMap, C->getClassInterface());
962 } else
963 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +0000964}
965
966/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +0000967Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000968Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000969 IdentifierInfo **IdentList,
970 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000971 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000972
973 for (unsigned i = 0; i != NumElts; ++i) {
974 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000975 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000976 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000977 // Maybe we will complain about the shadowed template parameter.
978 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
979 // Just pretend that we didn't see the previous declaration.
980 PrevDecl = 0;
981 }
982
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000983 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000984 // GCC apparently allows the following idiom:
985 //
986 // typedef NSObject < XCElementTogglerP > XCElementToggler;
987 // @class XCElementToggler;
988 //
989 // FIXME: Make an extension?
990 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
991 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000992 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000993 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000994 }
Chris Lattner4d391482007-12-12 07:09:47 +0000995 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000996 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000997 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000998 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
999 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +00001000 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001001
Douglas Gregord0434102009-01-09 00:49:46 +00001002 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +00001003 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001004 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001005 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner4d391482007-12-12 07:09:47 +00001006 }
1007
1008 Interfaces.push_back(IDecl);
1009 }
1010
Douglas Gregord0434102009-01-09 00:49:46 +00001011 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001012 &Interfaces[0],
1013 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +00001014 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001015 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001016 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001017}
1018
1019
1020/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1021/// returns true, or false, accordingly.
1022/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001023bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001024 const ObjCMethodDecl *PrevMethod,
1025 bool matchBasedOnSizeAndAlignment) {
1026 QualType T1 = Context.getCanonicalType(Method->getResultType());
1027 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1028
1029 if (T1 != T2) {
1030 // The result types are different.
1031 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001032 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001033 // Incomplete types don't have a size and alignment.
1034 if (T1->isIncompleteType() || T2->isIncompleteType())
1035 return false;
1036 // Check is based on size and alignment.
1037 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1038 return false;
1039 }
Chris Lattner89951a82009-02-20 18:43:26 +00001040
1041 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1042 E = Method->param_end();
1043 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1044
1045 for (; ParamI != E; ++ParamI, ++PrevI) {
1046 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1047 T1 = Context.getCanonicalType((*ParamI)->getType());
1048 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001049 if (T1 != T2) {
1050 // The result types are different.
1051 if (!matchBasedOnSizeAndAlignment)
1052 return false;
1053 // Incomplete types don't have a size and alignment.
1054 if (T1->isIncompleteType() || T2->isIncompleteType())
1055 return false;
1056 // Check is based on size and alignment.
1057 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1058 return false;
1059 }
Chris Lattner4d391482007-12-12 07:09:47 +00001060 }
1061 return true;
1062}
1063
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001064void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Chris Lattnerb25df352009-03-04 05:16:45 +00001065 ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()];
1066 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001067 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001068 Entry.Method = Method;
1069 Entry.Next = 0;
1070 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001071 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001072
1073 // We've seen a method with this name, see if we have already seen this type
1074 // signature.
1075 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1076 if (MatchTwoMethodDeclarations(Method, List->Method))
1077 return;
1078
1079 // We have a new signature for an existing method - add it.
1080 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1081 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001082}
1083
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001084// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001085ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1086 SourceRange R) {
1087 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001088 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001089
1090 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001091 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1092 // This checks if the methods differ by size & alignment.
1093 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1094 issueWarning = true;
1095 }
1096 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001097 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001098 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001099 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001100 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001101 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001102 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001103 }
1104 return MethList.Method;
1105}
1106
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001107void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1108 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001109 if (!FirstMethod.Method) {
1110 // Haven't seen a method with this selector name yet - add it.
1111 FirstMethod.Method = Method;
1112 FirstMethod.Next = 0;
1113 } else {
1114 // We've seen a method with this name, now check the type signature(s).
1115 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1116
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001117 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001118 Next = Next->Next)
1119 match = MatchTwoMethodDeclarations(Method, Next->Method);
1120
1121 if (!match) {
1122 // We have a new signature for an existing method - add it.
1123 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001124 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001125 FirstMethod.Next = OMI;
1126 }
1127 }
1128}
1129
Steve Naroff0701bbb2009-01-08 17:28:14 +00001130/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1131/// have the property type and issue diagnostics if they don't.
1132/// Also synthesize a getter/setter method if none exist (and update the
1133/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1134/// methods is the "right" thing to do.
1135void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1136 ObjCContainerDecl *CD) {
1137 ObjCMethodDecl *GetterMethod, *SetterMethod;
1138
1139 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1140 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1141
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001142 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001143 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001144 Diag(property->getLocation(),
1145 diag::err_accessor_property_type_mismatch)
1146 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001147 << GetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001148 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1149 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001150
1151 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001152 if (Context.getCanonicalType(SetterMethod->getResultType())
1153 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001154 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001155 if (SetterMethod->param_size() != 1 ||
1156 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001157 Diag(property->getLocation(),
1158 diag::err_accessor_property_type_mismatch)
1159 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001160 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001161 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1162 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001163 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001164
1165 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001166 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001167 // FIXME: The synthesized property we set here is misleading. We
1168 // almost always synthesize these methods unless the user explicitly
1169 // provided prototypes (which is odd, but allowed). Sema should be
1170 // typechecking that the declarations jive in that situation (which
1171 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001172 if (!GetterMethod) {
1173 // No instance method of same name as property getter name was found.
1174 // Declare a getter method and add it to the list of methods
1175 // for this class.
1176 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1177 property->getLocation(), property->getGetterName(),
1178 property->getType(), CD, true, false, true,
1179 (property->getPropertyImplementation() ==
1180 ObjCPropertyDecl::Optional) ?
1181 ObjCMethodDecl::Optional :
1182 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001183 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001184 } else
1185 // A user declared getter will be synthesize when @synthesize of
1186 // the property with the same name is seen in the @implementation
1187 GetterMethod->setIsSynthesized();
1188 property->setGetterMethodDecl(GetterMethod);
1189
1190 // Skip setter if property is read-only.
1191 if (!property->isReadOnly()) {
1192 // Find the default setter and if one not found, add one.
1193 if (!SetterMethod) {
1194 // No instance method of same name as property setter name was found.
1195 // Declare a setter method and add it to the list of methods
1196 // for this class.
1197 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1198 property->getLocation(),
1199 property->getSetterName(),
1200 Context.VoidTy, CD, true, false, true,
1201 (property->getPropertyImplementation() ==
1202 ObjCPropertyDecl::Optional) ?
1203 ObjCMethodDecl::Optional :
1204 ObjCMethodDecl::Required);
1205 // Invent the arguments for the setter. We don't bother making a
1206 // nice name for the argument.
1207 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1208 SourceLocation(),
1209 property->getIdentifier(),
1210 property->getType(),
1211 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001212 0);
Chris Lattner38af2de2009-02-20 21:35:13 +00001213 SetterMethod->setMethodParams(&Argument, 1, Context);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001214 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001215 } else
1216 // A user declared setter will be synthesize when @synthesize of
1217 // the property with the same name is seen in the @implementation
1218 SetterMethod->setIsSynthesized();
1219 property->setSetterMethodDecl(SetterMethod);
1220 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001221 // Add any synthesized methods to the global pool. This allows us to
1222 // handle the following, which is supported by GCC (and part of the design).
1223 //
1224 // @interface Foo
1225 // @property double bar;
1226 // @end
1227 //
1228 // void thisIsUnfortunate() {
1229 // id foo;
1230 // double bar = [foo bar];
1231 // }
1232 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001233 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001234 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001235 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001236 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001237}
1238
Steve Naroffa56f6162007-12-18 01:30:32 +00001239// Note: For class/category implemenations, allMethods/allProperties is
1240// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001241void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1242 DeclPtrTy *allMethods, unsigned allNum,
1243 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001244 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001245 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001246
Steve Naroffa56f6162007-12-18 01:30:32 +00001247 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1248 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001249 // should be true.
1250 if (!ClassDecl)
1251 return;
1252
Chris Lattner4d391482007-12-12 07:09:47 +00001253 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001254 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1255 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001256 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001257
Steve Naroff0701bbb2009-01-08 17:28:14 +00001258 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001259
1260 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1261 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1262 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1263
Chris Lattner4d391482007-12-12 07:09:47 +00001264 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001265 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001266 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001267
1268 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001269 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001270 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001271 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001272 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1273 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001274 if ((isInterfaceDeclKind && PrevMethod && !match)
1275 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001276 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001277 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001278 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001279 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001280 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001281 InsMap[Method->getSelector()] = Method;
1282 /// The following allows us to typecheck messages to "id".
1283 AddInstanceMethodToGlobalPool(Method);
1284 }
1285 }
1286 else {
1287 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001288 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001289 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1290 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001291 if ((isInterfaceDeclKind && PrevMethod && !match)
1292 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001293 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001294 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001295 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001296 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001297 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001298 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001299 /// The following allows us to typecheck messages to "Class".
1300 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001301 }
1302 }
1303 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001304 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001305 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001306 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001307 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001308 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001309 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001310 // Categories are used to extend the class by declaring new methods.
1311 // By the same token, they are also used to add new properties. No
1312 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001313
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001314 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001315 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001316 if (C->getIdentifier() == 0)
1317 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001318 }
Steve Naroff09c47192009-01-09 15:36:25 +00001319 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1320 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1321 // user-defined setter/getter. It also synthesizes setter/getter methods
1322 // and adds them to the DeclContext and global method pools.
Chris Lattner97a58872009-02-16 18:32:47 +00001323 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1324 E = CDecl->prop_end(); I != E; ++I)
1325 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001326 CDecl->setAtEndLoc(AtEndLoc);
1327 }
1328 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001329 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001330 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001331 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001332 } else if (ObjCCategoryImplDecl* CatImplClass =
1333 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001334 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001335
Chris Lattner4d391482007-12-12 07:09:47 +00001336 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001337 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001338 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001339 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001340 Categories; Categories = Categories->getNextClassCategory()) {
1341 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001342 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001343 break;
1344 }
1345 }
1346 }
1347 }
Chris Lattner682bf922009-03-29 16:50:03 +00001348 if (isInterfaceDeclKind) {
1349 // Reject invalid vardecls.
1350 for (unsigned i = 0; i != tuvNum; i++) {
1351 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1352 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1353 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
1354 if (VDecl->getStorageClass() != VarDecl::Extern &&
1355 VDecl->getStorageClass() != VarDecl::PrivateExtern)
1356 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass)
1357 << cast<NamedDecl>(ClassDecl)->getIdentifier();
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001358 }
Chris Lattner682bf922009-03-29 16:50:03 +00001359 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001360 }
Chris Lattner4d391482007-12-12 07:09:47 +00001361}
1362
1363
1364/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1365/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001366static Decl::ObjCDeclQualifier
1367CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1368 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1369 if (PQTVal & ObjCDeclSpec::DQ_In)
1370 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1371 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1372 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1373 if (PQTVal & ObjCDeclSpec::DQ_Out)
1374 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1375 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1376 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1377 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1378 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1379 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1380 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001381
1382 return ret;
1383}
1384
Chris Lattnerb28317a2009-03-28 19:18:32 +00001385Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001386 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001387 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001388 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001389 Selector Sel,
1390 // optional arguments. The number of types/arguments is obtained
1391 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001392 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001393 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001394 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1395 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001396 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001397
1398 // Make sure we can establish a context for the method.
1399 if (!ClassDecl) {
1400 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001401 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001402 }
Chris Lattner4d391482007-12-12 07:09:47 +00001403 QualType resultDeclType;
1404
Steve Naroffccef3712009-02-20 22:59:16 +00001405 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001406 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001407
1408 // Methods cannot return interface types. All ObjC objects are
1409 // passed by reference.
1410 if (resultDeclType->isObjCInterfaceType()) {
1411 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1412 << "returned";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001413 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001414 }
1415 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001416 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001417
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001418 ObjCMethodDecl* ObjCMethod =
1419 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner32d3f9c2009-03-29 04:30:19 +00001420 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001421 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001422 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001423 MethodDeclKind == tok::objc_optional ?
1424 ObjCMethodDecl::Optional :
1425 ObjCMethodDecl::Required);
1426
Chris Lattner0ed844b2008-04-04 06:12:32 +00001427 llvm::SmallVector<ParmVarDecl*, 16> Params;
1428
1429 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1430 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001431 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001432
Steve Naroff6082c622008-12-09 19:36:17 +00001433 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001434 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001435 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001436 if (argType->isArrayType()) { // (char *[]) -> (char **)
1437 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001438 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001439 }
Steve Naroff6082c622008-12-09 19:36:17 +00001440 else if (argType->isFunctionType())
1441 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001442 else if (argType->isObjCInterfaceType()) {
1443 // FIXME! provide more precise location for the parameter
Steve Naroffccef3712009-02-20 22:59:16 +00001444 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1445 << "passed";
1446 ObjCMethod->setInvalidDecl();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001447 return DeclPtrTy();
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001448 }
Steve Naroff6082c622008-12-09 19:36:17 +00001449 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001450 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001451 ParmVarDecl* Param;
1452 if (originalArgType.isNull())
1453 Param = ParmVarDecl::Create(Context, ObjCMethod,
1454 SourceLocation(/*FIXME*/),
1455 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001456 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001457 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001458 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1459 SourceLocation(/*FIXME*/),
1460 ArgNames[i], argType, originalArgType,
1461 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001462
Chris Lattner0ed844b2008-04-04 06:12:32 +00001463 Param->setObjCDeclQualifier(
1464 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1465 Params.push_back(Param);
1466 }
1467
Chris Lattner38af2de2009-02-20 21:35:13 +00001468 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001469 ObjCMethod->setObjCDeclQualifier(
1470 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1471 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001472
1473 if (AttrList)
1474 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001475
1476 // For implementations (which can be very "coarse grain"), we add the
1477 // method now. This allows the AST to implement lookup methods that work
1478 // incrementally (without waiting until we parse the @end). It also allows
1479 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001480 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001481 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001482 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001483 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001484 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001485 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001486 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001487 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001488 }
1489 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001490 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001491 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001492 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001493 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001494 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001495 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001496 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001497 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001498 }
1499 }
1500 if (PrevMethod) {
1501 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001502 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001503 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001504 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001505 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001506 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001507}
1508
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001509void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1510 SourceLocation Loc,
1511 unsigned &Attributes) {
1512 // FIXME: Improve the reported location.
1513
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001514 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001515 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001516 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1517 ObjCDeclSpec::DQ_PR_assign |
1518 ObjCDeclSpec::DQ_PR_copy |
1519 ObjCDeclSpec::DQ_PR_retain))) {
1520 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1521 "readwrite" :
1522 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1523 "assign" :
1524 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1525 "copy" : "retain";
1526
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001527 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001528 diag::err_objc_property_attr_mutually_exclusive :
1529 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001530 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001531 }
1532
1533 // Check for copy or retain on non-object types.
1534 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1535 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001536 Diag(Loc, diag::err_objc_property_requires_object)
1537 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001538 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1539 }
1540
1541 // Check for more than one of { assign, copy, retain }.
1542 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1543 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001544 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1545 << "assign" << "copy";
1546 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001547 }
1548 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001549 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1550 << "assign" << "retain";
1551 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001552 }
1553 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1554 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001555 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1556 << "copy" << "retain";
1557 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001558 }
1559 }
1560
1561 // Warn if user supplied no assignment attribute, property is
1562 // readwrite, and this is an object type.
1563 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1564 ObjCDeclSpec::DQ_PR_retain)) &&
1565 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1566 Context.isObjCObjectPointerType(PropertyTy)) {
1567 // Skip this warning in gc-only mode.
1568 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1569 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1570
1571 // If non-gc code warn that this is likely inappropriate.
1572 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1573 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1574
1575 // FIXME: Implement warning dependent on NSCopying being
1576 // implemented. See also:
1577 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1578 // (please trim this list while you are at it).
1579 }
1580}
1581
Chris Lattnerb28317a2009-03-28 19:18:32 +00001582Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1583 FieldDeclarator &FD,
1584 ObjCDeclSpec &ODS,
1585 Selector GetterSel,
1586 Selector SetterSel,
1587 DeclPtrTy ClassCategory,
1588 bool *isOverridingProperty,
1589 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001590 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001591 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1592 // default is readwrite!
1593 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1594 // property is defaulted to 'assign' if it is readwrite and is
1595 // not retain or copy
1596 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1597 (isReadWrite &&
1598 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1599 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1600 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001601 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001602 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001603 // May modify Attributes.
1604 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001605 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1606 if (!CDecl->getIdentifier()) {
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001607 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001608 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001609 if ((CCPrimary = CDecl->getClassInterface())) {
1610 // Find the property in continuation class's primary class only.
1611 ObjCPropertyDecl *PIDecl = 0;
1612 IdentifierInfo *PropertyId = FD.D.getIdentifier();
1613 for (ObjCInterfaceDecl::prop_iterator I = CCPrimary->prop_begin(),
1614 E = CCPrimary->prop_end(); I != E; ++I)
1615 if ((*I)->getIdentifier() == PropertyId) {
1616 PIDecl = *I;
1617 break;
1618 }
1619
1620 if (PIDecl) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001621 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001622 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001623 unsigned PIkind = PIDecl->getPropertyAttributes();
1624 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001625 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001626 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1627 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001628 PIDecl->makeitReadWriteAttribute();
1629 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1630 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1631 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1632 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1633 PIDecl->setSetterName(SetterSel);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001634 // FIXME: use a common routine with addPropertyMethods.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001635 ObjCMethodDecl *SetterDecl =
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001636 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1637 Context.VoidTy,
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001638 CCPrimary,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001639 true, false, true,
1640 ObjCMethodDecl::Required);
Chris Lattner38af2de2009-02-20 21:35:13 +00001641 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001642 SourceLocation(),
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001643 PropertyId,
Chris Lattner38af2de2009-02-20 21:35:13 +00001644 T, VarDecl::None, 0);
1645 SetterDecl->setMethodParams(&Argument, 1, Context);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001646 PIDecl->setSetterMethodDecl(SetterDecl);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001647 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001648 else
1649 Diag(AtLoc, diag::err_use_continuation_class)
1650 << CCPrimary->getDeclName();
1651 *isOverridingProperty = true;
1652 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001653 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001654 // No matching property found in the primary class. Just fall thru
1655 // and add property to continuation class's primary class.
1656 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001657 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001658 Diag(CDecl->getLocation(), diag::err_continuation_class);
1659 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001660 return DeclPtrTy();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001661 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001662 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001663
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001664 Type *t = T.getTypePtr();
1665 if (t->isArrayType() || t->isFunctionType())
1666 Diag(AtLoc, diag::err_property_type) << T;
1667
Steve Naroff93983f82009-01-11 12:47:58 +00001668 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1669 assert(DC && "ClassDecl is not a DeclContext");
1670 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001671 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001672 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001673
1674 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001675
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001676 // Regardless of setter/getter attribute, we save the default getter/setter
1677 // selector names in anticipation of declaration of setter/getter methods.
1678 PDecl->setGetterName(GetterSel);
1679 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001680
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001681 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001682 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001683
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001684 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001685 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001686
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001687 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001688 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001689
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001690 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001691 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001692
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001693 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001694 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001695
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001696 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001697 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001698
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001699 if (isAssign)
1700 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1701
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001702 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001703 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001704
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001705 if (MethodImplKind == tok::objc_required)
1706 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1707 else if (MethodImplKind == tok::objc_optional)
1708 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001709 // A case of continuation class adding a new property in the class. This
1710 // is not what it was meant for. However, gcc supports it and so should we.
1711 // Make sure setter/getters are declared here.
1712 if (CCPrimary)
1713 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001714
Chris Lattnerb28317a2009-03-28 19:18:32 +00001715 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001716}
1717
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001718/// ActOnPropertyImplDecl - This routine performs semantic checks and
1719/// builds the AST node for a property implementation declaration; declared
1720/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001721///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001722Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1723 SourceLocation PropertyLoc,
1724 bool Synthesize,
1725 DeclPtrTy ClassCatImpDecl,
1726 IdentifierInfo *PropertyId,
1727 IdentifierInfo *PropertyIvar) {
1728 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001729 // Make sure we have a context for the property implementation declaration.
1730 if (!ClassImpDecl) {
1731 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001732 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001733 }
1734 ObjCPropertyDecl *property = 0;
1735 ObjCInterfaceDecl* IDecl = 0;
1736 // Find the class or category class where this property must have
1737 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001738 ObjCImplementationDecl *IC = 0;
1739 ObjCCategoryImplDecl* CatImplClass = 0;
1740 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001741 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001742 // We always synthesize an interface for an implementation
1743 // without an interface decl. So, IDecl is always non-zero.
1744 assert(IDecl &&
1745 "ActOnPropertyImplDecl - @implementation without @interface");
1746
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001747 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001748 property = IDecl->FindPropertyDeclaration(PropertyId);
1749 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001750 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001751 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001752 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001753 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001754 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001755 if (Synthesize) {
1756 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001757 return DeclPtrTy();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001758 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001759 IDecl = CatImplClass->getClassInterface();
1760 if (!IDecl) {
1761 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001762 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001763 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001764 ObjCCategoryDecl *Category =
1765 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1766
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001767 // If category for this implementation not found, it is an error which
1768 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001769 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00001770 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001771 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001772 property = Category->FindPropertyDeclaration(PropertyId);
1773 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001774 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001775 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001776 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001777 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001778 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001779 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001780 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001781 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001782 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001783 // Check that we have a valid, previously declared ivar for @synthesize
1784 if (Synthesize) {
1785 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001786 if (!PropertyIvar)
1787 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00001788 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001789 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniana5afdd02009-02-16 19:35:27 +00001790 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001791 if (!Ivar) {
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00001792 if (getLangOptions().ObjCNonFragileABI) {
1793 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1794 PropertyIvar, PropType,
1795 ObjCIvarDecl::Private,
1796 (Expr *)0);
1797 property->setPropertyIvarDecl(Ivar);
1798 }
1799 else {
Steve Narofff4c00ff2009-03-03 22:09:41 +00001800 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00001801 return DeclPtrTy();
1802 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001803 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001804 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1805
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001806 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001807 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001808 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001809 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001810 << property->getDeclName() << Ivar->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001811 return DeclPtrTy();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001812 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001813
1814 // FIXME! Rules for properties are somewhat different that those
1815 // for assignments. Use a new routine to consolidate all cases;
1816 // specifically for property redeclarations as well as for ivars.
1817 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1818 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1819 if (lhsType != rhsType &&
1820 lhsType->isArithmeticType()) {
1821 Diag(PropertyLoc, diag::error_property_ivar_type)
1822 << property->getDeclName() << Ivar->getDeclName();
1823 return DeclPtrTy();
1824 }
1825 // __weak is explicit. So it works on Canonical type.
1826 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak()) {
1827 Diag(PropertyLoc, diag::error_weak_property)
1828 << property->getDeclName() << Ivar->getDeclName();
1829 return DeclPtrTy();
1830 }
1831 if ((Context.isObjCObjectPointerType(property->getType()) ||
1832 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak()) {
1833 Diag(PropertyLoc, diag::error_strong_property)
1834 << property->getDeclName() << Ivar->getDeclName();
1835 return DeclPtrTy();
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001836 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001837 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001838 } else if (PropertyIvar) {
1839 // @dynamic
1840 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001841 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001842 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001843 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001844 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001845 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1846 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001847 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001848 ObjCPropertyImplDecl::Synthesize
1849 : ObjCPropertyImplDecl::Dynamic),
1850 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001851 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001852 if (IC) {
1853 if (Synthesize)
1854 if (ObjCPropertyImplDecl *PPIDecl =
1855 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1856 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1857 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1858 << PropertyIvar;
1859 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1860 }
1861
1862 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1863 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1864 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001865 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001866 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001867 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001868 }
1869 else {
1870 if (Synthesize)
1871 if (ObjCPropertyImplDecl *PPIDecl =
1872 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1873 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1874 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1875 << PropertyIvar;
1876 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1877 }
1878
1879 if (ObjCPropertyImplDecl *PPIDecl =
1880 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1881 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1882 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001883 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001884 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001885 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001886 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001887
Chris Lattnerb28317a2009-03-28 19:18:32 +00001888 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001889}
Anders Carlsson15281452008-11-04 16:57:32 +00001890
Chris Lattnercc98eac2008-12-17 07:13:27 +00001891bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001892 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001893 return false;
1894
1895 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1896 D->setInvalidDecl();
1897
1898 return true;
1899}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001900
1901/// Collect the instance variables declared in an Objective-C object. Used in
1902/// the creation of structures from objects using the @defs directive.
1903/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1904/// part of the AST generation logic of @defs.
1905static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1906 ASTContext& Ctx,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001907 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001908 if (Class->getSuperClass())
1909 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1910
1911 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001912 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
1913 E = Class->ivar_end(); I != E; ++I) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001914 ObjCIvarDecl* ID = *I;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001915 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
1916 ID->getIdentifier(), ID->getType(),
1917 ID->getBitWidth());
1918 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattnercc98eac2008-12-17 07:13:27 +00001919 }
1920}
1921
1922/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1923/// instance variables of ClassName into Decls.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001924void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001925 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001926 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001927 // Check that ClassName is a valid class
1928 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1929 if (!Class) {
1930 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1931 return;
1932 }
1933 // Collect the instance variables
Chris Lattnerb28317a2009-03-28 19:18:32 +00001934 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001935
1936 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001937 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001938 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001939 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00001940 if (getLangOptions().CPlusPlus)
1941 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001942 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001943 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001944 }
1945}
1946