blob: 7badaa313049f9c1d37944f2c37e0a714806bc29 [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 Naroff31102512008-04-02 18:30:49 +0000101 ObjCInterfaceDecls[ClassName] = IDecl;
Steve Naroffa7503a72009-04-23 15:15:40 +0000102 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000103 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000104 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000105 }
106
107 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000108 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000109 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000110
Steve Naroff818cb9e2009-02-04 17:14:05 +0000111 ObjCInterfaceDecl *SuperClassDecl =
112 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000113
114 // Diagnose classes that inherit from deprecated classes.
115 if (SuperClassDecl)
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000116 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000117
Steve Naroff818cb9e2009-02-04 17:14:05 +0000118 if (PrevDecl && SuperClassDecl == 0) {
119 // The previous declaration was not a class decl. Check if we have a
120 // typedef. If we do, get the underlying class type.
121 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
122 QualType T = TDecl->getUnderlyingType();
123 if (T->isObjCInterfaceType()) {
124 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
125 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
126 }
127 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000128
Steve Naroff818cb9e2009-02-04 17:14:05 +0000129 // This handles the following case:
130 //
131 // typedef int SuperClass;
132 // @interface MyClass : SuperClass {} @end
133 //
134 if (!SuperClassDecl) {
135 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
136 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
137 }
138 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000139
Steve Naroff818cb9e2009-02-04 17:14:05 +0000140 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
141 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000142 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000143 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000144 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000145 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000146 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000147 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000148 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000149 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000150 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000151 IDecl->setLocEnd(SuperLoc);
152 } else { // we have a root class.
153 IDecl->setLocEnd(ClassLoc);
154 }
155
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000156 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000157 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000158 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
159 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000160 IDecl->setLocEnd(EndProtoLoc);
161 }
Anders Carlsson15281452008-11-04 16:57:32 +0000162
163 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000164 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000165}
166
167/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000168/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000169Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
170 IdentifierInfo *AliasName,
171 SourceLocation AliasLocation,
172 IdentifierInfo *ClassName,
173 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000174 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000175 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000176 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000177 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000178 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000179 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000180 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000181 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000182 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000183 }
184 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000185 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000186 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
187 QualType T = TDecl->getUnderlyingType();
188 if (T->isObjCInterfaceType()) {
189 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
190 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000191 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000192 }
193 }
194 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000195 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
196 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000197 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000198 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000199 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000200 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000201 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000202
203 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000204 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000205 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000206
207 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000208
Steve Naroff04c5f4f2009-04-23 17:46:47 +0000209 // FIXME: PushOnScopeChains?
210 CurContext->addDecl(Context, AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000211 if (!CheckObjCDeclScope(AliasDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000212 TUScope->AddDecl(DeclPtrTy::make(AliasDecl));
Douglas Gregord0434102009-01-09 00:49:46 +0000213
Chris Lattnerb28317a2009-03-28 19:18:32 +0000214 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000215}
216
Steve Naroff61d68522009-03-05 15:22:01 +0000217void Sema::CheckForwardProtocolDeclarationForCircularDependency(
218 IdentifierInfo *PName,
219 SourceLocation &Ploc, SourceLocation PrevLoc,
220 const ObjCList<ObjCProtocolDecl> &PList)
221{
222 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
223 E = PList.end(); I != E; ++I) {
224
Douglas Gregor6e378de2009-04-23 23:18:26 +0000225 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000226 if (PDecl->getIdentifier() == PName) {
227 Diag(Ploc, diag::err_protocol_has_circular_dependency);
228 Diag(PrevLoc, diag::note_previous_definition);
229 }
230 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
231 PDecl->getLocation(), PDecl->getReferencedProtocols());
232 }
233 }
234}
235
Chris Lattnerb28317a2009-03-28 19:18:32 +0000236Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000237Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
238 IdentifierInfo *ProtocolName,
239 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000240 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000241 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000242 SourceLocation EndProtoLoc,
243 AttributeList *AttrList) {
244 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000245 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000246 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000247 if (PDecl) {
248 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000249 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000250 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000251 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000252 // Just return the protocol we already had.
253 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000254 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000255 }
Steve Naroff61d68522009-03-05 15:22:01 +0000256 ObjCList<ObjCProtocolDecl> PList;
257 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
258 CheckForwardProtocolDeclarationForCircularDependency(
259 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
260 PList.Destroy(Context);
261
Steve Narofff11b5082008-08-13 16:39:22 +0000262 // Make sure the cached decl gets a valid start location.
263 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000264 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000265 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000266 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
267 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000268 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000269 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000270 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000271 if (AttrList)
272 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000273 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000274 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000275 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000276 PDecl->setLocEnd(EndProtoLoc);
277 }
Anders Carlsson15281452008-11-04 16:57:32 +0000278
279 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000280 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000281}
282
283/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000284/// issues an error if they are not declared. It returns list of
285/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000286void
Chris Lattnere13b9592008-07-26 04:03:38 +0000287Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000288 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000289 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000290 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000291 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000292 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000293 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000294 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000295 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000296 continue;
297 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000298
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000299 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000300
301 // If this is a forward declaration and we are supposed to warn in this
302 // case, do it.
303 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000304 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000305 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000306 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000307 }
308}
309
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000310/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000311/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000312///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000313void
314Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
315 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000316 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000317 ObjCPropertyDecl::PropertyAttributeKind CAttr =
318 Property->getPropertyAttributes();
319 ObjCPropertyDecl::PropertyAttributeKind SAttr =
320 SuperProperty->getPropertyAttributes();
321 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
322 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000323 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000324 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000325 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
326 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000327 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000328 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000329 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
330 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000331 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000332 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000333
334 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
335 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000336 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000337 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000338 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000339 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000340 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000341 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000342 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000343 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000344
345 QualType LHSType =
346 Context.getCanonicalType(SuperProperty->getType());
347 QualType RHSType =
348 Context.getCanonicalType(Property->getType());
349
350 if (!Context.typesAreCompatible(LHSType, RHSType)) {
351 // FIXME: Incorporate this test with typesAreCompatible.
352 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
353 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
354 return;
355 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
356 << Property->getType() << SuperProperty->getType() << inheritedName;
357 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000358}
359
360/// ComparePropertiesInBaseAndSuper - This routine compares property
361/// declarations in base and its super class, if any, and issues
362/// diagnostics in a variety of inconsistant situations.
363///
Chris Lattner70f19542009-02-16 21:26:43 +0000364void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000365 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
366 if (!SDecl)
367 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000368 // FIXME: O(N^2)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000369 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
370 E = SDecl->prop_end(Context); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000371 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000372 // Does property in super class has declaration in current class?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000373 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
374 E = IDecl->prop_end(Context); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000375 ObjCPropertyDecl *PDecl = (*I);
376 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000377 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000378 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000379 }
380 }
381}
382
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000383/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
384/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000385/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000386void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000387Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000388 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000389 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
390 if (!IDecl) {
391 // Category
392 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
393 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Douglas Gregor6ab35242009-04-09 21:40:53 +0000394 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
395 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000396 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000397 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000398 // Is this property already in category's list of properties?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000399 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000400 CP != CE; ++CP)
401 if ((*CP)->getIdentifier() == Pr->getIdentifier())
402 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000403 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000404 // Property protocol already exist in class. Diagnose any mismatch.
405 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
406 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000407 return;
408 }
Douglas Gregor6ab35242009-04-09 21:40:53 +0000409 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
410 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000411 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000412 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000413 // Is this property already in class's list of properties?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000414 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000415 CP != CE; ++CP)
416 if ((*CP)->getIdentifier() == Pr->getIdentifier())
417 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000418 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000419 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000420 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000421 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000422}
423
424/// MergeProtocolPropertiesIntoClass - This routine merges properties
425/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000426/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000427///
Chris Lattner70f19542009-02-16 21:26:43 +0000428void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000429 DeclPtrTy MergeItsProtocols) {
430 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000431 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
432
433 if (!IDecl) {
434 // Category
435 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
436 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
437 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
438 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
439 E = MDecl->protocol_end(); P != E; ++P)
440 // Merge properties of category (*P) into IDECL's
441 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
442
443 // Go thru the list of protocols for this category and recursively merge
444 // their properties into this class as well.
445 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
446 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000447 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000448 } else {
449 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
450 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
451 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000452 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000453 }
454 return;
455 }
456
Chris Lattnerb752f282008-07-21 07:06:49 +0000457 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000458 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
459 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000460 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000461 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
462
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000463 // Go thru the list of protocols for this class and recursively merge
464 // their properties into this class as well.
465 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
466 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000467 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000468 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000469 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
470 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
471 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000472 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000473 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000474}
475
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000476/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000477/// a class method in its extension.
478///
479void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
480 ObjCInterfaceDecl *ID) {
481 if (!ID)
482 return; // Possibly due to previous error
483
484 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000485 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
486 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000487 ObjCMethodDecl *MD = *i;
488 MethodMap[MD->getSelector()] = MD;
489 }
490
491 if (MethodMap.empty())
492 return;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000493 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
494 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000495 ObjCMethodDecl *Method = *i;
496 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
497 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
498 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
499 << Method->getDeclName();
500 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
501 }
502 }
503}
504
Chris Lattner58fe03b2009-04-12 08:43:13 +0000505/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000506Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000507Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000508 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000509 unsigned NumElts,
510 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000511 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000512
513 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000514 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000515 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000516 if (PDecl == 0) { // Not already seen?
517 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
518 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000519 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000520 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000521 if (attrList)
522 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000523 Protocols.push_back(PDecl);
524 }
Anders Carlsson15281452008-11-04 16:57:32 +0000525
526 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000527 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000528 &Protocols[0], Protocols.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000529 CurContext->addDecl(Context, PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000530 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000531 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000532}
533
Chris Lattnerb28317a2009-03-28 19:18:32 +0000534Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000535ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
536 IdentifierInfo *ClassName, SourceLocation ClassLoc,
537 IdentifierInfo *CategoryName,
538 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000539 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000540 unsigned NumProtoRefs,
541 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000542 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000543 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
544 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000545 CurContext->addDecl(Context, CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000546
547 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000548 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000549 if (!IDecl || IDecl->isForwardDecl()) {
550 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000551 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000552 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000553 }
Chris Lattner4d391482007-12-12 07:09:47 +0000554
Chris Lattner70f19542009-02-16 21:26:43 +0000555 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000556
557 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000558 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000559
560 /// Check for duplicate interface declaration for this category
561 ObjCCategoryDecl *CDeclChain;
562 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
563 CDeclChain = CDeclChain->getNextClassCategory()) {
564 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
565 Diag(CategoryLoc, diag::warn_dup_category_def)
566 << ClassName << CategoryName;
567 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
568 break;
569 }
570 }
571 if (!CDeclChain)
572 CDecl->insertNextClassCategory();
573
Chris Lattner4d391482007-12-12 07:09:47 +0000574 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000575 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000576 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000577 }
Anders Carlsson15281452008-11-04 16:57:32 +0000578
579 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000580 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000581}
582
583/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000584/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000585/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000586Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000587 SourceLocation AtCatImplLoc,
588 IdentifierInfo *ClassName, SourceLocation ClassLoc,
589 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000590 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000591 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000592 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
593 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000594 /// Check that class of this category is already completely declared.
595 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000596 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000597
Douglas Gregord0434102009-01-09 00:49:46 +0000598 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000599 CurContext->addDecl(Context, CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000600
Chris Lattner4d391482007-12-12 07:09:47 +0000601 /// TODO: Check that CatName, category name, is not used in another
602 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000603 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000604
605 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000606 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000607}
608
Chris Lattnerb28317a2009-03-28 19:18:32 +0000609Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000610 SourceLocation AtClassImplLoc,
611 IdentifierInfo *ClassName, SourceLocation ClassLoc,
612 IdentifierInfo *SuperClassname,
613 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000614 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000615 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000616 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000618 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000619 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000620 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000621 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000622 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000623 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000624 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000625 IDecl = 0;
626 }
Chris Lattner4d391482007-12-12 07:09:47 +0000627 }
628
629 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000630 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000631 if (SuperClassname) {
632 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000633 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000634 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000635 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
636 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000637 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000638 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000639 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000640 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000641 Diag(SuperClassLoc, diag::err_undef_superclass)
642 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000643 else if (IDecl && IDecl->getSuperClass() != SDecl) {
644 // This implementation and its interface do not have the same
645 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000646 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000647 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000648 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000649 }
650 }
651 }
652
653 if (!IDecl) {
654 // Legacy case of @implementation with no corresponding @interface.
655 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000656
657 // FIXME: Do we support attributes on the @implementation? If so
658 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000659 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
660 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000661 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000662 IDecl->setSuperClass(SDecl);
663 IDecl->setLocEnd(ClassLoc);
664
Douglas Gregord0434102009-01-09 00:49:46 +0000665 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000666 CurContext->addDecl(Context, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000667 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000668 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Daniel Dunbar24c89912009-04-21 21:41:56 +0000669 } else {
670 // Mark the interface as being completed, even if it was just as
671 // @class ....;
672 // declaration; the user cannot reopen it.
673 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000674 }
675
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000676 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000677 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000678 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000679
Douglas Gregord0434102009-01-09 00:49:46 +0000680 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000681 CurContext->addDecl(Context, IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000682
Anders Carlsson15281452008-11-04 16:57:32 +0000683 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000684 return DeclPtrTy::make(IMPDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000685
Chris Lattner4d391482007-12-12 07:09:47 +0000686 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000687 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000688 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000689 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000690 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000691 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000692 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000693}
694
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000695void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
696 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000697 SourceLocation RBrace) {
698 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000699 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000700 if (!IDecl)
701 return;
702 /// Check case of non-existing @interface decl.
703 /// (legacy objective-c @implementation decl without an @interface decl).
704 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000705 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000706 IDecl->setIVarList(ivars, numIvars, Context);
707 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000708 return;
709 }
710 // If implementation has empty ivar list, just return.
711 if (numIvars == 0)
712 return;
713
714 assert(ivars && "missing @implementation ivars");
715
716 // Check interface's Ivar list against those in the implementation.
717 // names and types must match.
718 //
Chris Lattner4d391482007-12-12 07:09:47 +0000719 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000720 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000721 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
722 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000723 ObjCIvarDecl* ImplIvar = ivars[j++];
724 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000725 assert (ImplIvar && "missing implementation ivar");
726 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000727
728 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000729 if (Context.getCanonicalType(ImplIvar->getType()) !=
730 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000731 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000732 << ImplIvar->getIdentifier()
733 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000734 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000735 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
736 Expr *ImplBitWidth = ImplIvar->getBitWidth();
737 Expr *ClsBitWidth = ClsIvar->getBitWidth();
738 if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() !=
739 ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) {
740 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
741 << ImplIvar->getIdentifier();
742 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
743 }
744 }
745 // Make sure the names are identical.
746 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000747 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000748 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000749 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000750 }
751 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000752 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000753
754 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000755 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000756 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000757 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000758}
759
Steve Naroff3c2eb662008-02-10 21:38:56 +0000760void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
761 bool &IncompleteImpl) {
762 if (!IncompleteImpl) {
763 Diag(ImpLoc, diag::warn_incomplete_impl);
764 IncompleteImpl = true;
765 }
Chris Lattner08631c52008-11-23 21:45:46 +0000766 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000767}
768
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000769void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
770 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000771 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Chris Lattner3aff9192009-04-11 19:58:42 +0000772 ImpMethodDecl->getResultType())) {
773 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
774 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
775 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000776 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
777 }
Chris Lattner3aff9192009-04-11 19:58:42 +0000778
779 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
780 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
781 IM != EM; ++IM, ++IF) {
782 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()))
783 continue;
784
785 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
786 << ImpMethodDecl->getDeclName() << (*IF)->getType()
787 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000788 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000789 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000790}
791
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000792/// isPropertyReadonly - Return true if property is readonly, by searching
793/// for the property in the class and in its categories and implementations
794///
795bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000796 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000797 // by far the most common case.
798 if (!PDecl->isReadOnly())
799 return false;
800 // Even if property is ready only, if interface has a user defined setter,
801 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000802 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000803 return false;
804
805 // Main class has the property as 'readonly'. Must search
806 // through the category list to see if the property's
807 // attribute has been over-ridden to 'readwrite'.
808 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
809 Category; Category = Category->getNextClassCategory()) {
810 // Even if property is ready only, if a category has a user defined setter,
811 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000812 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000813 return false;
814 ObjCPropertyDecl *P =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000815 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000816 if (P && !P->isReadOnly())
817 return false;
818 }
819
820 // Also, check for definition of a setter method in the implementation if
821 // all else failed.
822 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
823 if (ObjCImplementationDecl *IMD =
824 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000825 if (IMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000826 return false;
827 }
828 else if (ObjCCategoryImplDecl *CIMD =
829 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000830 if (CIMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000831 return false;
832 }
833 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000834 // Lastly, look through the implementation (if one is in scope).
835 if (ObjCImplementationDecl *ImpDecl =
836 ObjCImplementations[IDecl->getIdentifier()])
Douglas Gregor653f1b12009-04-23 01:02:12 +0000837 if (ImpDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000838 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000839 // If all fails, look at the super class.
840 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
841 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000842 return true;
843}
844
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000845/// FIXME: Type hierarchies in Objective-C can be deep. We could most
846/// likely improve the efficiency of selector lookups and type
847/// checking by associating with each protocol / interface / category
848/// the flattened instance tables. If we used an immutable set to keep
849/// the table then it wouldn't add significant memory cost and it
850/// would be handy for lookups.
851
Steve Naroffefe7f362008-02-08 22:06:17 +0000852/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000853/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000854void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
855 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000856 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000857 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000858 const llvm::DenseSet<Selector> &ClsMap,
859 ObjCInterfaceDecl *IDecl) {
860 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
861
862 // If a method lookup fails locally we still need to look and see if
863 // the method was implemented by a base class or an inherited
864 // protocol. This lookup is slow, but occurs rarely in correct code
865 // and otherwise would terminate in a warning.
866
Chris Lattner4d391482007-12-12 07:09:47 +0000867 // check unimplemented instance methods.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000868 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
869 E = PDecl->instmeth_end(Context); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000870 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000871 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000872 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000873 (!Super ||
874 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000875 // Ugly, but necessary. Method declared in protcol might have
876 // have been synthesized due to a property declared in the class which
877 // uses the protocol.
878 ObjCMethodDecl *MethodInClass =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000879 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000880 if (!MethodInClass || !MethodInClass->isSynthesized())
881 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
882 }
Chris Lattner4d391482007-12-12 07:09:47 +0000883 }
884 // check unimplemented class methods
Douglas Gregor6ab35242009-04-09 21:40:53 +0000885 for (ObjCProtocolDecl::classmeth_iterator
886 I = PDecl->classmeth_begin(Context),
887 E = PDecl->classmeth_end(Context);
888 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000889 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000890 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
891 !ClsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000892 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000893 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000894 }
Chris Lattner780f3292008-07-21 21:32:27 +0000895 // Check on this protocols's referenced protocols, recursively.
896 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
897 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000898 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000899}
900
Chris Lattnercddc8882009-03-01 00:56:52 +0000901void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
902 ObjCContainerDecl* CDecl,
903 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000904 llvm::DenseSet<Selector> InsMap;
905 // Check and see if instance methods in class interface have been
906 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000907 for (ObjCImplementationDecl::instmeth_iterator
908 I = IMPDecl->instmeth_begin(Context),
909 E = IMPDecl->instmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000910 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000911
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000912 // Check and see if properties declared in the interface have either 1)
913 // an implementation or 2) there is a @synthesize/@dynamic implementation
914 // of the property in the @implementation.
915 if (isa<ObjCInterfaceDecl>(CDecl))
916 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context),
917 E = CDecl->prop_end(Context); P != E; ++P) {
918 ObjCPropertyDecl *Prop = (*P);
919 if (Prop->isInvalidDecl())
920 continue;
921 ObjCPropertyImplDecl *PI = 0;
922 // Is there a matching propery synthesize/dynamic?
Douglas Gregor653f1b12009-04-23 01:02:12 +0000923 for (ObjCImplDecl::propimpl_iterator
924 I = IMPDecl->propimpl_begin(Context),
925 EI = IMPDecl->propimpl_end(Context); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000926 if ((*I)->getPropertyDecl() == Prop) {
927 PI = (*I);
928 break;
929 }
930 if (PI)
931 continue;
932 if (!InsMap.count(Prop->getGetterName())) {
933 Diag(Prop->getLocation(),
934 diag::warn_setter_getter_impl_required)
935 << Prop->getDeclName() << Prop->getGetterName();
936 Diag(IMPDecl->getLocation(),
937 diag::note_property_impl_required);
938 }
939
940 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
941 Diag(Prop->getLocation(),
942 diag::warn_setter_getter_impl_required)
943 << Prop->getDeclName() << Prop->getSetterName();
944 Diag(IMPDecl->getLocation(),
945 diag::note_property_impl_required);
946 }
947 }
948
Douglas Gregor6ab35242009-04-09 21:40:53 +0000949 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
950 E = CDecl->instmeth_end(Context); I != E; ++I) {
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000951 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000952 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000953 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000954 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000955
956 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +0000957 IMPDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000958 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000959 CDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000960 assert(IntfMethodDecl &&
961 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
962 // ImpMethodDecl may be null as in a @dynamic property.
963 if (ImpMethodDecl)
964 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
965 }
Chris Lattner4c525092007-12-12 17:58:05 +0000966
Chris Lattner4d391482007-12-12 07:09:47 +0000967 llvm::DenseSet<Selector> ClsMap;
968 // Check and see if class methods in class interface have been
969 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000970 for (ObjCImplementationDecl::classmeth_iterator
971 I = IMPDecl->classmeth_begin(Context),
972 E = IMPDecl->classmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000973 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000974
Douglas Gregor6ab35242009-04-09 21:40:53 +0000975 for (ObjCInterfaceDecl::classmeth_iterator
976 I = CDecl->classmeth_begin(Context),
977 E = CDecl->classmeth_end(Context);
978 I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000979 if (!ClsMap.count((*I)->getSelector()))
980 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000981 else {
982 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +0000983 IMPDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000984 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000985 CDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000986 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
987 }
988
Chris Lattner4d391482007-12-12 07:09:47 +0000989
990 // Check the protocol list for unimplemented methods in the @implementation
991 // class.
Chris Lattnercddc8882009-03-01 00:56:52 +0000992 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
993 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
994 E = I->protocol_end(); PI != E; ++PI)
995 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
996 InsMap, ClsMap, I);
997 // Check class extensions (unnamed categories)
998 for (ObjCCategoryDecl *Categories = I->getCategoryList();
999 Categories; Categories = Categories->getNextClassCategory()) {
1000 if (!Categories->getIdentifier()) {
1001 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1002 break;
1003 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001004 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001005 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1006 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1007 E = C->protocol_end(); PI != E; ++PI)
1008 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1009 InsMap, ClsMap, C->getClassInterface());
1010 } else
1011 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001012}
1013
1014/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001015Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001016Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001017 IdentifierInfo **IdentList,
1018 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001019 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +00001020
1021 for (unsigned i = 0; i != NumElts; ++i) {
1022 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001023 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001024 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001025 // Maybe we will complain about the shadowed template parameter.
1026 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1027 // Just pretend that we didn't see the previous declaration.
1028 PrevDecl = 0;
1029 }
1030
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001031 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001032 // GCC apparently allows the following idiom:
1033 //
1034 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1035 // @class XCElementToggler;
1036 //
1037 // FIXME: Make an extension?
1038 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1039 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001040 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001041 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +00001042 }
Chris Lattner4d391482007-12-12 07:09:47 +00001043 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001044 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001045 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +00001046 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1047 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +00001048 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001049
Steve Naroff8f06f842009-04-23 16:00:56 +00001050 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +00001051 // Remember that this needs to be removed when the scope is popped.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001052 TUScope->AddDecl(DeclPtrTy::make(IDecl));
Chris Lattner4d391482007-12-12 07:09:47 +00001053 }
1054
1055 Interfaces.push_back(IDecl);
1056 }
1057
Douglas Gregord0434102009-01-09 00:49:46 +00001058 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001059 &Interfaces[0],
1060 Interfaces.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +00001061 CurContext->addDecl(Context, CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001062 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001063 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001064}
1065
1066
1067/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1068/// returns true, or false, accordingly.
1069/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001070bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001071 const ObjCMethodDecl *PrevMethod,
1072 bool matchBasedOnSizeAndAlignment) {
1073 QualType T1 = Context.getCanonicalType(Method->getResultType());
1074 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1075
1076 if (T1 != T2) {
1077 // The result types are different.
1078 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001079 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001080 // Incomplete types don't have a size and alignment.
1081 if (T1->isIncompleteType() || T2->isIncompleteType())
1082 return false;
1083 // Check is based on size and alignment.
1084 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1085 return false;
1086 }
Chris Lattner89951a82009-02-20 18:43:26 +00001087
1088 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1089 E = Method->param_end();
1090 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1091
1092 for (; ParamI != E; ++ParamI, ++PrevI) {
1093 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1094 T1 = Context.getCanonicalType((*ParamI)->getType());
1095 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001096 if (T1 != T2) {
1097 // The result types are different.
1098 if (!matchBasedOnSizeAndAlignment)
1099 return false;
1100 // Incomplete types don't have a size and alignment.
1101 if (T1->isIncompleteType() || T2->isIncompleteType())
1102 return false;
1103 // Check is based on size and alignment.
1104 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1105 return false;
1106 }
Chris Lattner4d391482007-12-12 07:09:47 +00001107 }
1108 return true;
1109}
1110
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001111void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Chris Lattnerb25df352009-03-04 05:16:45 +00001112 ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()];
1113 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001114 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001115 Entry.Method = Method;
1116 Entry.Next = 0;
1117 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001118 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001119
1120 // We've seen a method with this name, see if we have already seen this type
1121 // signature.
1122 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1123 if (MatchTwoMethodDeclarations(Method, List->Method))
1124 return;
1125
1126 // We have a new signature for an existing method - add it.
1127 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1128 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001129}
1130
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001131// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001132ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1133 SourceRange R) {
1134 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001135 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001136
1137 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001138 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1139 // This checks if the methods differ by size & alignment.
1140 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1141 issueWarning = true;
1142 }
1143 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001144 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001145 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001146 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001147 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001148 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001149 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001150 }
1151 return MethList.Method;
1152}
1153
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001154void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1155 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001156 if (!FirstMethod.Method) {
1157 // Haven't seen a method with this selector name yet - add it.
1158 FirstMethod.Method = Method;
1159 FirstMethod.Next = 0;
1160 } else {
1161 // We've seen a method with this name, now check the type signature(s).
1162 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1163
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001164 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001165 Next = Next->Next)
1166 match = MatchTwoMethodDeclarations(Method, Next->Method);
1167
1168 if (!match) {
1169 // We have a new signature for an existing method - add it.
1170 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001171 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001172 FirstMethod.Next = OMI;
1173 }
1174 }
1175}
1176
Steve Naroff0701bbb2009-01-08 17:28:14 +00001177/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1178/// have the property type and issue diagnostics if they don't.
1179/// Also synthesize a getter/setter method if none exist (and update the
1180/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1181/// methods is the "right" thing to do.
1182void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1183 ObjCContainerDecl *CD) {
1184 ObjCMethodDecl *GetterMethod, *SetterMethod;
1185
Douglas Gregor6ab35242009-04-09 21:40:53 +00001186 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1187 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Steve Naroff0701bbb2009-01-08 17:28:14 +00001188
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001189 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001190 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001191 Diag(property->getLocation(),
1192 diag::err_accessor_property_type_mismatch)
1193 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001194 << GetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001195 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1196 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001197
1198 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001199 if (Context.getCanonicalType(SetterMethod->getResultType())
1200 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001201 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001202 if (SetterMethod->param_size() != 1 ||
1203 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001204 Diag(property->getLocation(),
1205 diag::err_accessor_property_type_mismatch)
1206 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001207 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001208 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1209 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001210 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001211
1212 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001213 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001214 // FIXME: The synthesized property we set here is misleading. We
1215 // almost always synthesize these methods unless the user explicitly
1216 // provided prototypes (which is odd, but allowed). Sema should be
1217 // typechecking that the declarations jive in that situation (which
1218 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001219 if (!GetterMethod) {
1220 // No instance method of same name as property getter name was found.
1221 // Declare a getter method and add it to the list of methods
1222 // for this class.
1223 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1224 property->getLocation(), property->getGetterName(),
1225 property->getType(), CD, true, false, true,
1226 (property->getPropertyImplementation() ==
1227 ObjCPropertyDecl::Optional) ?
1228 ObjCMethodDecl::Optional :
1229 ObjCMethodDecl::Required);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001230 CD->addDecl(Context, GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001231 } else
1232 // A user declared getter will be synthesize when @synthesize of
1233 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001234 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001235 property->setGetterMethodDecl(GetterMethod);
1236
1237 // Skip setter if property is read-only.
1238 if (!property->isReadOnly()) {
1239 // Find the default setter and if one not found, add one.
1240 if (!SetterMethod) {
1241 // No instance method of same name as property setter name was found.
1242 // Declare a setter method and add it to the list of methods
1243 // for this class.
1244 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1245 property->getLocation(),
1246 property->getSetterName(),
1247 Context.VoidTy, CD, true, false, true,
1248 (property->getPropertyImplementation() ==
1249 ObjCPropertyDecl::Optional) ?
1250 ObjCMethodDecl::Optional :
1251 ObjCMethodDecl::Required);
1252 // Invent the arguments for the setter. We don't bother making a
1253 // nice name for the argument.
1254 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001255 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001256 property->getIdentifier(),
1257 property->getType(),
1258 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001259 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001260 SetterMethod->setMethodParams(Context, &Argument, 1);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001261 CD->addDecl(Context, SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001262 } else
1263 // A user declared setter will be synthesize when @synthesize of
1264 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001265 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001266 property->setSetterMethodDecl(SetterMethod);
1267 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001268 // Add any synthesized methods to the global pool. This allows us to
1269 // handle the following, which is supported by GCC (and part of the design).
1270 //
1271 // @interface Foo
1272 // @property double bar;
1273 // @end
1274 //
1275 // void thisIsUnfortunate() {
1276 // id foo;
1277 // double bar = [foo bar];
1278 // }
1279 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001280 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001281 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001282 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001283 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001284}
1285
Steve Naroffa56f6162007-12-18 01:30:32 +00001286// Note: For class/category implemenations, allMethods/allProperties is
1287// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001288void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1289 DeclPtrTy *allMethods, unsigned allNum,
1290 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001291 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001292 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001293
Steve Naroffa56f6162007-12-18 01:30:32 +00001294 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1295 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001296 // should be true.
1297 if (!ClassDecl)
1298 return;
1299
Chris Lattner4d391482007-12-12 07:09:47 +00001300 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001301 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1302 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001303 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001304
Steve Naroff0701bbb2009-01-08 17:28:14 +00001305 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001306
1307 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1308 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1309 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1310
Chris Lattner4d391482007-12-12 07:09:47 +00001311 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001312 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001313 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001314
1315 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001316 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001317 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001318 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001319 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1320 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001321 if ((isInterfaceDeclKind && PrevMethod && !match)
1322 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001323 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001324 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001325 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001326 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001327 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001328 InsMap[Method->getSelector()] = Method;
1329 /// The following allows us to typecheck messages to "id".
1330 AddInstanceMethodToGlobalPool(Method);
1331 }
1332 }
1333 else {
1334 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001335 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001336 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1337 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001338 if ((isInterfaceDeclKind && PrevMethod && !match)
1339 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001340 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001341 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001342 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001343 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001344 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001345 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001346 /// The following allows us to typecheck messages to "Class".
1347 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001348 }
1349 }
1350 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001351 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001352 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001353 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001354 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001355 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001356 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001357 // Categories are used to extend the class by declaring new methods.
1358 // By the same token, they are also used to add new properties. No
1359 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001360
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001361 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001362 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001363 if (C->getIdentifier() == 0)
1364 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001365 }
Steve Naroff09c47192009-01-09 15:36:25 +00001366 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1367 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1368 // user-defined setter/getter. It also synthesizes setter/getter methods
1369 // and adds them to the DeclContext and global method pools.
Douglas Gregor6ab35242009-04-09 21:40:53 +00001370 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1371 E = CDecl->prop_end(Context);
1372 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001373 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001374 CDecl->setAtEndLoc(AtEndLoc);
1375 }
1376 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001377 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001378 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001379 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001380 } else if (ObjCCategoryImplDecl* CatImplClass =
1381 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001382 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001383
Chris Lattner4d391482007-12-12 07:09:47 +00001384 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001385 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001386 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001387 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001388 Categories; Categories = Categories->getNextClassCategory()) {
1389 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001390 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001391 break;
1392 }
1393 }
1394 }
1395 }
Chris Lattner682bf922009-03-29 16:50:03 +00001396 if (isInterfaceDeclKind) {
1397 // Reject invalid vardecls.
1398 for (unsigned i = 0; i != tuvNum; i++) {
1399 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1400 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1401 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001402 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001403 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001404 }
Chris Lattner682bf922009-03-29 16:50:03 +00001405 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001406 }
Chris Lattner4d391482007-12-12 07:09:47 +00001407}
1408
1409
1410/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1411/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001412static Decl::ObjCDeclQualifier
1413CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1414 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1415 if (PQTVal & ObjCDeclSpec::DQ_In)
1416 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1417 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1418 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1419 if (PQTVal & ObjCDeclSpec::DQ_Out)
1420 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1421 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1422 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1423 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1424 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1425 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1426 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001427
1428 return ret;
1429}
1430
Chris Lattnerb28317a2009-03-28 19:18:32 +00001431Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001432 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001433 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001434 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001435 Selector Sel,
1436 // optional arguments. The number of types/arguments is obtained
1437 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001438 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001439 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001440 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1441 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001442 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001443
1444 // Make sure we can establish a context for the method.
1445 if (!ClassDecl) {
1446 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001447 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001448 }
Chris Lattner4d391482007-12-12 07:09:47 +00001449 QualType resultDeclType;
1450
Steve Naroffccef3712009-02-20 22:59:16 +00001451 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001452 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001453
1454 // Methods cannot return interface types. All ObjC objects are
1455 // passed by reference.
1456 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001457 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1458 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001459 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001460 }
1461 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001462 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001463
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001464 ObjCMethodDecl* ObjCMethod =
1465 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner32d3f9c2009-03-29 04:30:19 +00001466 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001467 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001468 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001469 MethodDeclKind == tok::objc_optional ?
1470 ObjCMethodDecl::Optional :
1471 ObjCMethodDecl::Required);
1472
Chris Lattner0ed844b2008-04-04 06:12:32 +00001473 llvm::SmallVector<ParmVarDecl*, 16> Params;
1474
Chris Lattner7db638d2009-04-11 19:42:43 +00001475 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001476 QualType ArgType, UnpromotedArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001477
Chris Lattnere294d3f2009-04-11 18:57:04 +00001478 if (ArgInfo[i].Type == 0) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001479 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001480 } else {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001481 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff6082c622008-12-09 19:36:17 +00001482 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001483 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001484 }
1485
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001486 ParmVarDecl* Param;
Chris Lattner2dd979f2009-04-11 19:08:56 +00001487 if (ArgType == UnpromotedArgType)
Chris Lattner7db638d2009-04-11 19:42:43 +00001488 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001489 ArgInfo[i].Name, ArgType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001490 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001491 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001492 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner7db638d2009-04-11 19:42:43 +00001493 ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001494 ArgInfo[i].Name, ArgType,
1495 UnpromotedArgType,
Douglas Gregor64650af2009-02-02 23:39:07 +00001496 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001497
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001498 if (ArgType->isObjCInterfaceType()) {
1499 Diag(ArgInfo[i].NameLoc,
1500 diag::err_object_cannot_be_passed_returned_by_value)
1501 << 1 << ArgType;
1502 Param->setInvalidDecl();
1503 }
1504
Chris Lattner0ed844b2008-04-04 06:12:32 +00001505 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001506 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001507
1508 // Apply the attributes to the parameter.
1509 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1510
Chris Lattner0ed844b2008-04-04 06:12:32 +00001511 Params.push_back(Param);
1512 }
1513
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001514 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001515 ObjCMethod->setObjCDeclQualifier(
1516 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1517 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001518
1519 if (AttrList)
1520 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001521
1522 // For implementations (which can be very "coarse grain"), we add the
1523 // method now. This allows the AST to implement lookup methods that work
1524 // incrementally (without waiting until we parse the @end). It also allows
1525 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001526 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001527 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001528 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001529 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel);
1530 ImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001531 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001532 PrevMethod = ImpDecl->getClassMethod(Context, Sel);
1533 ImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001534 }
1535 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001536 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001537 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001538 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001539 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel);
1540 CatImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001541 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001542 PrevMethod = CatImpDecl->getClassMethod(Context, Sel);
1543 CatImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001544 }
1545 }
1546 if (PrevMethod) {
1547 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001548 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001549 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001550 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001551 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001552 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001553}
1554
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001555void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1556 SourceLocation Loc,
1557 unsigned &Attributes) {
1558 // FIXME: Improve the reported location.
1559
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001560 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001561 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001562 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1563 ObjCDeclSpec::DQ_PR_assign |
1564 ObjCDeclSpec::DQ_PR_copy |
1565 ObjCDeclSpec::DQ_PR_retain))) {
1566 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1567 "readwrite" :
1568 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1569 "assign" :
1570 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1571 "copy" : "retain";
1572
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001573 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001574 diag::err_objc_property_attr_mutually_exclusive :
1575 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001576 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001577 }
1578
1579 // Check for copy or retain on non-object types.
1580 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1581 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001582 Diag(Loc, diag::err_objc_property_requires_object)
1583 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001584 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1585 }
1586
1587 // Check for more than one of { assign, copy, retain }.
1588 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1589 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001590 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1591 << "assign" << "copy";
1592 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001593 }
1594 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001595 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1596 << "assign" << "retain";
1597 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001598 }
1599 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1600 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001601 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1602 << "copy" << "retain";
1603 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001604 }
1605 }
1606
1607 // Warn if user supplied no assignment attribute, property is
1608 // readwrite, and this is an object type.
1609 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1610 ObjCDeclSpec::DQ_PR_retain)) &&
1611 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1612 Context.isObjCObjectPointerType(PropertyTy)) {
1613 // Skip this warning in gc-only mode.
1614 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1615 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1616
1617 // If non-gc code warn that this is likely inappropriate.
1618 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1619 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1620
1621 // FIXME: Implement warning dependent on NSCopying being
1622 // implemented. See also:
1623 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1624 // (please trim this list while you are at it).
1625 }
1626}
1627
Chris Lattnerb28317a2009-03-28 19:18:32 +00001628Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1629 FieldDeclarator &FD,
1630 ObjCDeclSpec &ODS,
1631 Selector GetterSel,
1632 Selector SetterSel,
1633 DeclPtrTy ClassCategory,
1634 bool *isOverridingProperty,
1635 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001636 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001637 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1638 // default is readwrite!
1639 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1640 // property is defaulted to 'assign' if it is readwrite and is
1641 // not retain or copy
1642 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1643 (isReadWrite &&
1644 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1645 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1646 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001647 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001648 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001649 // May modify Attributes.
1650 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001651 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1652 if (!CDecl->getIdentifier()) {
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001653 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001654 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001655 if ((CCPrimary = CDecl->getClassInterface())) {
1656 // Find the property in continuation class's primary class only.
1657 ObjCPropertyDecl *PIDecl = 0;
1658 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001659 for (ObjCInterfaceDecl::prop_iterator
1660 I = CCPrimary->prop_begin(Context),
1661 E = CCPrimary->prop_end(Context);
1662 I != E; ++I)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001663 if ((*I)->getIdentifier() == PropertyId) {
1664 PIDecl = *I;
1665 break;
1666 }
1667
1668 if (PIDecl) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001669 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001670 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001671 unsigned PIkind = PIDecl->getPropertyAttributes();
1672 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001673 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001674 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1675 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001676 PIDecl->makeitReadWriteAttribute();
1677 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1678 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1679 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1680 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1681 PIDecl->setSetterName(SetterSel);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001682 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001683 else
1684 Diag(AtLoc, diag::err_use_continuation_class)
1685 << CCPrimary->getDeclName();
1686 *isOverridingProperty = true;
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001687 // Make sure setter decl is synthesized, and added to primary
1688 // class's list.
1689 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001690 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001691 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001692 // No matching property found in the primary class. Just fall thru
1693 // and add property to continuation class's primary class.
1694 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001695 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001696 Diag(CDecl->getLocation(), diag::err_continuation_class);
1697 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001698 return DeclPtrTy();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001699 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001700 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001701
Steve Naroff93983f82009-01-11 12:47:58 +00001702 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1703 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001704 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1705 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001706 FD.D.getIdentifier(), T);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001707 DC->addDecl(Context, PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001708
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001709 if (T->isArrayType() || T->isFunctionType()) {
1710 Diag(AtLoc, diag::err_property_type) << T;
1711 PDecl->setInvalidDecl();
1712 }
1713
Chris Lattner97a58872009-02-16 18:32:47 +00001714 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001715
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001716 // Regardless of setter/getter attribute, we save the default getter/setter
1717 // selector names in anticipation of declaration of setter/getter methods.
1718 PDecl->setGetterName(GetterSel);
1719 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001720
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001721 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001722 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001723
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001724 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001725 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001726
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001727 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001728 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001729
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001730 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001731 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001732
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001733 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001734 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001735
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001736 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001737 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001738
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001739 if (isAssign)
1740 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1741
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001742 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001743 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001744
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001745 if (MethodImplKind == tok::objc_required)
1746 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1747 else if (MethodImplKind == tok::objc_optional)
1748 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001749 // A case of continuation class adding a new property in the class. This
1750 // is not what it was meant for. However, gcc supports it and so should we.
1751 // Make sure setter/getters are declared here.
1752 if (CCPrimary)
1753 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001754
Chris Lattnerb28317a2009-03-28 19:18:32 +00001755 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001756}
1757
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001758/// ActOnPropertyImplDecl - This routine performs semantic checks and
1759/// builds the AST node for a property implementation declaration; declared
1760/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001761///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001762Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1763 SourceLocation PropertyLoc,
1764 bool Synthesize,
1765 DeclPtrTy ClassCatImpDecl,
1766 IdentifierInfo *PropertyId,
1767 IdentifierInfo *PropertyIvar) {
1768 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001769 // Make sure we have a context for the property implementation declaration.
1770 if (!ClassImpDecl) {
1771 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001772 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001773 }
1774 ObjCPropertyDecl *property = 0;
1775 ObjCInterfaceDecl* IDecl = 0;
1776 // Find the class or category class where this property must have
1777 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001778 ObjCImplementationDecl *IC = 0;
1779 ObjCCategoryImplDecl* CatImplClass = 0;
1780 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001781 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001782 // We always synthesize an interface for an implementation
1783 // without an interface decl. So, IDecl is always non-zero.
1784 assert(IDecl &&
1785 "ActOnPropertyImplDecl - @implementation without @interface");
1786
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001787 // Look for this property declaration in the @implementation's @interface
Douglas Gregor6ab35242009-04-09 21:40:53 +00001788 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001789 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001790 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001791 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001792 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001793 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001794 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001795 if (Synthesize) {
1796 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001797 return DeclPtrTy();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001798 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001799 IDecl = CatImplClass->getClassInterface();
1800 if (!IDecl) {
1801 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001802 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001803 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001804 ObjCCategoryDecl *Category =
1805 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1806
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001807 // If category for this implementation not found, it is an error which
1808 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001809 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00001810 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001811 // Look for this property declaration in @implementation's category
Douglas Gregor6ab35242009-04-09 21:40:53 +00001812 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001813 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001814 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001815 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001816 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001817 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001818 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001819 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001820 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001821 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001822 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001823 // Check that we have a valid, previously declared ivar for @synthesize
1824 if (Synthesize) {
1825 // @synthesize
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001826 bool NoExplicitPropertyIvar = (!PropertyIvar);
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001827 if (!PropertyIvar)
1828 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00001829 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001830 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001831 ObjCInterfaceDecl *ClassDeclared;
1832 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001833 if (!Ivar) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001834 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1835 PropertyIvar, PropType,
1836 ObjCIvarDecl::Public,
1837 (Expr *)0);
1838 property->setPropertyIvarDecl(Ivar);
1839 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00001840 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001841 // Note! I deliberately want it to fall thru so, we have a
1842 // a property implementation and to avoid future warnings.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001843 }
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001844 else if (getLangOptions().ObjCNonFragileABI &&
1845 NoExplicitPropertyIvar && ClassDeclared != IDecl) {
1846 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
1847 << property->getDeclName() << Ivar->getDeclName()
1848 << ClassDeclared->getDeclName();
1849 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1850 << Ivar << Ivar->getNameAsCString();
1851 // Note! I deliberately want it to fall thru so more errors are caught.
1852 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001853 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1854
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001855 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001856 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001857 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001858 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001859 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001860 // Note! I deliberately want it to fall thru so, we have a
1861 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001862 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001863
1864 // FIXME! Rules for properties are somewhat different that those
1865 // for assignments. Use a new routine to consolidate all cases;
1866 // specifically for property redeclarations as well as for ivars.
1867 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1868 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1869 if (lhsType != rhsType &&
1870 lhsType->isArithmeticType()) {
1871 Diag(PropertyLoc, diag::error_property_ivar_type)
1872 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001873 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00001874 }
1875 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00001876 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
1877 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001878 Diag(PropertyLoc, diag::error_weak_property)
1879 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001880 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00001881 }
1882 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00001883 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
1884 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001885 Diag(PropertyLoc, diag::error_strong_property)
1886 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001887 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001888 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001889 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001890 } else if (PropertyIvar)
1891 // @dynamic
1892 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001893 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001894 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001895 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1896 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001897 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001898 ObjCPropertyImplDecl::Synthesize
1899 : ObjCPropertyImplDecl::Dynamic),
1900 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001901 if (IC) {
1902 if (Synthesize)
1903 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001904 IC->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
Douglas Gregor653f1b12009-04-23 01:02:12 +00001911 if (ObjCPropertyImplDecl *PPIDecl
1912 = IC->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 IC->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001918 }
1919 else {
1920 if (Synthesize)
1921 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001922 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001923 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1924 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1925 << PropertyIvar;
1926 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1927 }
1928
1929 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001930 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001931 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1932 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001933 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001934 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001935 CatImplClass->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001936 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001937
Chris Lattnerb28317a2009-03-28 19:18:32 +00001938 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001939}
Anders Carlsson15281452008-11-04 16:57:32 +00001940
Chris Lattnercc98eac2008-12-17 07:13:27 +00001941bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001942 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001943 return false;
1944
1945 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1946 D->setInvalidDecl();
1947
1948 return true;
1949}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001950
1951/// Collect the instance variables declared in an Objective-C object. Used in
1952/// the creation of structures from objects using the @defs directive.
1953/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1954/// part of the AST generation logic of @defs.
1955static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1956 ASTContext& Ctx,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001957 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001958 if (Class->getSuperClass())
1959 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1960
1961 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001962 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
1963 E = Class->ivar_end(); I != E; ++I) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001964 ObjCIvarDecl* ID = *I;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001965 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
1966 ID->getIdentifier(), ID->getType(),
1967 ID->getBitWidth());
1968 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattnercc98eac2008-12-17 07:13:27 +00001969 }
1970}
1971
1972/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1973/// instance variables of ClassName into Decls.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001974void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001975 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001976 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001977 // Check that ClassName is a valid class
1978 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1979 if (!Class) {
1980 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1981 return;
1982 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001983 if (LangOpts.ObjCNonFragileABI) {
1984 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1985 return;
1986 }
1987
Chris Lattnercc98eac2008-12-17 07:13:27 +00001988 // Collect the instance variables
Chris Lattnerb28317a2009-03-28 19:18:32 +00001989 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001990
1991 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001992 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001993 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001994 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00001995 if (getLangOptions().CPlusPlus)
1996 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001997 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregor6ab35242009-04-09 21:40:53 +00001998 Record->addDecl(Context, FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001999 }
2000}
2001