blob: d90f2adc77abf8c7b3afdb7ef9eee3f150e2a9b6 [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.
Steve Naroffebf64432009-02-28 16:59:13 +000023void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff394f3f42008-07-25 17:57:26 +000025 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
26
27 // If we don't have a valid method decl, simply return.
28 if (!MDecl)
29 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000030
31 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000032 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000033 AddInstanceMethodToGlobalPool(MDecl);
34 else
35 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000036
37 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000038 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000039
Steve Narofff3cf8972009-02-28 16:48:43 +000040 ActiveScope = FnBodyScope;
41
Chris Lattner4d391482007-12-12 07:09:47 +000042 // 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 Lattner7caeabd2008-07-21 22:17:28 +000058Sema::DeclTy *Sema::
59ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
60 IdentifierInfo *ClassName, SourceLocation ClassLoc,
61 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000062 DeclTy * const *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!
90 return IDecl;
91 } 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;
Douglas Gregord0434102009-01-09 00:49:46 +0000102 // FIXME: PushOnScopeChains
Douglas Gregor482b77d2009-01-12 23:27:07 +0000103 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000104 // Remember that this needs to be removed when the scope is popped.
105 TUScope->AddDecl(IDecl);
106 }
107
108 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000109 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000110 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000111
Steve Naroff818cb9e2009-02-04 17:14:05 +0000112 ObjCInterfaceDecl *SuperClassDecl =
113 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000114
115 // Diagnose classes that inherit from deprecated classes.
116 if (SuperClassDecl)
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000117 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000118
Steve Naroff818cb9e2009-02-04 17:14:05 +0000119 if (PrevDecl && SuperClassDecl == 0) {
120 // The previous declaration was not a class decl. Check if we have a
121 // typedef. If we do, get the underlying class type.
122 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
123 QualType T = TDecl->getUnderlyingType();
124 if (T->isObjCInterfaceType()) {
125 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
126 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
127 }
128 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000129
Steve Naroff818cb9e2009-02-04 17:14:05 +0000130 // This handles the following case:
131 //
132 // typedef int SuperClass;
133 // @interface MyClass : SuperClass {} @end
134 //
135 if (!SuperClassDecl) {
136 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
137 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
138 }
139 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000140
Steve Naroff818cb9e2009-02-04 17:14:05 +0000141 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
142 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000143 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000144 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000145 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000146 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000147 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000148 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000149 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000150 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000151 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000152 IDecl->setLocEnd(SuperLoc);
153 } else { // we have a root class.
154 IDecl->setLocEnd(ClassLoc);
155 }
156
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000157 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000158 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000159 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
160 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000161 IDecl->setLocEnd(EndProtoLoc);
162 }
Anders Carlsson15281452008-11-04 16:57:32 +0000163
164 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000165 return IDecl;
166}
167
168/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000169/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000170Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
171 IdentifierInfo *AliasName,
172 SourceLocation AliasLocation,
173 IdentifierInfo *ClassName,
174 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000175 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000176 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000177 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000178 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000179 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000180 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000181 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000182 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000183 return 0;
184 }
185 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000186 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000187 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
188 QualType T = TDecl->getUnderlyingType();
189 if (T->isObjCInterfaceType()) {
190 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
191 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000192 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000193 }
194 }
195 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000196 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
197 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000198 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000199 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000200 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000201 return 0;
202 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000203
204 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000205 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000206 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000207
208 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000209
210 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000211 CurContext->addDecl(AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000212 if (!CheckObjCDeclScope(AliasDecl))
213 TUScope->AddDecl(AliasDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000214
Chris Lattner4d391482007-12-12 07:09:47 +0000215 return AliasDecl;
216}
217
Steve Naroff61d68522009-03-05 15:22:01 +0000218void Sema::CheckForwardProtocolDeclarationForCircularDependency(
219 IdentifierInfo *PName,
220 SourceLocation &Ploc, SourceLocation PrevLoc,
221 const ObjCList<ObjCProtocolDecl> &PList)
222{
223 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
224 E = PList.end(); I != E; ++I) {
225
226 if (ObjCProtocolDecl *PDecl = ObjCProtocols[(*I)->getIdentifier()]) {
227 if (PDecl->getIdentifier() == PName) {
228 Diag(Ploc, diag::err_protocol_has_circular_dependency);
229 Diag(PrevLoc, diag::note_previous_definition);
230 }
231 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
232 PDecl->getLocation(), PDecl->getReferencedProtocols());
233 }
234 }
235}
236
Chris Lattnere13b9592008-07-26 04:03:38 +0000237Sema::DeclTy *
238Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
239 IdentifierInfo *ProtocolName,
240 SourceLocation ProtocolLoc,
241 DeclTy * const *ProtoRefs,
242 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000243 SourceLocation EndProtoLoc,
244 AttributeList *AttrList) {
245 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000246 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000247 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000248 if (PDecl) {
249 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000250 if (!PDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000251 PDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000252 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000253 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000254 // Just return the protocol we already had.
255 // FIXME: don't leak the objects passed in!
256 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000257 }
Steve Naroff61d68522009-03-05 15:22:01 +0000258 ObjCList<ObjCProtocolDecl> PList;
259 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
260 CheckForwardProtocolDeclarationForCircularDependency(
261 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
262 PList.Destroy(Context);
263
Steve Narofff11b5082008-08-13 16:39:22 +0000264 // Make sure the cached decl gets a valid start location.
265 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000266 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000267 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000268 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
269 AtProtoInterfaceLoc,ProtocolName);
270 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000271 CurContext->addDecl(PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000272 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000273 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000274 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000275 if (AttrList)
276 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000277 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000278 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000279 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000280 PDecl->setLocEnd(EndProtoLoc);
281 }
Anders Carlsson15281452008-11-04 16:57:32 +0000282
283 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000284 return PDecl;
285}
286
287/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000288/// issues an error if they are not declared. It returns list of
289/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000290void
Chris Lattnere13b9592008-07-26 04:03:38 +0000291Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000292 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000293 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000294 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000295 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000296 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
297 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000298 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000299 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000300 continue;
301 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000302
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000303 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000304
305 // If this is a forward declaration and we are supposed to warn in this
306 // case, do it.
307 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000308 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000309 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000310 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000311 }
312}
313
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000314/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000315/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000316///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000317void
318Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
319 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000320 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000321 ObjCPropertyDecl::PropertyAttributeKind CAttr =
322 Property->getPropertyAttributes();
323 ObjCPropertyDecl::PropertyAttributeKind SAttr =
324 SuperProperty->getPropertyAttributes();
325 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
326 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000327 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000328 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000329 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
330 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
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() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000333 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
334 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000335 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000336 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000337
338 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
339 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000340 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000341 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000342 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000343 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000344 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000345 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000346 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000347 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000348
349 QualType LHSType =
350 Context.getCanonicalType(SuperProperty->getType());
351 QualType RHSType =
352 Context.getCanonicalType(Property->getType());
353
354 if (!Context.typesAreCompatible(LHSType, RHSType)) {
355 // FIXME: Incorporate this test with typesAreCompatible.
356 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
357 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
358 return;
359 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
360 << Property->getType() << SuperProperty->getType() << inheritedName;
361 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000362}
363
364/// ComparePropertiesInBaseAndSuper - This routine compares property
365/// declarations in base and its super class, if any, and issues
366/// diagnostics in a variety of inconsistant situations.
367///
Chris Lattner70f19542009-02-16 21:26:43 +0000368void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000369 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
370 if (!SDecl)
371 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000372 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000373 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
374 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000375 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000376 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000377 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
378 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000379 ObjCPropertyDecl *PDecl = (*I);
380 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000381 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000382 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000383 }
384 }
385}
386
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000387/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
388/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000389/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000390void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000391Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000392 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000393 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
394 if (!IDecl) {
395 // Category
396 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
397 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff09c47192009-01-09 15:36:25 +0000398 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
399 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000400 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000401 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000402 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000403 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000404 CP != CE; ++CP)
405 if ((*CP)->getIdentifier() == Pr->getIdentifier())
406 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000407 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000408 // Property protocol already exist in class. Diagnose any mismatch.
409 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
410 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000411 return;
412 }
Steve Naroff09c47192009-01-09 15:36:25 +0000413 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
414 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000415 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000416 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000417 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000418 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000419 CP != CE; ++CP)
420 if ((*CP)->getIdentifier() == Pr->getIdentifier())
421 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000422 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000423 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000424 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000425 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000426}
427
428/// MergeProtocolPropertiesIntoClass - This routine merges properties
429/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000430/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000431///
Chris Lattner70f19542009-02-16 21:26:43 +0000432void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
433 DeclTy *MergeItsProtocols) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000434 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000435 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
436
437 if (!IDecl) {
438 // Category
439 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
440 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
441 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
442 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
443 E = MDecl->protocol_end(); P != E; ++P)
444 // Merge properties of category (*P) into IDECL's
445 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
446
447 // Go thru the list of protocols for this category and recursively merge
448 // their properties into this class as well.
449 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
450 E = CatDecl->protocol_end(); P != E; ++P)
451 MergeProtocolPropertiesIntoClass(CatDecl, *P);
452 } else {
453 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
454 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
455 E = MD->protocol_end(); P != E; ++P)
456 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
457 }
458 return;
459 }
460
Chris Lattnerb752f282008-07-21 07:06:49 +0000461 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000462 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
463 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000464 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000465 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
466
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000467 // Go thru the list of protocols for this class and recursively merge
468 // their properties into this class as well.
469 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
470 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000471 MergeProtocolPropertiesIntoClass(IDecl, *P);
472 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000473 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
474 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
475 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000476 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000477 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000478}
479
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000480/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000481/// a class method in its extension.
482///
483void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
484 ObjCInterfaceDecl *ID) {
485 if (!ID)
486 return; // Possibly due to previous error
487
488 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
489 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
490 e = ID->meth_end(); i != e; ++i) {
491 ObjCMethodDecl *MD = *i;
492 MethodMap[MD->getSelector()] = MD;
493 }
494
495 if (MethodMap.empty())
496 return;
497 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
498 e = CAT->meth_end(); i != e; ++i) {
499 ObjCMethodDecl *Method = *i;
500 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
501 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
502 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
503 << Method->getDeclName();
504 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
505 }
506 }
507}
508
Chris Lattner4d391482007-12-12 07:09:47 +0000509/// ActOnForwardProtocolDeclaration -
510Action::DeclTy *
511Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000512 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000513 unsigned NumElts,
514 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000515 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000516
517 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000518 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000519 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000520 if (PDecl == 0) { // Not already seen?
521 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
522 IdentList[i].second, Ident);
523 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000524 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000525 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000526 if (attrList)
527 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000528 Protocols.push_back(PDecl);
529 }
Anders Carlsson15281452008-11-04 16:57:32 +0000530
531 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000532 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000533 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000534 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000535 CheckObjCDeclScope(PDecl);
536 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000537}
538
Chris Lattner7caeabd2008-07-21 22:17:28 +0000539Sema::DeclTy *Sema::
540ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
541 IdentifierInfo *ClassName, SourceLocation ClassLoc,
542 IdentifierInfo *CategoryName,
543 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000544 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000545 unsigned NumProtoRefs,
546 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000547 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000548 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
549 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000550 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000551
552 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000553 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000554 if (!IDecl || IDecl->isForwardDecl()) {
555 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000556 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner70f19542009-02-16 21:26:43 +0000557 return CDecl;
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000558 }
Chris Lattner4d391482007-12-12 07:09:47 +0000559
Chris Lattner70f19542009-02-16 21:26:43 +0000560 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000561
562 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000563 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000564
565 /// Check for duplicate interface declaration for this category
566 ObjCCategoryDecl *CDeclChain;
567 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
568 CDeclChain = CDeclChain->getNextClassCategory()) {
569 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
570 Diag(CategoryLoc, diag::warn_dup_category_def)
571 << ClassName << CategoryName;
572 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
573 break;
574 }
575 }
576 if (!CDeclChain)
577 CDecl->insertNextClassCategory();
578
Chris Lattner4d391482007-12-12 07:09:47 +0000579 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000580 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000581 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000582 }
Anders Carlsson15281452008-11-04 16:57:32 +0000583
584 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000585 return CDecl;
586}
587
588/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000589/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000590/// object.
591Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
592 SourceLocation AtCatImplLoc,
593 IdentifierInfo *ClassName, SourceLocation ClassLoc,
594 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000595 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000596 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000597 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
598 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000599 /// Check that class of this category is already completely declared.
600 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000601 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000602
Douglas Gregord0434102009-01-09 00:49:46 +0000603 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000604 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000605
Chris Lattner4d391482007-12-12 07:09:47 +0000606 /// TODO: Check that CatName, category name, is not used in another
607 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000608 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000609
610 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000611 return CDecl;
612}
613
614Sema::DeclTy *Sema::ActOnStartClassImplementation(
615 SourceLocation AtClassImplLoc,
616 IdentifierInfo *ClassName, SourceLocation ClassLoc,
617 IdentifierInfo *SuperClassname,
618 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000619 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000620 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000621 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000622 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000623 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000624 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000625 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000626 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000627 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000628 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000629 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000630 }
631
632 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000633 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000634 if (SuperClassname) {
635 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000636 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000637 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000638 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
639 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000640 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000641 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000642 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000643 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000644 Diag(SuperClassLoc, diag::err_undef_superclass)
645 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000646 else if (IDecl && IDecl->getSuperClass() != SDecl) {
647 // This implementation and its interface do not have the same
648 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000649 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000650 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000651 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000652 }
653 }
654 }
655
656 if (!IDecl) {
657 // Legacy case of @implementation with no corresponding @interface.
658 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000659
660 // FIXME: Do we support attributes on the @implementation? If so
661 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000662 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
663 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000664 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000665 IDecl->setSuperClass(SDecl);
666 IDecl->setLocEnd(ClassLoc);
667
Douglas Gregord0434102009-01-09 00:49:46 +0000668 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000669 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000670 // Remember that this needs to be removed when the scope is popped.
671 TUScope->AddDecl(IDecl);
672 }
673
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000674 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000675 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000676 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000677
Douglas Gregord0434102009-01-09 00:49:46 +0000678 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000679 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000680
Anders Carlsson15281452008-11-04 16:57:32 +0000681 if (CheckObjCDeclScope(IMPDecl))
682 return IMPDecl;
683
Chris Lattner4d391482007-12-12 07:09:47 +0000684 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000685 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000686 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000687 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000688 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000689 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000690 return IMPDecl;
691}
692
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000693void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
694 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000695 SourceLocation RBrace) {
696 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000697 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000698 if (!IDecl)
699 return;
700 /// Check case of non-existing @interface decl.
701 /// (legacy objective-c @implementation decl without an @interface decl).
702 /// Add implementations's ivar to the synthesize class's ivar list.
703 if (IDecl->ImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000704 IDecl->setIVarList(ivars, numIvars, Context);
705 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000706 return;
707 }
708 // If implementation has empty ivar list, just return.
709 if (numIvars == 0)
710 return;
711
712 assert(ivars && "missing @implementation ivars");
713
714 // Check interface's Ivar list against those in the implementation.
715 // names and types must match.
716 //
Chris Lattner4d391482007-12-12 07:09:47 +0000717 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000718 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000719 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
720 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000721 ObjCIvarDecl* ImplIvar = ivars[j++];
722 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000723 assert (ImplIvar && "missing implementation ivar");
724 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000725
726 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000727 if (Context.getCanonicalType(ImplIvar->getType()) !=
728 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000729 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000730 << ImplIvar->getIdentifier()
731 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000732 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000733 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
734 Expr *ImplBitWidth = ImplIvar->getBitWidth();
735 Expr *ClsBitWidth = ClsIvar->getBitWidth();
736 if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() !=
737 ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) {
738 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
739 << ImplIvar->getIdentifier();
740 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
741 }
742 }
743 // Make sure the names are identical.
744 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000745 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000746 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000747 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000748 }
749 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000750 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000751
752 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000753 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000754 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000755 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000756}
757
Steve Naroff3c2eb662008-02-10 21:38:56 +0000758void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
759 bool &IncompleteImpl) {
760 if (!IncompleteImpl) {
761 Diag(ImpLoc, diag::warn_incomplete_impl);
762 IncompleteImpl = true;
763 }
Chris Lattner08631c52008-11-23 21:45:46 +0000764 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000765}
766
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000767void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
768 ObjCMethodDecl *IntfMethodDecl) {
769 bool err = false;
770 QualType ImpMethodQType =
771 Context.getCanonicalType(ImpMethodDecl->getResultType());
772 QualType IntfMethodQType =
773 Context.getCanonicalType(IntfMethodDecl->getResultType());
774 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
775 err = true;
776 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
777 IF=IntfMethodDecl->param_begin(),
778 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
779 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
780 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
781 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
782 err = true;
783 break;
784 }
785 }
786 if (err) {
787 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
788 << ImpMethodDecl->getDeclName();
789 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
790 }
791}
792
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000793/// isPropertyReadonly - Return true if property is readonly, by searching
794/// for the property in the class and in its categories and implementations
795///
796bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000797 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000798 // by far the most common case.
799 if (!PDecl->isReadOnly())
800 return false;
801 // Even if property is ready only, if interface has a user defined setter,
802 // it is not considered read only.
803 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
804 return false;
805
806 // Main class has the property as 'readonly'. Must search
807 // through the category list to see if the property's
808 // attribute has been over-ridden to 'readwrite'.
809 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
810 Category; Category = Category->getNextClassCategory()) {
811 // Even if property is ready only, if a category has a user defined setter,
812 // it is not considered read only.
813 if (Category->getInstanceMethod(PDecl->getSetterName()))
814 return false;
815 ObjCPropertyDecl *P =
816 Category->FindPropertyDeclaration(PDecl->getIdentifier());
817 if (P && !P->isReadOnly())
818 return false;
819 }
820
821 // Also, check for definition of a setter method in the implementation if
822 // all else failed.
823 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
824 if (ObjCImplementationDecl *IMD =
825 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
826 if (IMD->getInstanceMethod(PDecl->getSetterName()))
827 return false;
828 }
829 else if (ObjCCategoryImplDecl *CIMD =
830 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
831 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
832 return false;
833 }
834 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000835 // Lastly, look through the implementation (if one is in scope).
836 if (ObjCImplementationDecl *ImpDecl =
837 ObjCImplementations[IDecl->getIdentifier()])
838 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
839 return false;
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000840 return true;
841}
842
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000843/// FIXME: Type hierarchies in Objective-C can be deep. We could most
844/// likely improve the efficiency of selector lookups and type
845/// checking by associating with each protocol / interface / category
846/// the flattened instance tables. If we used an immutable set to keep
847/// the table then it wouldn't add significant memory cost and it
848/// would be handy for lookups.
849
Steve Naroffefe7f362008-02-08 22:06:17 +0000850/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000851/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000852void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
853 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000854 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000855 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000856 const llvm::DenseSet<Selector> &ClsMap,
857 ObjCInterfaceDecl *IDecl) {
858 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
859
860 // If a method lookup fails locally we still need to look and see if
861 // the method was implemented by a base class or an inherited
862 // protocol. This lookup is slow, but occurs rarely in correct code
863 // and otherwise would terminate in a warning.
864
Chris Lattner4d391482007-12-12 07:09:47 +0000865 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000866 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000867 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000868 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000869 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000870 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000871 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000872 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000873 }
874 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000875 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000876 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000877 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000878 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
879 !ClsMap.count(method->getSelector()) &&
880 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000881 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000882 }
Chris Lattner780f3292008-07-21 21:32:27 +0000883 // Check on this protocols's referenced protocols, recursively.
884 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
885 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000886 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000887}
888
Chris Lattnercddc8882009-03-01 00:56:52 +0000889void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
890 ObjCContainerDecl* CDecl,
891 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000892 llvm::DenseSet<Selector> InsMap;
893 // Check and see if instance methods in class interface have been
894 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000895 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000896 E = IMPDecl->instmeth_end(); I != E; ++I)
897 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000898
Chris Lattnercddc8882009-03-01 00:56:52 +0000899 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
900 E = CDecl->instmeth_end(); I != E; ++I) {
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000901 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000902 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000903 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000904 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000905
906 ObjCMethodDecl *ImpMethodDecl =
907 IMPDecl->getInstanceMethod((*I)->getSelector());
908 ObjCMethodDecl *IntfMethodDecl =
Chris Lattnercddc8882009-03-01 00:56:52 +0000909 CDecl->getInstanceMethod((*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000910 assert(IntfMethodDecl &&
911 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
912 // ImpMethodDecl may be null as in a @dynamic property.
913 if (ImpMethodDecl)
914 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
915 }
Chris Lattner4c525092007-12-12 17:58:05 +0000916
Chris Lattner4d391482007-12-12 07:09:47 +0000917 llvm::DenseSet<Selector> ClsMap;
918 // Check and see if class methods in class interface have been
919 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000920 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000921 E = IMPDecl->classmeth_end(); I != E; ++I)
922 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000923
Chris Lattnercddc8882009-03-01 00:56:52 +0000924 for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(),
925 E = CDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000926 if (!ClsMap.count((*I)->getSelector()))
927 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000928 else {
929 ObjCMethodDecl *ImpMethodDecl =
930 IMPDecl->getClassMethod((*I)->getSelector());
931 ObjCMethodDecl *IntfMethodDecl =
Chris Lattnercddc8882009-03-01 00:56:52 +0000932 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000933 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
934 }
935
Chris Lattner4d391482007-12-12 07:09:47 +0000936
937 // Check the protocol list for unimplemented methods in the @implementation
938 // class.
Chris Lattnercddc8882009-03-01 00:56:52 +0000939 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
940 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
941 E = I->protocol_end(); PI != E; ++PI)
942 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
943 InsMap, ClsMap, I);
944 // Check class extensions (unnamed categories)
945 for (ObjCCategoryDecl *Categories = I->getCategoryList();
946 Categories; Categories = Categories->getNextClassCategory()) {
947 if (!Categories->getIdentifier()) {
948 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
949 break;
950 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000951 }
Chris Lattnercddc8882009-03-01 00:56:52 +0000952 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
953 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
954 E = C->protocol_end(); PI != E; ++PI)
955 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
956 InsMap, ClsMap, C->getClassInterface());
957 } else
958 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +0000959}
960
961/// ActOnForwardClassDeclaration -
962Action::DeclTy *
963Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000964 IdentifierInfo **IdentList,
965 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000966 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000967
968 for (unsigned i = 0; i != NumElts; ++i) {
969 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000970 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000971 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000972 // Maybe we will complain about the shadowed template parameter.
973 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
974 // Just pretend that we didn't see the previous declaration.
975 PrevDecl = 0;
976 }
977
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000978 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000979 // GCC apparently allows the following idiom:
980 //
981 // typedef NSObject < XCElementTogglerP > XCElementToggler;
982 // @class XCElementToggler;
983 //
984 // FIXME: Make an extension?
985 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
986 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000987 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000988 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000989 }
Chris Lattner4d391482007-12-12 07:09:47 +0000990 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000991 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000992 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000993 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
994 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000995 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000996
Douglas Gregord0434102009-01-09 00:49:46 +0000997 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000998 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000999 // Remember that this needs to be removed when the scope is popped.
1000 TUScope->AddDecl(IDecl);
1001 }
1002
1003 Interfaces.push_back(IDecl);
1004 }
1005
Douglas Gregord0434102009-01-09 00:49:46 +00001006 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001007 &Interfaces[0],
1008 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +00001009 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001010 CheckObjCDeclScope(CDecl);
1011 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001012}
1013
1014
1015/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1016/// returns true, or false, accordingly.
1017/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001018bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001019 const ObjCMethodDecl *PrevMethod,
1020 bool matchBasedOnSizeAndAlignment) {
1021 QualType T1 = Context.getCanonicalType(Method->getResultType());
1022 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1023
1024 if (T1 != T2) {
1025 // The result types are different.
1026 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001027 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001028 // Incomplete types don't have a size and alignment.
1029 if (T1->isIncompleteType() || T2->isIncompleteType())
1030 return false;
1031 // Check is based on size and alignment.
1032 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1033 return false;
1034 }
Chris Lattner89951a82009-02-20 18:43:26 +00001035
1036 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1037 E = Method->param_end();
1038 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1039
1040 for (; ParamI != E; ++ParamI, ++PrevI) {
1041 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1042 T1 = Context.getCanonicalType((*ParamI)->getType());
1043 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001044 if (T1 != T2) {
1045 // The result types are different.
1046 if (!matchBasedOnSizeAndAlignment)
1047 return false;
1048 // Incomplete types don't have a size and alignment.
1049 if (T1->isIncompleteType() || T2->isIncompleteType())
1050 return false;
1051 // Check is based on size and alignment.
1052 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1053 return false;
1054 }
Chris Lattner4d391482007-12-12 07:09:47 +00001055 }
1056 return true;
1057}
1058
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001059void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Chris Lattnerb25df352009-03-04 05:16:45 +00001060 ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()];
1061 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001062 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001063 Entry.Method = Method;
1064 Entry.Next = 0;
1065 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001066 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001067
1068 // We've seen a method with this name, see if we have already seen this type
1069 // signature.
1070 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1071 if (MatchTwoMethodDeclarations(Method, List->Method))
1072 return;
1073
1074 // We have a new signature for an existing method - add it.
1075 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1076 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001077}
1078
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001079// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001080ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1081 SourceRange R) {
1082 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001083 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001084
1085 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001086 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1087 // This checks if the methods differ by size & alignment.
1088 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1089 issueWarning = true;
1090 }
1091 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001092 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001093 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001094 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001095 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001096 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001097 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001098 }
1099 return MethList.Method;
1100}
1101
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001102void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1103 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001104 if (!FirstMethod.Method) {
1105 // Haven't seen a method with this selector name yet - add it.
1106 FirstMethod.Method = Method;
1107 FirstMethod.Next = 0;
1108 } else {
1109 // We've seen a method with this name, now check the type signature(s).
1110 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1111
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001112 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001113 Next = Next->Next)
1114 match = MatchTwoMethodDeclarations(Method, Next->Method);
1115
1116 if (!match) {
1117 // We have a new signature for an existing method - add it.
1118 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001119 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001120 FirstMethod.Next = OMI;
1121 }
1122 }
1123}
1124
Steve Naroff0701bbb2009-01-08 17:28:14 +00001125/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1126/// have the property type and issue diagnostics if they don't.
1127/// Also synthesize a getter/setter method if none exist (and update the
1128/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1129/// methods is the "right" thing to do.
1130void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1131 ObjCContainerDecl *CD) {
1132 ObjCMethodDecl *GetterMethod, *SetterMethod;
1133
1134 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1135 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1136
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001137 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001138 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001139 Diag(property->getLocation(),
1140 diag::err_accessor_property_type_mismatch)
1141 << property->getDeclName()
1142 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001143 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1144 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001145
1146 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001147 if (Context.getCanonicalType(SetterMethod->getResultType())
1148 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001149 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001150 if (SetterMethod->param_size() != 1 ||
1151 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001152 Diag(property->getLocation(),
1153 diag::err_accessor_property_type_mismatch)
1154 << property->getDeclName()
1155 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001156 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1157 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001158 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001159
1160 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001161 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001162 // FIXME: The synthesized property we set here is misleading. We
1163 // almost always synthesize these methods unless the user explicitly
1164 // provided prototypes (which is odd, but allowed). Sema should be
1165 // typechecking that the declarations jive in that situation (which
1166 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001167 if (!GetterMethod) {
1168 // No instance method of same name as property getter name was found.
1169 // Declare a getter method and add it to the list of methods
1170 // for this class.
1171 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1172 property->getLocation(), property->getGetterName(),
1173 property->getType(), CD, true, false, true,
1174 (property->getPropertyImplementation() ==
1175 ObjCPropertyDecl::Optional) ?
1176 ObjCMethodDecl::Optional :
1177 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001178 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001179 } else
1180 // A user declared getter will be synthesize when @synthesize of
1181 // the property with the same name is seen in the @implementation
1182 GetterMethod->setIsSynthesized();
1183 property->setGetterMethodDecl(GetterMethod);
1184
1185 // Skip setter if property is read-only.
1186 if (!property->isReadOnly()) {
1187 // Find the default setter and if one not found, add one.
1188 if (!SetterMethod) {
1189 // No instance method of same name as property setter name was found.
1190 // Declare a setter method and add it to the list of methods
1191 // for this class.
1192 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1193 property->getLocation(),
1194 property->getSetterName(),
1195 Context.VoidTy, CD, true, false, true,
1196 (property->getPropertyImplementation() ==
1197 ObjCPropertyDecl::Optional) ?
1198 ObjCMethodDecl::Optional :
1199 ObjCMethodDecl::Required);
1200 // Invent the arguments for the setter. We don't bother making a
1201 // nice name for the argument.
1202 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1203 SourceLocation(),
1204 property->getIdentifier(),
1205 property->getType(),
1206 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001207 0);
Chris Lattner38af2de2009-02-20 21:35:13 +00001208 SetterMethod->setMethodParams(&Argument, 1, Context);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001209 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001210 } else
1211 // A user declared setter will be synthesize when @synthesize of
1212 // the property with the same name is seen in the @implementation
1213 SetterMethod->setIsSynthesized();
1214 property->setSetterMethodDecl(SetterMethod);
1215 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001216 // Add any synthesized methods to the global pool. This allows us to
1217 // handle the following, which is supported by GCC (and part of the design).
1218 //
1219 // @interface Foo
1220 // @property double bar;
1221 // @end
1222 //
1223 // void thisIsUnfortunate() {
1224 // id foo;
1225 // double bar = [foo bar];
1226 // }
1227 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001228 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001229 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001230 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001231 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001232}
1233
Steve Naroffa56f6162007-12-18 01:30:32 +00001234// Note: For class/category implemenations, allMethods/allProperties is
1235// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001236void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1237 DeclTy **allMethods, unsigned allNum,
1238 DeclTy **allProperties, unsigned pNum) {
1239 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1240
Steve Naroffa56f6162007-12-18 01:30:32 +00001241 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1242 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001243 // should be true.
1244 if (!ClassDecl)
1245 return;
1246
Chris Lattner4d391482007-12-12 07:09:47 +00001247 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001248 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1249 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001250 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001251
Steve Naroff0701bbb2009-01-08 17:28:14 +00001252 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001253
1254 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1255 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1256 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1257
Chris Lattner4d391482007-12-12 07:09:47 +00001258 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001259 ObjCMethodDecl *Method =
1260 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001261
1262 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001263 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001264 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001265 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001266 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1267 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001268 if ((isInterfaceDeclKind && PrevMethod && !match)
1269 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001270 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001271 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001272 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001273 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001274 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001275 InsMap[Method->getSelector()] = Method;
1276 /// The following allows us to typecheck messages to "id".
1277 AddInstanceMethodToGlobalPool(Method);
1278 }
1279 }
1280 else {
1281 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001282 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001283 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1284 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001285 if ((isInterfaceDeclKind && PrevMethod && !match)
1286 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001287 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001288 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001289 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001290 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001291 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001292 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001293 /// The following allows us to typecheck messages to "Class".
1294 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001295 }
1296 }
1297 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001298 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001299 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001300 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001301 ComparePropertiesInBaseAndSuper(I);
1302 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001303 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001304 // Categories are used to extend the class by declaring new methods.
1305 // By the same token, they are also used to add new properties. No
1306 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001307
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001308 // Merge protocol properties into category
1309 MergeProtocolPropertiesIntoClass(C, C);
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001310 if (C->getIdentifier() == 0)
1311 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001312 }
Steve Naroff09c47192009-01-09 15:36:25 +00001313 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1314 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1315 // user-defined setter/getter. It also synthesizes setter/getter methods
1316 // and adds them to the DeclContext and global method pools.
Chris Lattner97a58872009-02-16 18:32:47 +00001317 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1318 E = CDecl->prop_end(); I != E; ++I)
1319 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001320 CDecl->setAtEndLoc(AtEndLoc);
1321 }
1322 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001323 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001324 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001325 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001326 } else if (ObjCCategoryImplDecl* CatImplClass =
1327 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001328 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001329
Chris Lattner4d391482007-12-12 07:09:47 +00001330 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001331 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001332 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001333 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001334 Categories; Categories = Categories->getNextClassCategory()) {
1335 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001336 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001337 break;
1338 }
1339 }
1340 }
1341 }
1342}
1343
1344
1345/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1346/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001347static Decl::ObjCDeclQualifier
1348CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1349 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1350 if (PQTVal & ObjCDeclSpec::DQ_In)
1351 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1352 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1353 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1354 if (PQTVal & ObjCDeclSpec::DQ_Out)
1355 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1356 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1357 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1358 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1359 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1360 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1361 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001362
1363 return ret;
1364}
1365
1366Sema::DeclTy *Sema::ActOnMethodDeclaration(
1367 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001368 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001369 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001370 Selector Sel,
1371 // optional arguments. The number of types/arguments is obtained
1372 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001373 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001374 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001375 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1376 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001377 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001378
1379 // Make sure we can establish a context for the method.
1380 if (!ClassDecl) {
1381 Diag(MethodLoc, diag::error_missing_method_context);
1382 return 0;
1383 }
Chris Lattner4d391482007-12-12 07:09:47 +00001384 QualType resultDeclType;
1385
Steve Naroffccef3712009-02-20 22:59:16 +00001386 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001387 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001388
1389 // Methods cannot return interface types. All ObjC objects are
1390 // passed by reference.
1391 if (resultDeclType->isObjCInterfaceType()) {
1392 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1393 << "returned";
1394 return 0;
1395 }
1396 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001397 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001398
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001399 ObjCMethodDecl* ObjCMethod =
1400 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001401 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001402 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001403 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001404 MethodDeclKind == tok::objc_optional ?
1405 ObjCMethodDecl::Optional :
1406 ObjCMethodDecl::Required);
1407
Chris Lattner0ed844b2008-04-04 06:12:32 +00001408 llvm::SmallVector<ParmVarDecl*, 16> Params;
1409
1410 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1411 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001412 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001413
Steve Naroff6082c622008-12-09 19:36:17 +00001414 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001415 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001416 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001417 if (argType->isArrayType()) { // (char *[]) -> (char **)
1418 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001419 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001420 }
Steve Naroff6082c622008-12-09 19:36:17 +00001421 else if (argType->isFunctionType())
1422 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001423 else if (argType->isObjCInterfaceType()) {
1424 // FIXME! provide more precise location for the parameter
Steve Naroffccef3712009-02-20 22:59:16 +00001425 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1426 << "passed";
1427 ObjCMethod->setInvalidDecl();
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001428 return 0;
1429 }
Steve Naroff6082c622008-12-09 19:36:17 +00001430 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001431 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001432 ParmVarDecl* Param;
1433 if (originalArgType.isNull())
1434 Param = ParmVarDecl::Create(Context, ObjCMethod,
1435 SourceLocation(/*FIXME*/),
1436 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001437 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001438 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001439 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1440 SourceLocation(/*FIXME*/),
1441 ArgNames[i], argType, originalArgType,
1442 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001443
Chris Lattner0ed844b2008-04-04 06:12:32 +00001444 Param->setObjCDeclQualifier(
1445 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1446 Params.push_back(Param);
1447 }
1448
Chris Lattner38af2de2009-02-20 21:35:13 +00001449 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001450 ObjCMethod->setObjCDeclQualifier(
1451 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1452 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001453
1454 if (AttrList)
1455 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001456
1457 // For implementations (which can be very "coarse grain"), we add the
1458 // method now. This allows the AST to implement lookup methods that work
1459 // incrementally (without waiting until we parse the @end). It also allows
1460 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001461 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001462 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001463 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001464 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001465 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001466 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001467 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001468 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001469 }
1470 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001471 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001472 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001473 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001474 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001475 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001476 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001477 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001478 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001479 }
1480 }
1481 if (PrevMethod) {
1482 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001483 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001484 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001485 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001486 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001487 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001488}
1489
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001490void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1491 SourceLocation Loc,
1492 unsigned &Attributes) {
1493 // FIXME: Improve the reported location.
1494
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001495 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001496 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001497 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1498 ObjCDeclSpec::DQ_PR_assign |
1499 ObjCDeclSpec::DQ_PR_copy |
1500 ObjCDeclSpec::DQ_PR_retain))) {
1501 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1502 "readwrite" :
1503 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1504 "assign" :
1505 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1506 "copy" : "retain";
1507
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001508 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001509 diag::err_objc_property_attr_mutually_exclusive :
1510 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001511 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001512 }
1513
1514 // Check for copy or retain on non-object types.
1515 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1516 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001517 Diag(Loc, diag::err_objc_property_requires_object)
1518 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001519 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1520 }
1521
1522 // Check for more than one of { assign, copy, retain }.
1523 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1524 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001525 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1526 << "assign" << "copy";
1527 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001528 }
1529 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001530 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1531 << "assign" << "retain";
1532 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001533 }
1534 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1535 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001536 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1537 << "copy" << "retain";
1538 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001539 }
1540 }
1541
1542 // Warn if user supplied no assignment attribute, property is
1543 // readwrite, and this is an object type.
1544 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1545 ObjCDeclSpec::DQ_PR_retain)) &&
1546 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1547 Context.isObjCObjectPointerType(PropertyTy)) {
1548 // Skip this warning in gc-only mode.
1549 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1550 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1551
1552 // If non-gc code warn that this is likely inappropriate.
1553 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1554 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1555
1556 // FIXME: Implement warning dependent on NSCopying being
1557 // implemented. See also:
1558 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1559 // (please trim this list while you are at it).
1560 }
1561}
1562
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001563Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1564 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001565 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001566 Selector GetterSel,
1567 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001568 DeclTy *ClassCategory,
1569 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001570 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001571 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001572 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1573 // default is readwrite!
1574 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1575 // property is defaulted to 'assign' if it is readwrite and is
1576 // not retain or copy
1577 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1578 (isReadWrite &&
1579 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1580 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1581 QualType T = GetTypeForDeclarator(FD.D, S);
1582 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001583
1584 // May modify Attributes.
1585 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001586
1587 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1588 if (!CDecl->getIdentifier()) {
1589 // This is an anonymous category. property requires special
1590 // handling.
1591 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1592 if (ObjCPropertyDecl *PIDecl =
1593 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1594 // property 'PIDecl's readonly attribute will be over-ridden
1595 // with anonymous category's readwrite property attribute!
1596 unsigned PIkind = PIDecl->getPropertyAttributes();
1597 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001598 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001599 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1600 Diag(AtLoc, diag::warn_property_attr_mismatch);
1601 PIDecl->makeitReadWriteAttribute();
1602 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1603 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1604 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1605 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1606 PIDecl->setSetterName(SetterSel);
1607 // FIXME: use a common routine with addPropertyMethods.
1608 ObjCMethodDecl *SetterDecl =
1609 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1610 Context.VoidTy,
1611 ICDecl,
1612 true, false, true,
1613 ObjCMethodDecl::Required);
Chris Lattner38af2de2009-02-20 21:35:13 +00001614 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001615 SourceLocation(),
1616 FD.D.getIdentifier(),
Chris Lattner38af2de2009-02-20 21:35:13 +00001617 T, VarDecl::None, 0);
1618 SetterDecl->setMethodParams(&Argument, 1, Context);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001619 PIDecl->setSetterMethodDecl(SetterDecl);
1620 }
1621 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001622 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001623 *isOverridingProperty = true;
1624 return 0;
1625 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001626 // No matching property found in the main class. Just fall thru
1627 // and add property to the anonymous category. It looks like
Ben Laurie9af5e672009-02-16 09:18:41 +00001628 // it works as is. This category becomes just like a category
1629 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001630 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001631 Diag(CDecl->getLocation(), diag::err_continuation_class);
1632 *isOverridingProperty = true;
1633 return 0;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001634 }
1635 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001636
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001637 Type *t = T.getTypePtr();
1638 if (t->isArrayType() || t->isFunctionType())
1639 Diag(AtLoc, diag::err_property_type) << T;
1640
Steve Naroff93983f82009-01-11 12:47:58 +00001641 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1642 assert(DC && "ClassDecl is not a DeclContext");
1643 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001644 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001645 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001646
1647 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001648
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001649 // Regardless of setter/getter attribute, we save the default getter/setter
1650 // selector names in anticipation of declaration of setter/getter methods.
1651 PDecl->setGetterName(GetterSel);
1652 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001653
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001654 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001655 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001656
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001657 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001658 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001659
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001660 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001661 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001662
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001663 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001664 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001665
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001666 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001667 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001668
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001669 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001670 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001671
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001672 if (isAssign)
1673 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1674
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001675 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001676 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001677
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001678 if (MethodImplKind == tok::objc_required)
1679 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1680 else if (MethodImplKind == tok::objc_optional)
1681 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1682
Chris Lattner4d391482007-12-12 07:09:47 +00001683 return PDecl;
1684}
1685
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001686/// ActOnPropertyImplDecl - This routine performs semantic checks and
1687/// builds the AST node for a property implementation declaration; declared
1688/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001689///
1690Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1691 SourceLocation PropertyLoc,
1692 bool Synthesize,
1693 DeclTy *ClassCatImpDecl,
1694 IdentifierInfo *PropertyId,
1695 IdentifierInfo *PropertyIvar) {
1696 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1697 // Make sure we have a context for the property implementation declaration.
1698 if (!ClassImpDecl) {
1699 Diag(AtLoc, diag::error_missing_property_context);
1700 return 0;
1701 }
1702 ObjCPropertyDecl *property = 0;
1703 ObjCInterfaceDecl* IDecl = 0;
1704 // Find the class or category class where this property must have
1705 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001706 ObjCImplementationDecl *IC = 0;
1707 ObjCCategoryImplDecl* CatImplClass = 0;
1708 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001709 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001710 // We always synthesize an interface for an implementation
1711 // without an interface decl. So, IDecl is always non-zero.
1712 assert(IDecl &&
1713 "ActOnPropertyImplDecl - @implementation without @interface");
1714
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001715 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001716 property = IDecl->FindPropertyDeclaration(PropertyId);
1717 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001718 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001719 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001720 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001721 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001722 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001723 if (Synthesize) {
1724 Diag(AtLoc, diag::error_synthesize_category_decl);
1725 return 0;
1726 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001727 IDecl = CatImplClass->getClassInterface();
1728 if (!IDecl) {
1729 Diag(AtLoc, diag::error_missing_property_interface);
1730 return 0;
1731 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001732 ObjCCategoryDecl *Category =
1733 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1734
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001735 // If category for this implementation not found, it is an error which
1736 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001737 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001738 return 0;
1739 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001740 property = Category->FindPropertyDeclaration(PropertyId);
1741 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001742 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001743 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001744 return 0;
1745 }
1746 }
1747 else {
1748 Diag(AtLoc, diag::error_bad_property_context);
1749 return 0;
1750 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001751 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001752 // Check that we have a valid, previously declared ivar for @synthesize
1753 if (Synthesize) {
1754 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001755 if (!PropertyIvar)
1756 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001757 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniana5afdd02009-02-16 19:35:27 +00001758 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001759 if (!Ivar) {
Steve Narofff4c00ff2009-03-03 22:09:41 +00001760 if (!getLangOptions().ObjCNonFragileABI)
1761 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001762 return 0;
1763 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001764 QualType PropType = Context.getCanonicalType(property->getType());
1765 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1766
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001767 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001768 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001769 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001770 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001771 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001772 return 0;
1773 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001774 else {
1775 // FIXME! Rules for properties are somewhat different that those
1776 // for assignments. Use a new routine to consolidate all cases;
1777 // specifically for property redeclarations as well as for ivars.
1778 QualType lhsType =
1779 Context.getCanonicalType(PropType).getUnqualifiedType();
1780 QualType rhsType =
1781 Context.getCanonicalType(IvarType).getUnqualifiedType();
1782 if (lhsType != rhsType &&
1783 lhsType->isArithmeticType()) {
1784 Diag(PropertyLoc, diag::error_property_ivar_type)
1785 << property->getDeclName() << Ivar->getDeclName();
1786 return 0;
1787 }
Fariborz Jahanian9bc77b22009-02-27 22:38:11 +00001788 // __weak is explicit. So it works on Canonical type.
1789 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak()) {
1790 Diag(PropertyLoc, diag::error_weak_property)
1791 << property->getDeclName() << Ivar->getDeclName();
1792 return 0;
1793 }
1794 if ((Context.isObjCObjectPointerType(property->getType()) ||
1795 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak()) {
1796 Diag(PropertyLoc, diag::error_strong_property)
1797 << property->getDeclName() << Ivar->getDeclName();
1798 return 0;
1799 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001800 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001801 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001802 } else if (PropertyIvar) {
1803 // @dynamic
1804 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1805 return 0;
1806 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001807 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001808 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001809 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1810 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001811 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001812 ObjCPropertyImplDecl::Synthesize
1813 : ObjCPropertyImplDecl::Dynamic),
1814 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001815 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001816 if (IC) {
1817 if (Synthesize)
1818 if (ObjCPropertyImplDecl *PPIDecl =
1819 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1820 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1821 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1822 << PropertyIvar;
1823 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1824 }
1825
1826 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1827 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1828 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1829 return 0;
1830 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001831 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001832 }
1833 else {
1834 if (Synthesize)
1835 if (ObjCPropertyImplDecl *PPIDecl =
1836 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1837 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1838 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1839 << PropertyIvar;
1840 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1841 }
1842
1843 if (ObjCPropertyImplDecl *PPIDecl =
1844 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1845 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1846 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1847 return 0;
1848 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001849 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001850 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001851
1852 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001853}
Anders Carlsson15281452008-11-04 16:57:32 +00001854
Chris Lattnercc98eac2008-12-17 07:13:27 +00001855bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001856 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001857 return false;
1858
1859 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1860 D->setInvalidDecl();
1861
1862 return true;
1863}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001864
1865/// Collect the instance variables declared in an Objective-C object. Used in
1866/// the creation of structures from objects using the @defs directive.
1867/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1868/// part of the AST generation logic of @defs.
1869static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1870 ASTContext& Ctx,
1871 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1872 if (Class->getSuperClass())
1873 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1874
1875 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1876 for (ObjCInterfaceDecl::ivar_iterator
1877 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1878
1879 ObjCIvarDecl* ID = *I;
1880 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1881 ID->getLocation(),
1882 ID->getIdentifier(),
1883 ID->getType(),
1884 ID->getBitWidth()));
1885 }
1886}
1887
1888/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1889/// instance variables of ClassName into Decls.
1890void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1891 IdentifierInfo *ClassName,
1892 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1893 // Check that ClassName is a valid class
1894 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1895 if (!Class) {
1896 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1897 return;
1898 }
1899 // Collect the instance variables
1900 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1901
1902 // Introduce all of these fields into the appropriate scope.
1903 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1904 D != Decls.end(); ++D) {
1905 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1906 if (getLangOptions().CPlusPlus)
1907 PushOnScopeChains(cast<FieldDecl>(FD), S);
1908 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001909 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001910 }
1911}
1912