blob: e35b7a919b49f1e24d1b71c5ed3d78d407b66f39 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000015#include "clang/Sema/ExternalSemaSource.h"
Steve Naroffca331292009-03-03 14:49:36 +000016#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000020using namespace clang;
21
Steve Naroffebf64432009-02-28 16:59:13 +000022/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000023/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000024void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000025 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000026 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff394f3f42008-07-25 17:57:26 +000027
28 // If we don't have a valid method decl, simply return.
29 if (!MDecl)
30 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000031
Chris Lattner38c5ebd2009-04-19 05:21:20 +000032 CurFunctionNeedsScopeChecking = false;
33
Steve Naroffa56f6162007-12-18 01:30:32 +000034 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000035 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000036 AddInstanceMethodToGlobalPool(MDecl);
37 else
38 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000039
40 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000041 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000042
43 // Create Decl objects for each parameter, entrring them in the scope for
44 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000045
46 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000047 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000048
Daniel Dunbar451318c2008-08-26 06:07:48 +000049 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
50 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000051
Chris Lattner8123a952008-04-10 02:22:51 +000052 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000053 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
54 E = MDecl->param_end(); PI != E; ++PI)
55 if ((*PI)->getIdentifier())
56 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000057}
58
Chris Lattnerb28317a2009-03-28 19:18:32 +000059Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000060ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000063 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000064 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000065 assert(ClassName && "Missing class identifier");
66
67 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000068 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000069 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000070 // Maybe we will complain about the shadowed template parameter.
71 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
72 // Just pretend that we didn't see the previous declaration.
73 PrevDecl = 0;
74 }
75
Ted Kremeneka526c5c2008-01-07 19:49:32 +000076 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000077 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000078 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000079 }
80
Ted Kremeneka526c5c2008-01-07 19:49:32 +000081 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000082 if (IDecl) {
83 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000084 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +000085 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000086 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000087 Diag(IDecl->getLocation(), diag::note_previous_definition);
88
Steve Naroffcfe8bf32008-11-18 19:15:30 +000089 // Return the previous class interface.
90 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +000091 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +000092 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000093 IDecl->setLocation(AtInterfaceLoc);
94 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000095 }
Chris Lattnerb752f282008-07-21 07:06:49 +000096 } else {
Douglas Gregord0434102009-01-09 00:49:46 +000097 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000098 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000099 if (AttrList)
100 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000101
Steve Naroffa7503a72009-04-23 15:15:40 +0000102 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000103 }
104
105 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000106 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000107 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000108
Steve Naroff818cb9e2009-02-04 17:14:05 +0000109 ObjCInterfaceDecl *SuperClassDecl =
110 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000111
112 // Diagnose classes that inherit from deprecated classes.
113 if (SuperClassDecl)
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000114 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000115
Steve Naroff818cb9e2009-02-04 17:14:05 +0000116 if (PrevDecl && SuperClassDecl == 0) {
117 // The previous declaration was not a class decl. Check if we have a
118 // typedef. If we do, get the underlying class type.
119 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
120 QualType T = TDecl->getUnderlyingType();
121 if (T->isObjCInterfaceType()) {
122 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
123 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
124 }
125 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000126
Steve Naroff818cb9e2009-02-04 17:14:05 +0000127 // This handles the following case:
128 //
129 // typedef int SuperClass;
130 // @interface MyClass : SuperClass {} @end
131 //
132 if (!SuperClassDecl) {
133 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
134 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
135 }
136 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000137
Steve Naroff818cb9e2009-02-04 17:14:05 +0000138 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
139 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000140 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000141 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000142 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000143 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000144 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000145 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000146 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000147 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000148 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000149 IDecl->setLocEnd(SuperLoc);
150 } else { // we have a root class.
151 IDecl->setLocEnd(ClassLoc);
152 }
153
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000154 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000155 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000156 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
157 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000158 IDecl->setLocEnd(EndProtoLoc);
159 }
Anders Carlsson15281452008-11-04 16:57:32 +0000160
161 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000162 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000163}
164
165/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000166/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000167Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
168 IdentifierInfo *AliasName,
169 SourceLocation AliasLocation,
170 IdentifierInfo *ClassName,
171 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000172 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000173 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000174 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000175 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000176 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000177 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000178 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000179 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000180 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000181 }
182 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000183 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000184 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
185 QualType T = TDecl->getUnderlyingType();
186 if (T->isObjCInterfaceType()) {
187 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
188 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000189 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000190 }
191 }
192 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000193 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
194 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000195 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000196 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000197 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000198 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000199 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000200
201 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000202 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000203 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000204
Anders Carlsson15281452008-11-04 16:57:32 +0000205 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000206 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000207
Chris Lattnerb28317a2009-03-28 19:18:32 +0000208 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000209}
210
Steve Naroff61d68522009-03-05 15:22:01 +0000211void Sema::CheckForwardProtocolDeclarationForCircularDependency(
212 IdentifierInfo *PName,
213 SourceLocation &Ploc, SourceLocation PrevLoc,
214 const ObjCList<ObjCProtocolDecl> &PList)
215{
216 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
217 E = PList.end(); I != E; ++I) {
218
Douglas Gregor6e378de2009-04-23 23:18:26 +0000219 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000220 if (PDecl->getIdentifier() == PName) {
221 Diag(Ploc, diag::err_protocol_has_circular_dependency);
222 Diag(PrevLoc, diag::note_previous_definition);
223 }
224 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
225 PDecl->getLocation(), PDecl->getReferencedProtocols());
226 }
227 }
228}
229
Chris Lattnerb28317a2009-03-28 19:18:32 +0000230Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000231Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
232 IdentifierInfo *ProtocolName,
233 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000234 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000235 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000236 SourceLocation EndProtoLoc,
237 AttributeList *AttrList) {
238 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000239 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000240 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000241 if (PDecl) {
242 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000243 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000244 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000245 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000246 // Just return the protocol we already had.
247 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000248 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000249 }
Steve Naroff61d68522009-03-05 15:22:01 +0000250 ObjCList<ObjCProtocolDecl> PList;
251 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
252 CheckForwardProtocolDeclarationForCircularDependency(
253 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
254 PList.Destroy(Context);
255
Steve Narofff11b5082008-08-13 16:39:22 +0000256 // Make sure the cached decl gets a valid start location.
257 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000258 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000259 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000260 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
261 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000262 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000263 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000264 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000265 if (AttrList)
266 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000267 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000268 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000269 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000270 PDecl->setLocEnd(EndProtoLoc);
271 }
Anders Carlsson15281452008-11-04 16:57:32 +0000272
273 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000274 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000275}
276
277/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000278/// issues an error if they are not declared. It returns list of
279/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000280void
Chris Lattnere13b9592008-07-26 04:03:38 +0000281Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000282 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000283 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000284 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000285 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000286 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000287 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000288 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000289 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000290 continue;
291 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000292
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000293 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000294
295 // If this is a forward declaration and we are supposed to warn in this
296 // case, do it.
297 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000298 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000299 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000300 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000301 }
302}
303
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000304/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000305/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000306///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000307void
308Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
309 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000310 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000311 ObjCPropertyDecl::PropertyAttributeKind CAttr =
312 Property->getPropertyAttributes();
313 ObjCPropertyDecl::PropertyAttributeKind SAttr =
314 SuperProperty->getPropertyAttributes();
315 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
316 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000317 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000318 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000319 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
320 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000321 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000322 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000323 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
324 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000325 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000326 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000327
328 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
329 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000330 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000331 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000332 if (Property->getSetterName() != SuperProperty->getSetterName())
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() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000335 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000336 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000337 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000338
339 QualType LHSType =
340 Context.getCanonicalType(SuperProperty->getType());
341 QualType RHSType =
342 Context.getCanonicalType(Property->getType());
343
344 if (!Context.typesAreCompatible(LHSType, RHSType)) {
345 // FIXME: Incorporate this test with typesAreCompatible.
346 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
347 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
348 return;
349 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
350 << Property->getType() << SuperProperty->getType() << inheritedName;
351 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000352}
353
354/// ComparePropertiesInBaseAndSuper - This routine compares property
355/// declarations in base and its super class, if any, and issues
356/// diagnostics in a variety of inconsistant situations.
357///
Chris Lattner70f19542009-02-16 21:26:43 +0000358void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000359 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
360 if (!SDecl)
361 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000362 // FIXME: O(N^2)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000363 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
364 E = SDecl->prop_end(Context); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000365 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000366 // Does property in super class has declaration in current class?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000367 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
368 E = IDecl->prop_end(Context); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000369 ObjCPropertyDecl *PDecl = (*I);
370 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000371 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000372 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000373 }
374 }
375}
376
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000377/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
378/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000379/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000380void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000381Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000382 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000383 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
384 if (!IDecl) {
385 // Category
386 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
387 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Douglas Gregor6ab35242009-04-09 21:40:53 +0000388 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
389 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000390 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000391 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000392 // Is this property already in category's list of properties?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000393 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000394 CP != CE; ++CP)
395 if ((*CP)->getIdentifier() == Pr->getIdentifier())
396 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000397 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000398 // Property protocol already exist in class. Diagnose any mismatch.
399 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
400 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000401 return;
402 }
Douglas Gregor6ab35242009-04-09 21:40:53 +0000403 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
404 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000405 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000406 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000407 // Is this property already in class's list of properties?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000408 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000409 CP != CE; ++CP)
410 if ((*CP)->getIdentifier() == Pr->getIdentifier())
411 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000412 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000413 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000414 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000415 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000416}
417
418/// MergeProtocolPropertiesIntoClass - This routine merges properties
419/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000420/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000421///
Chris Lattner70f19542009-02-16 21:26:43 +0000422void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000423 DeclPtrTy MergeItsProtocols) {
424 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000425 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
426
427 if (!IDecl) {
428 // Category
429 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
430 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
431 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
432 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
433 E = MDecl->protocol_end(); P != E; ++P)
434 // Merge properties of category (*P) into IDECL's
435 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
436
437 // Go thru the list of protocols for this category and recursively merge
438 // their properties into this class as well.
439 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
440 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000441 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000442 } else {
443 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
444 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
445 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000446 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000447 }
448 return;
449 }
450
Chris Lattnerb752f282008-07-21 07:06:49 +0000451 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000452 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
453 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000454 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000455 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
456
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000457 // Go thru the list of protocols for this class and recursively merge
458 // their properties into this class as well.
459 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
460 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000461 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000462 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000463 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
464 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
465 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000466 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000467 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000468}
469
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000470/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000471/// a class method in its extension.
472///
473void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
474 ObjCInterfaceDecl *ID) {
475 if (!ID)
476 return; // Possibly due to previous error
477
478 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000479 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
480 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000481 ObjCMethodDecl *MD = *i;
482 MethodMap[MD->getSelector()] = MD;
483 }
484
485 if (MethodMap.empty())
486 return;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000487 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
488 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000489 ObjCMethodDecl *Method = *i;
490 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
491 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
492 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
493 << Method->getDeclName();
494 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
495 }
496 }
497}
498
Chris Lattner58fe03b2009-04-12 08:43:13 +0000499/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000500Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000501Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000502 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000503 unsigned NumElts,
504 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000505 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000506
507 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000508 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000509 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000510 if (PDecl == 0) { // Not already seen?
511 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
512 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000513 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000514 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000515 if (attrList)
516 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000517 Protocols.push_back(PDecl);
518 }
Anders Carlsson15281452008-11-04 16:57:32 +0000519
520 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000521 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000522 &Protocols[0], Protocols.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000523 CurContext->addDecl(Context, PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000524 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000525 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000526}
527
Chris Lattnerb28317a2009-03-28 19:18:32 +0000528Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000529ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
530 IdentifierInfo *ClassName, SourceLocation ClassLoc,
531 IdentifierInfo *CategoryName,
532 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000533 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000534 unsigned NumProtoRefs,
535 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000536 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000537 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
538 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000539 CurContext->addDecl(Context, CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000540
541 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000542 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000543 if (!IDecl || IDecl->isForwardDecl()) {
544 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000545 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000546 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000547 }
Chris Lattner4d391482007-12-12 07:09:47 +0000548
Chris Lattner70f19542009-02-16 21:26:43 +0000549 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000550
551 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000552 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000553
554 /// Check for duplicate interface declaration for this category
555 ObjCCategoryDecl *CDeclChain;
556 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
557 CDeclChain = CDeclChain->getNextClassCategory()) {
558 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
559 Diag(CategoryLoc, diag::warn_dup_category_def)
560 << ClassName << CategoryName;
561 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
562 break;
563 }
564 }
565 if (!CDeclChain)
566 CDecl->insertNextClassCategory();
567
Chris Lattner4d391482007-12-12 07:09:47 +0000568 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000569 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000570 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000571 }
Anders Carlsson15281452008-11-04 16:57:32 +0000572
573 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000574 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000575}
576
577/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000578/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000579/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000580Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000581 SourceLocation AtCatImplLoc,
582 IdentifierInfo *ClassName, SourceLocation ClassLoc,
583 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000584 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000585 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000586 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
587 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000588 /// Check that class of this category is already completely declared.
589 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000590 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000591
Douglas Gregord0434102009-01-09 00:49:46 +0000592 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000593 CurContext->addDecl(Context, CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000594
Chris Lattner4d391482007-12-12 07:09:47 +0000595 /// TODO: Check that CatName, category name, is not used in another
596 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000597 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000598
599 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000600 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000601}
602
Chris Lattnerb28317a2009-03-28 19:18:32 +0000603Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000604 SourceLocation AtClassImplLoc,
605 IdentifierInfo *ClassName, SourceLocation ClassLoc,
606 IdentifierInfo *SuperClassname,
607 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000608 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000609 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000610 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000611 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000612 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000613 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000614 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000615 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000616 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000617 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000618 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000619 IDecl = 0;
620 }
Chris Lattner4d391482007-12-12 07:09:47 +0000621 }
622
623 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000624 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000625 if (SuperClassname) {
626 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000627 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000628 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000629 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
630 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000631 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000632 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000633 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000634 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000635 Diag(SuperClassLoc, diag::err_undef_superclass)
636 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000637 else if (IDecl && IDecl->getSuperClass() != SDecl) {
638 // This implementation and its interface do not have the same
639 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000640 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000641 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000642 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000643 }
644 }
645 }
646
647 if (!IDecl) {
648 // Legacy case of @implementation with no corresponding @interface.
649 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000650
651 // FIXME: Do we support attributes on the @implementation? If so
652 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000653 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
654 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000655 IDecl->setSuperClass(SDecl);
656 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000657
658 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000659 } else {
660 // Mark the interface as being completed, even if it was just as
661 // @class ....;
662 // declaration; the user cannot reopen it.
663 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000664 }
665
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000666 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000667 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000668 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000669
Anders Carlsson15281452008-11-04 16:57:32 +0000670 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000671 return DeclPtrTy::make(IMPDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000672
Chris Lattner4d391482007-12-12 07:09:47 +0000673 // Check that there is no duplicate implementation of this class.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000674 if (LookupObjCImplementation(ClassName))
Chris Lattner75c9cae2008-03-16 20:53:07 +0000675 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000676 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000677 else // add it to the list.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000678 PushOnScopeChains(IMPDecl, TUScope);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000679 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000680}
681
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000682void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
683 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000684 SourceLocation RBrace) {
685 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000686 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000687 if (!IDecl)
688 return;
689 /// Check case of non-existing @interface decl.
690 /// (legacy objective-c @implementation decl without an @interface decl).
691 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000692 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000693 IDecl->setIVarList(ivars, numIvars, Context);
694 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000695 return;
696 }
697 // If implementation has empty ivar list, just return.
698 if (numIvars == 0)
699 return;
700
701 assert(ivars && "missing @implementation ivars");
702
703 // Check interface's Ivar list against those in the implementation.
704 // names and types must match.
705 //
Chris Lattner4d391482007-12-12 07:09:47 +0000706 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000707 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000708 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
709 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000710 ObjCIvarDecl* ImplIvar = ivars[j++];
711 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000712 assert (ImplIvar && "missing implementation ivar");
713 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000714
715 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000716 if (Context.getCanonicalType(ImplIvar->getType()) !=
717 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000718 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000719 << ImplIvar->getIdentifier()
720 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000721 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000722 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
723 Expr *ImplBitWidth = ImplIvar->getBitWidth();
724 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000725 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
726 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000727 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
728 << ImplIvar->getIdentifier();
729 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
730 }
731 }
732 // Make sure the names are identical.
733 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000734 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000735 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000736 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000737 }
738 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000739 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000740
741 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000742 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000743 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000744 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000745}
746
Steve Naroff3c2eb662008-02-10 21:38:56 +0000747void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
748 bool &IncompleteImpl) {
749 if (!IncompleteImpl) {
750 Diag(ImpLoc, diag::warn_incomplete_impl);
751 IncompleteImpl = true;
752 }
Chris Lattner08631c52008-11-23 21:45:46 +0000753 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000754}
755
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000756void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
757 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000758 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Chris Lattner3aff9192009-04-11 19:58:42 +0000759 ImpMethodDecl->getResultType())) {
760 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
761 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
762 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000763 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
764 }
Chris Lattner3aff9192009-04-11 19:58:42 +0000765
766 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
767 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
768 IM != EM; ++IM, ++IF) {
769 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()))
770 continue;
771
772 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
773 << ImpMethodDecl->getDeclName() << (*IF)->getType()
774 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000775 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000776 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000777}
778
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000779/// isPropertyReadonly - Return true if property is readonly, by searching
780/// for the property in the class and in its categories and implementations
781///
782bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000783 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000784 // by far the most common case.
785 if (!PDecl->isReadOnly())
786 return false;
787 // Even if property is ready only, if interface has a user defined setter,
788 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000789 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000790 return false;
791
792 // Main class has the property as 'readonly'. Must search
793 // through the category list to see if the property's
794 // attribute has been over-ridden to 'readwrite'.
795 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
796 Category; Category = Category->getNextClassCategory()) {
797 // Even if property is ready only, if a category has a user defined setter,
798 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000799 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000800 return false;
801 ObjCPropertyDecl *P =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000802 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000803 if (P && !P->isReadOnly())
804 return false;
805 }
806
807 // Also, check for definition of a setter method in the implementation if
808 // all else failed.
809 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
810 if (ObjCImplementationDecl *IMD =
811 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000812 if (IMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000813 return false;
814 }
815 else if (ObjCCategoryImplDecl *CIMD =
816 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000817 if (CIMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000818 return false;
819 }
820 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000821 // Lastly, look through the implementation (if one is in scope).
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000822 if (ObjCImplementationDecl *ImpDecl
823 = LookupObjCImplementation(IDecl->getIdentifier()))
Douglas Gregor653f1b12009-04-23 01:02:12 +0000824 if (ImpDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000825 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000826 // If all fails, look at the super class.
827 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
828 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000829 return true;
830}
831
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000832/// FIXME: Type hierarchies in Objective-C can be deep. We could most
833/// likely improve the efficiency of selector lookups and type
834/// checking by associating with each protocol / interface / category
835/// the flattened instance tables. If we used an immutable set to keep
836/// the table then it wouldn't add significant memory cost and it
837/// would be handy for lookups.
838
Steve Naroffefe7f362008-02-08 22:06:17 +0000839/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000840/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000841void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
842 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000843 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000844 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000845 const llvm::DenseSet<Selector> &ClsMap,
846 ObjCInterfaceDecl *IDecl) {
847 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
848
849 // If a method lookup fails locally we still need to look and see if
850 // the method was implemented by a base class or an inherited
851 // protocol. This lookup is slow, but occurs rarely in correct code
852 // and otherwise would terminate in a warning.
853
Chris Lattner4d391482007-12-12 07:09:47 +0000854 // check unimplemented instance methods.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000855 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
856 E = PDecl->instmeth_end(Context); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000857 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000858 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000859 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000860 (!Super ||
861 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000862 // Ugly, but necessary. Method declared in protcol might have
863 // have been synthesized due to a property declared in the class which
864 // uses the protocol.
865 ObjCMethodDecl *MethodInClass =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000866 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000867 if (!MethodInClass || !MethodInClass->isSynthesized())
868 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
869 }
Chris Lattner4d391482007-12-12 07:09:47 +0000870 }
871 // check unimplemented class methods
Douglas Gregor6ab35242009-04-09 21:40:53 +0000872 for (ObjCProtocolDecl::classmeth_iterator
873 I = PDecl->classmeth_begin(Context),
874 E = PDecl->classmeth_end(Context);
875 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000876 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000877 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
878 !ClsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000879 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000880 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000881 }
Chris Lattner780f3292008-07-21 21:32:27 +0000882 // Check on this protocols's referenced protocols, recursively.
883 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
884 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000885 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000886}
887
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000888/// MatchAllMethodDeclarations - Check methods declaraed in interface or
889/// or protocol against those declared in their implementations.
890///
891void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
892 const llvm::DenseSet<Selector> &ClsMap,
893 llvm::DenseSet<Selector> &InsMapSeen,
894 llvm::DenseSet<Selector> &ClsMapSeen,
895 ObjCImplDecl* IMPDecl,
896 ObjCContainerDecl* CDecl,
897 bool &IncompleteImpl,
898 bool ImmediateClass)
899{
900 // Check and see if instance methods in class interface have been
901 // implemented in the implementation class. If so, their types match.
902 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
903 E = CDecl->instmeth_end(Context); I != E; ++I) {
904 if (InsMapSeen.count((*I)->getSelector()))
905 continue;
906 InsMapSeen.insert((*I)->getSelector());
907 if (!(*I)->isSynthesized() &&
908 !InsMap.count((*I)->getSelector())) {
909 if (ImmediateClass)
910 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
911 continue;
912 }
913 else {
914 ObjCMethodDecl *ImpMethodDecl =
915 IMPDecl->getInstanceMethod(Context, (*I)->getSelector());
916 ObjCMethodDecl *IntfMethodDecl =
917 CDecl->getInstanceMethod(Context, (*I)->getSelector());
918 assert(IntfMethodDecl &&
919 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
920 // ImpMethodDecl may be null as in a @dynamic property.
921 if (ImpMethodDecl)
922 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
923 }
924 }
925
926 // Check and see if class methods in class interface have been
927 // implemented in the implementation class. If so, their types match.
928 for (ObjCInterfaceDecl::classmeth_iterator
929 I = CDecl->classmeth_begin(Context),
930 E = CDecl->classmeth_end(Context);
931 I != E; ++I) {
932 if (ClsMapSeen.count((*I)->getSelector()))
933 continue;
934 ClsMapSeen.insert((*I)->getSelector());
935 if (!ClsMap.count((*I)->getSelector())) {
936 if (ImmediateClass)
937 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
938 }
939 else {
940 ObjCMethodDecl *ImpMethodDecl =
941 IMPDecl->getClassMethod(Context, (*I)->getSelector());
942 ObjCMethodDecl *IntfMethodDecl =
943 CDecl->getClassMethod(Context, (*I)->getSelector());
944 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
945 }
946 }
947 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
948 // Check for any implementation of a methods declared in protocol.
949 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
950 E = I->protocol_end(); PI != E; ++PI)
951 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
952 IMPDecl,
953 (*PI), IncompleteImpl, false);
954 if (I->getSuperClass())
955 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
956 IMPDecl,
957 I->getSuperClass(), IncompleteImpl, false);
958 }
959}
960
Chris Lattnercddc8882009-03-01 00:56:52 +0000961void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
962 ObjCContainerDecl* CDecl,
963 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000964 llvm::DenseSet<Selector> InsMap;
965 // Check and see if instance methods in class interface have been
966 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000967 for (ObjCImplementationDecl::instmeth_iterator
968 I = IMPDecl->instmeth_begin(Context),
969 E = IMPDecl->instmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000970 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000971
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000972 // Check and see if properties declared in the interface have either 1)
973 // an implementation or 2) there is a @synthesize/@dynamic implementation
974 // of the property in the @implementation.
975 if (isa<ObjCInterfaceDecl>(CDecl))
976 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context),
977 E = CDecl->prop_end(Context); P != E; ++P) {
978 ObjCPropertyDecl *Prop = (*P);
979 if (Prop->isInvalidDecl())
980 continue;
981 ObjCPropertyImplDecl *PI = 0;
982 // Is there a matching propery synthesize/dynamic?
Douglas Gregor653f1b12009-04-23 01:02:12 +0000983 for (ObjCImplDecl::propimpl_iterator
984 I = IMPDecl->propimpl_begin(Context),
985 EI = IMPDecl->propimpl_end(Context); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000986 if ((*I)->getPropertyDecl() == Prop) {
987 PI = (*I);
988 break;
989 }
990 if (PI)
991 continue;
992 if (!InsMap.count(Prop->getGetterName())) {
993 Diag(Prop->getLocation(),
994 diag::warn_setter_getter_impl_required)
995 << Prop->getDeclName() << Prop->getGetterName();
996 Diag(IMPDecl->getLocation(),
997 diag::note_property_impl_required);
998 }
999
1000 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1001 Diag(Prop->getLocation(),
1002 diag::warn_setter_getter_impl_required)
1003 << Prop->getDeclName() << Prop->getSetterName();
1004 Diag(IMPDecl->getLocation(),
1005 diag::note_property_impl_required);
1006 }
1007 }
1008
Chris Lattner4d391482007-12-12 07:09:47 +00001009 llvm::DenseSet<Selector> ClsMap;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001010 for (ObjCImplementationDecl::classmeth_iterator
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001011 I = IMPDecl->classmeth_begin(Context),
1012 E = IMPDecl->classmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001013 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +00001014
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001015 // Check for type conflict of methods declared in a class/protocol and
1016 // its implementation; if any.
1017 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1018 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1019 IMPDecl, CDecl,
1020 IncompleteImpl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001021
1022 // Check the protocol list for unimplemented methods in the @implementation
1023 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001024 // Check and see if class methods in class interface have been
1025 // implemented in the implementation class.
1026
Chris Lattnercddc8882009-03-01 00:56:52 +00001027 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001028 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001029 E = I->protocol_end(); PI != E; ++PI)
1030 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1031 InsMap, ClsMap, I);
1032 // Check class extensions (unnamed categories)
1033 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1034 Categories; Categories = Categories->getNextClassCategory()) {
1035 if (!Categories->getIdentifier()) {
1036 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1037 break;
1038 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001039 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001040 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1041 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1042 E = C->protocol_end(); PI != E; ++PI)
1043 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1044 InsMap, ClsMap, C->getClassInterface());
1045 } else
1046 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001047}
1048
1049/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001050Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001051Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001052 IdentifierInfo **IdentList,
1053 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001054 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +00001055
1056 for (unsigned i = 0; i != NumElts; ++i) {
1057 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001058 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001059 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001060 // Maybe we will complain about the shadowed template parameter.
1061 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1062 // Just pretend that we didn't see the previous declaration.
1063 PrevDecl = 0;
1064 }
1065
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001066 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001067 // GCC apparently allows the following idiom:
1068 //
1069 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1070 // @class XCElementToggler;
1071 //
1072 // FIXME: Make an extension?
1073 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1074 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001075 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001076 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +00001077 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001078 else if (TDD) {
1079 // a forward class declaration matching a typedef name of a class
1080 // refers to the underlying class.
1081 if (ObjCInterfaceType * OI =
1082 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1083 PrevDecl = OI->getDecl();
1084 }
Chris Lattner4d391482007-12-12 07:09:47 +00001085 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001086 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001087 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +00001088 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1089 IdentList[i], SourceLocation(), true);
Steve Naroff8f06f842009-04-23 16:00:56 +00001090 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +00001091 }
1092
1093 Interfaces.push_back(IDecl);
1094 }
1095
Douglas Gregord0434102009-01-09 00:49:46 +00001096 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001097 &Interfaces[0],
1098 Interfaces.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +00001099 CurContext->addDecl(Context, CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001100 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001101 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001102}
1103
1104
1105/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1106/// returns true, or false, accordingly.
1107/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001108bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001109 const ObjCMethodDecl *PrevMethod,
1110 bool matchBasedOnSizeAndAlignment) {
1111 QualType T1 = Context.getCanonicalType(Method->getResultType());
1112 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1113
1114 if (T1 != T2) {
1115 // The result types are different.
1116 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001117 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001118 // Incomplete types don't have a size and alignment.
1119 if (T1->isIncompleteType() || T2->isIncompleteType())
1120 return false;
1121 // Check is based on size and alignment.
1122 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1123 return false;
1124 }
Chris Lattner89951a82009-02-20 18:43:26 +00001125
1126 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1127 E = Method->param_end();
1128 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1129
1130 for (; ParamI != E; ++ParamI, ++PrevI) {
1131 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1132 T1 = Context.getCanonicalType((*ParamI)->getType());
1133 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001134 if (T1 != T2) {
1135 // The result types are different.
1136 if (!matchBasedOnSizeAndAlignment)
1137 return false;
1138 // Incomplete types don't have a size and alignment.
1139 if (T1->isIncompleteType() || T2->isIncompleteType())
1140 return false;
1141 // Check is based on size and alignment.
1142 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1143 return false;
1144 }
Chris Lattner4d391482007-12-12 07:09:47 +00001145 }
1146 return true;
1147}
1148
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001149/// \brief Read the contents of the instance and factory method pools
1150/// for a given selector from external storage.
1151///
1152/// This routine should only be called once, when neither the instance
1153/// nor the factory method pool has an entry for this selector.
1154Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
1155 bool isInstance) {
1156 assert(ExternalSource && "We need an external AST source");
1157 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1158 "Selector data already loaded into the instance method pool");
1159 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1160 "Selector data already loaded into the factory method pool");
1161
1162 // Read the method list from the external source.
1163 std::pair<ObjCMethodList, ObjCMethodList> Methods
1164 = ExternalSource->ReadMethodPool(Sel);
1165
1166 if (isInstance) {
1167 if (Methods.second.Method)
1168 FactoryMethodPool[Sel] = Methods.second;
1169 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
1170 }
1171
1172 if (Methods.first.Method)
1173 InstanceMethodPool[Sel] = Methods.first;
1174
1175 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1176}
1177
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001178void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001179 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1180 = InstanceMethodPool.find(Method->getSelector());
1181 if (Pos == InstanceMethodPool.end()) {
1182 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1183 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1184 else
1185 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1186 ObjCMethodList())).first;
1187 }
1188
1189 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001190 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001191 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001192 Entry.Method = Method;
1193 Entry.Next = 0;
1194 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001195 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001196
1197 // We've seen a method with this name, see if we have already seen this type
1198 // signature.
1199 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1200 if (MatchTwoMethodDeclarations(Method, List->Method))
1201 return;
1202
1203 // We have a new signature for an existing method - add it.
1204 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1205 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001206}
1207
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001208// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001209ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1210 SourceRange R) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001211 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1212 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001213 if (Pos == InstanceMethodPool.end()) {
1214 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001215 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1216 else
1217 return 0;
1218 }
1219
1220 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001221 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001222
1223 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001224 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1225 // This checks if the methods differ by size & alignment.
1226 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1227 issueWarning = true;
1228 }
1229 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001230 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001231 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001232 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001233 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001234 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001235 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001236 }
1237 return MethList.Method;
1238}
1239
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001240void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001241 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1242 = FactoryMethodPool.find(Method->getSelector());
1243 if (Pos == FactoryMethodPool.end()) {
1244 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1245 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1246 else
1247 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1248 ObjCMethodList())).first;
1249 }
1250
1251 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001252 if (!FirstMethod.Method) {
1253 // Haven't seen a method with this selector name yet - add it.
1254 FirstMethod.Method = Method;
1255 FirstMethod.Next = 0;
1256 } else {
1257 // We've seen a method with this name, now check the type signature(s).
1258 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1259
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001260 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001261 Next = Next->Next)
1262 match = MatchTwoMethodDeclarations(Method, Next->Method);
1263
1264 if (!match) {
1265 // We have a new signature for an existing method - add it.
1266 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001267 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001268 FirstMethod.Next = OMI;
1269 }
1270 }
1271}
1272
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001273ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
1274 SourceRange R) {
1275 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1276 = FactoryMethodPool.find(Sel);
1277 if (Pos == FactoryMethodPool.end()) {
1278 if (ExternalSource && !InstanceMethodPool.count(Sel))
1279 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1280 else
1281 return 0;
1282 }
1283
1284 ObjCMethodList &MethList = Pos->second;
1285 bool issueWarning = false;
1286
1287 if (MethList.Method && MethList.Next) {
1288 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1289 // This checks if the methods differ by size & alignment.
1290 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1291 issueWarning = true;
1292 }
1293 if (issueWarning && (MethList.Method && MethList.Next)) {
1294 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1295 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1296 << MethList.Method->getSourceRange();
1297 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1298 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1299 << Next->Method->getSourceRange();
1300 }
1301 return MethList.Method;
1302}
1303
Steve Naroff0701bbb2009-01-08 17:28:14 +00001304/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1305/// have the property type and issue diagnostics if they don't.
1306/// Also synthesize a getter/setter method if none exist (and update the
1307/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1308/// methods is the "right" thing to do.
1309void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1310 ObjCContainerDecl *CD) {
1311 ObjCMethodDecl *GetterMethod, *SetterMethod;
1312
Douglas Gregor6ab35242009-04-09 21:40:53 +00001313 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1314 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Steve Naroff0701bbb2009-01-08 17:28:14 +00001315
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001316 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001317 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001318 Diag(property->getLocation(),
1319 diag::err_accessor_property_type_mismatch)
1320 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001321 << GetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001322 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1323 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001324
1325 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001326 if (Context.getCanonicalType(SetterMethod->getResultType())
1327 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001328 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001329 if (SetterMethod->param_size() != 1 ||
1330 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001331 Diag(property->getLocation(),
1332 diag::err_accessor_property_type_mismatch)
1333 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001334 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001335 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1336 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001337 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001338
1339 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001340 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001341 // FIXME: The synthesized property we set here is misleading. We
1342 // almost always synthesize these methods unless the user explicitly
1343 // provided prototypes (which is odd, but allowed). Sema should be
1344 // typechecking that the declarations jive in that situation (which
1345 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001346 if (!GetterMethod) {
1347 // No instance method of same name as property getter name was found.
1348 // Declare a getter method and add it to the list of methods
1349 // for this class.
1350 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1351 property->getLocation(), property->getGetterName(),
1352 property->getType(), CD, true, false, true,
1353 (property->getPropertyImplementation() ==
1354 ObjCPropertyDecl::Optional) ?
1355 ObjCMethodDecl::Optional :
1356 ObjCMethodDecl::Required);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001357 CD->addDecl(Context, GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001358 } else
1359 // A user declared getter will be synthesize when @synthesize of
1360 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001361 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001362 property->setGetterMethodDecl(GetterMethod);
1363
1364 // Skip setter if property is read-only.
1365 if (!property->isReadOnly()) {
1366 // Find the default setter and if one not found, add one.
1367 if (!SetterMethod) {
1368 // No instance method of same name as property setter name was found.
1369 // Declare a setter method and add it to the list of methods
1370 // for this class.
1371 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1372 property->getLocation(),
1373 property->getSetterName(),
1374 Context.VoidTy, CD, true, false, true,
1375 (property->getPropertyImplementation() ==
1376 ObjCPropertyDecl::Optional) ?
1377 ObjCMethodDecl::Optional :
1378 ObjCMethodDecl::Required);
1379 // Invent the arguments for the setter. We don't bother making a
1380 // nice name for the argument.
1381 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001382 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001383 property->getIdentifier(),
1384 property->getType(),
1385 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001386 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001387 SetterMethod->setMethodParams(Context, &Argument, 1);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001388 CD->addDecl(Context, SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001389 } else
1390 // A user declared setter will be synthesize when @synthesize of
1391 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001392 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001393 property->setSetterMethodDecl(SetterMethod);
1394 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001395 // Add any synthesized methods to the global pool. This allows us to
1396 // handle the following, which is supported by GCC (and part of the design).
1397 //
1398 // @interface Foo
1399 // @property double bar;
1400 // @end
1401 //
1402 // void thisIsUnfortunate() {
1403 // id foo;
1404 // double bar = [foo bar];
1405 // }
1406 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001407 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001408 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001409 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001410 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001411}
1412
Steve Naroffa56f6162007-12-18 01:30:32 +00001413// Note: For class/category implemenations, allMethods/allProperties is
1414// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001415void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1416 DeclPtrTy *allMethods, unsigned allNum,
1417 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001418 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001419 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001420
Steve Naroffa56f6162007-12-18 01:30:32 +00001421 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1422 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001423 // should be true.
1424 if (!ClassDecl)
1425 return;
1426
Chris Lattner4d391482007-12-12 07:09:47 +00001427 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001428 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1429 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001430 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001431
Steve Naroff0701bbb2009-01-08 17:28:14 +00001432 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001433
1434 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1435 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1436 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1437
Chris Lattner4d391482007-12-12 07:09:47 +00001438 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001439 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001440 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001441
1442 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001443 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001444 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001445 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001446 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1447 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001448 if ((isInterfaceDeclKind && PrevMethod && !match)
1449 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001450 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001451 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001452 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001453 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001454 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001455 InsMap[Method->getSelector()] = Method;
1456 /// The following allows us to typecheck messages to "id".
1457 AddInstanceMethodToGlobalPool(Method);
1458 }
1459 }
1460 else {
1461 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001462 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001463 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1464 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001465 if ((isInterfaceDeclKind && PrevMethod && !match)
1466 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001467 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001468 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001469 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001470 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001471 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001472 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001473 /// The following allows us to typecheck messages to "Class".
1474 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001475 }
1476 }
1477 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001478 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001479 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001480 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001481 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001482 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001483 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001484 // Categories are used to extend the class by declaring new methods.
1485 // By the same token, they are also used to add new properties. No
1486 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001487
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001488 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001489 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001490 if (C->getIdentifier() == 0)
1491 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001492 }
Steve Naroff09c47192009-01-09 15:36:25 +00001493 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1494 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1495 // user-defined setter/getter. It also synthesizes setter/getter methods
1496 // and adds them to the DeclContext and global method pools.
Douglas Gregor6ab35242009-04-09 21:40:53 +00001497 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1498 E = CDecl->prop_end(Context);
1499 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001500 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001501 CDecl->setAtEndLoc(AtEndLoc);
1502 }
1503 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001504 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001505 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001506 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001507 } else if (ObjCCategoryImplDecl* CatImplClass =
1508 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001509 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001510
Chris Lattner4d391482007-12-12 07:09:47 +00001511 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001512 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001513 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001514 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001515 Categories; Categories = Categories->getNextClassCategory()) {
1516 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001517 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001518 break;
1519 }
1520 }
1521 }
1522 }
Chris Lattner682bf922009-03-29 16:50:03 +00001523 if (isInterfaceDeclKind) {
1524 // Reject invalid vardecls.
1525 for (unsigned i = 0; i != tuvNum; i++) {
1526 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1527 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1528 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001529 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001530 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001531 }
Chris Lattner682bf922009-03-29 16:50:03 +00001532 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001533 }
Chris Lattner4d391482007-12-12 07:09:47 +00001534}
1535
1536
1537/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1538/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001539static Decl::ObjCDeclQualifier
1540CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1541 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1542 if (PQTVal & ObjCDeclSpec::DQ_In)
1543 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1544 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1545 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1546 if (PQTVal & ObjCDeclSpec::DQ_Out)
1547 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1548 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1549 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1550 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1551 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1552 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1553 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001554
1555 return ret;
1556}
1557
Chris Lattnerb28317a2009-03-28 19:18:32 +00001558Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001559 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001560 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001561 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001562 Selector Sel,
1563 // optional arguments. The number of types/arguments is obtained
1564 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001565 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001566 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001567 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1568 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001569 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001570
1571 // Make sure we can establish a context for the method.
1572 if (!ClassDecl) {
1573 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001574 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001575 }
Chris Lattner4d391482007-12-12 07:09:47 +00001576 QualType resultDeclType;
1577
Steve Naroffccef3712009-02-20 22:59:16 +00001578 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001579 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001580
1581 // Methods cannot return interface types. All ObjC objects are
1582 // passed by reference.
1583 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001584 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1585 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001586 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001587 }
1588 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001589 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001590
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001591 ObjCMethodDecl* ObjCMethod =
1592 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner32d3f9c2009-03-29 04:30:19 +00001593 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001594 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001595 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001596 MethodDeclKind == tok::objc_optional ?
1597 ObjCMethodDecl::Optional :
1598 ObjCMethodDecl::Required);
1599
Chris Lattner0ed844b2008-04-04 06:12:32 +00001600 llvm::SmallVector<ParmVarDecl*, 16> Params;
1601
Chris Lattner7db638d2009-04-11 19:42:43 +00001602 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001603 QualType ArgType, UnpromotedArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001604
Chris Lattnere294d3f2009-04-11 18:57:04 +00001605 if (ArgInfo[i].Type == 0) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001606 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001607 } else {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001608 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff6082c622008-12-09 19:36:17 +00001609 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001610 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001611 }
1612
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001613 ParmVarDecl* Param;
Chris Lattner2dd979f2009-04-11 19:08:56 +00001614 if (ArgType == UnpromotedArgType)
Chris Lattner7db638d2009-04-11 19:42:43 +00001615 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001616 ArgInfo[i].Name, ArgType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001617 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001618 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001619 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner7db638d2009-04-11 19:42:43 +00001620 ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001621 ArgInfo[i].Name, ArgType,
1622 UnpromotedArgType,
Douglas Gregor64650af2009-02-02 23:39:07 +00001623 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001624
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001625 if (ArgType->isObjCInterfaceType()) {
1626 Diag(ArgInfo[i].NameLoc,
1627 diag::err_object_cannot_be_passed_returned_by_value)
1628 << 1 << ArgType;
1629 Param->setInvalidDecl();
1630 }
1631
Chris Lattner0ed844b2008-04-04 06:12:32 +00001632 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001633 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001634
1635 // Apply the attributes to the parameter.
1636 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1637
Chris Lattner0ed844b2008-04-04 06:12:32 +00001638 Params.push_back(Param);
1639 }
1640
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001641 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001642 ObjCMethod->setObjCDeclQualifier(
1643 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1644 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001645
1646 if (AttrList)
1647 ProcessDeclAttributeList(ObjCMethod, AttrList);
Ted Kremenekb27d1172009-04-30 18:41:06 +00001648
Chris Lattner4d391482007-12-12 07:09:47 +00001649 // For implementations (which can be very "coarse grain"), we add the
1650 // method now. This allows the AST to implement lookup methods that work
1651 // incrementally (without waiting until we parse the @end). It also allows
1652 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001653 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001654 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001655 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001656 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel);
1657 ImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001658 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001659 PrevMethod = ImpDecl->getClassMethod(Context, Sel);
1660 ImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001661 }
1662 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001663 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001664 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001665 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001666 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel);
1667 CatImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001668 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001669 PrevMethod = CatImpDecl->getClassMethod(Context, Sel);
1670 CatImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001671 }
1672 }
1673 if (PrevMethod) {
1674 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001675 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001676 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001677 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001678 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001679 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001680}
1681
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001682void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1683 SourceLocation Loc,
1684 unsigned &Attributes) {
1685 // FIXME: Improve the reported location.
1686
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001687 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001688 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001689 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1690 ObjCDeclSpec::DQ_PR_assign |
1691 ObjCDeclSpec::DQ_PR_copy |
1692 ObjCDeclSpec::DQ_PR_retain))) {
1693 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1694 "readwrite" :
1695 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1696 "assign" :
1697 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1698 "copy" : "retain";
1699
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001700 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001701 diag::err_objc_property_attr_mutually_exclusive :
1702 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001703 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001704 }
1705
1706 // Check for copy or retain on non-object types.
1707 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1708 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001709 Diag(Loc, diag::err_objc_property_requires_object)
1710 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001711 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1712 }
1713
1714 // Check for more than one of { assign, copy, retain }.
1715 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1716 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001717 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1718 << "assign" << "copy";
1719 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001720 }
1721 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001722 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1723 << "assign" << "retain";
1724 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001725 }
1726 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1727 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001728 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1729 << "copy" << "retain";
1730 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001731 }
1732 }
1733
1734 // Warn if user supplied no assignment attribute, property is
1735 // readwrite, and this is an object type.
1736 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1737 ObjCDeclSpec::DQ_PR_retain)) &&
1738 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1739 Context.isObjCObjectPointerType(PropertyTy)) {
1740 // Skip this warning in gc-only mode.
1741 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1742 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1743
1744 // If non-gc code warn that this is likely inappropriate.
1745 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1746 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1747
1748 // FIXME: Implement warning dependent on NSCopying being
1749 // implemented. See also:
1750 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1751 // (please trim this list while you are at it).
1752 }
Mike Stump046efd92009-05-07 23:06:50 +00001753
1754 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1755 && getLangOptions().getGCMode() == LangOptions::GCOnly
1756 && PropertyTy->isBlockPointerType())
1757 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001758}
1759
Chris Lattnerb28317a2009-03-28 19:18:32 +00001760Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1761 FieldDeclarator &FD,
1762 ObjCDeclSpec &ODS,
1763 Selector GetterSel,
1764 Selector SetterSel,
1765 DeclPtrTy ClassCategory,
1766 bool *isOverridingProperty,
1767 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001768 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001769 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1770 // default is readwrite!
1771 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1772 // property is defaulted to 'assign' if it is readwrite and is
1773 // not retain or copy
1774 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1775 (isReadWrite &&
1776 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1777 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1778 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001779 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001780 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001781 // May modify Attributes.
1782 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001783 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1784 if (!CDecl->getIdentifier()) {
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001785 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001786 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001787 if ((CCPrimary = CDecl->getClassInterface())) {
1788 // Find the property in continuation class's primary class only.
1789 ObjCPropertyDecl *PIDecl = 0;
1790 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001791 for (ObjCInterfaceDecl::prop_iterator
1792 I = CCPrimary->prop_begin(Context),
1793 E = CCPrimary->prop_end(Context);
1794 I != E; ++I)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001795 if ((*I)->getIdentifier() == PropertyId) {
1796 PIDecl = *I;
1797 break;
1798 }
1799
1800 if (PIDecl) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001801 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001802 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001803 unsigned PIkind = PIDecl->getPropertyAttributes();
1804 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001805 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001806 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1807 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001808 PIDecl->makeitReadWriteAttribute();
1809 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1810 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1811 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1812 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1813 PIDecl->setSetterName(SetterSel);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001814 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001815 else
1816 Diag(AtLoc, diag::err_use_continuation_class)
1817 << CCPrimary->getDeclName();
1818 *isOverridingProperty = true;
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001819 // Make sure setter decl is synthesized, and added to primary
1820 // class's list.
1821 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001822 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001823 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001824 // No matching property found in the primary class. Just fall thru
1825 // and add property to continuation class's primary class.
1826 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001827 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001828 Diag(CDecl->getLocation(), diag::err_continuation_class);
1829 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001830 return DeclPtrTy();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001831 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001832 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001833
Steve Naroff93983f82009-01-11 12:47:58 +00001834 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1835 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001836 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1837 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001838 FD.D.getIdentifier(), T);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001839 DC->addDecl(Context, PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001840
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001841 if (T->isArrayType() || T->isFunctionType()) {
1842 Diag(AtLoc, diag::err_property_type) << T;
1843 PDecl->setInvalidDecl();
1844 }
1845
Chris Lattner97a58872009-02-16 18:32:47 +00001846 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001847
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001848 // Regardless of setter/getter attribute, we save the default getter/setter
1849 // selector names in anticipation of declaration of setter/getter methods.
1850 PDecl->setGetterName(GetterSel);
1851 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001852
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001853 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001854 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001855
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001856 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001857 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001858
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001859 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001860 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001861
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001862 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001863 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001864
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001865 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001866 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001867
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001868 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001869 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001870
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001871 if (isAssign)
1872 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1873
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001874 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001875 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001876
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001877 if (MethodImplKind == tok::objc_required)
1878 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1879 else if (MethodImplKind == tok::objc_optional)
1880 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001881 // A case of continuation class adding a new property in the class. This
1882 // is not what it was meant for. However, gcc supports it and so should we.
1883 // Make sure setter/getters are declared here.
1884 if (CCPrimary)
1885 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001886
Chris Lattnerb28317a2009-03-28 19:18:32 +00001887 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001888}
1889
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001890/// ActOnPropertyImplDecl - This routine performs semantic checks and
1891/// builds the AST node for a property implementation declaration; declared
1892/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001893///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001894Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1895 SourceLocation PropertyLoc,
1896 bool Synthesize,
1897 DeclPtrTy ClassCatImpDecl,
1898 IdentifierInfo *PropertyId,
1899 IdentifierInfo *PropertyIvar) {
1900 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001901 // Make sure we have a context for the property implementation declaration.
1902 if (!ClassImpDecl) {
1903 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001904 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001905 }
1906 ObjCPropertyDecl *property = 0;
1907 ObjCInterfaceDecl* IDecl = 0;
1908 // Find the class or category class where this property must have
1909 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001910 ObjCImplementationDecl *IC = 0;
1911 ObjCCategoryImplDecl* CatImplClass = 0;
1912 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001913 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001914 // We always synthesize an interface for an implementation
1915 // without an interface decl. So, IDecl is always non-zero.
1916 assert(IDecl &&
1917 "ActOnPropertyImplDecl - @implementation without @interface");
1918
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001919 // Look for this property declaration in the @implementation's @interface
Douglas Gregor6ab35242009-04-09 21:40:53 +00001920 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001921 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001922 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001923 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001924 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001925 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001926 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001927 if (Synthesize) {
1928 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001929 return DeclPtrTy();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001930 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001931 IDecl = CatImplClass->getClassInterface();
1932 if (!IDecl) {
1933 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001934 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001935 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001936 ObjCCategoryDecl *Category =
1937 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1938
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001939 // If category for this implementation not found, it is an error which
1940 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001941 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00001942 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001943 // Look for this property declaration in @implementation's category
Douglas Gregor6ab35242009-04-09 21:40:53 +00001944 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001945 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001946 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001947 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001948 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001949 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001950 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001951 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001952 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001953 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001954 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001955 // Check that we have a valid, previously declared ivar for @synthesize
1956 if (Synthesize) {
1957 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001958 if (!PropertyIvar)
1959 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00001960 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001961 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001962 ObjCInterfaceDecl *ClassDeclared;
1963 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001964 if (!Ivar) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001965 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1966 PropertyIvar, PropType,
1967 ObjCIvarDecl::Public,
1968 (Expr *)0);
1969 property->setPropertyIvarDecl(Ivar);
1970 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00001971 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001972 // Note! I deliberately want it to fall thru so, we have a
1973 // a property implementation and to avoid future warnings.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001974 }
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001975 else if (getLangOptions().ObjCNonFragileABI &&
Fariborz Jahanian6e5201b2009-04-29 21:45:02 +00001976 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00001977 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001978 << property->getDeclName() << Ivar->getDeclName()
1979 << ClassDeclared->getDeclName();
1980 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1981 << Ivar << Ivar->getNameAsCString();
1982 // Note! I deliberately want it to fall thru so more errors are caught.
1983 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001984 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1985
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001986 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001987 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001988 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001989 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001990 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001991 // Note! I deliberately want it to fall thru so, we have a
1992 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001993 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001994
1995 // FIXME! Rules for properties are somewhat different that those
1996 // for assignments. Use a new routine to consolidate all cases;
1997 // specifically for property redeclarations as well as for ivars.
1998 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1999 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
2000 if (lhsType != rhsType &&
2001 lhsType->isArithmeticType()) {
2002 Diag(PropertyLoc, diag::error_property_ivar_type)
2003 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002004 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002005 }
2006 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002007 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2008 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002009 Diag(PropertyLoc, diag::error_weak_property)
2010 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002011 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002012 }
2013 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002014 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2015 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002016 Diag(PropertyLoc, diag::error_strong_property)
2017 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002018 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002019 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002020 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002021 } else if (PropertyIvar)
2022 // @dynamic
2023 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002024 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002025 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00002026 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2027 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002028 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002029 ObjCPropertyImplDecl::Synthesize
2030 : ObjCPropertyImplDecl::Dynamic),
2031 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002032 if (IC) {
2033 if (Synthesize)
2034 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00002035 IC->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002036 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2037 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2038 << PropertyIvar;
2039 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2040 }
2041
Douglas Gregor653f1b12009-04-23 01:02:12 +00002042 if (ObjCPropertyImplDecl *PPIDecl
2043 = IC->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002044 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2045 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002046 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002047 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00002048 IC->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002049 }
2050 else {
2051 if (Synthesize)
2052 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00002053 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002054 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2055 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2056 << PropertyIvar;
2057 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2058 }
2059
2060 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00002061 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002062 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2063 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002064 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002065 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00002066 CatImplClass->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002067 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002068
Chris Lattnerb28317a2009-03-28 19:18:32 +00002069 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002070}
Anders Carlsson15281452008-11-04 16:57:32 +00002071
Chris Lattnercc98eac2008-12-17 07:13:27 +00002072bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002073 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002074 return false;
2075
2076 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2077 D->setInvalidDecl();
2078
2079 return true;
2080}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002081
2082/// Collect the instance variables declared in an Objective-C object. Used in
2083/// the creation of structures from objects using the @defs directive.
2084/// FIXME: This should be consolidated with CollectObjCIvars as it is also
2085/// part of the AST generation logic of @defs.
2086static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
2087 ASTContext& Ctx,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002088 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002089 if (Class->getSuperClass())
2090 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
2091
2092 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002093 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
2094 E = Class->ivar_end(); I != E; ++I) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002095 ObjCIvarDecl* ID = *I;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002096 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
2097 ID->getIdentifier(), ID->getType(),
2098 ID->getBitWidth());
2099 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattnercc98eac2008-12-17 07:13:27 +00002100 }
2101}
2102
2103/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2104/// instance variables of ClassName into Decls.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002105void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002106 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002107 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002108 // Check that ClassName is a valid class
2109 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2110 if (!Class) {
2111 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2112 return;
2113 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002114 if (LangOpts.ObjCNonFragileABI) {
2115 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2116 return;
2117 }
2118
Chris Lattnercc98eac2008-12-17 07:13:27 +00002119 // Collect the instance variables
Chris Lattnerb28317a2009-03-28 19:18:32 +00002120 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002121
2122 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002123 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002124 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002125 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002126 if (getLangOptions().CPlusPlus)
2127 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002128 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregor6ab35242009-04-09 21:40:53 +00002129 Record->addDecl(Context, FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002130 }
2131}
2132