blob: da50573f23bff4580658239eacce0f903c421763 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000015#include "clang/Sema/ExternalSemaSource.h"
Steve Naroffca331292009-03-03 14:49:36 +000016#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000020using namespace clang;
21
Steve Naroffebf64432009-02-28 16:59:13 +000022/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000023/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000024void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000025 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000026 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff394f3f42008-07-25 17:57:26 +000027
28 // If we don't have a valid method decl, simply return.
29 if (!MDecl)
30 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000031
Chris Lattner38c5ebd2009-04-19 05:21:20 +000032 CurFunctionNeedsScopeChecking = false;
33
Steve Naroffa56f6162007-12-18 01:30:32 +000034 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000035 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000036 AddInstanceMethodToGlobalPool(MDecl);
37 else
38 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000039
40 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000041 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000042
43 // Create Decl objects for each parameter, entrring them in the scope for
44 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000045
46 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000047 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000048
Daniel Dunbar451318c2008-08-26 06:07:48 +000049 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
50 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000051
Chris Lattner8123a952008-04-10 02:22:51 +000052 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000053 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
54 E = MDecl->param_end(); PI != E; ++PI)
55 if ((*PI)->getIdentifier())
56 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000057}
58
Chris Lattnerb28317a2009-03-28 19:18:32 +000059Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000060ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000063 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000064 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000065 assert(ClassName && "Missing class identifier");
66
67 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000068 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000069 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000070 // Maybe we will complain about the shadowed template parameter.
71 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
72 // Just pretend that we didn't see the previous declaration.
73 PrevDecl = 0;
74 }
75
Ted Kremeneka526c5c2008-01-07 19:49:32 +000076 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000077 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000078 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000079 }
80
Ted Kremeneka526c5c2008-01-07 19:49:32 +000081 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000082 if (IDecl) {
83 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000084 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +000085 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000086 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000087 Diag(IDecl->getLocation(), diag::note_previous_definition);
88
Steve Naroffcfe8bf32008-11-18 19:15:30 +000089 // Return the previous class interface.
90 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +000091 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +000092 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000093 IDecl->setLocation(AtInterfaceLoc);
94 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000095 }
Chris Lattnerb752f282008-07-21 07:06:49 +000096 } else {
Douglas Gregord0434102009-01-09 00:49:46 +000097 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000098 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000099 if (AttrList)
100 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000101
Steve Naroffa7503a72009-04-23 15:15:40 +0000102 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000103 }
104
105 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000106 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000107 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000108
Steve Naroff818cb9e2009-02-04 17:14:05 +0000109 ObjCInterfaceDecl *SuperClassDecl =
110 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000111
112 // Diagnose classes that inherit from deprecated classes.
113 if (SuperClassDecl)
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000114 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000115
Steve Naroff818cb9e2009-02-04 17:14:05 +0000116 if (PrevDecl && SuperClassDecl == 0) {
117 // The previous declaration was not a class decl. Check if we have a
118 // typedef. If we do, get the underlying class type.
119 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
120 QualType T = TDecl->getUnderlyingType();
121 if (T->isObjCInterfaceType()) {
122 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
123 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
124 }
125 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000126
Steve Naroff818cb9e2009-02-04 17:14:05 +0000127 // This handles the following case:
128 //
129 // typedef int SuperClass;
130 // @interface MyClass : SuperClass {} @end
131 //
132 if (!SuperClassDecl) {
133 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
134 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
135 }
136 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000137
Steve Naroff818cb9e2009-02-04 17:14:05 +0000138 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
139 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000140 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000141 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000142 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000143 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000144 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000145 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000146 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000147 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000148 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000149 IDecl->setLocEnd(SuperLoc);
150 } else { // we have a root class.
151 IDecl->setLocEnd(ClassLoc);
152 }
153
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000154 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000155 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000156 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
157 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000158 IDecl->setLocEnd(EndProtoLoc);
159 }
Anders Carlsson15281452008-11-04 16:57:32 +0000160
161 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000162 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000163}
164
165/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000166/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000167Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
168 IdentifierInfo *AliasName,
169 SourceLocation AliasLocation,
170 IdentifierInfo *ClassName,
171 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000172 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000173 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000174 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000175 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000176 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000177 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000178 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000179 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000180 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000181 }
182 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000183 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000184 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
185 QualType T = TDecl->getUnderlyingType();
186 if (T->isObjCInterfaceType()) {
187 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
188 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000189 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000190 }
191 }
192 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000193 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
194 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000195 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000196 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000197 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000198 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000199 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000200
201 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000202 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000203 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000204
Anders Carlsson15281452008-11-04 16:57:32 +0000205 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000206 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000207
Chris Lattnerb28317a2009-03-28 19:18:32 +0000208 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000209}
210
Steve Naroff61d68522009-03-05 15:22:01 +0000211void Sema::CheckForwardProtocolDeclarationForCircularDependency(
212 IdentifierInfo *PName,
213 SourceLocation &Ploc, SourceLocation PrevLoc,
214 const ObjCList<ObjCProtocolDecl> &PList)
215{
216 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
217 E = PList.end(); I != E; ++I) {
218
Douglas Gregor6e378de2009-04-23 23:18:26 +0000219 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000220 if (PDecl->getIdentifier() == PName) {
221 Diag(Ploc, diag::err_protocol_has_circular_dependency);
222 Diag(PrevLoc, diag::note_previous_definition);
223 }
224 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
225 PDecl->getLocation(), PDecl->getReferencedProtocols());
226 }
227 }
228}
229
Chris Lattnerb28317a2009-03-28 19:18:32 +0000230Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000231Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
232 IdentifierInfo *ProtocolName,
233 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000234 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000235 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000236 SourceLocation EndProtoLoc,
237 AttributeList *AttrList) {
238 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000239 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000240 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000241 if (PDecl) {
242 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000243 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000244 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000245 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000246 // Just return the protocol we already had.
247 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000248 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000249 }
Steve Naroff61d68522009-03-05 15:22:01 +0000250 ObjCList<ObjCProtocolDecl> PList;
251 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
252 CheckForwardProtocolDeclarationForCircularDependency(
253 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
254 PList.Destroy(Context);
255
Steve Narofff11b5082008-08-13 16:39:22 +0000256 // Make sure the cached decl gets a valid start location.
257 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000258 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000259 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000260 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
261 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000262 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000263 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000264 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000265 if (AttrList)
266 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000267 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000268 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000269 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000270 PDecl->setLocEnd(EndProtoLoc);
271 }
Anders Carlsson15281452008-11-04 16:57:32 +0000272
273 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000274 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000275}
276
277/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000278/// issues an error if they are not declared. It returns list of
279/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000280void
Chris Lattnere13b9592008-07-26 04:03:38 +0000281Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000282 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000283 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000284 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000285 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000286 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000287 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000288 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000289 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000290 continue;
291 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000292
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000293 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000294
295 // If this is a forward declaration and we are supposed to warn in this
296 // case, do it.
297 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000298 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000299 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000300 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000301 }
302}
303
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000304/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000305/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000306///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000307void
308Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
309 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000310 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000311 ObjCPropertyDecl::PropertyAttributeKind CAttr =
312 Property->getPropertyAttributes();
313 ObjCPropertyDecl::PropertyAttributeKind SAttr =
314 SuperProperty->getPropertyAttributes();
315 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
316 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000317 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000318 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000319 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
320 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000321 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000322 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000323 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
324 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000325 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000326 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000327
328 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
329 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000330 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000331 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000332 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000333 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000334 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000335 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000336 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000337 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000338
339 QualType LHSType =
340 Context.getCanonicalType(SuperProperty->getType());
341 QualType RHSType =
342 Context.getCanonicalType(Property->getType());
343
344 if (!Context.typesAreCompatible(LHSType, RHSType)) {
345 // FIXME: Incorporate this test with typesAreCompatible.
346 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
347 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
348 return;
349 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
350 << Property->getType() << SuperProperty->getType() << inheritedName;
351 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000352}
353
354/// ComparePropertiesInBaseAndSuper - This routine compares property
355/// declarations in base and its super class, if any, and issues
356/// diagnostics in a variety of inconsistant situations.
357///
Chris Lattner70f19542009-02-16 21:26:43 +0000358void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000359 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
360 if (!SDecl)
361 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000362 // FIXME: O(N^2)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000363 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
364 E = SDecl->prop_end(Context); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000365 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000366 // Does property in super class has declaration in current class?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000367 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
368 E = IDecl->prop_end(Context); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000369 ObjCPropertyDecl *PDecl = (*I);
370 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000371 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000372 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000373 }
374 }
375}
376
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000377/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
378/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000379/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000380void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000381Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000382 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000383 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
384 if (!IDecl) {
385 // Category
386 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
387 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Douglas Gregor6ab35242009-04-09 21:40:53 +0000388 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
389 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000390 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000391 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000392 // Is this property already in category's list of properties?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000393 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000394 CP != CE; ++CP)
395 if ((*CP)->getIdentifier() == Pr->getIdentifier())
396 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000397 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000398 // Property protocol already exist in class. Diagnose any mismatch.
399 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
400 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000401 return;
402 }
Douglas Gregor6ab35242009-04-09 21:40:53 +0000403 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
404 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000405 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000406 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000407 // Is this property already in class's list of properties?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000408 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000409 CP != CE; ++CP)
410 if ((*CP)->getIdentifier() == Pr->getIdentifier())
411 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000412 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000413 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000414 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000415 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000416}
417
418/// MergeProtocolPropertiesIntoClass - This routine merges properties
419/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000420/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000421///
Chris Lattner70f19542009-02-16 21:26:43 +0000422void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000423 DeclPtrTy MergeItsProtocols) {
424 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000425 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
426
427 if (!IDecl) {
428 // Category
429 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
430 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
431 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
432 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
433 E = MDecl->protocol_end(); P != E; ++P)
434 // Merge properties of category (*P) into IDECL's
435 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
436
437 // Go thru the list of protocols for this category and recursively merge
438 // their properties into this class as well.
439 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
440 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000441 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000442 } else {
443 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
444 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
445 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000446 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000447 }
448 return;
449 }
450
Chris Lattnerb752f282008-07-21 07:06:49 +0000451 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000452 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
453 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000454 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000455 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
456
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000457 // Go thru the list of protocols for this class and recursively merge
458 // their properties into this class as well.
459 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
460 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000461 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000462 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000463 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
464 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
465 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000466 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000467 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000468}
469
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000470/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000471/// a class method in its extension.
472///
473void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
474 ObjCInterfaceDecl *ID) {
475 if (!ID)
476 return; // Possibly due to previous error
477
478 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000479 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
480 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000481 ObjCMethodDecl *MD = *i;
482 MethodMap[MD->getSelector()] = MD;
483 }
484
485 if (MethodMap.empty())
486 return;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000487 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
488 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000489 ObjCMethodDecl *Method = *i;
490 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
491 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
492 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
493 << Method->getDeclName();
494 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
495 }
496 }
497}
498
Chris Lattner58fe03b2009-04-12 08:43:13 +0000499/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000500Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000501Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000502 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000503 unsigned NumElts,
504 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000505 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000506
507 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000508 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000509 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000510 if (PDecl == 0) { // Not already seen?
511 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
512 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000513 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000514 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000515 if (attrList)
516 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000517 Protocols.push_back(PDecl);
518 }
Anders Carlsson15281452008-11-04 16:57:32 +0000519
520 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000521 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000522 &Protocols[0], Protocols.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000523 CurContext->addDecl(Context, PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000524 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000525 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000526}
527
Chris Lattnerb28317a2009-03-28 19:18:32 +0000528Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000529ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
530 IdentifierInfo *ClassName, SourceLocation ClassLoc,
531 IdentifierInfo *CategoryName,
532 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000533 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000534 unsigned NumProtoRefs,
535 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000536 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000537 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
538 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000539 CurContext->addDecl(Context, CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000540
541 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000542 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000543 if (!IDecl || IDecl->isForwardDecl()) {
544 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000545 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000546 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000547 }
Chris Lattner4d391482007-12-12 07:09:47 +0000548
Chris Lattner70f19542009-02-16 21:26:43 +0000549 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000550
551 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000552 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000553
554 /// Check for duplicate interface declaration for this category
555 ObjCCategoryDecl *CDeclChain;
556 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
557 CDeclChain = CDeclChain->getNextClassCategory()) {
558 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
559 Diag(CategoryLoc, diag::warn_dup_category_def)
560 << ClassName << CategoryName;
561 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
562 break;
563 }
564 }
565 if (!CDeclChain)
566 CDecl->insertNextClassCategory();
567
Chris Lattner4d391482007-12-12 07:09:47 +0000568 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000569 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000570 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000571 }
Anders Carlsson15281452008-11-04 16:57:32 +0000572
573 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000574 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000575}
576
577/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000578/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000579/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000580Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000581 SourceLocation AtCatImplLoc,
582 IdentifierInfo *ClassName, SourceLocation ClassLoc,
583 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000584 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000585 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000586 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
587 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000588 /// Check that class of this category is already completely declared.
589 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000590 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000591
Douglas Gregord0434102009-01-09 00:49:46 +0000592 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000593 CurContext->addDecl(Context, CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000594
Chris Lattner4d391482007-12-12 07:09:47 +0000595 /// TODO: Check that CatName, category name, is not used in another
596 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000597 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000598
599 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000600 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000601}
602
Chris Lattnerb28317a2009-03-28 19:18:32 +0000603Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000604 SourceLocation AtClassImplLoc,
605 IdentifierInfo *ClassName, SourceLocation ClassLoc,
606 IdentifierInfo *SuperClassname,
607 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000608 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000609 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000610 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000611 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000612 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000613 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000614 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000615 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000616 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000617 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000618 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000619 IDecl = 0;
620 }
Chris Lattner4d391482007-12-12 07:09:47 +0000621 }
622
623 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000624 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000625 if (SuperClassname) {
626 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000627 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000628 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000629 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
630 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000631 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000632 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000633 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000634 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000635 Diag(SuperClassLoc, diag::err_undef_superclass)
636 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000637 else if (IDecl && IDecl->getSuperClass() != SDecl) {
638 // This implementation and its interface do not have the same
639 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000640 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000641 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000642 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000643 }
644 }
645 }
646
647 if (!IDecl) {
648 // Legacy case of @implementation with no corresponding @interface.
649 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000650
651 // FIXME: Do we support attributes on the @implementation? If so
652 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000653 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
654 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000655 IDecl->setSuperClass(SDecl);
656 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000657
658 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000659 } else {
660 // Mark the interface as being completed, even if it was just as
661 // @class ....;
662 // declaration; the user cannot reopen it.
663 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000664 }
665
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000666 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000667 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000668 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000669
Anders Carlsson15281452008-11-04 16:57:32 +0000670 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000671 return DeclPtrTy::make(IMPDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000672
Chris Lattner4d391482007-12-12 07:09:47 +0000673 // Check that there is no duplicate implementation of this class.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000674 if (LookupObjCImplementation(ClassName))
Chris Lattner75c9cae2008-03-16 20:53:07 +0000675 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000676 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000677 else // add it to the list.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000678 PushOnScopeChains(IMPDecl, TUScope);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000679 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000680}
681
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000682void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
683 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000684 SourceLocation RBrace) {
685 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000686 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000687 if (!IDecl)
688 return;
689 /// Check case of non-existing @interface decl.
690 /// (legacy objective-c @implementation decl without an @interface decl).
691 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000692 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000693 IDecl->setIVarList(ivars, numIvars, Context);
694 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000695 return;
696 }
697 // If implementation has empty ivar list, just return.
698 if (numIvars == 0)
699 return;
700
701 assert(ivars && "missing @implementation ivars");
702
703 // Check interface's Ivar list against those in the implementation.
704 // names and types must match.
705 //
Chris Lattner4d391482007-12-12 07:09:47 +0000706 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000707 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000708 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
709 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000710 ObjCIvarDecl* ImplIvar = ivars[j++];
711 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000712 assert (ImplIvar && "missing implementation ivar");
713 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000714
715 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000716 if (Context.getCanonicalType(ImplIvar->getType()) !=
717 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000718 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000719 << ImplIvar->getIdentifier()
720 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000721 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000722 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
723 Expr *ImplBitWidth = ImplIvar->getBitWidth();
724 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000725 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
726 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000727 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
728 << ImplIvar->getIdentifier();
729 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
730 }
731 }
732 // Make sure the names are identical.
733 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000734 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000735 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000736 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000737 }
738 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000739 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000740
741 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000742 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000743 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000744 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000745}
746
Steve Naroff3c2eb662008-02-10 21:38:56 +0000747void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
748 bool &IncompleteImpl) {
749 if (!IncompleteImpl) {
750 Diag(ImpLoc, diag::warn_incomplete_impl);
751 IncompleteImpl = true;
752 }
Chris Lattner08631c52008-11-23 21:45:46 +0000753 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000754}
755
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000756void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
757 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000758 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Chris Lattner3aff9192009-04-11 19:58:42 +0000759 ImpMethodDecl->getResultType())) {
760 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
761 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
762 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000763 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
764 }
Chris Lattner3aff9192009-04-11 19:58:42 +0000765
766 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
767 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
768 IM != EM; ++IM, ++IF) {
769 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()))
770 continue;
771
772 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
773 << ImpMethodDecl->getDeclName() << (*IF)->getType()
774 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000775 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000776 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000777}
778
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000779/// isPropertyReadonly - Return true if property is readonly, by searching
780/// for the property in the class and in its categories and implementations
781///
782bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000783 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000784 // by far the most common case.
785 if (!PDecl->isReadOnly())
786 return false;
787 // Even if property is ready only, if interface has a user defined setter,
788 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000789 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000790 return false;
791
792 // Main class has the property as 'readonly'. Must search
793 // through the category list to see if the property's
794 // attribute has been over-ridden to 'readwrite'.
795 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
796 Category; Category = Category->getNextClassCategory()) {
797 // Even if property is ready only, if a category has a user defined setter,
798 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000799 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000800 return false;
801 ObjCPropertyDecl *P =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000802 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000803 if (P && !P->isReadOnly())
804 return false;
805 }
806
807 // Also, check for definition of a setter method in the implementation if
808 // all else failed.
809 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
810 if (ObjCImplementationDecl *IMD =
811 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000812 if (IMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000813 return false;
814 }
815 else if (ObjCCategoryImplDecl *CIMD =
816 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000817 if (CIMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000818 return false;
819 }
820 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000821 // Lastly, look through the implementation (if one is in scope).
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000822 if (ObjCImplementationDecl *ImpDecl
823 = LookupObjCImplementation(IDecl->getIdentifier()))
Douglas Gregor653f1b12009-04-23 01:02:12 +0000824 if (ImpDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000825 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000826 // If all fails, look at the super class.
827 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
828 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000829 return true;
830}
831
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000832/// FIXME: Type hierarchies in Objective-C can be deep. We could most
833/// likely improve the efficiency of selector lookups and type
834/// checking by associating with each protocol / interface / category
835/// the flattened instance tables. If we used an immutable set to keep
836/// the table then it wouldn't add significant memory cost and it
837/// would be handy for lookups.
838
Steve Naroffefe7f362008-02-08 22:06:17 +0000839/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000840/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000841void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
842 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000843 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000844 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000845 const llvm::DenseSet<Selector> &ClsMap,
846 ObjCInterfaceDecl *IDecl) {
847 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
848
849 // If a method lookup fails locally we still need to look and see if
850 // the method was implemented by a base class or an inherited
851 // protocol. This lookup is slow, but occurs rarely in correct code
852 // and otherwise would terminate in a warning.
853
Chris Lattner4d391482007-12-12 07:09:47 +0000854 // check unimplemented instance methods.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000855 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
856 E = PDecl->instmeth_end(Context); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000857 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000858 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000859 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000860 (!Super ||
861 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000862 // Ugly, but necessary. Method declared in protcol might have
863 // have been synthesized due to a property declared in the class which
864 // uses the protocol.
865 ObjCMethodDecl *MethodInClass =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000866 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000867 if (!MethodInClass || !MethodInClass->isSynthesized())
868 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
869 }
Chris Lattner4d391482007-12-12 07:09:47 +0000870 }
871 // check unimplemented class methods
Douglas Gregor6ab35242009-04-09 21:40:53 +0000872 for (ObjCProtocolDecl::classmeth_iterator
873 I = PDecl->classmeth_begin(Context),
874 E = PDecl->classmeth_end(Context);
875 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000876 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000877 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
878 !ClsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000879 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000880 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000881 }
Chris Lattner780f3292008-07-21 21:32:27 +0000882 // Check on this protocols's referenced protocols, recursively.
883 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
884 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000885 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000886}
887
Chris Lattnercddc8882009-03-01 00:56:52 +0000888void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
889 ObjCContainerDecl* CDecl,
890 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000891 llvm::DenseSet<Selector> InsMap;
892 // Check and see if instance methods in class interface have been
893 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000894 for (ObjCImplementationDecl::instmeth_iterator
895 I = IMPDecl->instmeth_begin(Context),
896 E = IMPDecl->instmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000897 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000898
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000899 // Check and see if properties declared in the interface have either 1)
900 // an implementation or 2) there is a @synthesize/@dynamic implementation
901 // of the property in the @implementation.
902 if (isa<ObjCInterfaceDecl>(CDecl))
903 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context),
904 E = CDecl->prop_end(Context); P != E; ++P) {
905 ObjCPropertyDecl *Prop = (*P);
906 if (Prop->isInvalidDecl())
907 continue;
908 ObjCPropertyImplDecl *PI = 0;
909 // Is there a matching propery synthesize/dynamic?
Douglas Gregor653f1b12009-04-23 01:02:12 +0000910 for (ObjCImplDecl::propimpl_iterator
911 I = IMPDecl->propimpl_begin(Context),
912 EI = IMPDecl->propimpl_end(Context); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000913 if ((*I)->getPropertyDecl() == Prop) {
914 PI = (*I);
915 break;
916 }
917 if (PI)
918 continue;
919 if (!InsMap.count(Prop->getGetterName())) {
920 Diag(Prop->getLocation(),
921 diag::warn_setter_getter_impl_required)
922 << Prop->getDeclName() << Prop->getGetterName();
923 Diag(IMPDecl->getLocation(),
924 diag::note_property_impl_required);
925 }
926
927 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
928 Diag(Prop->getLocation(),
929 diag::warn_setter_getter_impl_required)
930 << Prop->getDeclName() << Prop->getSetterName();
931 Diag(IMPDecl->getLocation(),
932 diag::note_property_impl_required);
933 }
934 }
935
Douglas Gregor6ab35242009-04-09 21:40:53 +0000936 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
937 E = CDecl->instmeth_end(Context); I != E; ++I) {
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000938 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000939 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000940 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000941 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000942
943 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +0000944 IMPDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000945 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000946 CDecl->getInstanceMethod(Context, (*I)->getSelector());
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000947 assert(IntfMethodDecl &&
948 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
949 // ImpMethodDecl may be null as in a @dynamic property.
950 if (ImpMethodDecl)
951 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
952 }
Chris Lattner4c525092007-12-12 17:58:05 +0000953
Chris Lattner4d391482007-12-12 07:09:47 +0000954 llvm::DenseSet<Selector> ClsMap;
955 // Check and see if class methods in class interface have been
956 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000957 for (ObjCImplementationDecl::classmeth_iterator
958 I = IMPDecl->classmeth_begin(Context),
959 E = IMPDecl->classmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000960 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000961
Douglas Gregor6ab35242009-04-09 21:40:53 +0000962 for (ObjCInterfaceDecl::classmeth_iterator
963 I = CDecl->classmeth_begin(Context),
964 E = CDecl->classmeth_end(Context);
965 I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000966 if (!ClsMap.count((*I)->getSelector()))
967 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000968 else {
969 ObjCMethodDecl *ImpMethodDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +0000970 IMPDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000971 ObjCMethodDecl *IntfMethodDecl =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000972 CDecl->getClassMethod(Context, (*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000973 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
974 }
975
Chris Lattner4d391482007-12-12 07:09:47 +0000976
977 // Check the protocol list for unimplemented methods in the @implementation
978 // class.
Chris Lattnercddc8882009-03-01 00:56:52 +0000979 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
980 for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(),
981 E = I->protocol_end(); PI != E; ++PI)
982 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
983 InsMap, ClsMap, I);
984 // Check class extensions (unnamed categories)
985 for (ObjCCategoryDecl *Categories = I->getCategoryList();
986 Categories; Categories = Categories->getNextClassCategory()) {
987 if (!Categories->getIdentifier()) {
988 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
989 break;
990 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000991 }
Chris Lattnercddc8882009-03-01 00:56:52 +0000992 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
993 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
994 E = C->protocol_end(); PI != E; ++PI)
995 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
996 InsMap, ClsMap, C->getClassInterface());
997 } else
998 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +0000999}
1000
1001/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001002Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001003Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001004 IdentifierInfo **IdentList,
1005 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001006 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +00001007
1008 for (unsigned i = 0; i != NumElts; ++i) {
1009 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001010 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001011 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001012 // Maybe we will complain about the shadowed template parameter.
1013 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1014 // Just pretend that we didn't see the previous declaration.
1015 PrevDecl = 0;
1016 }
1017
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001018 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001019 // GCC apparently allows the following idiom:
1020 //
1021 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1022 // @class XCElementToggler;
1023 //
1024 // FIXME: Make an extension?
1025 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1026 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001027 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001028 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +00001029 }
Chris Lattner4d391482007-12-12 07:09:47 +00001030 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001031 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001032 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +00001033 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1034 IdentList[i], SourceLocation(), true);
Steve Naroff8f06f842009-04-23 16:00:56 +00001035 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +00001036 }
1037
1038 Interfaces.push_back(IDecl);
1039 }
1040
Douglas Gregord0434102009-01-09 00:49:46 +00001041 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001042 &Interfaces[0],
1043 Interfaces.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +00001044 CurContext->addDecl(Context, CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001045 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001046 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001047}
1048
1049
1050/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1051/// returns true, or false, accordingly.
1052/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001053bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001054 const ObjCMethodDecl *PrevMethod,
1055 bool matchBasedOnSizeAndAlignment) {
1056 QualType T1 = Context.getCanonicalType(Method->getResultType());
1057 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1058
1059 if (T1 != T2) {
1060 // The result types are different.
1061 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001062 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001063 // Incomplete types don't have a size and alignment.
1064 if (T1->isIncompleteType() || T2->isIncompleteType())
1065 return false;
1066 // Check is based on size and alignment.
1067 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1068 return false;
1069 }
Chris Lattner89951a82009-02-20 18:43:26 +00001070
1071 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1072 E = Method->param_end();
1073 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1074
1075 for (; ParamI != E; ++ParamI, ++PrevI) {
1076 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1077 T1 = Context.getCanonicalType((*ParamI)->getType());
1078 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001079 if (T1 != T2) {
1080 // The result types are different.
1081 if (!matchBasedOnSizeAndAlignment)
1082 return false;
1083 // Incomplete types don't have a size and alignment.
1084 if (T1->isIncompleteType() || T2->isIncompleteType())
1085 return false;
1086 // Check is based on size and alignment.
1087 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1088 return false;
1089 }
Chris Lattner4d391482007-12-12 07:09:47 +00001090 }
1091 return true;
1092}
1093
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001094/// \brief Read the contents of the instance and factory method pools
1095/// for a given selector from external storage.
1096///
1097/// This routine should only be called once, when neither the instance
1098/// nor the factory method pool has an entry for this selector.
1099Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
1100 bool isInstance) {
1101 assert(ExternalSource && "We need an external AST source");
1102 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1103 "Selector data already loaded into the instance method pool");
1104 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1105 "Selector data already loaded into the factory method pool");
1106
1107 // Read the method list from the external source.
1108 std::pair<ObjCMethodList, ObjCMethodList> Methods
1109 = ExternalSource->ReadMethodPool(Sel);
1110
1111 if (isInstance) {
1112 if (Methods.second.Method)
1113 FactoryMethodPool[Sel] = Methods.second;
1114 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
1115 }
1116
1117 if (Methods.first.Method)
1118 InstanceMethodPool[Sel] = Methods.first;
1119
1120 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1121}
1122
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001123void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001124 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1125 = InstanceMethodPool.find(Method->getSelector());
1126 if (Pos == InstanceMethodPool.end()) {
1127 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1128 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1129 else
1130 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1131 ObjCMethodList())).first;
1132 }
1133
1134 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001135 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001136 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001137 Entry.Method = Method;
1138 Entry.Next = 0;
1139 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001140 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001141
1142 // We've seen a method with this name, see if we have already seen this type
1143 // signature.
1144 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1145 if (MatchTwoMethodDeclarations(Method, List->Method))
1146 return;
1147
1148 // We have a new signature for an existing method - add it.
1149 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1150 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001151}
1152
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001153// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001154ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1155 SourceRange R) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001156 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1157 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001158 if (Pos == InstanceMethodPool.end()) {
1159 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001160 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1161 else
1162 return 0;
1163 }
1164
1165 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001166 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001167
1168 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001169 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1170 // This checks if the methods differ by size & alignment.
1171 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1172 issueWarning = true;
1173 }
1174 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001175 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001176 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001177 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001178 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001179 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001180 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001181 }
1182 return MethList.Method;
1183}
1184
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001185void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001186 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1187 = FactoryMethodPool.find(Method->getSelector());
1188 if (Pos == FactoryMethodPool.end()) {
1189 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1190 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1191 else
1192 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1193 ObjCMethodList())).first;
1194 }
1195
1196 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001197 if (!FirstMethod.Method) {
1198 // Haven't seen a method with this selector name yet - add it.
1199 FirstMethod.Method = Method;
1200 FirstMethod.Next = 0;
1201 } else {
1202 // We've seen a method with this name, now check the type signature(s).
1203 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1204
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001205 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001206 Next = Next->Next)
1207 match = MatchTwoMethodDeclarations(Method, Next->Method);
1208
1209 if (!match) {
1210 // We have a new signature for an existing method - add it.
1211 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001212 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001213 FirstMethod.Next = OMI;
1214 }
1215 }
1216}
1217
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001218ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
1219 SourceRange R) {
1220 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1221 = FactoryMethodPool.find(Sel);
1222 if (Pos == FactoryMethodPool.end()) {
1223 if (ExternalSource && !InstanceMethodPool.count(Sel))
1224 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1225 else
1226 return 0;
1227 }
1228
1229 ObjCMethodList &MethList = Pos->second;
1230 bool issueWarning = false;
1231
1232 if (MethList.Method && MethList.Next) {
1233 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1234 // This checks if the methods differ by size & alignment.
1235 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1236 issueWarning = true;
1237 }
1238 if (issueWarning && (MethList.Method && MethList.Next)) {
1239 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1240 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1241 << MethList.Method->getSourceRange();
1242 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1243 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1244 << Next->Method->getSourceRange();
1245 }
1246 return MethList.Method;
1247}
1248
Steve Naroff0701bbb2009-01-08 17:28:14 +00001249/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1250/// have the property type and issue diagnostics if they don't.
1251/// Also synthesize a getter/setter method if none exist (and update the
1252/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1253/// methods is the "right" thing to do.
1254void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1255 ObjCContainerDecl *CD) {
1256 ObjCMethodDecl *GetterMethod, *SetterMethod;
1257
Douglas Gregor6ab35242009-04-09 21:40:53 +00001258 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1259 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Steve Naroff0701bbb2009-01-08 17:28:14 +00001260
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001261 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001262 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001263 Diag(property->getLocation(),
1264 diag::err_accessor_property_type_mismatch)
1265 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001266 << GetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001267 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1268 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001269
1270 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001271 if (Context.getCanonicalType(SetterMethod->getResultType())
1272 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001273 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001274 if (SetterMethod->param_size() != 1 ||
1275 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001276 Diag(property->getLocation(),
1277 diag::err_accessor_property_type_mismatch)
1278 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001279 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001280 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1281 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001282 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001283
1284 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001285 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001286 // FIXME: The synthesized property we set here is misleading. We
1287 // almost always synthesize these methods unless the user explicitly
1288 // provided prototypes (which is odd, but allowed). Sema should be
1289 // typechecking that the declarations jive in that situation (which
1290 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001291 if (!GetterMethod) {
1292 // No instance method of same name as property getter name was found.
1293 // Declare a getter method and add it to the list of methods
1294 // for this class.
1295 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1296 property->getLocation(), property->getGetterName(),
1297 property->getType(), CD, true, false, true,
1298 (property->getPropertyImplementation() ==
1299 ObjCPropertyDecl::Optional) ?
1300 ObjCMethodDecl::Optional :
1301 ObjCMethodDecl::Required);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001302 CD->addDecl(Context, GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001303 } else
1304 // A user declared getter will be synthesize when @synthesize of
1305 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001306 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001307 property->setGetterMethodDecl(GetterMethod);
1308
1309 // Skip setter if property is read-only.
1310 if (!property->isReadOnly()) {
1311 // Find the default setter and if one not found, add one.
1312 if (!SetterMethod) {
1313 // No instance method of same name as property setter name was found.
1314 // Declare a setter method and add it to the list of methods
1315 // for this class.
1316 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1317 property->getLocation(),
1318 property->getSetterName(),
1319 Context.VoidTy, CD, true, false, true,
1320 (property->getPropertyImplementation() ==
1321 ObjCPropertyDecl::Optional) ?
1322 ObjCMethodDecl::Optional :
1323 ObjCMethodDecl::Required);
1324 // Invent the arguments for the setter. We don't bother making a
1325 // nice name for the argument.
1326 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001327 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001328 property->getIdentifier(),
1329 property->getType(),
1330 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001331 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001332 SetterMethod->setMethodParams(Context, &Argument, 1);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001333 CD->addDecl(Context, SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001334 } else
1335 // A user declared setter will be synthesize when @synthesize of
1336 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001337 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001338 property->setSetterMethodDecl(SetterMethod);
1339 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001340 // Add any synthesized methods to the global pool. This allows us to
1341 // handle the following, which is supported by GCC (and part of the design).
1342 //
1343 // @interface Foo
1344 // @property double bar;
1345 // @end
1346 //
1347 // void thisIsUnfortunate() {
1348 // id foo;
1349 // double bar = [foo bar];
1350 // }
1351 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001352 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001353 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001354 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001355 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001356}
1357
Steve Naroffa56f6162007-12-18 01:30:32 +00001358// Note: For class/category implemenations, allMethods/allProperties is
1359// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001360void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1361 DeclPtrTy *allMethods, unsigned allNum,
1362 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001363 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001364 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001365
Steve Naroffa56f6162007-12-18 01:30:32 +00001366 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1367 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001368 // should be true.
1369 if (!ClassDecl)
1370 return;
1371
Chris Lattner4d391482007-12-12 07:09:47 +00001372 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001373 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1374 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001375 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001376
Steve Naroff0701bbb2009-01-08 17:28:14 +00001377 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001378
1379 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1380 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1381 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1382
Chris Lattner4d391482007-12-12 07:09:47 +00001383 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001384 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001385 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001386
1387 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001388 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001389 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001390 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001391 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1392 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001393 if ((isInterfaceDeclKind && PrevMethod && !match)
1394 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001395 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001396 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001397 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001398 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001399 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001400 InsMap[Method->getSelector()] = Method;
1401 /// The following allows us to typecheck messages to "id".
1402 AddInstanceMethodToGlobalPool(Method);
1403 }
1404 }
1405 else {
1406 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001407 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001408 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1409 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001410 if ((isInterfaceDeclKind && PrevMethod && !match)
1411 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001412 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001413 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001414 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001415 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001416 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001417 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001418 /// The following allows us to typecheck messages to "Class".
1419 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001420 }
1421 }
1422 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001423 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001424 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001425 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001426 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001427 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001428 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001429 // Categories are used to extend the class by declaring new methods.
1430 // By the same token, they are also used to add new properties. No
1431 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001432
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001433 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001434 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001435 if (C->getIdentifier() == 0)
1436 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001437 }
Steve Naroff09c47192009-01-09 15:36:25 +00001438 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1439 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1440 // user-defined setter/getter. It also synthesizes setter/getter methods
1441 // and adds them to the DeclContext and global method pools.
Douglas Gregor6ab35242009-04-09 21:40:53 +00001442 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1443 E = CDecl->prop_end(Context);
1444 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001445 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001446 CDecl->setAtEndLoc(AtEndLoc);
1447 }
1448 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001449 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001450 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001451 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001452 } else if (ObjCCategoryImplDecl* CatImplClass =
1453 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001454 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001455
Chris Lattner4d391482007-12-12 07:09:47 +00001456 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001457 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001458 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001459 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001460 Categories; Categories = Categories->getNextClassCategory()) {
1461 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001462 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001463 break;
1464 }
1465 }
1466 }
1467 }
Chris Lattner682bf922009-03-29 16:50:03 +00001468 if (isInterfaceDeclKind) {
1469 // Reject invalid vardecls.
1470 for (unsigned i = 0; i != tuvNum; i++) {
1471 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1472 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1473 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001474 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001475 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001476 }
Chris Lattner682bf922009-03-29 16:50:03 +00001477 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001478 }
Chris Lattner4d391482007-12-12 07:09:47 +00001479}
1480
1481
1482/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1483/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001484static Decl::ObjCDeclQualifier
1485CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1486 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1487 if (PQTVal & ObjCDeclSpec::DQ_In)
1488 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1489 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1490 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1491 if (PQTVal & ObjCDeclSpec::DQ_Out)
1492 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1493 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1494 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1495 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1496 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1497 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1498 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001499
1500 return ret;
1501}
1502
Chris Lattnerb28317a2009-03-28 19:18:32 +00001503Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001504 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001505 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001506 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001507 Selector Sel,
1508 // optional arguments. The number of types/arguments is obtained
1509 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001510 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001511 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001512 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1513 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001514 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001515
1516 // Make sure we can establish a context for the method.
1517 if (!ClassDecl) {
1518 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001519 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001520 }
Chris Lattner4d391482007-12-12 07:09:47 +00001521 QualType resultDeclType;
1522
Steve Naroffccef3712009-02-20 22:59:16 +00001523 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001524 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001525
1526 // Methods cannot return interface types. All ObjC objects are
1527 // passed by reference.
1528 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001529 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1530 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001531 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001532 }
1533 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001534 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001535
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001536 ObjCMethodDecl* ObjCMethod =
1537 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner32d3f9c2009-03-29 04:30:19 +00001538 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001539 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001540 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001541 MethodDeclKind == tok::objc_optional ?
1542 ObjCMethodDecl::Optional :
1543 ObjCMethodDecl::Required);
1544
Chris Lattner0ed844b2008-04-04 06:12:32 +00001545 llvm::SmallVector<ParmVarDecl*, 16> Params;
1546
Chris Lattner7db638d2009-04-11 19:42:43 +00001547 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001548 QualType ArgType, UnpromotedArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001549
Chris Lattnere294d3f2009-04-11 18:57:04 +00001550 if (ArgInfo[i].Type == 0) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001551 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001552 } else {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001553 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff6082c622008-12-09 19:36:17 +00001554 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001555 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001556 }
1557
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001558 ParmVarDecl* Param;
Chris Lattner2dd979f2009-04-11 19:08:56 +00001559 if (ArgType == UnpromotedArgType)
Chris Lattner7db638d2009-04-11 19:42:43 +00001560 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001561 ArgInfo[i].Name, ArgType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001562 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001563 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001564 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner7db638d2009-04-11 19:42:43 +00001565 ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001566 ArgInfo[i].Name, ArgType,
1567 UnpromotedArgType,
Douglas Gregor64650af2009-02-02 23:39:07 +00001568 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001569
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001570 if (ArgType->isObjCInterfaceType()) {
1571 Diag(ArgInfo[i].NameLoc,
1572 diag::err_object_cannot_be_passed_returned_by_value)
1573 << 1 << ArgType;
1574 Param->setInvalidDecl();
1575 }
1576
Chris Lattner0ed844b2008-04-04 06:12:32 +00001577 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001578 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001579
1580 // Apply the attributes to the parameter.
1581 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1582
Chris Lattner0ed844b2008-04-04 06:12:32 +00001583 Params.push_back(Param);
1584 }
1585
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001586 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001587 ObjCMethod->setObjCDeclQualifier(
1588 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1589 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001590
1591 if (AttrList)
1592 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001593
1594 // For implementations (which can be very "coarse grain"), we add the
1595 // method now. This allows the AST to implement lookup methods that work
1596 // incrementally (without waiting until we parse the @end). It also allows
1597 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001598 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001599 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001600 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001601 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel);
1602 ImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001603 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001604 PrevMethod = ImpDecl->getClassMethod(Context, Sel);
1605 ImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001606 }
1607 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001608 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001609 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001610 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001611 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel);
1612 CatImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001613 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001614 PrevMethod = CatImpDecl->getClassMethod(Context, Sel);
1615 CatImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001616 }
1617 }
1618 if (PrevMethod) {
1619 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001620 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001621 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001622 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001623 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001624 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001625}
1626
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001627void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1628 SourceLocation Loc,
1629 unsigned &Attributes) {
1630 // FIXME: Improve the reported location.
1631
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001632 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001633 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001634 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1635 ObjCDeclSpec::DQ_PR_assign |
1636 ObjCDeclSpec::DQ_PR_copy |
1637 ObjCDeclSpec::DQ_PR_retain))) {
1638 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1639 "readwrite" :
1640 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1641 "assign" :
1642 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1643 "copy" : "retain";
1644
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001645 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001646 diag::err_objc_property_attr_mutually_exclusive :
1647 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001648 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001649 }
1650
1651 // Check for copy or retain on non-object types.
1652 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1653 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001654 Diag(Loc, diag::err_objc_property_requires_object)
1655 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001656 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1657 }
1658
1659 // Check for more than one of { assign, copy, retain }.
1660 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1661 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001662 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1663 << "assign" << "copy";
1664 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001665 }
1666 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001667 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1668 << "assign" << "retain";
1669 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001670 }
1671 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1672 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001673 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1674 << "copy" << "retain";
1675 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001676 }
1677 }
1678
1679 // Warn if user supplied no assignment attribute, property is
1680 // readwrite, and this is an object type.
1681 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1682 ObjCDeclSpec::DQ_PR_retain)) &&
1683 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1684 Context.isObjCObjectPointerType(PropertyTy)) {
1685 // Skip this warning in gc-only mode.
1686 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1687 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1688
1689 // If non-gc code warn that this is likely inappropriate.
1690 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1691 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1692
1693 // FIXME: Implement warning dependent on NSCopying being
1694 // implemented. See also:
1695 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1696 // (please trim this list while you are at it).
1697 }
1698}
1699
Chris Lattnerb28317a2009-03-28 19:18:32 +00001700Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1701 FieldDeclarator &FD,
1702 ObjCDeclSpec &ODS,
1703 Selector GetterSel,
1704 Selector SetterSel,
1705 DeclPtrTy ClassCategory,
1706 bool *isOverridingProperty,
1707 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001708 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001709 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1710 // default is readwrite!
1711 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1712 // property is defaulted to 'assign' if it is readwrite and is
1713 // not retain or copy
1714 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1715 (isReadWrite &&
1716 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1717 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1718 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001719 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001720 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001721 // May modify Attributes.
1722 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001723 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1724 if (!CDecl->getIdentifier()) {
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001725 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001726 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001727 if ((CCPrimary = CDecl->getClassInterface())) {
1728 // Find the property in continuation class's primary class only.
1729 ObjCPropertyDecl *PIDecl = 0;
1730 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001731 for (ObjCInterfaceDecl::prop_iterator
1732 I = CCPrimary->prop_begin(Context),
1733 E = CCPrimary->prop_end(Context);
1734 I != E; ++I)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001735 if ((*I)->getIdentifier() == PropertyId) {
1736 PIDecl = *I;
1737 break;
1738 }
1739
1740 if (PIDecl) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001741 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001742 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001743 unsigned PIkind = PIDecl->getPropertyAttributes();
1744 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001745 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001746 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1747 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001748 PIDecl->makeitReadWriteAttribute();
1749 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1750 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1751 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1752 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1753 PIDecl->setSetterName(SetterSel);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001754 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001755 else
1756 Diag(AtLoc, diag::err_use_continuation_class)
1757 << CCPrimary->getDeclName();
1758 *isOverridingProperty = true;
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001759 // Make sure setter decl is synthesized, and added to primary
1760 // class's list.
1761 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001762 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001763 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001764 // No matching property found in the primary class. Just fall thru
1765 // and add property to continuation class's primary class.
1766 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001767 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001768 Diag(CDecl->getLocation(), diag::err_continuation_class);
1769 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001770 return DeclPtrTy();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001771 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001772 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001773
Steve Naroff93983f82009-01-11 12:47:58 +00001774 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1775 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001776 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1777 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001778 FD.D.getIdentifier(), T);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001779 DC->addDecl(Context, PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001780
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001781 if (T->isArrayType() || T->isFunctionType()) {
1782 Diag(AtLoc, diag::err_property_type) << T;
1783 PDecl->setInvalidDecl();
1784 }
1785
Chris Lattner97a58872009-02-16 18:32:47 +00001786 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001787
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001788 // Regardless of setter/getter attribute, we save the default getter/setter
1789 // selector names in anticipation of declaration of setter/getter methods.
1790 PDecl->setGetterName(GetterSel);
1791 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001792
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001793 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001794 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001795
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001796 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001797 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001798
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001799 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001800 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001801
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001802 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001803 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001804
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001805 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001806 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001807
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001808 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001809 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001810
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001811 if (isAssign)
1812 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1813
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001814 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001815 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001816
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001817 if (MethodImplKind == tok::objc_required)
1818 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1819 else if (MethodImplKind == tok::objc_optional)
1820 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001821 // A case of continuation class adding a new property in the class. This
1822 // is not what it was meant for. However, gcc supports it and so should we.
1823 // Make sure setter/getters are declared here.
1824 if (CCPrimary)
1825 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001826
Chris Lattnerb28317a2009-03-28 19:18:32 +00001827 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001828}
1829
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001830/// ActOnPropertyImplDecl - This routine performs semantic checks and
1831/// builds the AST node for a property implementation declaration; declared
1832/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001833///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001834Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1835 SourceLocation PropertyLoc,
1836 bool Synthesize,
1837 DeclPtrTy ClassCatImpDecl,
1838 IdentifierInfo *PropertyId,
1839 IdentifierInfo *PropertyIvar) {
1840 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001841 // Make sure we have a context for the property implementation declaration.
1842 if (!ClassImpDecl) {
1843 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001844 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001845 }
1846 ObjCPropertyDecl *property = 0;
1847 ObjCInterfaceDecl* IDecl = 0;
1848 // Find the class or category class where this property must have
1849 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001850 ObjCImplementationDecl *IC = 0;
1851 ObjCCategoryImplDecl* CatImplClass = 0;
1852 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001853 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001854 // We always synthesize an interface for an implementation
1855 // without an interface decl. So, IDecl is always non-zero.
1856 assert(IDecl &&
1857 "ActOnPropertyImplDecl - @implementation without @interface");
1858
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001859 // Look for this property declaration in the @implementation's @interface
Douglas Gregor6ab35242009-04-09 21:40:53 +00001860 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001861 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001862 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001863 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001864 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001865 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001866 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001867 if (Synthesize) {
1868 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001869 return DeclPtrTy();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001870 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001871 IDecl = CatImplClass->getClassInterface();
1872 if (!IDecl) {
1873 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001874 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001875 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001876 ObjCCategoryDecl *Category =
1877 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1878
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001879 // If category for this implementation not found, it is an error which
1880 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001881 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00001882 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001883 // Look for this property declaration in @implementation's category
Douglas Gregor6ab35242009-04-09 21:40:53 +00001884 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001885 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001886 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001887 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001888 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001889 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001890 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001891 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001892 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001893 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001894 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001895 // Check that we have a valid, previously declared ivar for @synthesize
1896 if (Synthesize) {
1897 // @synthesize
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001898 bool NoExplicitPropertyIvar = (!PropertyIvar);
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001899 if (!PropertyIvar)
1900 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00001901 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001902 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001903 ObjCInterfaceDecl *ClassDeclared;
1904 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001905 if (!Ivar) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001906 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1907 PropertyIvar, PropType,
1908 ObjCIvarDecl::Public,
1909 (Expr *)0);
1910 property->setPropertyIvarDecl(Ivar);
1911 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00001912 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001913 // Note! I deliberately want it to fall thru so, we have a
1914 // a property implementation and to avoid future warnings.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001915 }
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001916 else if (getLangOptions().ObjCNonFragileABI &&
1917 NoExplicitPropertyIvar && ClassDeclared != IDecl) {
1918 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
1919 << property->getDeclName() << Ivar->getDeclName()
1920 << ClassDeclared->getDeclName();
1921 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1922 << Ivar << Ivar->getNameAsCString();
1923 // Note! I deliberately want it to fall thru so more errors are caught.
1924 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001925 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1926
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001927 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001928 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001929 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001930 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001931 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001932 // Note! I deliberately want it to fall thru so, we have a
1933 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001934 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001935
1936 // FIXME! Rules for properties are somewhat different that those
1937 // for assignments. Use a new routine to consolidate all cases;
1938 // specifically for property redeclarations as well as for ivars.
1939 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
1940 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1941 if (lhsType != rhsType &&
1942 lhsType->isArithmeticType()) {
1943 Diag(PropertyLoc, diag::error_property_ivar_type)
1944 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001945 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00001946 }
1947 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00001948 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
1949 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001950 Diag(PropertyLoc, diag::error_weak_property)
1951 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001952 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00001953 }
1954 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00001955 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
1956 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001957 Diag(PropertyLoc, diag::error_strong_property)
1958 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001959 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001960 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001961 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001962 } else if (PropertyIvar)
1963 // @dynamic
1964 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001965 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001966 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001967 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1968 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001969 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001970 ObjCPropertyImplDecl::Synthesize
1971 : ObjCPropertyImplDecl::Dynamic),
1972 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001973 if (IC) {
1974 if (Synthesize)
1975 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001976 IC->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001977 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1978 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1979 << PropertyIvar;
1980 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1981 }
1982
Douglas Gregor653f1b12009-04-23 01:02:12 +00001983 if (ObjCPropertyImplDecl *PPIDecl
1984 = IC->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001985 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1986 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001987 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001988 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001989 IC->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001990 }
1991 else {
1992 if (Synthesize)
1993 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00001994 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001995 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1996 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1997 << PropertyIvar;
1998 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1999 }
2000
2001 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00002002 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002003 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2004 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002005 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002006 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00002007 CatImplClass->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002008 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002009
Chris Lattnerb28317a2009-03-28 19:18:32 +00002010 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002011}
Anders Carlsson15281452008-11-04 16:57:32 +00002012
Chris Lattnercc98eac2008-12-17 07:13:27 +00002013bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002014 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002015 return false;
2016
2017 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2018 D->setInvalidDecl();
2019
2020 return true;
2021}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002022
2023/// Collect the instance variables declared in an Objective-C object. Used in
2024/// the creation of structures from objects using the @defs directive.
2025/// FIXME: This should be consolidated with CollectObjCIvars as it is also
2026/// part of the AST generation logic of @defs.
2027static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
2028 ASTContext& Ctx,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002029 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002030 if (Class->getSuperClass())
2031 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
2032
2033 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002034 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
2035 E = Class->ivar_end(); I != E; ++I) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002036 ObjCIvarDecl* ID = *I;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002037 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
2038 ID->getIdentifier(), ID->getType(),
2039 ID->getBitWidth());
2040 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattnercc98eac2008-12-17 07:13:27 +00002041 }
2042}
2043
2044/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2045/// instance variables of ClassName into Decls.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002046void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002047 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002048 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002049 // Check that ClassName is a valid class
2050 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2051 if (!Class) {
2052 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2053 return;
2054 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002055 if (LangOpts.ObjCNonFragileABI) {
2056 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2057 return;
2058 }
2059
Chris Lattnercc98eac2008-12-17 07:13:27 +00002060 // Collect the instance variables
Chris Lattnerb28317a2009-03-28 19:18:32 +00002061 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002062
2063 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002064 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002065 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002066 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002067 if (getLangOptions().CPlusPlus)
2068 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002069 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregor6ab35242009-04-09 21:40:53 +00002070 Record->addDecl(Context, FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002071 }
2072}
2073