blob: cb35890b3f1c96d52e476050b41bb059f5d6999b [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"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000018using namespace clang;
19
Ted Kremeneka526c5c2008-01-07 19:49:32 +000020/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000021/// and user declared, in the method definition's AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000022void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000023 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff394f3f42008-07-25 17:57:26 +000024 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
25
26 // If we don't have a valid method decl, simply return.
27 if (!MDecl)
28 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000029
30 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000031 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000032 AddInstanceMethodToGlobalPool(MDecl);
33 else
34 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000035
36 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000037 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000038
39 // Create Decl objects for each parameter, entrring them in the scope for
40 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000041
42 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000043 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000044
Daniel Dunbar451318c2008-08-26 06:07:48 +000045 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
46 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000047
Chris Lattner8123a952008-04-10 02:22:51 +000048 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000049 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
50 E = MDecl->param_end(); PI != E; ++PI)
51 if ((*PI)->getIdentifier())
52 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000053}
54
Chris Lattner7caeabd2008-07-21 22:17:28 +000055Sema::DeclTy *Sema::
56ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
57 IdentifierInfo *ClassName, SourceLocation ClassLoc,
58 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000059 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000060 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000061 assert(ClassName && "Missing class identifier");
62
63 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000064 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000065 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000066 // Maybe we will complain about the shadowed template parameter.
67 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
68 // Just pretend that we didn't see the previous declaration.
69 PrevDecl = 0;
70 }
71
Ted Kremeneka526c5c2008-01-07 19:49:32 +000072 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000073 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000074 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000075 }
76
Ted Kremeneka526c5c2008-01-07 19:49:32 +000077 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000078 if (IDecl) {
79 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000080 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +000081 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000082 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000083 Diag(IDecl->getLocation(), diag::note_previous_definition);
84
Steve Naroffcfe8bf32008-11-18 19:15:30 +000085 // Return the previous class interface.
86 // FIXME: don't leak the objects passed in!
87 return IDecl;
88 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000089 IDecl->setLocation(AtInterfaceLoc);
90 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000091 }
Chris Lattnerb752f282008-07-21 07:06:49 +000092 } else {
Douglas Gregord0434102009-01-09 00:49:46 +000093 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000094 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000095 if (AttrList)
96 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +000097
Steve Naroff31102512008-04-02 18:30:49 +000098 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregord0434102009-01-09 00:49:46 +000099 // FIXME: PushOnScopeChains
Douglas Gregor482b77d2009-01-12 23:27:07 +0000100 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000101 // Remember that this needs to be removed when the scope is popped.
102 TUScope->AddDecl(IDecl);
103 }
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 Lattner4d391482007-12-12 07:09:47 +0000162 return IDecl;
163}
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.
Steve Naroffe8043c32008-04-01 23:04:06 +0000167Sema::DeclTy *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 Lattner4d391482007-12-12 07:09:47 +0000180 return 0;
181 }
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 Lattner4d391482007-12-12 07:09:47 +0000198 return 0;
199 }
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
205 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000206
207 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000208 CurContext->addDecl(AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000209 if (!CheckObjCDeclScope(AliasDecl))
210 TUScope->AddDecl(AliasDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000211
Chris Lattner4d391482007-12-12 07:09:47 +0000212 return AliasDecl;
213}
214
Chris Lattnere13b9592008-07-26 04:03:38 +0000215Sema::DeclTy *
216Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
217 IdentifierInfo *ProtocolName,
218 SourceLocation ProtocolLoc,
219 DeclTy * const *ProtoRefs,
220 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000221 SourceLocation EndProtoLoc,
222 AttributeList *AttrList) {
223 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000224 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000225 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000226 if (PDecl) {
227 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000228 if (!PDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000229 PDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000230 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000231 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000232 // Just return the protocol we already had.
233 // FIXME: don't leak the objects passed in!
234 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000235 }
Steve Narofff11b5082008-08-13 16:39:22 +0000236 // Make sure the cached decl gets a valid start location.
237 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000238 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000239 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000240 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
241 AtProtoInterfaceLoc,ProtocolName);
242 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000243 CurContext->addDecl(PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000244 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000245 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000246 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000247 if (AttrList)
248 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000249 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000250 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000251 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000252 PDecl->setLocEnd(EndProtoLoc);
253 }
Anders Carlsson15281452008-11-04 16:57:32 +0000254
255 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000256 return PDecl;
257}
258
259/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000260/// issues an error if they are not declared. It returns list of
261/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000262void
Chris Lattnere13b9592008-07-26 04:03:38 +0000263Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000264 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000265 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000266 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000267 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000268 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
269 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000270 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000271 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000272 continue;
273 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000274
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000275 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000276
277 // If this is a forward declaration and we are supposed to warn in this
278 // case, do it.
279 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000280 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000281 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000282 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000283 }
284}
285
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000286/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000287/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000288///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000289void
290Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
291 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000292 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000293 ObjCPropertyDecl::PropertyAttributeKind CAttr =
294 Property->getPropertyAttributes();
295 ObjCPropertyDecl::PropertyAttributeKind SAttr =
296 SuperProperty->getPropertyAttributes();
297 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
298 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000299 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000300 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000301 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
302 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000303 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000304 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000305 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
306 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000307 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000308 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000309
310 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
311 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000312 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000313 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000314 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000315 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000316 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000317 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000318 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000319 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000320
Chris Lattner717250a2008-07-26 20:50:02 +0000321 if (Context.getCanonicalType(Property->getType()) !=
322 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000323 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000324 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000325
326}
327
328/// ComparePropertiesInBaseAndSuper - This routine compares property
329/// declarations in base and its super class, if any, and issues
330/// diagnostics in a variety of inconsistant situations.
331///
Chris Lattner70f19542009-02-16 21:26:43 +0000332void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000333 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
334 if (!SDecl)
335 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000336 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000337 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
338 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000339 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000340 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000341 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
342 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000343 ObjCPropertyDecl *PDecl = (*I);
344 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000345 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000346 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000347 }
348 }
349}
350
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000351/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
352/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000353/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000354void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000355Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000356 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000357 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
358 if (!IDecl) {
359 // Category
360 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
361 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff09c47192009-01-09 15:36:25 +0000362 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
363 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000364 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000365 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000366 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000367 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000368 CP != CE; ++CP)
369 if ((*CP)->getIdentifier() == Pr->getIdentifier())
370 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000371 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000372 // Property protocol already exist in class. Diagnose any mismatch.
373 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
374 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000375 return;
376 }
Steve Naroff09c47192009-01-09 15:36:25 +0000377 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
378 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000379 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000380 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000381 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000382 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000383 CP != CE; ++CP)
384 if ((*CP)->getIdentifier() == Pr->getIdentifier())
385 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000386 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000387 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000388 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000389 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000390}
391
392/// MergeProtocolPropertiesIntoClass - This routine merges properties
393/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000394/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000395///
Chris Lattner70f19542009-02-16 21:26:43 +0000396void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
397 DeclTy *MergeItsProtocols) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000398 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000399 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
400
401 if (!IDecl) {
402 // Category
403 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
404 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
405 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
406 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
407 E = MDecl->protocol_end(); P != E; ++P)
408 // Merge properties of category (*P) into IDECL's
409 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
410
411 // Go thru the list of protocols for this category and recursively merge
412 // their properties into this class as well.
413 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
414 E = CatDecl->protocol_end(); P != E; ++P)
415 MergeProtocolPropertiesIntoClass(CatDecl, *P);
416 } else {
417 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
418 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
419 E = MD->protocol_end(); P != E; ++P)
420 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
421 }
422 return;
423 }
424
Chris Lattnerb752f282008-07-21 07:06:49 +0000425 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000426 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
427 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000428 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000429 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
430
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000431 // Go thru the list of protocols for this class and recursively merge
432 // their properties into this class as well.
433 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
434 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000435 MergeProtocolPropertiesIntoClass(IDecl, *P);
436 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000437 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
438 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
439 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000440 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000441 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000442}
443
Chris Lattner4d391482007-12-12 07:09:47 +0000444/// ActOnForwardProtocolDeclaration -
445Action::DeclTy *
446Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000447 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000448 unsigned NumElts,
449 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000450 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000451
452 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000453 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000454 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000455 if (PDecl == 0) { // Not already seen?
456 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
457 IdentList[i].second, Ident);
458 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000459 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000460 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000461 if (attrList)
462 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000463 Protocols.push_back(PDecl);
464 }
Anders Carlsson15281452008-11-04 16:57:32 +0000465
466 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000467 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000468 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000469 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000470 CheckObjCDeclScope(PDecl);
471 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000472}
473
Chris Lattner7caeabd2008-07-21 22:17:28 +0000474Sema::DeclTy *Sema::
475ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
476 IdentifierInfo *ClassName, SourceLocation ClassLoc,
477 IdentifierInfo *CategoryName,
478 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000479 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000480 unsigned NumProtoRefs,
481 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000482 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000483 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
484 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000485 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000486
487 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000488 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000489 if (!IDecl || IDecl->isForwardDecl()) {
490 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000491 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner70f19542009-02-16 21:26:43 +0000492 return CDecl;
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000493 }
Chris Lattner4d391482007-12-12 07:09:47 +0000494
Chris Lattner70f19542009-02-16 21:26:43 +0000495 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000496
497 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000498 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000499
500 /// Check for duplicate interface declaration for this category
501 ObjCCategoryDecl *CDeclChain;
502 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
503 CDeclChain = CDeclChain->getNextClassCategory()) {
504 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
505 Diag(CategoryLoc, diag::warn_dup_category_def)
506 << ClassName << CategoryName;
507 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
508 break;
509 }
510 }
511 if (!CDeclChain)
512 CDecl->insertNextClassCategory();
513
Chris Lattner4d391482007-12-12 07:09:47 +0000514 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000515 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000516 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000517 }
Anders Carlsson15281452008-11-04 16:57:32 +0000518
519 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000520 return CDecl;
521}
522
523/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000524/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000525/// object.
526Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
527 SourceLocation AtCatImplLoc,
528 IdentifierInfo *ClassName, SourceLocation ClassLoc,
529 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000530 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000531 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000532 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
533 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000534 /// Check that class of this category is already completely declared.
535 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000536 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000537
Douglas Gregord0434102009-01-09 00:49:46 +0000538 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000539 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000540
Chris Lattner4d391482007-12-12 07:09:47 +0000541 /// TODO: Check that CatName, category name, is not used in another
542 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000543 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000544
545 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000546 return CDecl;
547}
548
549Sema::DeclTy *Sema::ActOnStartClassImplementation(
550 SourceLocation AtClassImplLoc,
551 IdentifierInfo *ClassName, SourceLocation ClassLoc,
552 IdentifierInfo *SuperClassname,
553 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000554 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000555 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000556 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000557 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000558 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000559 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000560 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000561 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000562 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000563 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000564 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000565 }
566
567 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000568 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000569 if (SuperClassname) {
570 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000571 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000572 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000573 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
574 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000575 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000576 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000577 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000578 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000579 Diag(SuperClassLoc, diag::err_undef_superclass)
580 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000581 else if (IDecl && IDecl->getSuperClass() != SDecl) {
582 // This implementation and its interface do not have the same
583 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000584 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000585 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000586 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000587 }
588 }
589 }
590
591 if (!IDecl) {
592 // Legacy case of @implementation with no corresponding @interface.
593 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000594
595 // FIXME: Do we support attributes on the @implementation? If so
596 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000597 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
598 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000599 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000600 IDecl->setSuperClass(SDecl);
601 IDecl->setLocEnd(ClassLoc);
602
Douglas Gregord0434102009-01-09 00:49:46 +0000603 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000604 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000605 // Remember that this needs to be removed when the scope is popped.
606 TUScope->AddDecl(IDecl);
607 }
608
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000609 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000610 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000611 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000612
Douglas Gregord0434102009-01-09 00:49:46 +0000613 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000614 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000615
Anders Carlsson15281452008-11-04 16:57:32 +0000616 if (CheckObjCDeclScope(IMPDecl))
617 return IMPDecl;
618
Chris Lattner4d391482007-12-12 07:09:47 +0000619 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000620 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000621 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000622 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000623 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000624 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000625 return IMPDecl;
626}
627
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000628void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
629 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000630 SourceLocation RBrace) {
631 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000632 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000633 if (!IDecl)
634 return;
635 /// Check case of non-existing @interface decl.
636 /// (legacy objective-c @implementation decl without an @interface decl).
637 /// Add implementations's ivar to the synthesize class's ivar list.
638 if (IDecl->ImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000639 IDecl->setIVarList(ivars, numIvars, Context);
640 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000641 return;
642 }
643 // If implementation has empty ivar list, just return.
644 if (numIvars == 0)
645 return;
646
647 assert(ivars && "missing @implementation ivars");
648
649 // Check interface's Ivar list against those in the implementation.
650 // names and types must match.
651 //
Chris Lattner4d391482007-12-12 07:09:47 +0000652 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000653 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000654 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
655 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000656 ObjCIvarDecl* ImplIvar = ivars[j++];
657 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000658 assert (ImplIvar && "missing implementation ivar");
659 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000660 if (Context.getCanonicalType(ImplIvar->getType()) !=
661 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000662 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000663 << ImplIvar->getIdentifier()
664 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000665 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000666 }
667 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
668 // as error.
669 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000670 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000671 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000672 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000673 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000674 }
675 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000676 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000677
678 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000679 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000680 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000681 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000682}
683
Steve Naroff3c2eb662008-02-10 21:38:56 +0000684void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
685 bool &IncompleteImpl) {
686 if (!IncompleteImpl) {
687 Diag(ImpLoc, diag::warn_incomplete_impl);
688 IncompleteImpl = true;
689 }
Chris Lattner08631c52008-11-23 21:45:46 +0000690 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000691}
692
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000693void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
694 ObjCMethodDecl *IntfMethodDecl) {
695 bool err = false;
696 QualType ImpMethodQType =
697 Context.getCanonicalType(ImpMethodDecl->getResultType());
698 QualType IntfMethodQType =
699 Context.getCanonicalType(IntfMethodDecl->getResultType());
700 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
701 err = true;
702 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
703 IF=IntfMethodDecl->param_begin(),
704 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
705 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
706 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
707 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
708 err = true;
709 break;
710 }
711 }
712 if (err) {
713 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
714 << ImpMethodDecl->getDeclName();
715 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
716 }
717}
718
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000719/// isPropertyReadonly - Return true if property is readonly, by searching
720/// for the property in the class and in its categories and implementations
721///
722bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000723 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000724 // by far the most common case.
725 if (!PDecl->isReadOnly())
726 return false;
727 // Even if property is ready only, if interface has a user defined setter,
728 // it is not considered read only.
729 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
730 return false;
731
732 // Main class has the property as 'readonly'. Must search
733 // through the category list to see if the property's
734 // attribute has been over-ridden to 'readwrite'.
735 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
736 Category; Category = Category->getNextClassCategory()) {
737 // Even if property is ready only, if a category has a user defined setter,
738 // it is not considered read only.
739 if (Category->getInstanceMethod(PDecl->getSetterName()))
740 return false;
741 ObjCPropertyDecl *P =
742 Category->FindPropertyDeclaration(PDecl->getIdentifier());
743 if (P && !P->isReadOnly())
744 return false;
745 }
746
747 // Also, check for definition of a setter method in the implementation if
748 // all else failed.
749 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
750 if (ObjCImplementationDecl *IMD =
751 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
752 if (IMD->getInstanceMethod(PDecl->getSetterName()))
753 return false;
754 }
755 else if (ObjCCategoryImplDecl *CIMD =
756 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
757 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
758 return false;
759 }
760 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000761 // Lastly, look through the implementation (if one is in scope).
762 if (ObjCImplementationDecl *ImpDecl =
763 ObjCImplementations[IDecl->getIdentifier()])
764 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
765 return false;
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000766 return true;
767}
768
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000769/// FIXME: Type hierarchies in Objective-C can be deep. We could most
770/// likely improve the efficiency of selector lookups and type
771/// checking by associating with each protocol / interface / category
772/// the flattened instance tables. If we used an immutable set to keep
773/// the table then it wouldn't add significant memory cost and it
774/// would be handy for lookups.
775
Steve Naroffefe7f362008-02-08 22:06:17 +0000776/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000777/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000778void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
779 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000780 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000781 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000782 const llvm::DenseSet<Selector> &ClsMap,
783 ObjCInterfaceDecl *IDecl) {
784 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
785
786 // If a method lookup fails locally we still need to look and see if
787 // the method was implemented by a base class or an inherited
788 // protocol. This lookup is slow, but occurs rarely in correct code
789 // and otherwise would terminate in a warning.
790
Chris Lattner4d391482007-12-12 07:09:47 +0000791 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000792 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000793 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000794 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000795 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000796 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000797 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000798 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000799 }
800 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000801 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000802 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000803 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000804 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
805 !ClsMap.count(method->getSelector()) &&
806 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000807 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000808 }
Chris Lattner780f3292008-07-21 21:32:27 +0000809 // Check on this protocols's referenced protocols, recursively.
810 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
811 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000812 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000813}
814
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000815void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
816 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000817 llvm::DenseSet<Selector> InsMap;
818 // Check and see if instance methods in class interface have been
819 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000820 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000821 E = IMPDecl->instmeth_end(); I != E; ++I)
822 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000823
824 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000825 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000826 E = IDecl->instmeth_end(); I != E; ++I) {
827 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000828 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000829 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000830 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000831
832 ObjCMethodDecl *ImpMethodDecl =
833 IMPDecl->getInstanceMethod((*I)->getSelector());
834 ObjCMethodDecl *IntfMethodDecl =
835 IDecl->getInstanceMethod((*I)->getSelector());
836 assert(IntfMethodDecl &&
837 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
838 // ImpMethodDecl may be null as in a @dynamic property.
839 if (ImpMethodDecl)
840 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
841 }
Chris Lattner4c525092007-12-12 17:58:05 +0000842
Chris Lattner4d391482007-12-12 07:09:47 +0000843 llvm::DenseSet<Selector> ClsMap;
844 // Check and see if class methods in class interface have been
845 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000846 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000847 E = IMPDecl->classmeth_end(); I != E; ++I)
848 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000849
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000850 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000851 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000852 if (!ClsMap.count((*I)->getSelector()))
853 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000854 else {
855 ObjCMethodDecl *ImpMethodDecl =
856 IMPDecl->getClassMethod((*I)->getSelector());
857 ObjCMethodDecl *IntfMethodDecl =
858 IDecl->getClassMethod((*I)->getSelector());
859 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
860 }
861
Chris Lattner4d391482007-12-12 07:09:47 +0000862
863 // Check the protocol list for unimplemented methods in the @implementation
864 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000865 const ObjCList<ObjCProtocolDecl> &Protocols =
866 IDecl->getReferencedProtocols();
867 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
868 E = Protocols.end(); I != E; ++I)
869 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000870 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000871}
872
873/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000874/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000875void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
876 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000877 llvm::DenseSet<Selector> InsMap;
878 // Check and see if instance methods in category interface have been
879 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000880 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000881 E = CatImplDecl->instmeth_end(); I != E; ++I)
882 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000883
884 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000885 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000886 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000887 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000888 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000889 else {
890 ObjCMethodDecl *ImpMethodDecl =
891 CatImplDecl->getInstanceMethod((*I)->getSelector());
892 ObjCMethodDecl *IntfMethodDecl =
893 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000894 assert(IntfMethodDecl &&
895 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
896 // ImpMethodDecl may be null as in a @dynamic property.
897 if (ImpMethodDecl)
898 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000899 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000900
Chris Lattner4d391482007-12-12 07:09:47 +0000901 llvm::DenseSet<Selector> ClsMap;
902 // Check and see if class methods in category interface have been
903 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000904 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000905 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
906 I != E; ++I)
907 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000908
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000909 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000910 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000911 if (!ClsMap.count((*I)->getSelector()))
912 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000913 else {
914 ObjCMethodDecl *ImpMethodDecl =
915 CatImplDecl->getClassMethod((*I)->getSelector());
916 ObjCMethodDecl *IntfMethodDecl =
917 CatClassDecl->getClassMethod((*I)->getSelector());
918 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
919 }
Chris Lattner4d391482007-12-12 07:09:47 +0000920 // Check the protocol list for unimplemented methods in the @implementation
921 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000922 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
923 E = CatClassDecl->protocol_end(); PI != E; ++PI)
924 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000925 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000926}
927
928/// ActOnForwardClassDeclaration -
929Action::DeclTy *
930Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000931 IdentifierInfo **IdentList,
932 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000933 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000934
935 for (unsigned i = 0; i != NumElts; ++i) {
936 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000937 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000938 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000939 // Maybe we will complain about the shadowed template parameter.
940 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
941 // Just pretend that we didn't see the previous declaration.
942 PrevDecl = 0;
943 }
944
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000945 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000946 // GCC apparently allows the following idiom:
947 //
948 // typedef NSObject < XCElementTogglerP > XCElementToggler;
949 // @class XCElementToggler;
950 //
951 // FIXME: Make an extension?
952 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
953 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000954 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000955 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000956 }
Chris Lattner4d391482007-12-12 07:09:47 +0000957 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000958 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000959 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000960 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
961 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000962 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000963
Douglas Gregord0434102009-01-09 00:49:46 +0000964 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000965 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000966 // Remember that this needs to be removed when the scope is popped.
967 TUScope->AddDecl(IDecl);
968 }
969
970 Interfaces.push_back(IDecl);
971 }
972
Douglas Gregord0434102009-01-09 00:49:46 +0000973 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000974 &Interfaces[0],
975 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000976 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000977 CheckObjCDeclScope(CDecl);
978 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000979}
980
981
982/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
983/// returns true, or false, accordingly.
984/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000985bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000986 const ObjCMethodDecl *PrevMethod,
987 bool matchBasedOnSizeAndAlignment) {
988 QualType T1 = Context.getCanonicalType(Method->getResultType());
989 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
990
991 if (T1 != T2) {
992 // The result types are different.
993 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000994 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000995 // Incomplete types don't have a size and alignment.
996 if (T1->isIncompleteType() || T2->isIncompleteType())
997 return false;
998 // Check is based on size and alignment.
999 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1000 return false;
1001 }
Chris Lattner89951a82009-02-20 18:43:26 +00001002
1003 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1004 E = Method->param_end();
1005 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1006
1007 for (; ParamI != E; ++ParamI, ++PrevI) {
1008 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1009 T1 = Context.getCanonicalType((*ParamI)->getType());
1010 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001011 if (T1 != T2) {
1012 // The result types are different.
1013 if (!matchBasedOnSizeAndAlignment)
1014 return false;
1015 // Incomplete types don't have a size and alignment.
1016 if (T1->isIncompleteType() || T2->isIncompleteType())
1017 return false;
1018 // Check is based on size and alignment.
1019 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1020 return false;
1021 }
Chris Lattner4d391482007-12-12 07:09:47 +00001022 }
1023 return true;
1024}
1025
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001026void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
1027 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001028 if (!FirstMethod.Method) {
1029 // Haven't seen a method with this selector name yet - add it.
1030 FirstMethod.Method = Method;
1031 FirstMethod.Next = 0;
1032 } else {
1033 // We've seen a method with this name, now check the type signature(s).
1034 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1035
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001036 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001037 Next = Next->Next)
1038 match = MatchTwoMethodDeclarations(Method, Next->Method);
1039
1040 if (!match) {
1041 // We have a new signature for an existing method - add it.
1042 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +00001043 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +00001044 }
1045 }
1046}
1047
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001048// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001049ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1050 SourceRange R) {
1051 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001052 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001053
1054 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001055 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1056 // This checks if the methods differ by size & alignment.
1057 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1058 issueWarning = true;
1059 }
1060 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001061 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001062 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001063 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001064 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001065 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001066 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001067 }
1068 return MethList.Method;
1069}
1070
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001071void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1072 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001073 if (!FirstMethod.Method) {
1074 // Haven't seen a method with this selector name yet - add it.
1075 FirstMethod.Method = Method;
1076 FirstMethod.Next = 0;
1077 } else {
1078 // We've seen a method with this name, now check the type signature(s).
1079 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1080
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001081 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001082 Next = Next->Next)
1083 match = MatchTwoMethodDeclarations(Method, Next->Method);
1084
1085 if (!match) {
1086 // We have a new signature for an existing method - add it.
1087 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001088 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001089 FirstMethod.Next = OMI;
1090 }
1091 }
1092}
1093
Steve Naroff0701bbb2009-01-08 17:28:14 +00001094/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1095/// have the property type and issue diagnostics if they don't.
1096/// Also synthesize a getter/setter method if none exist (and update the
1097/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1098/// methods is the "right" thing to do.
1099void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1100 ObjCContainerDecl *CD) {
1101 ObjCMethodDecl *GetterMethod, *SetterMethod;
1102
1103 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1104 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1105
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001106 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001107 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001108 Diag(property->getLocation(),
1109 diag::err_accessor_property_type_mismatch)
1110 << property->getDeclName()
1111 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001112 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1113 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001114
1115 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001116 if (Context.getCanonicalType(SetterMethod->getResultType())
1117 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001118 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001119 if (SetterMethod->param_size() != 1 ||
1120 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001121 Diag(property->getLocation(),
1122 diag::err_accessor_property_type_mismatch)
1123 << property->getDeclName()
1124 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001125 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1126 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001127 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001128
1129 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001130 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001131 // FIXME: The synthesized property we set here is misleading. We
1132 // almost always synthesize these methods unless the user explicitly
1133 // provided prototypes (which is odd, but allowed). Sema should be
1134 // typechecking that the declarations jive in that situation (which
1135 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001136 if (!GetterMethod) {
1137 // No instance method of same name as property getter name was found.
1138 // Declare a getter method and add it to the list of methods
1139 // for this class.
1140 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1141 property->getLocation(), property->getGetterName(),
1142 property->getType(), CD, true, false, true,
1143 (property->getPropertyImplementation() ==
1144 ObjCPropertyDecl::Optional) ?
1145 ObjCMethodDecl::Optional :
1146 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001147 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001148 } else
1149 // A user declared getter will be synthesize when @synthesize of
1150 // the property with the same name is seen in the @implementation
1151 GetterMethod->setIsSynthesized();
1152 property->setGetterMethodDecl(GetterMethod);
1153
1154 // Skip setter if property is read-only.
1155 if (!property->isReadOnly()) {
1156 // Find the default setter and if one not found, add one.
1157 if (!SetterMethod) {
1158 // No instance method of same name as property setter name was found.
1159 // Declare a setter method and add it to the list of methods
1160 // for this class.
1161 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1162 property->getLocation(),
1163 property->getSetterName(),
1164 Context.VoidTy, CD, true, false, true,
1165 (property->getPropertyImplementation() ==
1166 ObjCPropertyDecl::Optional) ?
1167 ObjCMethodDecl::Optional :
1168 ObjCMethodDecl::Required);
1169 // Invent the arguments for the setter. We don't bother making a
1170 // nice name for the argument.
1171 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1172 SourceLocation(),
1173 property->getIdentifier(),
1174 property->getType(),
1175 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001176 0);
Chris Lattner38af2de2009-02-20 21:35:13 +00001177 SetterMethod->setMethodParams(&Argument, 1, Context);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001178 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001179 } else
1180 // A user declared setter will be synthesize when @synthesize of
1181 // the property with the same name is seen in the @implementation
1182 SetterMethod->setIsSynthesized();
1183 property->setSetterMethodDecl(SetterMethod);
1184 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001185 // Add any synthesized methods to the global pool. This allows us to
1186 // handle the following, which is supported by GCC (and part of the design).
1187 //
1188 // @interface Foo
1189 // @property double bar;
1190 // @end
1191 //
1192 // void thisIsUnfortunate() {
1193 // id foo;
1194 // double bar = [foo bar];
1195 // }
1196 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001197 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001198 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001199 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001200 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001201}
1202
Steve Naroffa56f6162007-12-18 01:30:32 +00001203// Note: For class/category implemenations, allMethods/allProperties is
1204// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001205void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1206 DeclTy **allMethods, unsigned allNum,
1207 DeclTy **allProperties, unsigned pNum) {
1208 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1209
Steve Naroffa56f6162007-12-18 01:30:32 +00001210 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1211 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001212 // should be true.
1213 if (!ClassDecl)
1214 return;
1215
Chris Lattner4d391482007-12-12 07:09:47 +00001216 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001217 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1218 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001219 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001220
Steve Naroff0701bbb2009-01-08 17:28:14 +00001221 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001222
1223 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1224 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1225 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1226
Chris Lattner4d391482007-12-12 07:09:47 +00001227 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001228 ObjCMethodDecl *Method =
1229 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001230
1231 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001232 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001233 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001234 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001235 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1236 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001237 if ((isInterfaceDeclKind && PrevMethod && !match)
1238 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001239 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001240 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001241 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001242 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001243 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001244 InsMap[Method->getSelector()] = Method;
1245 /// The following allows us to typecheck messages to "id".
1246 AddInstanceMethodToGlobalPool(Method);
1247 }
1248 }
1249 else {
1250 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001251 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001252 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1253 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001254 if ((isInterfaceDeclKind && PrevMethod && !match)
1255 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001256 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001257 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001258 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001259 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001260 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001261 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001262 /// The following allows us to typecheck messages to "Class".
1263 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001264 }
1265 }
1266 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001267 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001268 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001269 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001270 ComparePropertiesInBaseAndSuper(I);
1271 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001272 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001273 // Categories are used to extend the class by declaring new methods.
1274 // By the same token, they are also used to add new properties. No
1275 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001276
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001277 // Merge protocol properties into category
1278 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner4d391482007-12-12 07:09:47 +00001279 }
Steve Naroff09c47192009-01-09 15:36:25 +00001280 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1281 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1282 // user-defined setter/getter. It also synthesizes setter/getter methods
1283 // and adds them to the DeclContext and global method pools.
Chris Lattner97a58872009-02-16 18:32:47 +00001284 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1285 E = CDecl->prop_end(); I != E; ++I)
1286 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001287 CDecl->setAtEndLoc(AtEndLoc);
1288 }
1289 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001290 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001291 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001292 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001293 } else if (ObjCCategoryImplDecl* CatImplClass =
1294 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001295 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001296
Chris Lattner4d391482007-12-12 07:09:47 +00001297 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001298 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001299 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001300 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001301 Categories; Categories = Categories->getNextClassCategory()) {
1302 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1303 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1304 break;
1305 }
1306 }
1307 }
1308 }
1309}
1310
1311
1312/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1313/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001314static Decl::ObjCDeclQualifier
1315CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1316 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1317 if (PQTVal & ObjCDeclSpec::DQ_In)
1318 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1319 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1320 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1321 if (PQTVal & ObjCDeclSpec::DQ_Out)
1322 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1323 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1324 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1325 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1326 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1327 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1328 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001329
1330 return ret;
1331}
1332
1333Sema::DeclTy *Sema::ActOnMethodDeclaration(
1334 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001335 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001336 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001337 Selector Sel,
1338 // optional arguments. The number of types/arguments is obtained
1339 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001340 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001341 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001342 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1343 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001344 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001345
1346 // Make sure we can establish a context for the method.
1347 if (!ClassDecl) {
1348 Diag(MethodLoc, diag::error_missing_method_context);
1349 return 0;
1350 }
Chris Lattner4d391482007-12-12 07:09:47 +00001351 QualType resultDeclType;
1352
Steve Naroffccef3712009-02-20 22:59:16 +00001353 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001354 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001355
1356 // Methods cannot return interface types. All ObjC objects are
1357 // passed by reference.
1358 if (resultDeclType->isObjCInterfaceType()) {
1359 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1360 << "returned";
1361 return 0;
1362 }
1363 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001364 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001365
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001366 ObjCMethodDecl* ObjCMethod =
1367 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001368 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001369 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001370 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001371 MethodDeclKind == tok::objc_optional ?
1372 ObjCMethodDecl::Optional :
1373 ObjCMethodDecl::Required);
1374
Chris Lattner0ed844b2008-04-04 06:12:32 +00001375 llvm::SmallVector<ParmVarDecl*, 16> Params;
1376
1377 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1378 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001379 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001380
Steve Naroff6082c622008-12-09 19:36:17 +00001381 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001382 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001383 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001384 if (argType->isArrayType()) { // (char *[]) -> (char **)
1385 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001386 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001387 }
Steve Naroff6082c622008-12-09 19:36:17 +00001388 else if (argType->isFunctionType())
1389 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001390 else if (argType->isObjCInterfaceType()) {
1391 // FIXME! provide more precise location for the parameter
Steve Naroffccef3712009-02-20 22:59:16 +00001392 Diag(MethodLoc, diag::err_object_cannot_be_by_value)
1393 << "passed";
1394 ObjCMethod->setInvalidDecl();
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001395 return 0;
1396 }
Steve Naroff6082c622008-12-09 19:36:17 +00001397 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001398 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001399 ParmVarDecl* Param;
1400 if (originalArgType.isNull())
1401 Param = ParmVarDecl::Create(Context, ObjCMethod,
1402 SourceLocation(/*FIXME*/),
1403 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001404 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001405 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001406 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1407 SourceLocation(/*FIXME*/),
1408 ArgNames[i], argType, originalArgType,
1409 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001410
Chris Lattner0ed844b2008-04-04 06:12:32 +00001411 Param->setObjCDeclQualifier(
1412 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1413 Params.push_back(Param);
1414 }
1415
Chris Lattner38af2de2009-02-20 21:35:13 +00001416 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001417 ObjCMethod->setObjCDeclQualifier(
1418 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1419 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001420
1421 if (AttrList)
1422 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001423
1424 // For implementations (which can be very "coarse grain"), we add the
1425 // method now. This allows the AST to implement lookup methods that work
1426 // incrementally (without waiting until we parse the @end). It also allows
1427 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001428 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001429 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001430 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001431 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001432 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001433 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001434 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001435 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001436 }
1437 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001438 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001439 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001440 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001441 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001442 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001443 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001444 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001445 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001446 }
1447 }
1448 if (PrevMethod) {
1449 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001450 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001451 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001452 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001453 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001454 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001455}
1456
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001457void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1458 SourceLocation Loc,
1459 unsigned &Attributes) {
1460 // FIXME: Improve the reported location.
1461
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001462 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001463 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001464 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1465 ObjCDeclSpec::DQ_PR_assign |
1466 ObjCDeclSpec::DQ_PR_copy |
1467 ObjCDeclSpec::DQ_PR_retain))) {
1468 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1469 "readwrite" :
1470 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1471 "assign" :
1472 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1473 "copy" : "retain";
1474
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001475 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001476 diag::err_objc_property_attr_mutually_exclusive :
1477 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001478 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001479 }
1480
1481 // Check for copy or retain on non-object types.
1482 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1483 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001484 Diag(Loc, diag::err_objc_property_requires_object)
1485 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001486 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1487 }
1488
1489 // Check for more than one of { assign, copy, retain }.
1490 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1491 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001492 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1493 << "assign" << "copy";
1494 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001495 }
1496 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001497 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1498 << "assign" << "retain";
1499 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001500 }
1501 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1502 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001503 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1504 << "copy" << "retain";
1505 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001506 }
1507 }
1508
1509 // Warn if user supplied no assignment attribute, property is
1510 // readwrite, and this is an object type.
1511 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1512 ObjCDeclSpec::DQ_PR_retain)) &&
1513 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1514 Context.isObjCObjectPointerType(PropertyTy)) {
1515 // Skip this warning in gc-only mode.
1516 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1517 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1518
1519 // If non-gc code warn that this is likely inappropriate.
1520 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1521 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1522
1523 // FIXME: Implement warning dependent on NSCopying being
1524 // implemented. See also:
1525 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1526 // (please trim this list while you are at it).
1527 }
1528}
1529
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001530Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1531 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001532 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001533 Selector GetterSel,
1534 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001535 DeclTy *ClassCategory,
1536 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001537 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001538 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001539 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1540 // default is readwrite!
1541 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1542 // property is defaulted to 'assign' if it is readwrite and is
1543 // not retain or copy
1544 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1545 (isReadWrite &&
1546 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1547 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1548 QualType T = GetTypeForDeclarator(FD.D, S);
1549 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001550
1551 // May modify Attributes.
1552 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001553
1554 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1555 if (!CDecl->getIdentifier()) {
1556 // This is an anonymous category. property requires special
1557 // handling.
1558 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1559 if (ObjCPropertyDecl *PIDecl =
1560 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1561 // property 'PIDecl's readonly attribute will be over-ridden
1562 // with anonymous category's readwrite property attribute!
1563 unsigned PIkind = PIDecl->getPropertyAttributes();
1564 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001565 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001566 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1567 Diag(AtLoc, diag::warn_property_attr_mismatch);
1568 PIDecl->makeitReadWriteAttribute();
1569 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1570 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1571 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1572 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1573 PIDecl->setSetterName(SetterSel);
1574 // FIXME: use a common routine with addPropertyMethods.
1575 ObjCMethodDecl *SetterDecl =
1576 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1577 Context.VoidTy,
1578 ICDecl,
1579 true, false, true,
1580 ObjCMethodDecl::Required);
Chris Lattner38af2de2009-02-20 21:35:13 +00001581 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001582 SourceLocation(),
1583 FD.D.getIdentifier(),
Chris Lattner38af2de2009-02-20 21:35:13 +00001584 T, VarDecl::None, 0);
1585 SetterDecl->setMethodParams(&Argument, 1, Context);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001586 PIDecl->setSetterMethodDecl(SetterDecl);
1587 }
1588 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001589 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001590 *isOverridingProperty = true;
1591 return 0;
1592 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001593 // No matching property found in the main class. Just fall thru
1594 // and add property to the anonymous category. It looks like
Ben Laurie9af5e672009-02-16 09:18:41 +00001595 // it works as is. This category becomes just like a category
1596 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001597 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001598 Diag(CDecl->getLocation(), diag::err_continuation_class);
1599 *isOverridingProperty = true;
1600 return 0;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001601 }
1602 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001603
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001604 Type *t = T.getTypePtr();
1605 if (t->isArrayType() || t->isFunctionType())
1606 Diag(AtLoc, diag::err_property_type) << T;
1607
Steve Naroff93983f82009-01-11 12:47:58 +00001608 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1609 assert(DC && "ClassDecl is not a DeclContext");
1610 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001611 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001612 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001613
1614 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001615
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001616 // Regardless of setter/getter attribute, we save the default getter/setter
1617 // selector names in anticipation of declaration of setter/getter methods.
1618 PDecl->setGetterName(GetterSel);
1619 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001620
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001621 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001622 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001623
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001624 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001625 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001626
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001627 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001628 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001629
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001630 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001631 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001632
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001633 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001634 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001635
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001636 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001637 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001638
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001639 if (isAssign)
1640 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1641
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001642 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001643 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001644
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001645 if (MethodImplKind == tok::objc_required)
1646 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1647 else if (MethodImplKind == tok::objc_optional)
1648 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1649
Chris Lattner4d391482007-12-12 07:09:47 +00001650 return PDecl;
1651}
1652
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001653/// ActOnPropertyImplDecl - This routine performs semantic checks and
1654/// builds the AST node for a property implementation declaration; declared
1655/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001656///
1657Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1658 SourceLocation PropertyLoc,
1659 bool Synthesize,
1660 DeclTy *ClassCatImpDecl,
1661 IdentifierInfo *PropertyId,
1662 IdentifierInfo *PropertyIvar) {
1663 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1664 // Make sure we have a context for the property implementation declaration.
1665 if (!ClassImpDecl) {
1666 Diag(AtLoc, diag::error_missing_property_context);
1667 return 0;
1668 }
1669 ObjCPropertyDecl *property = 0;
1670 ObjCInterfaceDecl* IDecl = 0;
1671 // Find the class or category class where this property must have
1672 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001673 ObjCImplementationDecl *IC = 0;
1674 ObjCCategoryImplDecl* CatImplClass = 0;
1675 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001676 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001677 // We always synthesize an interface for an implementation
1678 // without an interface decl. So, IDecl is always non-zero.
1679 assert(IDecl &&
1680 "ActOnPropertyImplDecl - @implementation without @interface");
1681
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001682 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001683 property = IDecl->FindPropertyDeclaration(PropertyId);
1684 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001685 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001686 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001687 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001688 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001689 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001690 if (Synthesize) {
1691 Diag(AtLoc, diag::error_synthesize_category_decl);
1692 return 0;
1693 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001694 IDecl = CatImplClass->getClassInterface();
1695 if (!IDecl) {
1696 Diag(AtLoc, diag::error_missing_property_interface);
1697 return 0;
1698 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001699 ObjCCategoryDecl *Category =
1700 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1701
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001702 // If category for this implementation not found, it is an error which
1703 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001704 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001705 return 0;
1706 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001707 property = Category->FindPropertyDeclaration(PropertyId);
1708 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001709 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001710 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001711 return 0;
1712 }
1713 }
1714 else {
1715 Diag(AtLoc, diag::error_bad_property_context);
1716 return 0;
1717 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001718 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001719 // Check that we have a valid, previously declared ivar for @synthesize
1720 if (Synthesize) {
1721 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001722 if (!PropertyIvar)
1723 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001724 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniana5afdd02009-02-16 19:35:27 +00001725 Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001726 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001727 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001728 return 0;
1729 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001730 QualType PropType = Context.getCanonicalType(property->getType());
1731 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1732
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001733 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001734 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001735 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001736 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001737 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001738 return 0;
1739 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001740 else {
1741 // FIXME! Rules for properties are somewhat different that those
1742 // for assignments. Use a new routine to consolidate all cases;
1743 // specifically for property redeclarations as well as for ivars.
1744 QualType lhsType =
1745 Context.getCanonicalType(PropType).getUnqualifiedType();
1746 QualType rhsType =
1747 Context.getCanonicalType(IvarType).getUnqualifiedType();
1748 if (lhsType != rhsType &&
1749 lhsType->isArithmeticType()) {
1750 Diag(PropertyLoc, diag::error_property_ivar_type)
1751 << property->getDeclName() << Ivar->getDeclName();
1752 return 0;
1753 }
1754 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001755 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001756 } else if (PropertyIvar) {
1757 // @dynamic
1758 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1759 return 0;
1760 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001761 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001762 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001763 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1764 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001765 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001766 ObjCPropertyImplDecl::Synthesize
1767 : ObjCPropertyImplDecl::Dynamic),
1768 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001769 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001770 if (IC) {
1771 if (Synthesize)
1772 if (ObjCPropertyImplDecl *PPIDecl =
1773 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1774 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1775 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1776 << PropertyIvar;
1777 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1778 }
1779
1780 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1781 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1782 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1783 return 0;
1784 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001785 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001786 }
1787 else {
1788 if (Synthesize)
1789 if (ObjCPropertyImplDecl *PPIDecl =
1790 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1791 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1792 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1793 << PropertyIvar;
1794 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1795 }
1796
1797 if (ObjCPropertyImplDecl *PPIDecl =
1798 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1799 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1800 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1801 return 0;
1802 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001803 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001804 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001805
1806 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001807}
Anders Carlsson15281452008-11-04 16:57:32 +00001808
Chris Lattnercc98eac2008-12-17 07:13:27 +00001809bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001810 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001811 return false;
1812
1813 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1814 D->setInvalidDecl();
1815
1816 return true;
1817}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001818
1819/// Collect the instance variables declared in an Objective-C object. Used in
1820/// the creation of structures from objects using the @defs directive.
1821/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1822/// part of the AST generation logic of @defs.
1823static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1824 ASTContext& Ctx,
1825 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1826 if (Class->getSuperClass())
1827 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1828
1829 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1830 for (ObjCInterfaceDecl::ivar_iterator
1831 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1832
1833 ObjCIvarDecl* ID = *I;
1834 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1835 ID->getLocation(),
1836 ID->getIdentifier(),
1837 ID->getType(),
1838 ID->getBitWidth()));
1839 }
1840}
1841
1842/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1843/// instance variables of ClassName into Decls.
1844void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1845 IdentifierInfo *ClassName,
1846 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1847 // Check that ClassName is a valid class
1848 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1849 if (!Class) {
1850 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1851 return;
1852 }
1853 // Collect the instance variables
1854 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1855
1856 // Introduce all of these fields into the appropriate scope.
1857 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1858 D != Decls.end(); ++D) {
1859 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1860 if (getLangOptions().CPlusPlus)
1861 PushOnScopeChains(cast<FieldDecl>(FD), S);
1862 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001863 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001864 }
1865}
1866