blob: dc8ce062f0b49a57a860d2f063f3073c9dbc087d [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 Lattner58cce3b2008-03-16 01:07:14 +000049 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +000050 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +000051 if (IdentifierInfo *II = PDecl->getIdentifier())
Argyrios Kyrtzidis642e38b2008-04-27 13:30:35 +000052 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000053 }
54}
55
Chris Lattner7caeabd2008-07-21 22:17:28 +000056Sema::DeclTy *Sema::
57ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
58 IdentifierInfo *ClassName, SourceLocation ClassLoc,
59 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000060 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000061 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000062 assert(ClassName && "Missing class identifier");
63
64 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000065 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000066 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000067 // Maybe we will complain about the shadowed template parameter.
68 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
69 // Just pretend that we didn't see the previous declaration.
70 PrevDecl = 0;
71 }
72
Ted Kremeneka526c5c2008-01-07 19:49:32 +000073 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000074 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000075 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000076 }
77
Ted Kremeneka526c5c2008-01-07 19:49:32 +000078 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000079 if (IDecl) {
80 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000081 if (!IDecl->isForwardDecl()) {
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);
111 if (PrevDecl && SuperClassDecl == 0) {
112 // The previous declaration was not a class decl. Check if we have a
113 // typedef. If we do, get the underlying class type.
114 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
115 QualType T = TDecl->getUnderlyingType();
116 if (T->isObjCInterfaceType()) {
117 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
118 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
119 }
120 }
121 // This handles the following case:
122 //
123 // typedef int SuperClass;
124 // @interface MyClass : SuperClass {} @end
125 //
126 if (!SuperClassDecl) {
127 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
128 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
129 }
130 }
131 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
132 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000133 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000134 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000135 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000136 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000137 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000138 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000139 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000140 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000141 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000142 IDecl->setLocEnd(SuperLoc);
143 } else { // we have a root class.
144 IDecl->setLocEnd(ClassLoc);
145 }
146
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000147 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000148 if (NumProtoRefs) {
149 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000150 IDecl->setLocEnd(EndProtoLoc);
151 }
Anders Carlsson15281452008-11-04 16:57:32 +0000152
153 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000154 return IDecl;
155}
156
157/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000158/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000159Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
160 IdentifierInfo *AliasName,
161 SourceLocation AliasLocation,
162 IdentifierInfo *ClassName,
163 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000164 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000165 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000166 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000167 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000168 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000169 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000170 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000171 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000172 return 0;
173 }
174 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000175 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000176 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
177 QualType T = TDecl->getUnderlyingType();
178 if (T->isObjCInterfaceType()) {
179 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
180 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000181 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000182 }
183 }
184 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000185 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
186 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000187 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000188 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000189 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000190 return 0;
191 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000192
193 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000195 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000196
197 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000198
199 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000200 CurContext->addDecl(AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000201 if (!CheckObjCDeclScope(AliasDecl))
202 TUScope->AddDecl(AliasDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000203
Chris Lattner4d391482007-12-12 07:09:47 +0000204 return AliasDecl;
205}
206
Chris Lattnere13b9592008-07-26 04:03:38 +0000207Sema::DeclTy *
208Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
209 IdentifierInfo *ProtocolName,
210 SourceLocation ProtocolLoc,
211 DeclTy * const *ProtoRefs,
212 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000213 SourceLocation EndProtoLoc,
214 AttributeList *AttrList) {
215 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000216 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000217 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000218 if (PDecl) {
219 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000220 if (!PDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000221 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000222 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000223 // Just return the protocol we already had.
224 // FIXME: don't leak the objects passed in!
225 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000226 }
Steve Narofff11b5082008-08-13 16:39:22 +0000227 // Make sure the cached decl gets a valid start location.
228 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000229 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000230 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000231 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
232 AtProtoInterfaceLoc,ProtocolName);
233 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000234 CurContext->addDecl(PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000235 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000236 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000237 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000238 if (AttrList)
239 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000240 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000241 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000242 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000243 PDecl->setLocEnd(EndProtoLoc);
244 }
Anders Carlsson15281452008-11-04 16:57:32 +0000245
246 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000247 return PDecl;
248}
249
250/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000251/// issues an error if they are not declared. It returns list of
252/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000253void
Chris Lattnere13b9592008-07-26 04:03:38 +0000254Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000255 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000256 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000257 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000258 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000259 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
260 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000261 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000262 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000263 continue;
264 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000265
266 if (PDecl->getAttr<UnavailableAttr>())
267 Diag(ProtocolId[i].second, diag::warn_unavailable) <<
268 PDecl->getDeclName();
269 if (PDecl->getAttr<DeprecatedAttr>())
270 Diag(ProtocolId[i].second, diag::warn_deprecated) <<
271 PDecl->getDeclName();
Chris Lattnereacc3922008-07-26 03:47:43 +0000272
273 // If this is a forward declaration and we are supposed to warn in this
274 // case, do it.
275 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000276 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000277 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000278 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000279 }
280}
281
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000282/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000283/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000284///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000285void
286Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
287 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000288 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000289 ObjCPropertyDecl::PropertyAttributeKind CAttr =
290 Property->getPropertyAttributes();
291 ObjCPropertyDecl::PropertyAttributeKind SAttr =
292 SuperProperty->getPropertyAttributes();
293 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
294 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000295 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000296 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000297 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
298 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000299 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000300 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000301 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
302 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
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() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000305
306 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
307 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000308 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000309 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000310 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000311 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000312 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000313 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000314 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000315 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000316
Chris Lattner717250a2008-07-26 20:50:02 +0000317 if (Context.getCanonicalType(Property->getType()) !=
318 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000319 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000320 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000321
322}
323
324/// ComparePropertiesInBaseAndSuper - This routine compares property
325/// declarations in base and its super class, if any, and issues
326/// diagnostics in a variety of inconsistant situations.
327///
328void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000329Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000330 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
331 if (!SDecl)
332 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000333 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000334 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
335 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000336 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000337 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000338 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
339 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000340 ObjCPropertyDecl *PDecl = (*I);
341 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000342 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000343 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000344 }
345 }
346}
347
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000348/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
349/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000350/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000351void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000352Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000353 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000354 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
355 if (!IDecl) {
356 // Category
357 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
358 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff09c47192009-01-09 15:36:25 +0000359 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
360 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000361 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000362 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000363 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000364 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000365 CP != CE; ++CP)
366 if ((*CP)->getIdentifier() == Pr->getIdentifier())
367 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000368 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000369 // Property protocol already exist in class. Diagnose any mismatch.
370 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
371 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000372 return;
373 }
Steve Naroff09c47192009-01-09 15:36:25 +0000374 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
375 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000376 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000377 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000378 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000379 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000380 CP != CE; ++CP)
381 if ((*CP)->getIdentifier() == Pr->getIdentifier())
382 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000383 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000384 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000385 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000386 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000387}
388
389/// MergeProtocolPropertiesIntoClass - This routine merges properties
390/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000391/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000392///
393
394void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000395Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000396 DeclTy *MergeItsProtocols) {
397 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000398 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
399
400 if (!IDecl) {
401 // Category
402 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
403 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
404 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
405 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
406 E = MDecl->protocol_end(); P != E; ++P)
407 // Merge properties of category (*P) into IDECL's
408 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
409
410 // Go thru the list of protocols for this category and recursively merge
411 // their properties into this class as well.
412 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
413 E = CatDecl->protocol_end(); P != E; ++P)
414 MergeProtocolPropertiesIntoClass(CatDecl, *P);
415 } else {
416 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
417 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
418 E = MD->protocol_end(); P != E; ++P)
419 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
420 }
421 return;
422 }
423
Chris Lattnerb752f282008-07-21 07:06:49 +0000424 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000425 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
426 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000427 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000428 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
429
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000430 // Go thru the list of protocols for this class and recursively merge
431 // their properties into this class as well.
432 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
433 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000434 MergeProtocolPropertiesIntoClass(IDecl, *P);
435 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000436 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
437 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
438 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000439 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000440 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000441}
442
Chris Lattner4d391482007-12-12 07:09:47 +0000443/// ActOnForwardProtocolDeclaration -
444Action::DeclTy *
445Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000446 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000447 unsigned NumElts,
448 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000449 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000450
451 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000452 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000453 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000454 if (PDecl == 0) { // Not already seen?
455 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
456 IdentList[i].second, Ident);
457 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000458 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000459 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000460 if (attrList)
461 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000462 Protocols.push_back(PDecl);
463 }
Anders Carlsson15281452008-11-04 16:57:32 +0000464
465 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000466 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000467 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000468 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000469 CheckObjCDeclScope(PDecl);
470 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000471}
472
Chris Lattner7caeabd2008-07-21 22:17:28 +0000473Sema::DeclTy *Sema::
474ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
475 IdentifierInfo *ClassName, SourceLocation ClassLoc,
476 IdentifierInfo *CategoryName,
477 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000478 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000479 unsigned NumProtoRefs,
480 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000481 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000482
Chris Lattner61f9d412008-03-16 20:34:23 +0000483 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000484 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
485 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000486 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000487 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000488
489 /// Check that class of this category is already completely declared.
490 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000491 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffd100c802008-06-05 15:03:27 +0000492 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000493 /// Check for duplicate interface declaration for this category
494 ObjCCategoryDecl *CDeclChain;
495 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
496 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000497 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000498 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner3c73c412008-11-19 08:23:25 +0000499 << ClassName << CategoryName;
Chris Lattner6ff0fc32008-11-23 22:38:38 +0000500 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000501 break;
502 }
Chris Lattner4d391482007-12-12 07:09:47 +0000503 }
Steve Naroffd100c802008-06-05 15:03:27 +0000504 if (!CDeclChain)
505 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000506 }
Chris Lattner4d391482007-12-12 07:09:47 +0000507
508 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000509 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
510 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000511 }
Anders Carlsson15281452008-11-04 16:57:32 +0000512
513 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000514 return CDecl;
515}
516
517/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000518/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000519/// object.
520Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
521 SourceLocation AtCatImplLoc,
522 IdentifierInfo *ClassName, SourceLocation ClassLoc,
523 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000524 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000525 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000526 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
527 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000528 /// Check that class of this category is already completely declared.
529 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000530 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000531
Douglas Gregord0434102009-01-09 00:49:46 +0000532 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000533 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000534
Chris Lattner4d391482007-12-12 07:09:47 +0000535 /// TODO: Check that CatName, category name, is not used in another
536 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000537 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000538
539 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000540 return CDecl;
541}
542
543Sema::DeclTy *Sema::ActOnStartClassImplementation(
544 SourceLocation AtClassImplLoc,
545 IdentifierInfo *ClassName, SourceLocation ClassLoc,
546 IdentifierInfo *SuperClassname,
547 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000548 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000549 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000550 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000551 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000552 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000553 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000554 }
555 else {
556 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000557 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000558 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000559 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000560 }
561
562 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000563 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000564 if (SuperClassname) {
565 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000566 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000567 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000568 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
569 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000570 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000571 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000572 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000573 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000574 Diag(SuperClassLoc, diag::err_undef_superclass)
575 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000576 else if (IDecl && IDecl->getSuperClass() != SDecl) {
577 // This implementation and its interface do not have the same
578 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000579 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000580 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000581 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000582 }
583 }
584 }
585
586 if (!IDecl) {
587 // Legacy case of @implementation with no corresponding @interface.
588 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000589
590 // FIXME: Do we support attributes on the @implementation? If so
591 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000592 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
593 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000594 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000595 IDecl->setSuperClass(SDecl);
596 IDecl->setLocEnd(ClassLoc);
597
Douglas Gregord0434102009-01-09 00:49:46 +0000598 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000599 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000600 // Remember that this needs to be removed when the scope is popped.
601 TUScope->AddDecl(IDecl);
602 }
603
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000604 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000605 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000606 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000607
Douglas Gregord0434102009-01-09 00:49:46 +0000608 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000609 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000610
Anders Carlsson15281452008-11-04 16:57:32 +0000611 if (CheckObjCDeclScope(IMPDecl))
612 return IMPDecl;
613
Chris Lattner4d391482007-12-12 07:09:47 +0000614 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000615 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000616 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000617 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000618 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000619 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000620 return IMPDecl;
621}
622
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000623void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
624 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000625 SourceLocation RBrace) {
626 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000627 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000628 if (!IDecl)
629 return;
630 /// Check case of non-existing @interface decl.
631 /// (legacy objective-c @implementation decl without an @interface decl).
632 /// Add implementations's ivar to the synthesize class's ivar list.
633 if (IDecl->ImplicitInterfaceDecl()) {
634 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
635 return;
636 }
637 // If implementation has empty ivar list, just return.
638 if (numIvars == 0)
639 return;
640
641 assert(ivars && "missing @implementation ivars");
642
643 // Check interface's Ivar list against those in the implementation.
644 // names and types must match.
645 //
Chris Lattner4d391482007-12-12 07:09:47 +0000646 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000647 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000648 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
649 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000650 ObjCIvarDecl* ImplIvar = ivars[j++];
651 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000652 assert (ImplIvar && "missing implementation ivar");
653 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000654 if (Context.getCanonicalType(ImplIvar->getType()) !=
655 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000656 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000657 << ImplIvar->getIdentifier()
658 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000659 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000660 }
661 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
662 // as error.
663 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000664 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000665 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000666 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000667 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000668 }
669 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000670 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000671
672 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000673 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000674 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000675 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000676}
677
Steve Naroff3c2eb662008-02-10 21:38:56 +0000678void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
679 bool &IncompleteImpl) {
680 if (!IncompleteImpl) {
681 Diag(ImpLoc, diag::warn_incomplete_impl);
682 IncompleteImpl = true;
683 }
Chris Lattner08631c52008-11-23 21:45:46 +0000684 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000685}
686
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000687void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
688 ObjCMethodDecl *IntfMethodDecl) {
689 bool err = false;
690 QualType ImpMethodQType =
691 Context.getCanonicalType(ImpMethodDecl->getResultType());
692 QualType IntfMethodQType =
693 Context.getCanonicalType(IntfMethodDecl->getResultType());
694 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
695 err = true;
696 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
697 IF=IntfMethodDecl->param_begin(),
698 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
699 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
700 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
701 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
702 err = true;
703 break;
704 }
705 }
706 if (err) {
707 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
708 << ImpMethodDecl->getDeclName();
709 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
710 }
711}
712
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000713/// isPropertyReadonly - Return true if property is readonly, by searching
714/// for the property in the class and in its categories and implementations
715///
716bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
717 ObjCInterfaceDecl *IDecl) const {
718 // by far the most common case.
719 if (!PDecl->isReadOnly())
720 return false;
721 // Even if property is ready only, if interface has a user defined setter,
722 // it is not considered read only.
723 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
724 return false;
725
726 // Main class has the property as 'readonly'. Must search
727 // through the category list to see if the property's
728 // attribute has been over-ridden to 'readwrite'.
729 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
730 Category; Category = Category->getNextClassCategory()) {
731 // Even if property is ready only, if a category has a user defined setter,
732 // it is not considered read only.
733 if (Category->getInstanceMethod(PDecl->getSetterName()))
734 return false;
735 ObjCPropertyDecl *P =
736 Category->FindPropertyDeclaration(PDecl->getIdentifier());
737 if (P && !P->isReadOnly())
738 return false;
739 }
740
741 // Also, check for definition of a setter method in the implementation if
742 // all else failed.
743 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
744 if (ObjCImplementationDecl *IMD =
745 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
746 if (IMD->getInstanceMethod(PDecl->getSetterName()))
747 return false;
748 }
749 else if (ObjCCategoryImplDecl *CIMD =
750 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
751 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
752 return false;
753 }
754 }
755 return true;
756}
757
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000758/// FIXME: Type hierarchies in Objective-C can be deep. We could most
759/// likely improve the efficiency of selector lookups and type
760/// checking by associating with each protocol / interface / category
761/// the flattened instance tables. If we used an immutable set to keep
762/// the table then it wouldn't add significant memory cost and it
763/// would be handy for lookups.
764
Steve Naroffefe7f362008-02-08 22:06:17 +0000765/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000766/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000767void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
768 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000769 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000770 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000771 const llvm::DenseSet<Selector> &ClsMap,
772 ObjCInterfaceDecl *IDecl) {
773 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
774
775 // If a method lookup fails locally we still need to look and see if
776 // the method was implemented by a base class or an inherited
777 // protocol. This lookup is slow, but occurs rarely in correct code
778 // and otherwise would terminate in a warning.
779
Chris Lattner4d391482007-12-12 07:09:47 +0000780 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000781 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000782 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000783 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000784 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000785 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000786 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000787 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000788 }
789 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000790 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000791 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000792 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000793 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
794 !ClsMap.count(method->getSelector()) &&
795 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000796 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000797 }
Chris Lattner780f3292008-07-21 21:32:27 +0000798 // Check on this protocols's referenced protocols, recursively.
799 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
800 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000801 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000802}
803
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000804void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
805 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000806 llvm::DenseSet<Selector> InsMap;
807 // Check and see if instance methods in class interface have been
808 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000809 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000810 E = IMPDecl->instmeth_end(); I != E; ++I)
811 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000812
813 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000814 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000815 E = IDecl->instmeth_end(); I != E; ++I) {
816 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000817 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000818 continue;
Fariborz Jahaniande739412008-12-05 01:35:25 +0000819 }
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000820
821 ObjCMethodDecl *ImpMethodDecl =
822 IMPDecl->getInstanceMethod((*I)->getSelector());
823 ObjCMethodDecl *IntfMethodDecl =
824 IDecl->getInstanceMethod((*I)->getSelector());
825 assert(IntfMethodDecl &&
826 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
827 // ImpMethodDecl may be null as in a @dynamic property.
828 if (ImpMethodDecl)
829 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
830 }
Chris Lattner4c525092007-12-12 17:58:05 +0000831
Chris Lattner4d391482007-12-12 07:09:47 +0000832 llvm::DenseSet<Selector> ClsMap;
833 // Check and see if class methods in class interface have been
834 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000835 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000836 E = IMPDecl->classmeth_end(); I != E; ++I)
837 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000838
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000839 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000840 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000841 if (!ClsMap.count((*I)->getSelector()))
842 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000843 else {
844 ObjCMethodDecl *ImpMethodDecl =
845 IMPDecl->getClassMethod((*I)->getSelector());
846 ObjCMethodDecl *IntfMethodDecl =
847 IDecl->getClassMethod((*I)->getSelector());
848 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
849 }
850
Chris Lattner4d391482007-12-12 07:09:47 +0000851
852 // Check the protocol list for unimplemented methods in the @implementation
853 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000854 const ObjCList<ObjCProtocolDecl> &Protocols =
855 IDecl->getReferencedProtocols();
856 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
857 E = Protocols.end(); I != E; ++I)
858 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000859 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000860}
861
862/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000863/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000864void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
865 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000866 llvm::DenseSet<Selector> InsMap;
867 // Check and see if instance methods in category interface have been
868 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000869 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000870 E = CatImplDecl->instmeth_end(); I != E; ++I)
871 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000872
873 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000874 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000875 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000876 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000877 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000878 else {
879 ObjCMethodDecl *ImpMethodDecl =
880 CatImplDecl->getInstanceMethod((*I)->getSelector());
881 ObjCMethodDecl *IntfMethodDecl =
882 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000883 assert(IntfMethodDecl &&
884 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
885 // ImpMethodDecl may be null as in a @dynamic property.
886 if (ImpMethodDecl)
887 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000888 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000889
Chris Lattner4d391482007-12-12 07:09:47 +0000890 llvm::DenseSet<Selector> ClsMap;
891 // Check and see if class methods in category interface have been
892 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000893 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000894 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
895 I != E; ++I)
896 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000897
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000898 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000899 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000900 if (!ClsMap.count((*I)->getSelector()))
901 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000902 else {
903 ObjCMethodDecl *ImpMethodDecl =
904 CatImplDecl->getClassMethod((*I)->getSelector());
905 ObjCMethodDecl *IntfMethodDecl =
906 CatClassDecl->getClassMethod((*I)->getSelector());
907 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
908 }
Chris Lattner4d391482007-12-12 07:09:47 +0000909 // Check the protocol list for unimplemented methods in the @implementation
910 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000911 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
912 E = CatClassDecl->protocol_end(); PI != E; ++PI)
913 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000914 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000915}
916
917/// ActOnForwardClassDeclaration -
918Action::DeclTy *
919Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +0000920 IdentifierInfo **IdentList,
921 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000922 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000923
924 for (unsigned i = 0; i != NumElts; ++i) {
925 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000926 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000927 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000928 // Maybe we will complain about the shadowed template parameter.
929 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
930 // Just pretend that we didn't see the previous declaration.
931 PrevDecl = 0;
932 }
933
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000934 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000935 // GCC apparently allows the following idiom:
936 //
937 // typedef NSObject < XCElementTogglerP > XCElementToggler;
938 // @class XCElementToggler;
939 //
940 // FIXME: Make an extension?
941 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
942 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000943 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000944 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000945 }
Chris Lattner4d391482007-12-12 07:09:47 +0000946 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000947 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000948 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000949 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
950 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000951 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000952
Douglas Gregord0434102009-01-09 00:49:46 +0000953 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000954 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000955 // Remember that this needs to be removed when the scope is popped.
956 TUScope->AddDecl(IDecl);
957 }
958
959 Interfaces.push_back(IDecl);
960 }
961
Douglas Gregord0434102009-01-09 00:49:46 +0000962 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000963 &Interfaces[0],
964 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000965 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000966 CheckObjCDeclScope(CDecl);
967 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000968}
969
970
971/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
972/// returns true, or false, accordingly.
973/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000974bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000975 const ObjCMethodDecl *PrevMethod,
976 bool matchBasedOnSizeAndAlignment) {
977 QualType T1 = Context.getCanonicalType(Method->getResultType());
978 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
979
980 if (T1 != T2) {
981 // The result types are different.
982 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000983 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000984 // Incomplete types don't have a size and alignment.
985 if (T1->isIncompleteType() || T2->isIncompleteType())
986 return false;
987 // Check is based on size and alignment.
988 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
989 return false;
990 }
991 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
992 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
993 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
994 if (T1 != T2) {
995 // The result types are different.
996 if (!matchBasedOnSizeAndAlignment)
997 return false;
998 // Incomplete types don't have a size and alignment.
999 if (T1->isIncompleteType() || T2->isIncompleteType())
1000 return false;
1001 // Check is based on size and alignment.
1002 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1003 return false;
1004 }
Chris Lattner4d391482007-12-12 07:09:47 +00001005 }
1006 return true;
1007}
1008
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001009void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
1010 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001011 if (!FirstMethod.Method) {
1012 // Haven't seen a method with this selector name yet - add it.
1013 FirstMethod.Method = Method;
1014 FirstMethod.Next = 0;
1015 } else {
1016 // We've seen a method with this name, now check the type signature(s).
1017 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1018
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001019 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001020 Next = Next->Next)
1021 match = MatchTwoMethodDeclarations(Method, Next->Method);
1022
1023 if (!match) {
1024 // We have a new signature for an existing method - add it.
1025 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +00001026 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +00001027 }
1028 }
1029}
1030
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001031// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001032ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1033 SourceRange R) {
1034 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001035 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001036
1037 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001038 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1039 // This checks if the methods differ by size & alignment.
1040 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1041 issueWarning = true;
1042 }
1043 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001044 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001045 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001046 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001047 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001048 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001049 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001050 }
1051 return MethList.Method;
1052}
1053
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001054void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1055 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001056 if (!FirstMethod.Method) {
1057 // Haven't seen a method with this selector name yet - add it.
1058 FirstMethod.Method = Method;
1059 FirstMethod.Next = 0;
1060 } else {
1061 // We've seen a method with this name, now check the type signature(s).
1062 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1063
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001064 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001065 Next = Next->Next)
1066 match = MatchTwoMethodDeclarations(Method, Next->Method);
1067
1068 if (!match) {
1069 // We have a new signature for an existing method - add it.
1070 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001071 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001072 FirstMethod.Next = OMI;
1073 }
1074 }
1075}
1076
Steve Naroff0701bbb2009-01-08 17:28:14 +00001077/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1078/// have the property type and issue diagnostics if they don't.
1079/// Also synthesize a getter/setter method if none exist (and update the
1080/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1081/// methods is the "right" thing to do.
1082void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1083 ObjCContainerDecl *CD) {
1084 ObjCMethodDecl *GetterMethod, *SetterMethod;
1085
1086 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1087 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1088
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001089 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001090 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001091 Diag(property->getLocation(),
1092 diag::err_accessor_property_type_mismatch)
1093 << property->getDeclName()
1094 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001095 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1096 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001097
1098 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001099 if (Context.getCanonicalType(SetterMethod->getResultType())
1100 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001101 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1102 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001103 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001104 Diag(property->getLocation(),
1105 diag::err_accessor_property_type_mismatch)
1106 << property->getDeclName()
1107 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001108 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1109 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001110 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001111
1112 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001113 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001114 // FIXME: The synthesized property we set here is misleading. We
1115 // almost always synthesize these methods unless the user explicitly
1116 // provided prototypes (which is odd, but allowed). Sema should be
1117 // typechecking that the declarations jive in that situation (which
1118 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001119 if (!GetterMethod) {
1120 // No instance method of same name as property getter name was found.
1121 // Declare a getter method and add it to the list of methods
1122 // for this class.
1123 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1124 property->getLocation(), property->getGetterName(),
1125 property->getType(), CD, true, false, true,
1126 (property->getPropertyImplementation() ==
1127 ObjCPropertyDecl::Optional) ?
1128 ObjCMethodDecl::Optional :
1129 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001130 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001131 } else
1132 // A user declared getter will be synthesize when @synthesize of
1133 // the property with the same name is seen in the @implementation
1134 GetterMethod->setIsSynthesized();
1135 property->setGetterMethodDecl(GetterMethod);
1136
1137 // Skip setter if property is read-only.
1138 if (!property->isReadOnly()) {
1139 // Find the default setter and if one not found, add one.
1140 if (!SetterMethod) {
1141 // No instance method of same name as property setter name was found.
1142 // Declare a setter method and add it to the list of methods
1143 // for this class.
1144 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1145 property->getLocation(),
1146 property->getSetterName(),
1147 Context.VoidTy, CD, true, false, true,
1148 (property->getPropertyImplementation() ==
1149 ObjCPropertyDecl::Optional) ?
1150 ObjCMethodDecl::Optional :
1151 ObjCMethodDecl::Required);
1152 // Invent the arguments for the setter. We don't bother making a
1153 // nice name for the argument.
1154 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1155 SourceLocation(),
1156 property->getIdentifier(),
1157 property->getType(),
1158 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001159 0);
Steve Naroff92f863b2009-01-08 20:15:03 +00001160 SetterMethod->setMethodParams(&Argument, 1);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001161 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001162 } else
1163 // A user declared setter will be synthesize when @synthesize of
1164 // the property with the same name is seen in the @implementation
1165 SetterMethod->setIsSynthesized();
1166 property->setSetterMethodDecl(SetterMethod);
1167 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001168 // Add any synthesized methods to the global pool. This allows us to
1169 // handle the following, which is supported by GCC (and part of the design).
1170 //
1171 // @interface Foo
1172 // @property double bar;
1173 // @end
1174 //
1175 // void thisIsUnfortunate() {
1176 // id foo;
1177 // double bar = [foo bar];
1178 // }
1179 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001180 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001181 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001182 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001183 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001184}
1185
Steve Naroffa56f6162007-12-18 01:30:32 +00001186// Note: For class/category implemenations, allMethods/allProperties is
1187// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001188void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1189 DeclTy **allMethods, unsigned allNum,
1190 DeclTy **allProperties, unsigned pNum) {
1191 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1192
Steve Naroffa56f6162007-12-18 01:30:32 +00001193 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1194 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001195 // should be true.
1196 if (!ClassDecl)
1197 return;
1198
Chris Lattner4d391482007-12-12 07:09:47 +00001199 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001200 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1201 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001202 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001203
Steve Naroff0701bbb2009-01-08 17:28:14 +00001204 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001205
1206 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1207 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1208 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1209
Chris Lattner4d391482007-12-12 07:09:47 +00001210 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001211 ObjCMethodDecl *Method =
1212 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001213
1214 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001215 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001216 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001217 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001218 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1219 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001220 if ((isInterfaceDeclKind && PrevMethod && !match)
1221 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001222 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001223 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001224 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001225 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001226 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001227 InsMap[Method->getSelector()] = Method;
1228 /// The following allows us to typecheck messages to "id".
1229 AddInstanceMethodToGlobalPool(Method);
1230 }
1231 }
1232 else {
1233 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001234 const ObjCMethodDecl *&PrevMethod = ClsMap[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 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001245 /// The following allows us to typecheck messages to "Class".
1246 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001247 }
1248 }
1249 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001250 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001251 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001252 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001253 ComparePropertiesInBaseAndSuper(I);
1254 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001255 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001256 // Categories are used to extend the class by declaring new methods.
1257 // By the same token, they are also used to add new properties. No
1258 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001259
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001260 // Merge protocol properties into category
1261 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner4d391482007-12-12 07:09:47 +00001262 }
Steve Naroff09c47192009-01-09 15:36:25 +00001263 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1264 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1265 // user-defined setter/getter. It also synthesizes setter/getter methods
1266 // and adds them to the DeclContext and global method pools.
Chris Lattner97a58872009-02-16 18:32:47 +00001267 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1268 E = CDecl->prop_end(); I != E; ++I)
1269 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001270 CDecl->setAtEndLoc(AtEndLoc);
1271 }
1272 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001273 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001274 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001275 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001276 } else if (ObjCCategoryImplDecl* CatImplClass =
1277 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001278 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001279
Chris Lattner4d391482007-12-12 07:09:47 +00001280 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001281 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001282 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001283 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001284 Categories; Categories = Categories->getNextClassCategory()) {
1285 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1286 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1287 break;
1288 }
1289 }
1290 }
1291 }
1292}
1293
1294
1295/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1296/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001297static Decl::ObjCDeclQualifier
1298CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1299 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1300 if (PQTVal & ObjCDeclSpec::DQ_In)
1301 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1302 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1303 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1304 if (PQTVal & ObjCDeclSpec::DQ_Out)
1305 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1306 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1307 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1308 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1309 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1310 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1311 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001312
1313 return ret;
1314}
1315
1316Sema::DeclTy *Sema::ActOnMethodDeclaration(
1317 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001318 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001319 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001320 Selector Sel,
1321 // optional arguments. The number of types/arguments is obtained
1322 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001323 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001324 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001325 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1326 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001327 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001328
1329 // Make sure we can establish a context for the method.
1330 if (!ClassDecl) {
1331 Diag(MethodLoc, diag::error_missing_method_context);
1332 return 0;
1333 }
Chris Lattner4d391482007-12-12 07:09:47 +00001334 QualType resultDeclType;
1335
1336 if (ReturnType)
1337 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1338 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001339 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001340
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001341 ObjCMethodDecl* ObjCMethod =
1342 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001343 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001344 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001345 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001346 MethodDeclKind == tok::objc_optional ?
1347 ObjCMethodDecl::Optional :
1348 ObjCMethodDecl::Required);
1349
Chris Lattner0ed844b2008-04-04 06:12:32 +00001350 llvm::SmallVector<ParmVarDecl*, 16> Params;
1351
1352 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1353 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001354 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001355
Steve Naroff6082c622008-12-09 19:36:17 +00001356 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001357 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001358 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001359 if (argType->isArrayType()) { // (char *[]) -> (char **)
1360 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001361 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001362 }
Steve Naroff6082c622008-12-09 19:36:17 +00001363 else if (argType->isFunctionType())
1364 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001365 else if (argType->isObjCInterfaceType()) {
1366 // FIXME! provide more precise location for the parameter
1367 Diag(MethodLoc, diag::err_object_as_method_param);
1368 return 0;
1369 }
Steve Naroff6082c622008-12-09 19:36:17 +00001370 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001371 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001372 ParmVarDecl* Param;
1373 if (originalArgType.isNull())
1374 Param = ParmVarDecl::Create(Context, ObjCMethod,
1375 SourceLocation(/*FIXME*/),
1376 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001377 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001378 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001379 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1380 SourceLocation(/*FIXME*/),
1381 ArgNames[i], argType, originalArgType,
1382 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001383
Chris Lattner0ed844b2008-04-04 06:12:32 +00001384 Param->setObjCDeclQualifier(
1385 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1386 Params.push_back(Param);
1387 }
1388
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001389 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1390 ObjCMethod->setObjCDeclQualifier(
1391 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1392 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001393
1394 if (AttrList)
1395 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001396
1397 // For implementations (which can be very "coarse grain"), we add the
1398 // method now. This allows the AST to implement lookup methods that work
1399 // incrementally (without waiting until we parse the @end). It also allows
1400 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001401 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001402 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001403 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001404 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001405 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001406 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001407 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001408 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001409 }
1410 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001411 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001412 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001413 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001414 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001415 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001416 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001417 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001418 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001419 }
1420 }
1421 if (PrevMethod) {
1422 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001423 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001424 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001425 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001426 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001427 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001428}
1429
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001430void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1431 SourceLocation Loc,
1432 unsigned &Attributes) {
1433 // FIXME: Improve the reported location.
1434
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001435 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001436 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001437 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1438 ObjCDeclSpec::DQ_PR_assign |
1439 ObjCDeclSpec::DQ_PR_copy |
1440 ObjCDeclSpec::DQ_PR_retain))) {
1441 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1442 "readwrite" :
1443 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1444 "assign" :
1445 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1446 "copy" : "retain";
1447
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001448 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001449 diag::err_objc_property_attr_mutually_exclusive :
1450 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001451 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001452 }
1453
1454 // Check for copy or retain on non-object types.
1455 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1456 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001457 Diag(Loc, diag::err_objc_property_requires_object)
1458 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001459 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1460 }
1461
1462 // Check for more than one of { assign, copy, retain }.
1463 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1464 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001465 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1466 << "assign" << "copy";
1467 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001468 }
1469 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001470 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1471 << "assign" << "retain";
1472 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001473 }
1474 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1475 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001476 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1477 << "copy" << "retain";
1478 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001479 }
1480 }
1481
1482 // Warn if user supplied no assignment attribute, property is
1483 // readwrite, and this is an object type.
1484 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1485 ObjCDeclSpec::DQ_PR_retain)) &&
1486 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1487 Context.isObjCObjectPointerType(PropertyTy)) {
1488 // Skip this warning in gc-only mode.
1489 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1490 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1491
1492 // If non-gc code warn that this is likely inappropriate.
1493 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1494 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1495
1496 // FIXME: Implement warning dependent on NSCopying being
1497 // implemented. See also:
1498 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1499 // (please trim this list while you are at it).
1500 }
1501}
1502
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001503Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1504 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001505 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001506 Selector GetterSel,
1507 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001508 DeclTy *ClassCategory,
1509 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001510 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001511 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001512 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1513 // default is readwrite!
1514 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1515 // property is defaulted to 'assign' if it is readwrite and is
1516 // not retain or copy
1517 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1518 (isReadWrite &&
1519 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1520 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1521 QualType T = GetTypeForDeclarator(FD.D, S);
1522 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001523
1524 // May modify Attributes.
1525 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001526
1527 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1528 if (!CDecl->getIdentifier()) {
1529 // This is an anonymous category. property requires special
1530 // handling.
1531 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1532 if (ObjCPropertyDecl *PIDecl =
1533 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1534 // property 'PIDecl's readonly attribute will be over-ridden
1535 // with anonymous category's readwrite property attribute!
1536 unsigned PIkind = PIDecl->getPropertyAttributes();
1537 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001538 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001539 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1540 Diag(AtLoc, diag::warn_property_attr_mismatch);
1541 PIDecl->makeitReadWriteAttribute();
1542 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1543 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1544 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1545 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1546 PIDecl->setSetterName(SetterSel);
1547 // FIXME: use a common routine with addPropertyMethods.
1548 ObjCMethodDecl *SetterDecl =
1549 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1550 Context.VoidTy,
1551 ICDecl,
1552 true, false, true,
1553 ObjCMethodDecl::Required);
1554 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1555 SetterDecl,
1556 SourceLocation(),
1557 FD.D.getIdentifier(),
1558 T,
1559 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001560 0);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001561 SetterDecl->setMethodParams(&Argument, 1);
1562 PIDecl->setSetterMethodDecl(SetterDecl);
1563 }
1564 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001565 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001566 *isOverridingProperty = true;
1567 return 0;
1568 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001569 // No matching property found in the main class. Just fall thru
1570 // and add property to the anonymous category. It looks like
Ben Laurie9af5e672009-02-16 09:18:41 +00001571 // it works as is. This category becomes just like a category
1572 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001573 } else {
1574 Diag(CDecl->getLocation(), diag::err_continuation_class);
1575 *isOverridingProperty = true;
1576 return 0;
1577 }
1578 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001579
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001580 Type *t = T.getTypePtr();
1581 if (t->isArrayType() || t->isFunctionType())
1582 Diag(AtLoc, diag::err_property_type) << T;
1583
Steve Naroff93983f82009-01-11 12:47:58 +00001584 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1585 assert(DC && "ClassDecl is not a DeclContext");
1586 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001587 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001588 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001589
1590 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001591
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001592 // Regardless of setter/getter attribute, we save the default getter/setter
1593 // selector names in anticipation of declaration of setter/getter methods.
1594 PDecl->setGetterName(GetterSel);
1595 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001596
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001597 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001598 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001599
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001600 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001601 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001602
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001603 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001604 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001605
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001606 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001607 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001608
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001609 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001610 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001611
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001612 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001613 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001614
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001615 if (isAssign)
1616 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1617
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001618 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001619 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001620
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001621 if (MethodImplKind == tok::objc_required)
1622 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1623 else if (MethodImplKind == tok::objc_optional)
1624 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1625
Chris Lattner4d391482007-12-12 07:09:47 +00001626 return PDecl;
1627}
1628
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001629/// ActOnPropertyImplDecl - This routine performs semantic checks and
1630/// builds the AST node for a property implementation declaration; declared
1631/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001632///
1633Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1634 SourceLocation PropertyLoc,
1635 bool Synthesize,
1636 DeclTy *ClassCatImpDecl,
1637 IdentifierInfo *PropertyId,
1638 IdentifierInfo *PropertyIvar) {
1639 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1640 // Make sure we have a context for the property implementation declaration.
1641 if (!ClassImpDecl) {
1642 Diag(AtLoc, diag::error_missing_property_context);
1643 return 0;
1644 }
1645 ObjCPropertyDecl *property = 0;
1646 ObjCInterfaceDecl* IDecl = 0;
1647 // Find the class or category class where this property must have
1648 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001649 ObjCImplementationDecl *IC = 0;
1650 ObjCCategoryImplDecl* CatImplClass = 0;
1651 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001652 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001653 // We always synthesize an interface for an implementation
1654 // without an interface decl. So, IDecl is always non-zero.
1655 assert(IDecl &&
1656 "ActOnPropertyImplDecl - @implementation without @interface");
1657
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001658 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001659 property = IDecl->FindPropertyDeclaration(PropertyId);
1660 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001661 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001662 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001663 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001664 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001665 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001666 if (Synthesize) {
1667 Diag(AtLoc, diag::error_synthesize_category_decl);
1668 return 0;
1669 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001670 IDecl = CatImplClass->getClassInterface();
1671 if (!IDecl) {
1672 Diag(AtLoc, diag::error_missing_property_interface);
1673 return 0;
1674 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001675 ObjCCategoryDecl *Category =
1676 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1677
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001678 // If category for this implementation not found, it is an error which
1679 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001680 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001681 return 0;
1682 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001683 property = Category->FindPropertyDeclaration(PropertyId);
1684 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001685 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001686 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001687 return 0;
1688 }
1689 }
1690 else {
1691 Diag(AtLoc, diag::error_bad_property_context);
1692 return 0;
1693 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001694 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001695 // Check that we have a valid, previously declared ivar for @synthesize
1696 if (Synthesize) {
1697 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001698 if (!PropertyIvar)
1699 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001700 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001701 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001702 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001703 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001704 return 0;
1705 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001706 QualType PropType = Context.getCanonicalType(property->getType());
1707 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1708
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001709 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001710 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001711 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001712 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001713 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001714 return 0;
1715 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001716 else {
1717 // FIXME! Rules for properties are somewhat different that those
1718 // for assignments. Use a new routine to consolidate all cases;
1719 // specifically for property redeclarations as well as for ivars.
1720 QualType lhsType =
1721 Context.getCanonicalType(PropType).getUnqualifiedType();
1722 QualType rhsType =
1723 Context.getCanonicalType(IvarType).getUnqualifiedType();
1724 if (lhsType != rhsType &&
1725 lhsType->isArithmeticType()) {
1726 Diag(PropertyLoc, diag::error_property_ivar_type)
1727 << property->getDeclName() << Ivar->getDeclName();
1728 return 0;
1729 }
1730 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001731 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001732 } else if (PropertyIvar) {
1733 // @dynamic
1734 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1735 return 0;
1736 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001737 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001738 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001739 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1740 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001741 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001742 ObjCPropertyImplDecl::Synthesize
1743 : ObjCPropertyImplDecl::Dynamic),
1744 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001745 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001746 if (IC) {
1747 if (Synthesize)
1748 if (ObjCPropertyImplDecl *PPIDecl =
1749 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1750 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1751 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1752 << PropertyIvar;
1753 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1754 }
1755
1756 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1757 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1758 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1759 return 0;
1760 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001761 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001762 }
1763 else {
1764 if (Synthesize)
1765 if (ObjCPropertyImplDecl *PPIDecl =
1766 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1767 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1768 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1769 << PropertyIvar;
1770 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1771 }
1772
1773 if (ObjCPropertyImplDecl *PPIDecl =
1774 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1775 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1776 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1777 return 0;
1778 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001779 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001780 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001781
1782 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001783}
Anders Carlsson15281452008-11-04 16:57:32 +00001784
Chris Lattnercc98eac2008-12-17 07:13:27 +00001785bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001786 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001787 return false;
1788
1789 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1790 D->setInvalidDecl();
1791
1792 return true;
1793}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001794
1795/// Collect the instance variables declared in an Objective-C object. Used in
1796/// the creation of structures from objects using the @defs directive.
1797/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1798/// part of the AST generation logic of @defs.
1799static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1800 ASTContext& Ctx,
1801 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1802 if (Class->getSuperClass())
1803 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1804
1805 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1806 for (ObjCInterfaceDecl::ivar_iterator
1807 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1808
1809 ObjCIvarDecl* ID = *I;
1810 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1811 ID->getLocation(),
1812 ID->getIdentifier(),
1813 ID->getType(),
1814 ID->getBitWidth()));
1815 }
1816}
1817
1818/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1819/// instance variables of ClassName into Decls.
1820void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1821 IdentifierInfo *ClassName,
1822 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1823 // Check that ClassName is a valid class
1824 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1825 if (!Class) {
1826 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1827 return;
1828 }
1829 // Collect the instance variables
1830 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1831
1832 // Introduce all of these fields into the appropriate scope.
1833 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1834 D != Decls.end(); ++D) {
1835 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1836 if (getLangOptions().CPlusPlus)
1837 PushOnScopeChains(cast<FieldDecl>(FD), S);
1838 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001839 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001840 }
1841}
1842