blob: f4014d1f25d50a30c44852605b28fe3e8c6da717 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Steve Naroffca331292009-03-03 14:49:36 +000015#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000019using namespace clang;
20
Steve Naroffebf64432009-02-28 16:59:13 +000021/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000023void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000025 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff394f3f42008-07-25 17:57:26 +000026
27 // If we don't have a valid method decl, simply return.
28 if (!MDecl)
29 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000030
Chris Lattner38c5ebd2009-04-19 05:21:20 +000031 CurFunctionNeedsScopeChecking = false;
32
Steve Naroffa56f6162007-12-18 01:30:32 +000033 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000034 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000035 AddInstanceMethodToGlobalPool(MDecl);
36 else
37 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000038
39 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000040 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000041
42 // Create Decl objects for each parameter, entrring them in the scope for
43 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000044
45 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000046 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000047
Daniel Dunbar451318c2008-08-26 06:07:48 +000048 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
49 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000050
Chris Lattner8123a952008-04-10 02:22:51 +000051 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000052 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
53 E = MDecl->param_end(); PI != E; ++PI)
54 if ((*PI)->getIdentifier())
55 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000056}
57
Chris Lattnerb28317a2009-03-28 19:18:32 +000058Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000059ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
60 IdentifierInfo *ClassName, SourceLocation ClassLoc,
61 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000062 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000063 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000064 assert(ClassName && "Missing class identifier");
65
66 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000067 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000068 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000069 // Maybe we will complain about the shadowed template parameter.
70 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
71 // Just pretend that we didn't see the previous declaration.
72 PrevDecl = 0;
73 }
74
Ted Kremeneka526c5c2008-01-07 19:49:32 +000075 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000076 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000077 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000078 }
79
Ted Kremeneka526c5c2008-01-07 19:49:32 +000080 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000081 if (IDecl) {
82 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000083 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +000084 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000085 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000086 Diag(IDecl->getLocation(), diag::note_previous_definition);
87
Steve Naroffcfe8bf32008-11-18 19:15:30 +000088 // Return the previous class interface.
89 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +000090 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +000091 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000092 IDecl->setLocation(AtInterfaceLoc);
93 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000094 }
Chris Lattnerb752f282008-07-21 07:06:49 +000095 } else {
Douglas Gregord0434102009-01-09 00:49:46 +000096 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000097 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000098 if (AttrList)
99 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000100
Steve Naroffa7503a72009-04-23 15:15:40 +0000101 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000102 }
103
104 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000105 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000106 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000107
Steve Naroff818cb9e2009-02-04 17:14:05 +0000108 ObjCInterfaceDecl *SuperClassDecl =
109 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000110
111 // Diagnose classes that inherit from deprecated classes.
112 if (SuperClassDecl)
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000113 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000114
Steve Naroff818cb9e2009-02-04 17:14:05 +0000115 if (PrevDecl && SuperClassDecl == 0) {
116 // The previous declaration was not a class decl. Check if we have a
117 // typedef. If we do, get the underlying class type.
118 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
119 QualType T = TDecl->getUnderlyingType();
120 if (T->isObjCInterfaceType()) {
121 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
122 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
123 }
124 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000125
Steve Naroff818cb9e2009-02-04 17:14:05 +0000126 // This handles the following case:
127 //
128 // typedef int SuperClass;
129 // @interface MyClass : SuperClass {} @end
130 //
131 if (!SuperClassDecl) {
132 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
133 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
134 }
135 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000136
Steve Naroff818cb9e2009-02-04 17:14:05 +0000137 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
138 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000139 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000140 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000141 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000142 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000143 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000144 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000145 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000146 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000147 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000148 IDecl->setLocEnd(SuperLoc);
149 } else { // we have a root class.
150 IDecl->setLocEnd(ClassLoc);
151 }
152
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000153 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000154 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000155 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
156 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000157 IDecl->setLocEnd(EndProtoLoc);
158 }
Anders Carlsson15281452008-11-04 16:57:32 +0000159
160 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000161 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000162}
163
164/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000165/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000166Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
167 IdentifierInfo *AliasName,
168 SourceLocation AliasLocation,
169 IdentifierInfo *ClassName,
170 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000171 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000172 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000173 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000174 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000175 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000176 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000177 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000178 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000179 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000180 }
181 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000182 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000183 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
184 QualType T = TDecl->getUnderlyingType();
185 if (T->isObjCInterfaceType()) {
186 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
187 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000188 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000189 }
190 }
191 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000192 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
193 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000194 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000195 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000196 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000197 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000198 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000199
200 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000201 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000202 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000203
Anders Carlsson15281452008-11-04 16:57:32 +0000204 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000205 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000206
Chris Lattnerb28317a2009-03-28 19:18:32 +0000207 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000208}
209
Steve Naroff61d68522009-03-05 15:22:01 +0000210void Sema::CheckForwardProtocolDeclarationForCircularDependency(
211 IdentifierInfo *PName,
212 SourceLocation &Ploc, SourceLocation PrevLoc,
213 const ObjCList<ObjCProtocolDecl> &PList)
214{
215 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
216 E = PList.end(); I != E; ++I) {
217
Douglas Gregor6e378de2009-04-23 23:18:26 +0000218 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000219 if (PDecl->getIdentifier() == PName) {
220 Diag(Ploc, diag::err_protocol_has_circular_dependency);
221 Diag(PrevLoc, diag::note_previous_definition);
222 }
223 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
224 PDecl->getLocation(), PDecl->getReferencedProtocols());
225 }
226 }
227}
228
Chris Lattnerb28317a2009-03-28 19:18:32 +0000229Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000230Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
231 IdentifierInfo *ProtocolName,
232 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000233 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000234 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000235 SourceLocation EndProtoLoc,
236 AttributeList *AttrList) {
237 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000238 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000239 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000240 if (PDecl) {
241 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000242 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000243 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000244 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000245 // Just return the protocol we already had.
246 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000247 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000248 }
Steve Naroff61d68522009-03-05 15:22:01 +0000249 ObjCList<ObjCProtocolDecl> PList;
250 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
251 CheckForwardProtocolDeclarationForCircularDependency(
252 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
253 PList.Destroy(Context);
254
Steve Narofff11b5082008-08-13 16:39:22 +0000255 // Make sure the cached decl gets a valid start location.
256 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000257 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000258 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000259 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
260 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000261 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000262 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000263 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000264 if (AttrList)
265 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000266 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000267 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000268 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000269 PDecl->setLocEnd(EndProtoLoc);
270 }
Anders Carlsson15281452008-11-04 16:57:32 +0000271
272 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000273 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000274}
275
276/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000277/// issues an error if they are not declared. It returns list of
278/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000279void
Chris Lattnere13b9592008-07-26 04:03:38 +0000280Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000281 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000282 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000283 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000284 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000285 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000286 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000287 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000288 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000289 continue;
290 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000291
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000292 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000293
294 // If this is a forward declaration and we are supposed to warn in this
295 // case, do it.
296 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000297 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000298 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000299 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000300 }
301}
302
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000303/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000304/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000305///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000306void
307Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
308 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000309 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000310 ObjCPropertyDecl::PropertyAttributeKind CAttr =
311 Property->getPropertyAttributes();
312 ObjCPropertyDecl::PropertyAttributeKind SAttr =
313 SuperProperty->getPropertyAttributes();
314 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
315 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000316 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000317 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000318 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
319 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000320 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000321 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000322 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
323 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000324 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000325 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000326
327 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
328 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000329 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000330 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000331 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000332 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000333 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000334 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000335 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000336 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000337
338 QualType LHSType =
339 Context.getCanonicalType(SuperProperty->getType());
340 QualType RHSType =
341 Context.getCanonicalType(Property->getType());
342
343 if (!Context.typesAreCompatible(LHSType, RHSType)) {
344 // FIXME: Incorporate this test with typesAreCompatible.
345 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
346 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
347 return;
348 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
349 << Property->getType() << SuperProperty->getType() << inheritedName;
350 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000351}
352
353/// ComparePropertiesInBaseAndSuper - This routine compares property
354/// declarations in base and its super class, if any, and issues
355/// diagnostics in a variety of inconsistant situations.
356///
Chris Lattner70f19542009-02-16 21:26:43 +0000357void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000358 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
359 if (!SDecl)
360 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000361 // FIXME: O(N^2)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000362 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
363 E = SDecl->prop_end(Context); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000364 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000365 // Does property in super class has declaration in current class?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000366 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
367 E = IDecl->prop_end(Context); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000368 ObjCPropertyDecl *PDecl = (*I);
369 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000370 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000371 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000372 }
373 }
374}
375
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000376/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
377/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000378/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000379void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000380Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000381 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000382 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
383 if (!IDecl) {
384 // Category
385 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
386 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Douglas Gregor6ab35242009-04-09 21:40:53 +0000387 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
388 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000389 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000390 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000391 // Is this property already in category's list of properties?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000392 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000393 CP != CE; ++CP)
394 if ((*CP)->getIdentifier() == Pr->getIdentifier())
395 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000396 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000397 // Property protocol already exist in class. Diagnose any mismatch.
398 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
399 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000400 return;
401 }
Douglas Gregor6ab35242009-04-09 21:40:53 +0000402 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
403 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000404 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000405 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000406 // Is this property already in class's list of properties?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000407 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000408 CP != CE; ++CP)
409 if ((*CP)->getIdentifier() == Pr->getIdentifier())
410 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000411 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000412 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000413 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000414 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000415}
416
417/// MergeProtocolPropertiesIntoClass - This routine merges properties
418/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000419/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000420///
Chris Lattner70f19542009-02-16 21:26:43 +0000421void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000422 DeclPtrTy MergeItsProtocols) {
423 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000424 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
425
426 if (!IDecl) {
427 // Category
428 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
429 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
430 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
431 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
432 E = MDecl->protocol_end(); P != E; ++P)
433 // Merge properties of category (*P) into IDECL's
434 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
435
436 // Go thru the list of protocols for this category and recursively merge
437 // their properties into this class as well.
438 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
439 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000440 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000441 } else {
442 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
443 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
444 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000445 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000446 }
447 return;
448 }
449
Chris Lattnerb752f282008-07-21 07:06:49 +0000450 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000451 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
452 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000453 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000454 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
455
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000456 // Go thru the list of protocols for this class and recursively merge
457 // their properties into this class as well.
458 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
459 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000460 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000461 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000462 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
463 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
464 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000465 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000466 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000467}
468
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000469/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000470/// a class method in its extension.
471///
472void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
473 ObjCInterfaceDecl *ID) {
474 if (!ID)
475 return; // Possibly due to previous error
476
477 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000478 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
479 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000480 ObjCMethodDecl *MD = *i;
481 MethodMap[MD->getSelector()] = MD;
482 }
483
484 if (MethodMap.empty())
485 return;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000486 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
487 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000488 ObjCMethodDecl *Method = *i;
489 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
490 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
491 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
492 << Method->getDeclName();
493 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
494 }
495 }
496}
497
Chris Lattner58fe03b2009-04-12 08:43:13 +0000498/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000499Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000500Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000501 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000502 unsigned NumElts,
503 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000504 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000505
506 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000507 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000508 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000509 if (PDecl == 0) { // Not already seen?
510 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
511 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000512 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000513 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000514 if (attrList)
515 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000516 Protocols.push_back(PDecl);
517 }
Anders Carlsson15281452008-11-04 16:57:32 +0000518
519 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000520 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000521 &Protocols[0], Protocols.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000522 CurContext->addDecl(Context, PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000523 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000524 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000525}
526
Chris Lattnerb28317a2009-03-28 19:18:32 +0000527Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000528ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
529 IdentifierInfo *ClassName, SourceLocation ClassLoc,
530 IdentifierInfo *CategoryName,
531 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000532 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000533 unsigned NumProtoRefs,
534 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000535 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000536 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
537 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000538 CurContext->addDecl(Context, CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000539
540 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000541 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000542 if (!IDecl || IDecl->isForwardDecl()) {
543 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000544 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000545 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000546 }
Chris Lattner4d391482007-12-12 07:09:47 +0000547
Chris Lattner70f19542009-02-16 21:26:43 +0000548 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000549
550 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000551 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000552
553 /// Check for duplicate interface declaration for this category
554 ObjCCategoryDecl *CDeclChain;
555 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
556 CDeclChain = CDeclChain->getNextClassCategory()) {
557 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
558 Diag(CategoryLoc, diag::warn_dup_category_def)
559 << ClassName << CategoryName;
560 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
561 break;
562 }
563 }
564 if (!CDeclChain)
565 CDecl->insertNextClassCategory();
566
Chris Lattner4d391482007-12-12 07:09:47 +0000567 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000568 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000569 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000570 }
Anders Carlsson15281452008-11-04 16:57:32 +0000571
572 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000573 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000574}
575
576/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000577/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000578/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000579Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000580 SourceLocation AtCatImplLoc,
581 IdentifierInfo *ClassName, SourceLocation ClassLoc,
582 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000583 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000584 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000585 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
586 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000587 /// Check that class of this category is already completely declared.
588 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000589 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000590
Douglas Gregord0434102009-01-09 00:49:46 +0000591 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000592 CurContext->addDecl(Context, CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000593
Chris Lattner4d391482007-12-12 07:09:47 +0000594 /// TODO: Check that CatName, category name, is not used in another
595 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000596 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000597
598 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000599 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000600}
601
Chris Lattnerb28317a2009-03-28 19:18:32 +0000602Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000603 SourceLocation AtClassImplLoc,
604 IdentifierInfo *ClassName, SourceLocation ClassLoc,
605 IdentifierInfo *SuperClassname,
606 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000607 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000608 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000609 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000610 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000611 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000612 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000613 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000614 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000615 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000616 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000617 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000618 IDecl = 0;
619 }
Chris Lattner4d391482007-12-12 07:09:47 +0000620 }
621
622 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000623 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000624 if (SuperClassname) {
625 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000626 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000627 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000628 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
629 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000630 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000631 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000632 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000633 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000634 Diag(SuperClassLoc, diag::err_undef_superclass)
635 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000636 else if (IDecl && IDecl->getSuperClass() != SDecl) {
637 // This implementation and its interface do not have the same
638 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000639 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000640 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000641 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000642 }
643 }
644 }
645
646 if (!IDecl) {
647 // Legacy case of @implementation with no corresponding @interface.
648 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000649
650 // FIXME: Do we support attributes on the @implementation? If so
651 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000652 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
653 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000654 IDecl->setSuperClass(SDecl);
655 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000656
657 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000658 } else {
659 // Mark the interface as being completed, even if it was just as
660 // @class ....;
661 // declaration; the user cannot reopen it.
662 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000663 }
664
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000665 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000666 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000667 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000668
Anders Carlsson15281452008-11-04 16:57:32 +0000669 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000670 return DeclPtrTy::make(IMPDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000671
Chris Lattner4d391482007-12-12 07:09:47 +0000672 // Check that there is no duplicate implementation of this class.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000673 if (LookupObjCImplementation(ClassName))
Chris Lattner75c9cae2008-03-16 20:53:07 +0000674 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000675 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000676 else // add it to the list.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000677 PushOnScopeChains(IMPDecl, TUScope);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000678 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000679}
680
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000681void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
682 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000683 SourceLocation RBrace) {
684 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000685 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000686 if (!IDecl)
687 return;
688 /// Check case of non-existing @interface decl.
689 /// (legacy objective-c @implementation decl without an @interface decl).
690 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000691 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000692 IDecl->setIVarList(ivars, numIvars, Context);
693 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000694 return;
695 }
696 // If implementation has empty ivar list, just return.
697 if (numIvars == 0)
698 return;
699
700 assert(ivars && "missing @implementation ivars");
701
702 // Check interface's Ivar list against those in the implementation.
703 // names and types must match.
704 //
Chris Lattner4d391482007-12-12 07:09:47 +0000705 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000706 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000707 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
708 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000709 ObjCIvarDecl* ImplIvar = ivars[j++];
710 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000711 assert (ImplIvar && "missing implementation ivar");
712 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000713
714 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000715 if (Context.getCanonicalType(ImplIvar->getType()) !=
716 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000717 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000718 << ImplIvar->getIdentifier()
719 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000720 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000721 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
722 Expr *ImplBitWidth = ImplIvar->getBitWidth();
723 Expr *ClsBitWidth = ClsIvar->getBitWidth();
724 if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() !=
725 ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) {
726 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
727 << ImplIvar->getIdentifier();
728 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
729 }
730 }
731 // Make sure the names are identical.
732 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000733 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000734 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000735 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000736 }
737 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000738 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000739
740 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000741 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000742 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000743 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000744}
745
Steve Naroff3c2eb662008-02-10 21:38:56 +0000746void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
747 bool &IncompleteImpl) {
748 if (!IncompleteImpl) {
749 Diag(ImpLoc, diag::warn_incomplete_impl);
750 IncompleteImpl = true;
751 }
Chris Lattner08631c52008-11-23 21:45:46 +0000752 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000753}
754
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000755void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
756 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000757 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Chris Lattner3aff9192009-04-11 19:58:42 +0000758 ImpMethodDecl->getResultType())) {
759 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
760 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
761 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000762 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
763 }
Chris Lattner3aff9192009-04-11 19:58:42 +0000764
765 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
766 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
767 IM != EM; ++IM, ++IF) {
768 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()))
769 continue;
770
771 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
772 << ImpMethodDecl->getDeclName() << (*IF)->getType()
773 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000774 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000775 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000776}
777
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000778/// isPropertyReadonly - Return true if property is readonly, by searching
779/// for the property in the class and in its categories and implementations
780///
781bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000782 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000783 // by far the most common case.
784 if (!PDecl->isReadOnly())
785 return false;
786 // Even if property is ready only, if interface has a user defined setter,
787 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000788 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000789 return false;
790
791 // Main class has the property as 'readonly'. Must search
792 // through the category list to see if the property's
793 // attribute has been over-ridden to 'readwrite'.
794 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
795 Category; Category = Category->getNextClassCategory()) {
796 // Even if property is ready only, if a category has a user defined setter,
797 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000798 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000799 return false;
800 ObjCPropertyDecl *P =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000801 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000802 if (P && !P->isReadOnly())
803 return false;
804 }
805
806 // Also, check for definition of a setter method in the implementation if
807 // all else failed.
808 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
809 if (ObjCImplementationDecl *IMD =
810 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000811 if (IMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000812 return false;
813 }
814 else if (ObjCCategoryImplDecl *CIMD =
815 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000816 if (CIMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000817 return false;
818 }
819 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000820 // Lastly, look through the implementation (if one is in scope).
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000821 if (ObjCImplementationDecl *ImpDecl
822 = LookupObjCImplementation(IDecl->getIdentifier()))
Douglas Gregor653f1b12009-04-23 01:02:12 +0000823 if (ImpDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000824 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000825 // If all fails, look at the super class.
826 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
827 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000828 return true;
829}
830
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000831/// FIXME: Type hierarchies in Objective-C can be deep. We could most
832/// likely improve the efficiency of selector lookups and type
833/// checking by associating with each protocol / interface / category
834/// the flattened instance tables. If we used an immutable set to keep
835/// the table then it wouldn't add significant memory cost and it
836/// would be handy for lookups.
837
Steve Naroffefe7f362008-02-08 22:06:17 +0000838/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000839/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000840void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
841 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000842 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000843 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000844 const llvm::DenseSet<Selector> &ClsMap,
845 ObjCInterfaceDecl *IDecl) {
846 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
847
848 // If a method lookup fails locally we still need to look and see if
849 // the method was implemented by a base class or an inherited
850 // protocol. This lookup is slow, but occurs rarely in correct code
851 // and otherwise would terminate in a warning.
852
Chris Lattner4d391482007-12-12 07:09:47 +0000853 // check unimplemented instance methods.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000854 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
855 E = PDecl->instmeth_end(Context); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000856 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000857 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000858 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000859 (!Super ||
860 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000861 // Ugly, but necessary. Method declared in protcol might have
862 // have been synthesized due to a property declared in the class which
863 // uses the protocol.
864 ObjCMethodDecl *MethodInClass =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000865 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000866 if (!MethodInClass || !MethodInClass->isSynthesized())
867 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
868 }
Chris Lattner4d391482007-12-12 07:09:47 +0000869 }
870 // check unimplemented class methods
Douglas Gregor6ab35242009-04-09 21:40:53 +0000871 for (ObjCProtocolDecl::classmeth_iterator
872 I = PDecl->classmeth_begin(Context),
873 E = PDecl->classmeth_end(Context);
874 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000875 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000876 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
877 !ClsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000878 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000879 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000880 }
Chris Lattner780f3292008-07-21 21:32:27 +0000881 // Check on this protocols's referenced protocols, recursively.
882 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
883 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000884 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000885}
886
Chris Lattnercddc8882009-03-01 00:56:52 +0000887void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
888 ObjCContainerDecl* CDecl,
889 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000890 llvm::DenseSet<Selector> InsMap;
891 // Check and see if instance methods in class interface have been
892 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000893 for (ObjCImplementationDecl::instmeth_iterator
894 I = IMPDecl->instmeth_begin(Context),
895 E = IMPDecl->instmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000896 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000897
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000898 // Check and see if properties declared in the interface have either 1)
899 // an implementation or 2) there is a @synthesize/@dynamic implementation
900 // of the property in the @implementation.
901 if (isa<ObjCInterfaceDecl>(CDecl))
902 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context),
903 E = CDecl->prop_end(Context); P != E; ++P) {
904 ObjCPropertyDecl *Prop = (*P);
905 if (Prop->isInvalidDecl())
906 continue;
907 ObjCPropertyImplDecl *PI = 0;
908 // Is there a matching propery synthesize/dynamic?
Douglas Gregor653f1b12009-04-23 01:02:12 +0000909 for (ObjCImplDecl::propimpl_iterator
910 I = IMPDecl->propimpl_begin(Context),
911 EI = IMPDecl->propimpl_end(Context); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000912 if ((*I)->getPropertyDecl() == Prop) {
913 PI = (*I);
914 break;
915 }
916 if (PI)
917 continue;
918 if (!InsMap.count(Prop->getGetterName())) {
919 Diag(Prop->getLocation(),
920 diag::warn_setter_getter_impl_required)
921 << Prop->getDeclName() << Prop->getGetterName();
922 Diag(IMPDecl->getLocation(),
923 diag::note_property_impl_required);
924 }
925
926 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
927 Diag(Prop->getLocation(),
928 diag::warn_setter_getter_impl_required)
929 << Prop->getDeclName() << Prop->getSetterName();
930 Diag(IMPDecl->getLocation(),
931 diag::note_property_impl_required);
932 }
933 }
934
Douglas Gregor6ab35242009-04-09 21:40:53 +0000935 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
936 E = CDecl->instmeth_end(Context); I != E; ++I) {
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000937 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000938 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000939 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000940 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000941
942 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +0000943 IMPDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000944 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000945 CDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000946 assert(IntfMethodDecl &&
947 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
948 // ImpMethodDecl may be null as in a @dynamic property.
949 if (ImpMethodDecl)
950 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
951 }
Chris Lattner4c525092007-12-12 17:58:05 +0000952
Chris Lattner4d391482007-12-12 07:09:47 +0000953 llvm::DenseSet<Selector> ClsMap;
954 // Check and see if class methods in class interface have been
955 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000956 for (ObjCImplementationDecl::classmeth_iterator
957 I = IMPDecl->classmeth_begin(Context),
958 E = IMPDecl->classmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000959 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000960
Douglas Gregor6ab35242009-04-09 21:40:53 +0000961 for (ObjCInterfaceDecl::classmeth_iterator
962 I = CDecl->classmeth_begin(Context),
963 E = CDecl->classmeth_end(Context);
964 I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000965 if (!ClsMap.count((*I)->getSelector()))
966 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000967 else {
968 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +0000969 IMPDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000970 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000971 CDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000972 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
973 }
974
Chris Lattner4d391482007-12-12 07:09:47 +0000975
976 // Check the protocol list for unimplemented methods in the @implementation
977 // class.
Chris Lattnercddc8882009-03-01 00:56:52 +0000978 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
979 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
980 E = I->protocol_end(); PI != E; ++PI)
981 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
982 InsMap, ClsMap, I);
983 // Check class extensions (unnamed categories)
984 for (ObjCCategoryDecl *Categories = I->getCategoryList();
985 Categories; Categories = Categories->getNextClassCategory()) {
986 if (!Categories->getIdentifier()) {
987 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
988 break;
989 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000990 }
Chris Lattnercddc8882009-03-01 00:56:52 +0000991 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
992 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
993 E = C->protocol_end(); PI != E; ++PI)
994 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
995 InsMap, ClsMap, C->getClassInterface());
996 } else
997 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +0000998}
999
1000/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001001Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001002Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001003 IdentifierInfo **IdentList,
1004 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001005 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +00001006
1007 for (unsigned i = 0; i != NumElts; ++i) {
1008 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001009 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001010 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001011 // Maybe we will complain about the shadowed template parameter.
1012 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1013 // Just pretend that we didn't see the previous declaration.
1014 PrevDecl = 0;
1015 }
1016
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001017 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001018 // GCC apparently allows the following idiom:
1019 //
1020 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1021 // @class XCElementToggler;
1022 //
1023 // FIXME: Make an extension?
1024 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1025 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001026 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001027 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +00001028 }
Chris Lattner4d391482007-12-12 07:09:47 +00001029 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001030 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001031 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +00001032 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1033 IdentList[i], SourceLocation(), true);
Steve Naroff8f06f842009-04-23 16:00:56 +00001034 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +00001035 }
1036
1037 Interfaces.push_back(IDecl);
1038 }
1039
Douglas Gregord0434102009-01-09 00:49:46 +00001040 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001041 &Interfaces[0],
1042 Interfaces.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +00001043 CurContext->addDecl(Context, CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001044 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001045 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001046}
1047
1048
1049/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1050/// returns true, or false, accordingly.
1051/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001052bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001053 const ObjCMethodDecl *PrevMethod,
1054 bool matchBasedOnSizeAndAlignment) {
1055 QualType T1 = Context.getCanonicalType(Method->getResultType());
1056 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1057
1058 if (T1 != T2) {
1059 // The result types are different.
1060 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001061 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001062 // Incomplete types don't have a size and alignment.
1063 if (T1->isIncompleteType() || T2->isIncompleteType())
1064 return false;
1065 // Check is based on size and alignment.
1066 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1067 return false;
1068 }
Chris Lattner89951a82009-02-20 18:43:26 +00001069
1070 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1071 E = Method->param_end();
1072 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1073
1074 for (; ParamI != E; ++ParamI, ++PrevI) {
1075 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1076 T1 = Context.getCanonicalType((*ParamI)->getType());
1077 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001078 if (T1 != T2) {
1079 // The result types are different.
1080 if (!matchBasedOnSizeAndAlignment)
1081 return false;
1082 // Incomplete types don't have a size and alignment.
1083 if (T1->isIncompleteType() || T2->isIncompleteType())
1084 return false;
1085 // Check is based on size and alignment.
1086 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1087 return false;
1088 }
Chris Lattner4d391482007-12-12 07:09:47 +00001089 }
1090 return true;
1091}
1092
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001093void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Chris Lattnerb25df352009-03-04 05:16:45 +00001094 ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()];
1095 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001096 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001097 Entry.Method = Method;
1098 Entry.Next = 0;
1099 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001100 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001101
1102 // We've seen a method with this name, see if we have already seen this type
1103 // signature.
1104 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1105 if (MatchTwoMethodDeclarations(Method, List->Method))
1106 return;
1107
1108 // We have a new signature for an existing method - add it.
1109 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1110 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001111}
1112
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001113// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001114ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1115 SourceRange R) {
1116 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001117 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001118
1119 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001120 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1121 // This checks if the methods differ by size & alignment.
1122 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1123 issueWarning = true;
1124 }
1125 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001126 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001127 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001128 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001129 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001130 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001131 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001132 }
1133 return MethList.Method;
1134}
1135
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001136void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1137 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001138 if (!FirstMethod.Method) {
1139 // Haven't seen a method with this selector name yet - add it.
1140 FirstMethod.Method = Method;
1141 FirstMethod.Next = 0;
1142 } else {
1143 // We've seen a method with this name, now check the type signature(s).
1144 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1145
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001146 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001147 Next = Next->Next)
1148 match = MatchTwoMethodDeclarations(Method, Next->Method);
1149
1150 if (!match) {
1151 // We have a new signature for an existing method - add it.
1152 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001153 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001154 FirstMethod.Next = OMI;
1155 }
1156 }
1157}
1158
Steve Naroff0701bbb2009-01-08 17:28:14 +00001159/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1160/// have the property type and issue diagnostics if they don't.
1161/// Also synthesize a getter/setter method if none exist (and update the
1162/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1163/// methods is the "right" thing to do.
1164void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1165 ObjCContainerDecl *CD) {
1166 ObjCMethodDecl *GetterMethod, *SetterMethod;
1167
Douglas Gregor6ab35242009-04-09 21:40:53 +00001168 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1169 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Steve Naroff0701bbb2009-01-08 17:28:14 +00001170
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001171 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001172 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001173 Diag(property->getLocation(),
1174 diag::err_accessor_property_type_mismatch)
1175 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001176 << GetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001177 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1178 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001179
1180 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001181 if (Context.getCanonicalType(SetterMethod->getResultType())
1182 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001183 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001184 if (SetterMethod->param_size() != 1 ||
1185 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001186 Diag(property->getLocation(),
1187 diag::err_accessor_property_type_mismatch)
1188 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001189 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001190 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1191 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001192 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001193
1194 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001195 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001196 // FIXME: The synthesized property we set here is misleading. We
1197 // almost always synthesize these methods unless the user explicitly
1198 // provided prototypes (which is odd, but allowed). Sema should be
1199 // typechecking that the declarations jive in that situation (which
1200 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001201 if (!GetterMethod) {
1202 // No instance method of same name as property getter name was found.
1203 // Declare a getter method and add it to the list of methods
1204 // for this class.
1205 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1206 property->getLocation(), property->getGetterName(),
1207 property->getType(), CD, true, false, true,
1208 (property->getPropertyImplementation() ==
1209 ObjCPropertyDecl::Optional) ?
1210 ObjCMethodDecl::Optional :
1211 ObjCMethodDecl::Required);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001212 CD->addDecl(Context, GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001213 } else
1214 // A user declared getter will be synthesize when @synthesize of
1215 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001216 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001217 property->setGetterMethodDecl(GetterMethod);
1218
1219 // Skip setter if property is read-only.
1220 if (!property->isReadOnly()) {
1221 // Find the default setter and if one not found, add one.
1222 if (!SetterMethod) {
1223 // No instance method of same name as property setter name was found.
1224 // Declare a setter method and add it to the list of methods
1225 // for this class.
1226 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1227 property->getLocation(),
1228 property->getSetterName(),
1229 Context.VoidTy, CD, true, false, true,
1230 (property->getPropertyImplementation() ==
1231 ObjCPropertyDecl::Optional) ?
1232 ObjCMethodDecl::Optional :
1233 ObjCMethodDecl::Required);
1234 // Invent the arguments for the setter. We don't bother making a
1235 // nice name for the argument.
1236 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001237 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001238 property->getIdentifier(),
1239 property->getType(),
1240 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001241 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001242 SetterMethod->setMethodParams(Context, &Argument, 1);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001243 CD->addDecl(Context, SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001244 } else
1245 // A user declared setter will be synthesize when @synthesize of
1246 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001247 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001248 property->setSetterMethodDecl(SetterMethod);
1249 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001250 // Add any synthesized methods to the global pool. This allows us to
1251 // handle the following, which is supported by GCC (and part of the design).
1252 //
1253 // @interface Foo
1254 // @property double bar;
1255 // @end
1256 //
1257 // void thisIsUnfortunate() {
1258 // id foo;
1259 // double bar = [foo bar];
1260 // }
1261 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001262 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001263 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001264 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001265 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001266}
1267
Steve Naroffa56f6162007-12-18 01:30:32 +00001268// Note: For class/category implemenations, allMethods/allProperties is
1269// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001270void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1271 DeclPtrTy *allMethods, unsigned allNum,
1272 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001273 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001274 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001275
Steve Naroffa56f6162007-12-18 01:30:32 +00001276 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1277 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001278 // should be true.
1279 if (!ClassDecl)
1280 return;
1281
Chris Lattner4d391482007-12-12 07:09:47 +00001282 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001283 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1284 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001285 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001286
Steve Naroff0701bbb2009-01-08 17:28:14 +00001287 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001288
1289 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1290 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1291 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1292
Chris Lattner4d391482007-12-12 07:09:47 +00001293 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001294 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001295 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001296
1297 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001298 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001299 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001300 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001301 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1302 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001303 if ((isInterfaceDeclKind && PrevMethod && !match)
1304 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001305 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001306 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001307 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001308 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001309 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001310 InsMap[Method->getSelector()] = Method;
1311 /// The following allows us to typecheck messages to "id".
1312 AddInstanceMethodToGlobalPool(Method);
1313 }
1314 }
1315 else {
1316 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001317 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001318 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1319 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001320 if ((isInterfaceDeclKind && PrevMethod && !match)
1321 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001322 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001323 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001324 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001325 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001326 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001327 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001328 /// The following allows us to typecheck messages to "Class".
1329 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001330 }
1331 }
1332 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001333 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001334 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001335 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001336 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001337 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001338 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001339 // Categories are used to extend the class by declaring new methods.
1340 // By the same token, they are also used to add new properties. No
1341 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001342
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001343 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001344 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001345 if (C->getIdentifier() == 0)
1346 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001347 }
Steve Naroff09c47192009-01-09 15:36:25 +00001348 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1349 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1350 // user-defined setter/getter. It also synthesizes setter/getter methods
1351 // and adds them to the DeclContext and global method pools.
Douglas Gregor6ab35242009-04-09 21:40:53 +00001352 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1353 E = CDecl->prop_end(Context);
1354 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001355 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001356 CDecl->setAtEndLoc(AtEndLoc);
1357 }
1358 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001359 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001360 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001361 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001362 } else if (ObjCCategoryImplDecl* CatImplClass =
1363 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001364 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001365
Chris Lattner4d391482007-12-12 07:09:47 +00001366 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001367 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001368 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001369 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001370 Categories; Categories = Categories->getNextClassCategory()) {
1371 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001372 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001373 break;
1374 }
1375 }
1376 }
1377 }
Chris Lattner682bf922009-03-29 16:50:03 +00001378 if (isInterfaceDeclKind) {
1379 // Reject invalid vardecls.
1380 for (unsigned i = 0; i != tuvNum; i++) {
1381 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1382 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1383 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001384 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001385 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001386 }
Chris Lattner682bf922009-03-29 16:50:03 +00001387 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001388 }
Chris Lattner4d391482007-12-12 07:09:47 +00001389}
1390
1391
1392/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1393/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001394static Decl::ObjCDeclQualifier
1395CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1396 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1397 if (PQTVal & ObjCDeclSpec::DQ_In)
1398 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1399 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1400 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1401 if (PQTVal & ObjCDeclSpec::DQ_Out)
1402 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1403 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1404 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1405 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1406 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1407 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1408 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001409
1410 return ret;
1411}
1412
Chris Lattnerb28317a2009-03-28 19:18:32 +00001413Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001414 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001415 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001416 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001417 Selector Sel,
1418 // optional arguments. The number of types/arguments is obtained
1419 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001420 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001421 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001422 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1423 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001424 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001425
1426 // Make sure we can establish a context for the method.
1427 if (!ClassDecl) {
1428 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001429 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001430 }
Chris Lattner4d391482007-12-12 07:09:47 +00001431 QualType resultDeclType;
1432
Steve Naroffccef3712009-02-20 22:59:16 +00001433 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001434 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001435
1436 // Methods cannot return interface types. All ObjC objects are
1437 // passed by reference.
1438 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001439 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1440 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001441 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001442 }
1443 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001444 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001445
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001446 ObjCMethodDecl* ObjCMethod =
1447 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner32d3f9c2009-03-29 04:30:19 +00001448 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001449 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001450 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001451 MethodDeclKind == tok::objc_optional ?
1452 ObjCMethodDecl::Optional :
1453 ObjCMethodDecl::Required);
1454
Chris Lattner0ed844b2008-04-04 06:12:32 +00001455 llvm::SmallVector<ParmVarDecl*, 16> Params;
1456
Chris Lattner7db638d2009-04-11 19:42:43 +00001457 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001458 QualType ArgType, UnpromotedArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001459
Chris Lattnere294d3f2009-04-11 18:57:04 +00001460 if (ArgInfo[i].Type == 0) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001461 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001462 } else {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001463 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff6082c622008-12-09 19:36:17 +00001464 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001465 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001466 }
1467
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001468 ParmVarDecl* Param;
Chris Lattner2dd979f2009-04-11 19:08:56 +00001469 if (ArgType == UnpromotedArgType)
Chris Lattner7db638d2009-04-11 19:42:43 +00001470 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001471 ArgInfo[i].Name, ArgType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001472 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001473 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001474 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner7db638d2009-04-11 19:42:43 +00001475 ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001476 ArgInfo[i].Name, ArgType,
1477 UnpromotedArgType,
Douglas Gregor64650af2009-02-02 23:39:07 +00001478 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001479
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001480 if (ArgType->isObjCInterfaceType()) {
1481 Diag(ArgInfo[i].NameLoc,
1482 diag::err_object_cannot_be_passed_returned_by_value)
1483 << 1 << ArgType;
1484 Param->setInvalidDecl();
1485 }
1486
Chris Lattner0ed844b2008-04-04 06:12:32 +00001487 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001488 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001489
1490 // Apply the attributes to the parameter.
1491 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1492
Chris Lattner0ed844b2008-04-04 06:12:32 +00001493 Params.push_back(Param);
1494 }
1495
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001496 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001497 ObjCMethod->setObjCDeclQualifier(
1498 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1499 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001500
1501 if (AttrList)
1502 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001503
1504 // For implementations (which can be very "coarse grain"), we add the
1505 // method now. This allows the AST to implement lookup methods that work
1506 // incrementally (without waiting until we parse the @end). It also allows
1507 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001508 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001509 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001510 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001511 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel);
1512 ImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001513 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001514 PrevMethod = ImpDecl->getClassMethod(Context, Sel);
1515 ImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001516 }
1517 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001518 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001519 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001520 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001521 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel);
1522 CatImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001523 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001524 PrevMethod = CatImpDecl->getClassMethod(Context, Sel);
1525 CatImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001526 }
1527 }
1528 if (PrevMethod) {
1529 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001530 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001531 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001532 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001533 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001534 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001535}
1536
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001537void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1538 SourceLocation Loc,
1539 unsigned &Attributes) {
1540 // FIXME: Improve the reported location.
1541
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001542 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001543 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001544 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1545 ObjCDeclSpec::DQ_PR_assign |
1546 ObjCDeclSpec::DQ_PR_copy |
1547 ObjCDeclSpec::DQ_PR_retain))) {
1548 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1549 "readwrite" :
1550 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1551 "assign" :
1552 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1553 "copy" : "retain";
1554
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001555 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001556 diag::err_objc_property_attr_mutually_exclusive :
1557 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001558 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001559 }
1560
1561 // Check for copy or retain on non-object types.
1562 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1563 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001564 Diag(Loc, diag::err_objc_property_requires_object)
1565 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001566 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1567 }
1568
1569 // Check for more than one of { assign, copy, retain }.
1570 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1571 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001572 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1573 << "assign" << "copy";
1574 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001575 }
1576 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001577 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1578 << "assign" << "retain";
1579 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001580 }
1581 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1582 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001583 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1584 << "copy" << "retain";
1585 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001586 }
1587 }
1588
1589 // Warn if user supplied no assignment attribute, property is
1590 // readwrite, and this is an object type.
1591 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1592 ObjCDeclSpec::DQ_PR_retain)) &&
1593 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1594 Context.isObjCObjectPointerType(PropertyTy)) {
1595 // Skip this warning in gc-only mode.
1596 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1597 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1598
1599 // If non-gc code warn that this is likely inappropriate.
1600 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1601 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1602
1603 // FIXME: Implement warning dependent on NSCopying being
1604 // implemented. See also:
1605 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1606 // (please trim this list while you are at it).
1607 }
1608}
1609
Chris Lattnerb28317a2009-03-28 19:18:32 +00001610Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1611 FieldDeclarator &FD,
1612 ObjCDeclSpec &ODS,
1613 Selector GetterSel,
1614 Selector SetterSel,
1615 DeclPtrTy ClassCategory,
1616 bool *isOverridingProperty,
1617 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001618 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001619 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1620 // default is readwrite!
1621 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1622 // property is defaulted to 'assign' if it is readwrite and is
1623 // not retain or copy
1624 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1625 (isReadWrite &&
1626 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1627 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1628 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001629 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001630 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001631 // May modify Attributes.
1632 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001633 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1634 if (!CDecl->getIdentifier()) {
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001635 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001636 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001637 if ((CCPrimary = CDecl->getClassInterface())) {
1638 // Find the property in continuation class's primary class only.
1639 ObjCPropertyDecl *PIDecl = 0;
1640 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001641 for (ObjCInterfaceDecl::prop_iterator
1642 I = CCPrimary->prop_begin(Context),
1643 E = CCPrimary->prop_end(Context);
1644 I != E; ++I)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001645 if ((*I)->getIdentifier() == PropertyId) {
1646 PIDecl = *I;
1647 break;
1648 }
1649
1650 if (PIDecl) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001651 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001652 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001653 unsigned PIkind = PIDecl->getPropertyAttributes();
1654 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001655 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001656 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1657 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001658 PIDecl->makeitReadWriteAttribute();
1659 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1660 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1661 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1662 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1663 PIDecl->setSetterName(SetterSel);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001664 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001665 else
1666 Diag(AtLoc, diag::err_use_continuation_class)
1667 << CCPrimary->getDeclName();
1668 *isOverridingProperty = true;
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001669 // Make sure setter decl is synthesized, and added to primary
1670 // class's list.
1671 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001672 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001673 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001674 // No matching property found in the primary class. Just fall thru
1675 // and add property to continuation class's primary class.
1676 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001677 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001678 Diag(CDecl->getLocation(), diag::err_continuation_class);
1679 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001680 return DeclPtrTy();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001681 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001682 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001683
Steve Naroff93983f82009-01-11 12:47:58 +00001684 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1685 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001686 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1687 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001688 FD.D.getIdentifier(), T);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001689 DC->addDecl(Context, PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001690
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001691 if (T->isArrayType() || T->isFunctionType()) {
1692 Diag(AtLoc, diag::err_property_type) << T;
1693 PDecl->setInvalidDecl();
1694 }
1695
Chris Lattner97a58872009-02-16 18:32:47 +00001696 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001697
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001698 // Regardless of setter/getter attribute, we save the default getter/setter
1699 // selector names in anticipation of declaration of setter/getter methods.
1700 PDecl->setGetterName(GetterSel);
1701 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001702
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001703 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001704 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001705
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001706 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001707 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001708
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001709 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001710 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001711
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001712 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001713 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001714
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001715 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001716 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001717
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001718 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001719 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001720
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001721 if (isAssign)
1722 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1723
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001724 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001725 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001726
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001727 if (MethodImplKind == tok::objc_required)
1728 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1729 else if (MethodImplKind == tok::objc_optional)
1730 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001731 // A case of continuation class adding a new property in the class. This
1732 // is not what it was meant for. However, gcc supports it and so should we.
1733 // Make sure setter/getters are declared here.
1734 if (CCPrimary)
1735 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001736
Chris Lattnerb28317a2009-03-28 19:18:32 +00001737 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001738}
1739
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001740/// ActOnPropertyImplDecl - This routine performs semantic checks and
1741/// builds the AST node for a property implementation declaration; declared
1742/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001743///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001744Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1745 SourceLocation PropertyLoc,
1746 bool Synthesize,
1747 DeclPtrTy ClassCatImpDecl,
1748 IdentifierInfo *PropertyId,
1749 IdentifierInfo *PropertyIvar) {
1750 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001751 // Make sure we have a context for the property implementation declaration.
1752 if (!ClassImpDecl) {
1753 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001754 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001755 }
1756 ObjCPropertyDecl *property = 0;
1757 ObjCInterfaceDecl* IDecl = 0;
1758 // Find the class or category class where this property must have
1759 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001760 ObjCImplementationDecl *IC = 0;
1761 ObjCCategoryImplDecl* CatImplClass = 0;
1762 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001763 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001764 // We always synthesize an interface for an implementation
1765 // without an interface decl. So, IDecl is always non-zero.
1766 assert(IDecl &&
1767 "ActOnPropertyImplDecl - @implementation without @interface");
1768
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001769 // Look for this property declaration in the @implementation's @interface
Douglas Gregor6ab35242009-04-09 21:40:53 +00001770 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001771 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001772 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001773 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001774 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001775 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001776 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001777 if (Synthesize) {
1778 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001779 return DeclPtrTy();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001780 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001781 IDecl = CatImplClass->getClassInterface();
1782 if (!IDecl) {
1783 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001784 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001785 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001786 ObjCCategoryDecl *Category =
1787 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1788
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001789 // If category for this implementation not found, it is an error which
1790 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001791 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00001792 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001793 // Look for this property declaration in @implementation's category
Douglas Gregor6ab35242009-04-09 21:40:53 +00001794 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001795 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001796 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001797 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001798 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001799 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001800 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001801 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001802 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001803 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001804 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001805 // Check that we have a valid, previously declared ivar for @synthesize
1806 if (Synthesize) {
1807 // @synthesize
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001808 bool NoExplicitPropertyIvar = (!PropertyIvar);
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001809 if (!PropertyIvar)
1810 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00001811 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001812 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001813 ObjCInterfaceDecl *ClassDeclared;
1814 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001815 if (!Ivar) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001816 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1817 PropertyIvar, PropType,
1818 ObjCIvarDecl::Public,
1819 (Expr *)0);
1820 property->setPropertyIvarDecl(Ivar);
1821 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00001822 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001823 // Note! I deliberately want it to fall thru so, we have a
1824 // a property implementation and to avoid future warnings.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001825 }
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001826 else if (getLangOptions().ObjCNonFragileABI &&
1827 NoExplicitPropertyIvar && ClassDeclared != IDecl) {
1828 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
1829 << property->getDeclName() << Ivar->getDeclName()
1830 << ClassDeclared->getDeclName();
1831 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1832 << Ivar << Ivar->getNameAsCString();
1833 // Note! I deliberately want it to fall thru so more errors are caught.
1834 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001835 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1836
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001837 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001838 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001839 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001840 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001841 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001842 // Note! I deliberately want it to fall thru so, we have a
1843 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001844 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001845
1846 // FIXME! Rules for properties are somewhat different that those
1847 // for assignments. Use a new routine to consolidate all cases;
1848 // specifically for property redeclarations as well as for ivars.
1849 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1850 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1851 if (lhsType != rhsType &&
1852 lhsType->isArithmeticType()) {
1853 Diag(PropertyLoc, diag::error_property_ivar_type)
1854 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001855 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00001856 }
1857 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00001858 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
1859 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001860 Diag(PropertyLoc, diag::error_weak_property)
1861 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001862 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00001863 }
1864 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00001865 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
1866 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001867 Diag(PropertyLoc, diag::error_strong_property)
1868 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001869 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001870 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001871 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001872 } else if (PropertyIvar)
1873 // @dynamic
1874 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001875 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001876 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001877 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1878 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001879 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001880 ObjCPropertyImplDecl::Synthesize
1881 : ObjCPropertyImplDecl::Dynamic),
1882 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001883 if (IC) {
1884 if (Synthesize)
1885 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001886 IC->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001887 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1888 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1889 << PropertyIvar;
1890 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1891 }
1892
Douglas Gregor653f1b12009-04-23 01:02:12 +00001893 if (ObjCPropertyImplDecl *PPIDecl
1894 = IC->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001895 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1896 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001897 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001898 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001899 IC->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001900 }
1901 else {
1902 if (Synthesize)
1903 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001904 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001905 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1906 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1907 << PropertyIvar;
1908 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1909 }
1910
1911 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001912 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001913 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1914 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001915 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001916 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001917 CatImplClass->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001918 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001919
Chris Lattnerb28317a2009-03-28 19:18:32 +00001920 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001921}
Anders Carlsson15281452008-11-04 16:57:32 +00001922
Chris Lattnercc98eac2008-12-17 07:13:27 +00001923bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001924 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001925 return false;
1926
1927 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1928 D->setInvalidDecl();
1929
1930 return true;
1931}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001932
1933/// Collect the instance variables declared in an Objective-C object. Used in
1934/// the creation of structures from objects using the @defs directive.
1935/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1936/// part of the AST generation logic of @defs.
1937static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1938 ASTContext& Ctx,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001939 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001940 if (Class->getSuperClass())
1941 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1942
1943 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001944 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
1945 E = Class->ivar_end(); I != E; ++I) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001946 ObjCIvarDecl* ID = *I;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001947 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
1948 ID->getIdentifier(), ID->getType(),
1949 ID->getBitWidth());
1950 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattnercc98eac2008-12-17 07:13:27 +00001951 }
1952}
1953
1954/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1955/// instance variables of ClassName into Decls.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001956void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001957 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001958 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001959 // Check that ClassName is a valid class
1960 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1961 if (!Class) {
1962 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1963 return;
1964 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001965 if (LangOpts.ObjCNonFragileABI) {
1966 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1967 return;
1968 }
1969
Chris Lattnercc98eac2008-12-17 07:13:27 +00001970 // Collect the instance variables
Chris Lattnerb28317a2009-03-28 19:18:32 +00001971 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001972
1973 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001974 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001975 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001976 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00001977 if (getLangOptions().CPlusPlus)
1978 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001979 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregor6ab35242009-04-09 21:40:53 +00001980 Record->addDecl(Context, FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001981 }
1982}
1983