blob: f6a245a3172cb9e552041fc340246f5a193312e8 [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/Basic/Diagnostic.h"
18#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000019
20using namespace clang;
21
Ted Kremeneka526c5c2008-01-07 19:49:32 +000022/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000023/// and user declared, in the method definition's AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000024void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000025 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff394f3f42008-07-25 17:57:26 +000026 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
27
28 // If we don't have a valid method decl, simply return.
29 if (!MDecl)
30 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000031
32 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000033 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000034 AddInstanceMethodToGlobalPool(MDecl);
35 else
36 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000037
38 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000039 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000040
41 // Create Decl objects for each parameter, entrring them in the scope for
42 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000043
44 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000045 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000046
Daniel Dunbar451318c2008-08-26 06:07:48 +000047 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
48 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000049
Chris Lattner8123a952008-04-10 02:22:51 +000050 // Introduce all of the other parameters into this scope.
Chris Lattner58cce3b2008-03-16 01:07:14 +000051 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +000052 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner04421082008-04-08 04:40:51 +000053 IdentifierInfo *II = PDecl->getIdentifier();
Argyrios Kyrtzidis642e38b2008-04-27 13:30:35 +000054 if (II)
55 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000056 }
57}
58
Chris Lattner7caeabd2008-07-21 22:17:28 +000059Sema::DeclTy *Sema::
60ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000063 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000064 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000065 assert(ClassName && "Missing class identifier");
66
67 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +000068 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Douglas Gregorf57172b2008-12-08 18:40:42 +000069 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000070 // Maybe we will complain about the shadowed template parameter.
71 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
72 // Just pretend that we didn't see the previous declaration.
73 PrevDecl = 0;
74 }
75
Ted Kremeneka526c5c2008-01-07 19:49:32 +000076 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000077 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000078 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000079 }
80
Ted Kremeneka526c5c2008-01-07 19:49:32 +000081 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000082 if (IDecl) {
83 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000084 if (!IDecl->isForwardDecl()) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000085 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000086 Diag(IDecl->getLocation(), diag::note_previous_definition);
87
Steve Naroffcfe8bf32008-11-18 19:15:30 +000088 // Return the previous class interface.
89 // FIXME: don't leak the objects passed in!
90 return IDecl;
91 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000092 IDecl->setLocation(AtInterfaceLoc);
93 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000094 }
Chris Lattnerb752f282008-07-21 07:06:49 +000095 } else {
Douglas Gregord0434102009-01-09 00:49:46 +000096 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000097 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000098 if (AttrList)
99 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000100
Steve Naroff31102512008-04-02 18:30:49 +0000101 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000102 // FIXME: PushOnScopeChains
Douglas Gregor482b77d2009-01-12 23:27:07 +0000103 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000104 // Remember that this needs to be removed when the scope is popped.
105 TUScope->AddDecl(IDecl);
106 }
107
108 if (SuperName) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000109 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000110 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000111 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000112 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000113 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000114 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000115 }
116 else {
117 // Check that super class is previously defined
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000118 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000119
120 if (!SuperClassEntry)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000121 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000122 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
123 else if (SuperClassEntry->isForwardDecl())
124 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner08631c52008-11-23 21:45:46 +0000125 << SuperClassEntry->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000126 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000127 }
128 IDecl->setSuperClass(SuperClassEntry);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000129 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000130 IDecl->setLocEnd(SuperLoc);
131 } else { // we have a root class.
132 IDecl->setLocEnd(ClassLoc);
133 }
134
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000135 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000136 if (NumProtoRefs) {
137 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000138 IDecl->setLocEnd(EndProtoLoc);
139 }
Anders Carlsson15281452008-11-04 16:57:32 +0000140
141 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000142 return IDecl;
143}
144
145/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000146/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000147Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
148 IdentifierInfo *AliasName,
149 SourceLocation AliasLocation,
150 IdentifierInfo *ClassName,
151 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000152 // Look for previous declaration of alias name
Steve Naroffb327ce02008-04-02 14:35:35 +0000153 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000154 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000155 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000156 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000157 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000158 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000159 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000160 return 0;
161 }
162 // Check for class declaration
Steve Naroffb327ce02008-04-02 14:35:35 +0000163 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000164 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
165 QualType T = TDecl->getUnderlyingType();
166 if (T->isObjCInterfaceType()) {
167 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
168 ClassName = IDecl->getIdentifier();
169 CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
170 }
171 }
172 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000173 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
174 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000175 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000176 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000177 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000178 return 0;
179 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000180
181 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000182 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000183 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000184
185 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000186
187 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000188 CurContext->addDecl(AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000189 if (!CheckObjCDeclScope(AliasDecl))
190 TUScope->AddDecl(AliasDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000191
Chris Lattner4d391482007-12-12 07:09:47 +0000192 return AliasDecl;
193}
194
Chris Lattnere13b9592008-07-26 04:03:38 +0000195Sema::DeclTy *
196Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
197 IdentifierInfo *ProtocolName,
198 SourceLocation ProtocolLoc,
199 DeclTy * const *ProtoRefs,
200 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000201 SourceLocation EndProtoLoc,
202 AttributeList *AttrList) {
203 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000204 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000205 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000206 if (PDecl) {
207 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000208 if (!PDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000209 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000210 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000211 // Just return the protocol we already had.
212 // FIXME: don't leak the objects passed in!
213 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000214 }
Steve Narofff11b5082008-08-13 16:39:22 +0000215 // Make sure the cached decl gets a valid start location.
216 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000217 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000218 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000219 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
220 AtProtoInterfaceLoc,ProtocolName);
221 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000222 CurContext->addDecl(PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000223 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000224 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000225 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000226 if (AttrList)
227 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000228 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000229 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000230 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000231 PDecl->setLocEnd(EndProtoLoc);
232 }
Anders Carlsson15281452008-11-04 16:57:32 +0000233
234 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000235 return PDecl;
236}
237
238/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000239/// issues an error if they are not declared. It returns list of
240/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000241void
Chris Lattnere13b9592008-07-26 04:03:38 +0000242Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000243 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000244 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000245 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000246 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000247 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
248 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000249 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000250 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000251 continue;
252 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000253 for (const Attr *attr = PDecl->getAttrs(); attr; attr = attr->getNext()) {
Fariborz Jahanianfc820a42008-12-29 19:57:17 +0000254 if (attr->getKind() == Attr::Unavailable)
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000255 Diag(ProtocolId[i].second, diag::warn_unavailable) <<
256 PDecl->getDeclName();
Fariborz Jahanianfc820a42008-12-29 19:57:17 +0000257 if (attr->getKind() == Attr::Deprecated)
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000258 Diag(ProtocolId[i].second, diag::warn_deprecated) <<
259 PDecl->getDeclName();
260 }
Chris Lattnereacc3922008-07-26 03:47:43 +0000261
262 // If this is a forward declaration and we are supposed to warn in this
263 // case, do it.
264 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000265 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000266 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000267 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000268 }
269}
270
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000271/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000272/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000273///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000274void
275Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
276 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000277 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000278 ObjCPropertyDecl::PropertyAttributeKind CAttr =
279 Property->getPropertyAttributes();
280 ObjCPropertyDecl::PropertyAttributeKind SAttr =
281 SuperProperty->getPropertyAttributes();
282 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
283 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000284 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000285 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000286 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
287 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000288 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000289 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000290 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
291 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000292 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000293 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000294
295 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
296 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000297 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000298 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000299 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000300 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000301 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000302 if (Property->getGetterName() != SuperProperty->getGetterName())
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() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000305
Chris Lattner717250a2008-07-26 20:50:02 +0000306 if (Context.getCanonicalType(Property->getType()) !=
307 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000308 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000309 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000310
311}
312
313/// ComparePropertiesInBaseAndSuper - This routine compares property
314/// declarations in base and its super class, if any, and issues
315/// diagnostics in a variety of inconsistant situations.
316///
317void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000318Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000319 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
320 if (!SDecl)
321 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000322 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000323 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
324 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000325 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000326 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000327 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
328 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000329 ObjCPropertyDecl *PDecl = (*I);
330 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000331 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000332 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000333 }
334 }
335}
336
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000337/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
338/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000339/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000340void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000341Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000342 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000343 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
344 if (!IDecl) {
345 // Category
346 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
347 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff09c47192009-01-09 15:36:25 +0000348 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
349 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000350 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000351 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000352 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000353 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000354 CP != CE; ++CP)
355 if ((*CP)->getIdentifier() == Pr->getIdentifier())
356 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000357 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000358 // Property protocol already exist in class. Diagnose any mismatch.
359 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
360 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000361 return;
362 }
Steve Naroff09c47192009-01-09 15:36:25 +0000363 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
364 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000365 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000366 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000367 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000368 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000369 CP != CE; ++CP)
370 if ((*CP)->getIdentifier() == Pr->getIdentifier())
371 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000372 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000373 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000374 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000375 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000376}
377
378/// MergeProtocolPropertiesIntoClass - This routine merges properties
379/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000380/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000381///
382
383void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000384Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000385 DeclTy *MergeItsProtocols) {
386 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000387 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
388
389 if (!IDecl) {
390 // Category
391 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
392 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
393 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
394 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
395 E = MDecl->protocol_end(); P != E; ++P)
396 // Merge properties of category (*P) into IDECL's
397 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
398
399 // Go thru the list of protocols for this category and recursively merge
400 // their properties into this class as well.
401 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
402 E = CatDecl->protocol_end(); P != E; ++P)
403 MergeProtocolPropertiesIntoClass(CatDecl, *P);
404 } else {
405 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
406 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
407 E = MD->protocol_end(); P != E; ++P)
408 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
409 }
410 return;
411 }
412
Chris Lattnerb752f282008-07-21 07:06:49 +0000413 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000414 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
415 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000416 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000417 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
418
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000419 // Go thru the list of protocols for this class and recursively merge
420 // their properties into this class as well.
421 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
422 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000423 MergeProtocolPropertiesIntoClass(IDecl, *P);
424 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000425 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
426 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
427 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000428 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000429 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000430}
431
Chris Lattner4d391482007-12-12 07:09:47 +0000432/// ActOnForwardProtocolDeclaration -
433Action::DeclTy *
434Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000435 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000436 unsigned NumElts,
437 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000438 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000439
440 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000441 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000442 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000443 if (PDecl == 0) { // Not already seen?
444 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
445 IdentList[i].second, Ident);
446 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000447 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000448 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000449 if (attrList)
450 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000451 Protocols.push_back(PDecl);
452 }
Anders Carlsson15281452008-11-04 16:57:32 +0000453
454 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000455 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000456 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000457 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000458 CheckObjCDeclScope(PDecl);
459 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000460}
461
Chris Lattner7caeabd2008-07-21 22:17:28 +0000462Sema::DeclTy *Sema::
463ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
464 IdentifierInfo *ClassName, SourceLocation ClassLoc,
465 IdentifierInfo *CategoryName,
466 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000467 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000468 unsigned NumProtoRefs,
469 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000470 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000471
Chris Lattner61f9d412008-03-16 20:34:23 +0000472 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000473 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
474 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000475 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000476 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000477
478 /// Check that class of this category is already completely declared.
479 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000480 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffd100c802008-06-05 15:03:27 +0000481 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000482 /// Check for duplicate interface declaration for this category
483 ObjCCategoryDecl *CDeclChain;
484 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
485 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000486 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000487 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner3c73c412008-11-19 08:23:25 +0000488 << ClassName << CategoryName;
Chris Lattner6ff0fc32008-11-23 22:38:38 +0000489 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000490 break;
491 }
Chris Lattner4d391482007-12-12 07:09:47 +0000492 }
Steve Naroffd100c802008-06-05 15:03:27 +0000493 if (!CDeclChain)
494 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000495 }
Chris Lattner4d391482007-12-12 07:09:47 +0000496
497 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000498 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
499 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000500 }
Anders Carlsson15281452008-11-04 16:57:32 +0000501
502 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000503 return CDecl;
504}
505
506/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000507/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000508/// object.
509Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
510 SourceLocation AtCatImplLoc,
511 IdentifierInfo *ClassName, SourceLocation ClassLoc,
512 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000513 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000514 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000515 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
516 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000517 /// Check that class of this category is already completely declared.
518 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000519 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000520
Douglas Gregord0434102009-01-09 00:49:46 +0000521 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000522 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000523
Chris Lattner4d391482007-12-12 07:09:47 +0000524 /// TODO: Check that CatName, category name, is not used in another
525 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000526 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000527
528 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000529 return CDecl;
530}
531
532Sema::DeclTy *Sema::ActOnStartClassImplementation(
533 SourceLocation AtClassImplLoc,
534 IdentifierInfo *ClassName, SourceLocation ClassLoc,
535 IdentifierInfo *SuperClassname,
536 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000537 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000538 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000539 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000540 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000541 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000542 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000543 }
544 else {
545 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000546 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000547 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000548 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000549 }
550
551 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000552 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000553 if (SuperClassname) {
554 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000555 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000556 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000557 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
558 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000559 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000560 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000561 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000562 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000563 Diag(SuperClassLoc, diag::err_undef_superclass)
564 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000565 else if (IDecl && IDecl->getSuperClass() != SDecl) {
566 // This implementation and its interface do not have the same
567 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000568 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000569 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000570 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000571 }
572 }
573 }
574
575 if (!IDecl) {
576 // Legacy case of @implementation with no corresponding @interface.
577 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000578
579 // FIXME: Do we support attributes on the @implementation? If so
580 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000581 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
582 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000583 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000584 IDecl->setSuperClass(SDecl);
585 IDecl->setLocEnd(ClassLoc);
586
Douglas Gregord0434102009-01-09 00:49:46 +0000587 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000588 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000589 // Remember that this needs to be removed when the scope is popped.
590 TUScope->AddDecl(IDecl);
591 }
592
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000593 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000594 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
595 ClassName, IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000596
Douglas Gregord0434102009-01-09 00:49:46 +0000597 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000598 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000599
Anders Carlsson15281452008-11-04 16:57:32 +0000600 if (CheckObjCDeclScope(IMPDecl))
601 return IMPDecl;
602
Chris Lattner4d391482007-12-12 07:09:47 +0000603 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000604 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000605 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000606 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000607 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000608 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000609 return IMPDecl;
610}
611
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000612void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
613 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000614 SourceLocation RBrace) {
615 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000616 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000617 if (!IDecl)
618 return;
619 /// Check case of non-existing @interface decl.
620 /// (legacy objective-c @implementation decl without an @interface decl).
621 /// Add implementations's ivar to the synthesize class's ivar list.
622 if (IDecl->ImplicitInterfaceDecl()) {
623 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
624 return;
625 }
626 // If implementation has empty ivar list, just return.
627 if (numIvars == 0)
628 return;
629
630 assert(ivars && "missing @implementation ivars");
631
632 // Check interface's Ivar list against those in the implementation.
633 // names and types must match.
634 //
Chris Lattner4d391482007-12-12 07:09:47 +0000635 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000636 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000637 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
638 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000639 ObjCIvarDecl* ImplIvar = ivars[j++];
640 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000641 assert (ImplIvar && "missing implementation ivar");
642 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000643 if (Context.getCanonicalType(ImplIvar->getType()) !=
644 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000645 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000646 << ImplIvar->getIdentifier()
647 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000648 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000649 }
650 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
651 // as error.
652 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000653 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000654 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000655 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000656 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000657 }
658 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000659 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000660
661 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000662 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000663 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000664 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000665}
666
Steve Naroff3c2eb662008-02-10 21:38:56 +0000667void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
668 bool &IncompleteImpl) {
669 if (!IncompleteImpl) {
670 Diag(ImpLoc, diag::warn_incomplete_impl);
671 IncompleteImpl = true;
672 }
Chris Lattner08631c52008-11-23 21:45:46 +0000673 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000674}
675
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000676void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
677 ObjCMethodDecl *IntfMethodDecl) {
678 bool err = false;
679 QualType ImpMethodQType =
680 Context.getCanonicalType(ImpMethodDecl->getResultType());
681 QualType IntfMethodQType =
682 Context.getCanonicalType(IntfMethodDecl->getResultType());
683 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
684 err = true;
685 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
686 IF=IntfMethodDecl->param_begin(),
687 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
688 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
689 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
690 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
691 err = true;
692 break;
693 }
694 }
695 if (err) {
696 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
697 << ImpMethodDecl->getDeclName();
698 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
699 }
700}
701
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000702/// isPropertyReadonly - Return true if property is readonly, by searching
703/// for the property in the class and in its categories and implementations
704///
705bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
706 ObjCInterfaceDecl *IDecl) const {
707 // by far the most common case.
708 if (!PDecl->isReadOnly())
709 return false;
710 // Even if property is ready only, if interface has a user defined setter,
711 // it is not considered read only.
712 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
713 return false;
714
715 // Main class has the property as 'readonly'. Must search
716 // through the category list to see if the property's
717 // attribute has been over-ridden to 'readwrite'.
718 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
719 Category; Category = Category->getNextClassCategory()) {
720 // Even if property is ready only, if a category has a user defined setter,
721 // it is not considered read only.
722 if (Category->getInstanceMethod(PDecl->getSetterName()))
723 return false;
724 ObjCPropertyDecl *P =
725 Category->FindPropertyDeclaration(PDecl->getIdentifier());
726 if (P && !P->isReadOnly())
727 return false;
728 }
729
730 // Also, check for definition of a setter method in the implementation if
731 // all else failed.
732 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
733 if (ObjCImplementationDecl *IMD =
734 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
735 if (IMD->getInstanceMethod(PDecl->getSetterName()))
736 return false;
737 }
738 else if (ObjCCategoryImplDecl *CIMD =
739 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
740 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
741 return false;
742 }
743 }
744 return true;
745}
746
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000747/// FIXME: Type hierarchies in Objective-C can be deep. We could most
748/// likely improve the efficiency of selector lookups and type
749/// checking by associating with each protocol / interface / category
750/// the flattened instance tables. If we used an immutable set to keep
751/// the table then it wouldn't add significant memory cost and it
752/// would be handy for lookups.
753
Steve Naroffefe7f362008-02-08 22:06:17 +0000754/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000755/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000756void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
757 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000758 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000759 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000760 const llvm::DenseSet<Selector> &ClsMap,
761 ObjCInterfaceDecl *IDecl) {
762 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
763
764 // If a method lookup fails locally we still need to look and see if
765 // the method was implemented by a base class or an inherited
766 // protocol. This lookup is slow, but occurs rarely in correct code
767 // and otherwise would terminate in a warning.
768
Chris Lattner4d391482007-12-12 07:09:47 +0000769 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000770 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000771 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000772 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000773 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000774 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000775 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000776 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000777 }
778 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000779 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000780 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000781 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000782 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
783 !ClsMap.count(method->getSelector()) &&
784 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000785 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000786 }
Chris Lattner780f3292008-07-21 21:32:27 +0000787 // Check on this protocols's referenced protocols, recursively.
788 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
789 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000790 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000791}
792
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000793void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
794 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000795 llvm::DenseSet<Selector> InsMap;
796 // Check and see if instance methods in class interface have been
797 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000798 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000799 E = IMPDecl->instmeth_end(); I != E; ++I)
800 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000801
802 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000803 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000804 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000805 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000806 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000807 else {
Fariborz Jahaniande739412008-12-05 01:35:25 +0000808 ObjCMethodDecl *ImpMethodDecl =
809 IMPDecl->getInstanceMethod((*I)->getSelector());
810 ObjCMethodDecl *IntfMethodDecl =
811 IDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000812 assert(IntfMethodDecl &&
813 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
814 // ImpMethodDecl may be null as in a @dynamic property.
815 if (ImpMethodDecl)
816 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahaniande739412008-12-05 01:35:25 +0000817 }
Chris Lattner4c525092007-12-12 17:58:05 +0000818
Chris Lattner4d391482007-12-12 07:09:47 +0000819 llvm::DenseSet<Selector> ClsMap;
820 // Check and see if class methods in class interface have been
821 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000822 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000823 E = IMPDecl->classmeth_end(); I != E; ++I)
824 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000825
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000826 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000827 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000828 if (!ClsMap.count((*I)->getSelector()))
829 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000830 else {
831 ObjCMethodDecl *ImpMethodDecl =
832 IMPDecl->getClassMethod((*I)->getSelector());
833 ObjCMethodDecl *IntfMethodDecl =
834 IDecl->getClassMethod((*I)->getSelector());
835 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
836 }
837
Chris Lattner4d391482007-12-12 07:09:47 +0000838
839 // Check the protocol list for unimplemented methods in the @implementation
840 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000841 const ObjCList<ObjCProtocolDecl> &Protocols =
842 IDecl->getReferencedProtocols();
843 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
844 E = Protocols.end(); I != E; ++I)
845 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000846 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000847}
848
849/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000850/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000851void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
852 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000853 llvm::DenseSet<Selector> InsMap;
854 // Check and see if instance methods in category interface have been
855 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000856 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000857 E = CatImplDecl->instmeth_end(); I != E; ++I)
858 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000859
860 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000861 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000862 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000863 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000864 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000865 else {
866 ObjCMethodDecl *ImpMethodDecl =
867 CatImplDecl->getInstanceMethod((*I)->getSelector());
868 ObjCMethodDecl *IntfMethodDecl =
869 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000870 assert(IntfMethodDecl &&
871 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
872 // ImpMethodDecl may be null as in a @dynamic property.
873 if (ImpMethodDecl)
874 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000875 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000876
Chris Lattner4d391482007-12-12 07:09:47 +0000877 llvm::DenseSet<Selector> ClsMap;
878 // Check and see if class methods in category interface have been
879 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000880 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000881 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
882 I != E; ++I)
883 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000884
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000885 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000886 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000887 if (!ClsMap.count((*I)->getSelector()))
888 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000889 else {
890 ObjCMethodDecl *ImpMethodDecl =
891 CatImplDecl->getClassMethod((*I)->getSelector());
892 ObjCMethodDecl *IntfMethodDecl =
893 CatClassDecl->getClassMethod((*I)->getSelector());
894 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
895 }
Chris Lattner4d391482007-12-12 07:09:47 +0000896 // Check the protocol list for unimplemented methods in the @implementation
897 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000898 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
899 E = CatClassDecl->protocol_end(); PI != E; ++PI)
900 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000901 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000902}
903
904/// ActOnForwardClassDeclaration -
905Action::DeclTy *
906Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
907 IdentifierInfo **IdentList, unsigned NumElts)
908{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000909 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000910
911 for (unsigned i = 0; i != NumElts; ++i) {
912 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000913 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000914 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000915 // Maybe we will complain about the shadowed template parameter.
916 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
917 // Just pretend that we didn't see the previous declaration.
918 PrevDecl = 0;
919 }
920
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000921 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000922 // GCC apparently allows the following idiom:
923 //
924 // typedef NSObject < XCElementTogglerP > XCElementToggler;
925 // @class XCElementToggler;
926 //
927 // FIXME: Make an extension?
928 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
929 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000930 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000931 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000932 }
Chris Lattner4d391482007-12-12 07:09:47 +0000933 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000934 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000935 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000936 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
937 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000938 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000939
Douglas Gregord0434102009-01-09 00:49:46 +0000940 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000941 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000942 // Remember that this needs to be removed when the scope is popped.
943 TUScope->AddDecl(IDecl);
944 }
945
946 Interfaces.push_back(IDecl);
947 }
948
Douglas Gregord0434102009-01-09 00:49:46 +0000949 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000950 &Interfaces[0],
951 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000952 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000953 CheckObjCDeclScope(CDecl);
954 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000955}
956
957
958/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
959/// returns true, or false, accordingly.
960/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000961bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000962 const ObjCMethodDecl *PrevMethod,
963 bool matchBasedOnSizeAndAlignment) {
964 QualType T1 = Context.getCanonicalType(Method->getResultType());
965 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
966
967 if (T1 != T2) {
968 // The result types are different.
969 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000970 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000971 // Incomplete types don't have a size and alignment.
972 if (T1->isIncompleteType() || T2->isIncompleteType())
973 return false;
974 // Check is based on size and alignment.
975 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
976 return false;
977 }
978 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
979 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
980 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
981 if (T1 != T2) {
982 // The result types are different.
983 if (!matchBasedOnSizeAndAlignment)
984 return false;
985 // Incomplete types don't have a size and alignment.
986 if (T1->isIncompleteType() || T2->isIncompleteType())
987 return false;
988 // Check is based on size and alignment.
989 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
990 return false;
991 }
Chris Lattner4d391482007-12-12 07:09:47 +0000992 }
993 return true;
994}
995
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000996void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
997 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000998 if (!FirstMethod.Method) {
999 // Haven't seen a method with this selector name yet - add it.
1000 FirstMethod.Method = Method;
1001 FirstMethod.Next = 0;
1002 } else {
1003 // We've seen a method with this name, now check the type signature(s).
1004 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1005
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001006 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001007 Next = Next->Next)
1008 match = MatchTwoMethodDeclarations(Method, Next->Method);
1009
1010 if (!match) {
1011 // We have a new signature for an existing method - add it.
1012 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +00001013 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +00001014 }
1015 }
1016}
1017
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001018// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001019ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1020 SourceRange R) {
1021 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001022 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001023
1024 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001025 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1026 // This checks if the methods differ by size & alignment.
1027 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1028 issueWarning = true;
1029 }
1030 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001031 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001032 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001033 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001034 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001035 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001036 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001037 }
1038 return MethList.Method;
1039}
1040
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001041void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1042 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001043 if (!FirstMethod.Method) {
1044 // Haven't seen a method with this selector name yet - add it.
1045 FirstMethod.Method = Method;
1046 FirstMethod.Next = 0;
1047 } else {
1048 // We've seen a method with this name, now check the type signature(s).
1049 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1050
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001051 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001052 Next = Next->Next)
1053 match = MatchTwoMethodDeclarations(Method, Next->Method);
1054
1055 if (!match) {
1056 // We have a new signature for an existing method - add it.
1057 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001058 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001059 FirstMethod.Next = OMI;
1060 }
1061 }
1062}
1063
Steve Naroff0701bbb2009-01-08 17:28:14 +00001064/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1065/// have the property type and issue diagnostics if they don't.
1066/// Also synthesize a getter/setter method if none exist (and update the
1067/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1068/// methods is the "right" thing to do.
1069void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1070 ObjCContainerDecl *CD) {
1071 ObjCMethodDecl *GetterMethod, *SetterMethod;
1072
1073 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1074 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1075
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001076 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001077 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001078 Diag(property->getLocation(),
1079 diag::err_accessor_property_type_mismatch)
1080 << property->getDeclName()
1081 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001082 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1083 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001084
1085 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001086 if (Context.getCanonicalType(SetterMethod->getResultType())
1087 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001088 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1089 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001090 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001091 Diag(property->getLocation(),
1092 diag::err_accessor_property_type_mismatch)
1093 << property->getDeclName()
1094 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001095 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1096 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001097 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001098
1099 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001100 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001101 // FIXME: The synthesized property we set here is misleading. We
1102 // almost always synthesize these methods unless the user explicitly
1103 // provided prototypes (which is odd, but allowed). Sema should be
1104 // typechecking that the declarations jive in that situation (which
1105 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001106 if (!GetterMethod) {
1107 // No instance method of same name as property getter name was found.
1108 // Declare a getter method and add it to the list of methods
1109 // for this class.
1110 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1111 property->getLocation(), property->getGetterName(),
1112 property->getType(), CD, true, false, true,
1113 (property->getPropertyImplementation() ==
1114 ObjCPropertyDecl::Optional) ?
1115 ObjCMethodDecl::Optional :
1116 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001117 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001118 } else
1119 // A user declared getter will be synthesize when @synthesize of
1120 // the property with the same name is seen in the @implementation
1121 GetterMethod->setIsSynthesized();
1122 property->setGetterMethodDecl(GetterMethod);
1123
1124 // Skip setter if property is read-only.
1125 if (!property->isReadOnly()) {
1126 // Find the default setter and if one not found, add one.
1127 if (!SetterMethod) {
1128 // No instance method of same name as property setter name was found.
1129 // Declare a setter method and add it to the list of methods
1130 // for this class.
1131 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1132 property->getLocation(),
1133 property->getSetterName(),
1134 Context.VoidTy, CD, true, false, true,
1135 (property->getPropertyImplementation() ==
1136 ObjCPropertyDecl::Optional) ?
1137 ObjCMethodDecl::Optional :
1138 ObjCMethodDecl::Required);
1139 // Invent the arguments for the setter. We don't bother making a
1140 // nice name for the argument.
1141 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1142 SourceLocation(),
1143 property->getIdentifier(),
1144 property->getType(),
1145 VarDecl::None,
1146 0, 0);
1147 SetterMethod->setMethodParams(&Argument, 1);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001148 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001149 } else
1150 // A user declared setter will be synthesize when @synthesize of
1151 // the property with the same name is seen in the @implementation
1152 SetterMethod->setIsSynthesized();
1153 property->setSetterMethodDecl(SetterMethod);
1154 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001155 // Add any synthesized methods to the global pool. This allows us to
1156 // handle the following, which is supported by GCC (and part of the design).
1157 //
1158 // @interface Foo
1159 // @property double bar;
1160 // @end
1161 //
1162 // void thisIsUnfortunate() {
1163 // id foo;
1164 // double bar = [foo bar];
1165 // }
1166 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001167 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001168 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001169 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001170 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001171}
1172
Steve Naroffa56f6162007-12-18 01:30:32 +00001173// Note: For class/category implemenations, allMethods/allProperties is
1174// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001175void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1176 DeclTy **allMethods, unsigned allNum,
1177 DeclTy **allProperties, unsigned pNum) {
1178 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1179
Steve Naroffa56f6162007-12-18 01:30:32 +00001180 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1181 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001182 // should be true.
1183 if (!ClassDecl)
1184 return;
1185
Chris Lattner4d391482007-12-12 07:09:47 +00001186 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001187 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1188 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001189 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001190
Steve Naroff0701bbb2009-01-08 17:28:14 +00001191 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001192
1193 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1194 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1195 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1196
Chris Lattner4d391482007-12-12 07:09:47 +00001197 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001198 ObjCMethodDecl *Method =
1199 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001200
1201 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001202 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001203 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001204 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001205 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1206 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001207 if ((isInterfaceDeclKind && PrevMethod && !match)
1208 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001209 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001210 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001211 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001212 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001213 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001214 InsMap[Method->getSelector()] = Method;
1215 /// The following allows us to typecheck messages to "id".
1216 AddInstanceMethodToGlobalPool(Method);
1217 }
1218 }
1219 else {
1220 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001221 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001222 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1223 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001224 if ((isInterfaceDeclKind && PrevMethod && !match)
1225 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001226 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001227 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001228 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001229 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001230 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001231 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001232 /// The following allows us to typecheck messages to "Class".
1233 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001234 }
1235 }
1236 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001237 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001238 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001239 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001240 ComparePropertiesInBaseAndSuper(I);
1241 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001242 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001243 // Categories are used to extend the class by declaring new methods.
1244 // By the same token, they are also used to add new properties. No
1245 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001246
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001247 // Merge protocol properties into category
1248 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner4d391482007-12-12 07:09:47 +00001249 }
Steve Naroff09c47192009-01-09 15:36:25 +00001250 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1251 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1252 // user-defined setter/getter. It also synthesizes setter/getter methods
1253 // and adds them to the DeclContext and global method pools.
1254 for (ObjCContainerDecl::prop_iterator i = CDecl->prop_begin(),
1255 e = CDecl->prop_end(); i != e; ++i)
1256 ProcessPropertyDecl((*i), CDecl);
1257 CDecl->setAtEndLoc(AtEndLoc);
1258 }
1259 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001260 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001261 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +00001262 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001263 } else if (ObjCCategoryImplDecl* CatImplClass =
1264 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001265 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001266 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001267 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001268 // in this interface are implemented in the category @implementation.
Chris Lattner4d391482007-12-12 07:09:47 +00001269 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001270 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001271 Categories; Categories = Categories->getNextClassCategory()) {
1272 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1273 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1274 break;
1275 }
1276 }
1277 }
1278 }
1279}
1280
1281
1282/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1283/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001284static Decl::ObjCDeclQualifier
1285CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1286 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1287 if (PQTVal & ObjCDeclSpec::DQ_In)
1288 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1289 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1290 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1291 if (PQTVal & ObjCDeclSpec::DQ_Out)
1292 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1293 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1294 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1295 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1296 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1297 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1298 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001299
1300 return ret;
1301}
1302
1303Sema::DeclTy *Sema::ActOnMethodDeclaration(
1304 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001305 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001306 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001307 Selector Sel,
1308 // optional arguments. The number of types/arguments is obtained
1309 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001310 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001311 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001312 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1313 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001314 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001315
1316 // Make sure we can establish a context for the method.
1317 if (!ClassDecl) {
1318 Diag(MethodLoc, diag::error_missing_method_context);
1319 return 0;
1320 }
Chris Lattner4d391482007-12-12 07:09:47 +00001321 QualType resultDeclType;
1322
1323 if (ReturnType)
1324 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1325 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001326 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001327
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001328 ObjCMethodDecl* ObjCMethod =
1329 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001330 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001331 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001332 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001333 MethodDeclKind == tok::objc_optional ?
1334 ObjCMethodDecl::Optional :
1335 ObjCMethodDecl::Required);
1336
Chris Lattner0ed844b2008-04-04 06:12:32 +00001337 llvm::SmallVector<ParmVarDecl*, 16> Params;
1338
1339 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1340 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001341 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001342
Steve Naroff6082c622008-12-09 19:36:17 +00001343 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001344 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001345 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001346 if (argType->isArrayType()) { // (char *[]) -> (char **)
1347 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001348 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001349 }
Steve Naroff6082c622008-12-09 19:36:17 +00001350 else if (argType->isFunctionType())
1351 argType = Context.getPointerType(argType);
1352 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001353 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001354 ParmVarDecl* Param;
1355 if (originalArgType.isNull())
1356 Param = ParmVarDecl::Create(Context, ObjCMethod,
1357 SourceLocation(/*FIXME*/),
1358 ArgNames[i], argType,
1359 VarDecl::None, 0, 0);
1360 else
1361 Param = ParmVarWithOriginalTypeDecl::Create(Context, ObjCMethod,
1362 SourceLocation(/*FIXME*/),
1363 ArgNames[i], argType, originalArgType,
1364 VarDecl::None, 0, 0);
1365
Chris Lattner0ed844b2008-04-04 06:12:32 +00001366 Param->setObjCDeclQualifier(
1367 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1368 Params.push_back(Param);
1369 }
1370
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001371 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1372 ObjCMethod->setObjCDeclQualifier(
1373 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1374 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001375
1376 if (AttrList)
1377 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001378
1379 // For implementations (which can be very "coarse grain"), we add the
1380 // method now. This allows the AST to implement lookup methods that work
1381 // incrementally (without waiting until we parse the @end). It also allows
1382 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001383 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001384 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001385 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001386 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001387 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001388 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001389 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001390 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001391 }
1392 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001393 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001394 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001395 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001396 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001397 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001398 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001399 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001400 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001401 }
1402 }
1403 if (PrevMethod) {
1404 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001405 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001406 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001407 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001408 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001409 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001410}
1411
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001412void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1413 SourceLocation Loc,
1414 unsigned &Attributes) {
1415 // FIXME: Improve the reported location.
1416
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001417 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001418 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001419 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1420 ObjCDeclSpec::DQ_PR_assign |
1421 ObjCDeclSpec::DQ_PR_copy |
1422 ObjCDeclSpec::DQ_PR_retain))) {
1423 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1424 "readwrite" :
1425 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1426 "assign" :
1427 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1428 "copy" : "retain";
1429
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001430 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1431 diag::err_objc_property_attr_mutually_exclusive :
1432 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001433 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001434 }
1435
1436 // Check for copy or retain on non-object types.
1437 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1438 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001439 Diag(Loc, diag::err_objc_property_requires_object)
1440 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001441 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1442 }
1443
1444 // Check for more than one of { assign, copy, retain }.
1445 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1446 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001447 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1448 << "assign" << "copy";
1449 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001450 }
1451 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001452 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1453 << "assign" << "retain";
1454 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001455 }
1456 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1457 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001458 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1459 << "copy" << "retain";
1460 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001461 }
1462 }
1463
1464 // Warn if user supplied no assignment attribute, property is
1465 // readwrite, and this is an object type.
1466 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1467 ObjCDeclSpec::DQ_PR_retain)) &&
1468 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1469 Context.isObjCObjectPointerType(PropertyTy)) {
1470 // Skip this warning in gc-only mode.
1471 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1472 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1473
1474 // If non-gc code warn that this is likely inappropriate.
1475 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1476 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1477
1478 // FIXME: Implement warning dependent on NSCopying being
1479 // implemented. See also:
1480 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1481 // (please trim this list while you are at it).
1482 }
1483}
1484
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001485Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1486 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001487 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001488 Selector GetterSel,
1489 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001490 DeclTy *ClassCategory,
1491 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001492 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001493 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001494 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1495 // default is readwrite!
1496 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1497 // property is defaulted to 'assign' if it is readwrite and is
1498 // not retain or copy
1499 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1500 (isReadWrite &&
1501 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1502 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1503 QualType T = GetTypeForDeclarator(FD.D, S);
1504 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001505
1506 // May modify Attributes.
1507 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001508
1509 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1510 if (!CDecl->getIdentifier()) {
1511 // This is an anonymous category. property requires special
1512 // handling.
1513 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1514 if (ObjCPropertyDecl *PIDecl =
1515 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1516 // property 'PIDecl's readonly attribute will be over-ridden
1517 // with anonymous category's readwrite property attribute!
1518 unsigned PIkind = PIDecl->getPropertyAttributes();
1519 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001520 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001521 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1522 Diag(AtLoc, diag::warn_property_attr_mismatch);
1523 PIDecl->makeitReadWriteAttribute();
1524 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1525 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1526 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1527 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1528 PIDecl->setSetterName(SetterSel);
1529 // FIXME: use a common routine with addPropertyMethods.
1530 ObjCMethodDecl *SetterDecl =
1531 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1532 Context.VoidTy,
1533 ICDecl,
1534 true, false, true,
1535 ObjCMethodDecl::Required);
1536 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1537 SetterDecl,
1538 SourceLocation(),
1539 FD.D.getIdentifier(),
1540 T,
1541 VarDecl::None,
1542 0, 0);
1543 SetterDecl->setMethodParams(&Argument, 1);
1544 PIDecl->setSetterMethodDecl(SetterDecl);
1545 }
1546 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001547 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001548 *isOverridingProperty = true;
1549 return 0;
1550 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001551 // No matching property found in the main class. Just fall thru
1552 // and add property to the anonymous category. It looks like
1553 // it works as is. This category becomes just like a category
1554 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001555 } else {
1556 Diag(CDecl->getLocation(), diag::err_continuation_class);
1557 *isOverridingProperty = true;
1558 return 0;
1559 }
1560 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001561
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001562 Type *t = T.getTypePtr();
1563 if (t->isArrayType() || t->isFunctionType())
1564 Diag(AtLoc, diag::err_property_type) << T;
1565
Steve Naroff93983f82009-01-11 12:47:58 +00001566 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1567 assert(DC && "ClassDecl is not a DeclContext");
1568 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001569 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001570 DC->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +00001571
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001572 // Regardless of setter/getter attribute, we save the default getter/setter
1573 // selector names in anticipation of declaration of setter/getter methods.
1574 PDecl->setGetterName(GetterSel);
1575 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001576
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001577 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001578 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001579
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001580 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001581 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001582
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001583 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001584 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001585
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001586 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001587 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001588
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001589 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001590 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001591
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001592 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001593 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001594
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001595 if (isAssign)
1596 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1597
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001598 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001599 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001600
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001601 if (MethodImplKind == tok::objc_required)
1602 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1603 else if (MethodImplKind == tok::objc_optional)
1604 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1605
Chris Lattner4d391482007-12-12 07:09:47 +00001606 return PDecl;
1607}
1608
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001609/// ActOnPropertyImplDecl - This routine performs semantic checks and
1610/// builds the AST node for a property implementation declaration; declared
1611/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001612///
1613Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1614 SourceLocation PropertyLoc,
1615 bool Synthesize,
1616 DeclTy *ClassCatImpDecl,
1617 IdentifierInfo *PropertyId,
1618 IdentifierInfo *PropertyIvar) {
1619 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1620 // Make sure we have a context for the property implementation declaration.
1621 if (!ClassImpDecl) {
1622 Diag(AtLoc, diag::error_missing_property_context);
1623 return 0;
1624 }
1625 ObjCPropertyDecl *property = 0;
1626 ObjCInterfaceDecl* IDecl = 0;
1627 // Find the class or category class where this property must have
1628 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001629 ObjCImplementationDecl *IC = 0;
1630 ObjCCategoryImplDecl* CatImplClass = 0;
1631 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001632 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001633 // We always synthesize an interface for an implementation
1634 // without an interface decl. So, IDecl is always non-zero.
1635 assert(IDecl &&
1636 "ActOnPropertyImplDecl - @implementation without @interface");
1637
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001638 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001639 property = IDecl->FindPropertyDeclaration(PropertyId);
1640 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001641 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001642 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001643 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001644 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001645 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001646 if (Synthesize) {
1647 Diag(AtLoc, diag::error_synthesize_category_decl);
1648 return 0;
1649 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001650 IDecl = CatImplClass->getClassInterface();
1651 if (!IDecl) {
1652 Diag(AtLoc, diag::error_missing_property_interface);
1653 return 0;
1654 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001655 ObjCCategoryDecl *Category =
1656 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1657
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001658 // If category for this implementation not found, it is an error which
1659 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001660 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001661 return 0;
1662 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001663 property = Category->FindPropertyDeclaration(PropertyId);
1664 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001665 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001666 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001667 return 0;
1668 }
1669 }
1670 else {
1671 Diag(AtLoc, diag::error_bad_property_context);
1672 return 0;
1673 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001674 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001675 // Check that we have a valid, previously declared ivar for @synthesize
1676 if (Synthesize) {
1677 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001678 if (!PropertyIvar)
1679 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001680 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001681 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001682 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001683 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001684 return 0;
1685 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001686 QualType PropType = Context.getCanonicalType(property->getType());
1687 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1688
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001689 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001690 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001691 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001692 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001693 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001694 return 0;
1695 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001696 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001697 } else if (PropertyIvar) {
1698 // @dynamic
1699 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1700 return 0;
1701 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001702 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001703 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001704 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1705 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001706 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001707 ObjCPropertyImplDecl::Synthesize
1708 : ObjCPropertyImplDecl::Dynamic),
1709 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001710 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001711 if (IC) {
1712 if (Synthesize)
1713 if (ObjCPropertyImplDecl *PPIDecl =
1714 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1715 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1716 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1717 << PropertyIvar;
1718 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1719 }
1720
1721 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1722 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1723 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1724 return 0;
1725 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001726 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001727 }
1728 else {
1729 if (Synthesize)
1730 if (ObjCPropertyImplDecl *PPIDecl =
1731 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1732 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1733 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1734 << PropertyIvar;
1735 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1736 }
1737
1738 if (ObjCPropertyImplDecl *PPIDecl =
1739 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1740 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1741 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1742 return 0;
1743 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001744 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001745 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001746
1747 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001748}
Anders Carlsson15281452008-11-04 16:57:32 +00001749
Chris Lattnercc98eac2008-12-17 07:13:27 +00001750bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001751 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001752 return false;
1753
1754 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1755 D->setInvalidDecl();
1756
1757 return true;
1758}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001759
1760/// Collect the instance variables declared in an Objective-C object. Used in
1761/// the creation of structures from objects using the @defs directive.
1762/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1763/// part of the AST generation logic of @defs.
1764static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1765 ASTContext& Ctx,
1766 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1767 if (Class->getSuperClass())
1768 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1769
1770 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1771 for (ObjCInterfaceDecl::ivar_iterator
1772 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1773
1774 ObjCIvarDecl* ID = *I;
1775 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1776 ID->getLocation(),
1777 ID->getIdentifier(),
1778 ID->getType(),
1779 ID->getBitWidth()));
1780 }
1781}
1782
1783/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1784/// instance variables of ClassName into Decls.
1785void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1786 IdentifierInfo *ClassName,
1787 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1788 // Check that ClassName is a valid class
1789 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1790 if (!Class) {
1791 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1792 return;
1793 }
1794 // Collect the instance variables
1795 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1796
1797 // Introduce all of these fields into the appropriate scope.
1798 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1799 D != Decls.end(); ++D) {
1800 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1801 if (getLangOptions().CPlusPlus)
1802 PushOnScopeChains(cast<FieldDecl>(FD), S);
1803 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001804 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001805 }
1806}
1807