blob: 9a4023f0ef4682244385ff33c0bdc483718b76cf [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000018
19using namespace clang;
20
Ted Kremeneka526c5c2008-01-07 19:49:32 +000021/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000023void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff394f3f42008-07-25 17:57:26 +000025 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
26
27 // If we don't have a valid method decl, simply return.
28 if (!MDecl)
29 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000030
31 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000032 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000033 AddInstanceMethodToGlobalPool(MDecl);
34 else
35 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000036
37 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000038 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000039
40 // Create Decl objects for each parameter, entrring them in the scope for
41 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000042
43 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000044 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000045
Daniel Dunbar451318c2008-08-26 06:07:48 +000046 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
47 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000048
Chris Lattner8123a952008-04-10 02:22:51 +000049 // Introduce all of the other parameters into this scope.
Chris Lattner58cce3b2008-03-16 01:07:14 +000050 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +000051 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner04421082008-04-08 04:40:51 +000052 IdentifierInfo *II = PDecl->getIdentifier();
Argyrios Kyrtzidis642e38b2008-04-27 13:30:35 +000053 if (II)
54 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000055 }
56}
57
Chris Lattner7caeabd2008-07-21 22:17:28 +000058Sema::DeclTy *Sema::
59ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
60 IdentifierInfo *ClassName, SourceLocation ClassLoc,
61 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000062 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000063 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000064 assert(ClassName && "Missing class identifier");
65
66 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000067 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000068 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000069 // Maybe we will complain about the shadowed template parameter.
70 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
71 // Just pretend that we didn't see the previous declaration.
72 PrevDecl = 0;
73 }
74
Ted Kremeneka526c5c2008-01-07 19:49:32 +000075 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000076 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000077 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000078 }
79
Ted Kremeneka526c5c2008-01-07 19:49:32 +000080 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000081 if (IDecl) {
82 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000083 if (!IDecl->isForwardDecl()) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000084 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000085 Diag(IDecl->getLocation(), diag::note_previous_definition);
86
Steve Naroffcfe8bf32008-11-18 19:15:30 +000087 // Return the previous class interface.
88 // FIXME: don't leak the objects passed in!
89 return IDecl;
90 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000091 IDecl->setLocation(AtInterfaceLoc);
92 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000093 }
Chris Lattnerb752f282008-07-21 07:06:49 +000094 } else {
Douglas Gregord0434102009-01-09 00:49:46 +000095 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000096 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000097 if (AttrList)
98 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +000099
Steve Naroff31102512008-04-02 18:30:49 +0000100 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000101 // FIXME: PushOnScopeChains
Douglas Gregor482b77d2009-01-12 23:27:07 +0000102 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000103 // Remember that this needs to be removed when the scope is popped.
104 TUScope->AddDecl(IDecl);
105 }
106
107 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000108 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000109 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Chris Lattner3c73c412008-11-19 08:23:25 +0000110
Steve Naroff818cb9e2009-02-04 17:14:05 +0000111 ObjCInterfaceDecl *SuperClassDecl =
112 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
113 if (PrevDecl && SuperClassDecl == 0) {
114 // The previous declaration was not a class decl. Check if we have a
115 // typedef. If we do, get the underlying class type.
116 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
117 QualType T = TDecl->getUnderlyingType();
118 if (T->isObjCInterfaceType()) {
119 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
120 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
121 }
122 }
123 // This handles the following case:
124 //
125 // typedef int SuperClass;
126 // @interface MyClass : SuperClass {} @end
127 //
128 if (!SuperClassDecl) {
129 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
130 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
131 }
132 }
133 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
134 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000135 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000136 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000137 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000138 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000139 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000140 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000141 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000142 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000143 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000144 IDecl->setLocEnd(SuperLoc);
145 } else { // we have a root class.
146 IDecl->setLocEnd(ClassLoc);
147 }
148
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000149 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000150 if (NumProtoRefs) {
151 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000152 IDecl->setLocEnd(EndProtoLoc);
153 }
Anders Carlsson15281452008-11-04 16:57:32 +0000154
155 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000156 return IDecl;
157}
158
159/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000160/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000161Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
162 IdentifierInfo *AliasName,
163 SourceLocation AliasLocation,
164 IdentifierInfo *ClassName,
165 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000166 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000167 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000168 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000169 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000170 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000171 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000172 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000173 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000174 return 0;
175 }
176 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000177 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000178 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
179 QualType T = TDecl->getUnderlyingType();
180 if (T->isObjCInterfaceType()) {
181 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
182 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000183 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000184 }
185 }
186 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000187 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
188 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000189 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000190 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000191 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000192 return 0;
193 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000194
195 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000196 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000197 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000198
199 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregord0434102009-01-09 00:49:46 +0000200
201 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000202 CurContext->addDecl(AliasDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000203 if (!CheckObjCDeclScope(AliasDecl))
204 TUScope->AddDecl(AliasDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000205
Chris Lattner4d391482007-12-12 07:09:47 +0000206 return AliasDecl;
207}
208
Chris Lattnere13b9592008-07-26 04:03:38 +0000209Sema::DeclTy *
210Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
211 IdentifierInfo *ProtocolName,
212 SourceLocation ProtocolLoc,
213 DeclTy * const *ProtoRefs,
214 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000215 SourceLocation EndProtoLoc,
216 AttributeList *AttrList) {
217 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000218 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000219 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000220 if (PDecl) {
221 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000222 if (!PDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000223 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000224 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000225 // Just return the protocol we already had.
226 // FIXME: don't leak the objects passed in!
227 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000228 }
Steve Narofff11b5082008-08-13 16:39:22 +0000229 // Make sure the cached decl gets a valid start location.
230 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000231 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000232 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000233 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
234 AtProtoInterfaceLoc,ProtocolName);
235 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000236 CurContext->addDecl(PDecl);
Chris Lattnerc8581052008-03-16 20:19:15 +0000237 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000238 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000239 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000240 if (AttrList)
241 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000242 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000243 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000244 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000245 PDecl->setLocEnd(EndProtoLoc);
246 }
Anders Carlsson15281452008-11-04 16:57:32 +0000247
248 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000249 return PDecl;
250}
251
252/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000253/// issues an error if they are not declared. It returns list of
254/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000255void
Chris Lattnere13b9592008-07-26 04:03:38 +0000256Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000257 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000258 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000259 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000260 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000261 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
262 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000263 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000264 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000265 continue;
266 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000267
268 if (PDecl->getAttr<UnavailableAttr>())
269 Diag(ProtocolId[i].second, diag::warn_unavailable) <<
270 PDecl->getDeclName();
271 if (PDecl->getAttr<DeprecatedAttr>())
272 Diag(ProtocolId[i].second, diag::warn_deprecated) <<
273 PDecl->getDeclName();
Chris Lattnereacc3922008-07-26 03:47:43 +0000274
275 // If this is a forward declaration and we are supposed to warn in this
276 // case, do it.
277 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000278 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000279 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000280 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000281 }
282}
283
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000284/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000285/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000286///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000287void
288Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
289 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000290 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000291 ObjCPropertyDecl::PropertyAttributeKind CAttr =
292 Property->getPropertyAttributes();
293 ObjCPropertyDecl::PropertyAttributeKind SAttr =
294 SuperProperty->getPropertyAttributes();
295 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
296 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000297 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000298 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000299 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
300 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000301 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000302 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000303 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
304 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000305 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000306 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000307
308 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
309 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000310 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000311 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000312 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000313 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000314 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000315 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000316 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000317 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000318
Chris Lattner717250a2008-07-26 20:50:02 +0000319 if (Context.getCanonicalType(Property->getType()) !=
320 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000321 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000322 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000323
324}
325
326/// ComparePropertiesInBaseAndSuper - This routine compares property
327/// declarations in base and its super class, if any, and issues
328/// diagnostics in a variety of inconsistant situations.
329///
330void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000331Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000332 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
333 if (!SDecl)
334 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000335 // FIXME: O(N^2)
Steve Naroff09c47192009-01-09 15:36:25 +0000336 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
337 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000338 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000339 // Does property in super class has declaration in current class?
Steve Naroff09c47192009-01-09 15:36:25 +0000340 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
341 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000342 ObjCPropertyDecl *PDecl = (*I);
343 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000344 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000345 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000346 }
347 }
348}
349
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000350/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
351/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000352/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000353void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000354Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000355 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000356 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
357 if (!IDecl) {
358 // Category
359 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
360 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff09c47192009-01-09 15:36:25 +0000361 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
362 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000363 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000364 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000365 // Is this property already in category's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000366 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000367 CP != CE; ++CP)
368 if ((*CP)->getIdentifier() == Pr->getIdentifier())
369 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000370 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000371 // Property protocol already exist in class. Diagnose any mismatch.
372 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
373 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000374 return;
375 }
Steve Naroff09c47192009-01-09 15:36:25 +0000376 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
377 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000378 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000379 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000380 // Is this property already in class's list of properties?
Steve Naroff09c47192009-01-09 15:36:25 +0000381 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000382 CP != CE; ++CP)
383 if ((*CP)->getIdentifier() == Pr->getIdentifier())
384 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000385 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000386 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000387 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000388 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000389}
390
391/// MergeProtocolPropertiesIntoClass - This routine merges properties
392/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000393/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000394///
395
396void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000397Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000398 DeclTy *MergeItsProtocols) {
399 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000400 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
401
402 if (!IDecl) {
403 // Category
404 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
405 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
406 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
407 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
408 E = MDecl->protocol_end(); P != E; ++P)
409 // Merge properties of category (*P) into IDECL's
410 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
411
412 // Go thru the list of protocols for this category and recursively merge
413 // their properties into this class as well.
414 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
415 E = CatDecl->protocol_end(); P != E; ++P)
416 MergeProtocolPropertiesIntoClass(CatDecl, *P);
417 } else {
418 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
419 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
420 E = MD->protocol_end(); P != E; ++P)
421 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
422 }
423 return;
424 }
425
Chris Lattnerb752f282008-07-21 07:06:49 +0000426 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000427 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
428 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000429 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000430 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
431
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000432 // Go thru the list of protocols for this class and recursively merge
433 // their properties into this class as well.
434 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
435 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000436 MergeProtocolPropertiesIntoClass(IDecl, *P);
437 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000438 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
439 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
440 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000441 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000442 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000443}
444
Chris Lattner4d391482007-12-12 07:09:47 +0000445/// ActOnForwardProtocolDeclaration -
446Action::DeclTy *
447Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000448 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000449 unsigned NumElts,
450 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000451 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000452
453 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000454 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000455 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregord0434102009-01-09 00:49:46 +0000456 if (PDecl == 0) { // Not already seen?
457 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
458 IdentList[i].second, Ident);
459 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000460 CurContext->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000461 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000462 if (attrList)
463 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000464 Protocols.push_back(PDecl);
465 }
Anders Carlsson15281452008-11-04 16:57:32 +0000466
467 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000468 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000469 &Protocols[0], Protocols.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000470 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000471 CheckObjCDeclScope(PDecl);
472 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000473}
474
Chris Lattner7caeabd2008-07-21 22:17:28 +0000475Sema::DeclTy *Sema::
476ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
477 IdentifierInfo *ClassName, SourceLocation ClassLoc,
478 IdentifierInfo *CategoryName,
479 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000480 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000481 unsigned NumProtoRefs,
482 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000483 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000484
Chris Lattner61f9d412008-03-16 20:34:23 +0000485 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000486 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
487 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000488 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000489 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000490
491 /// Check that class of this category is already completely declared.
492 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000493 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffd100c802008-06-05 15:03:27 +0000494 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000495 /// Check for duplicate interface declaration for this category
496 ObjCCategoryDecl *CDeclChain;
497 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
498 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000499 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000500 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner3c73c412008-11-19 08:23:25 +0000501 << ClassName << CategoryName;
Chris Lattner6ff0fc32008-11-23 22:38:38 +0000502 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000503 break;
504 }
Chris Lattner4d391482007-12-12 07:09:47 +0000505 }
Steve Naroffd100c802008-06-05 15:03:27 +0000506 if (!CDeclChain)
507 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000508 }
Chris Lattner4d391482007-12-12 07:09:47 +0000509
510 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000511 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
512 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000513 }
Anders Carlsson15281452008-11-04 16:57:32 +0000514
515 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000516 return CDecl;
517}
518
519/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000520/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000521/// object.
522Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
523 SourceLocation AtCatImplLoc,
524 IdentifierInfo *ClassName, SourceLocation ClassLoc,
525 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000526 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000527 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000528 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
529 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000530 /// Check that class of this category is already completely declared.
531 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000532 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000533
Douglas Gregord0434102009-01-09 00:49:46 +0000534 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000535 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000536
Chris Lattner4d391482007-12-12 07:09:47 +0000537 /// TODO: Check that CatName, category name, is not used in another
538 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000539 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000540
541 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000542 return CDecl;
543}
544
545Sema::DeclTy *Sema::ActOnStartClassImplementation(
546 SourceLocation AtClassImplLoc,
547 IdentifierInfo *ClassName, SourceLocation ClassLoc,
548 IdentifierInfo *SuperClassname,
549 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000550 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000551 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000552 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000553 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000554 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000555 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000556 }
557 else {
558 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000559 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000560 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000561 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000562 }
563
564 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000565 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000566 if (SuperClassname) {
567 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000568 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000569 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000570 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
571 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000572 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000573 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000574 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000575 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000576 Diag(SuperClassLoc, diag::err_undef_superclass)
577 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000578 else if (IDecl && IDecl->getSuperClass() != SDecl) {
579 // This implementation and its interface do not have the same
580 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000581 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000582 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000583 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000584 }
585 }
586 }
587
588 if (!IDecl) {
589 // Legacy case of @implementation with no corresponding @interface.
590 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000591
592 // FIXME: Do we support attributes on the @implementation? If so
593 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000594 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
595 ClassName, ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000596 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000597 IDecl->setSuperClass(SDecl);
598 IDecl->setLocEnd(ClassLoc);
599
Douglas Gregord0434102009-01-09 00:49:46 +0000600 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000601 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000602 // Remember that this needs to be removed when the scope is popped.
603 TUScope->AddDecl(IDecl);
604 }
605
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000606 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000607 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000608 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000609
Douglas Gregord0434102009-01-09 00:49:46 +0000610 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000611 CurContext->addDecl(IMPDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000612
Anders Carlsson15281452008-11-04 16:57:32 +0000613 if (CheckObjCDeclScope(IMPDecl))
614 return IMPDecl;
615
Chris Lattner4d391482007-12-12 07:09:47 +0000616 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000618 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000619 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000620 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000621 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000622 return IMPDecl;
623}
624
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000625void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
626 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000627 SourceLocation RBrace) {
628 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000629 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000630 if (!IDecl)
631 return;
632 /// Check case of non-existing @interface decl.
633 /// (legacy objective-c @implementation decl without an @interface decl).
634 /// Add implementations's ivar to the synthesize class's ivar list.
635 if (IDecl->ImplicitInterfaceDecl()) {
636 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
637 return;
638 }
639 // If implementation has empty ivar list, just return.
640 if (numIvars == 0)
641 return;
642
643 assert(ivars && "missing @implementation ivars");
644
645 // Check interface's Ivar list against those in the implementation.
646 // names and types must match.
647 //
Chris Lattner4d391482007-12-12 07:09:47 +0000648 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000649 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000650 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
651 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000652 ObjCIvarDecl* ImplIvar = ivars[j++];
653 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000654 assert (ImplIvar && "missing implementation ivar");
655 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000656 if (Context.getCanonicalType(ImplIvar->getType()) !=
657 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000658 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000659 << ImplIvar->getIdentifier()
660 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000661 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000662 }
663 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
664 // as error.
665 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000666 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000667 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000668 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000669 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000670 }
671 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000672 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000673
674 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000675 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000676 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000677 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000678}
679
Steve Naroff3c2eb662008-02-10 21:38:56 +0000680void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
681 bool &IncompleteImpl) {
682 if (!IncompleteImpl) {
683 Diag(ImpLoc, diag::warn_incomplete_impl);
684 IncompleteImpl = true;
685 }
Chris Lattner08631c52008-11-23 21:45:46 +0000686 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000687}
688
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000689void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
690 ObjCMethodDecl *IntfMethodDecl) {
691 bool err = false;
692 QualType ImpMethodQType =
693 Context.getCanonicalType(ImpMethodDecl->getResultType());
694 QualType IntfMethodQType =
695 Context.getCanonicalType(IntfMethodDecl->getResultType());
696 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
697 err = true;
698 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
699 IF=IntfMethodDecl->param_begin(),
700 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
701 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
702 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
703 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
704 err = true;
705 break;
706 }
707 }
708 if (err) {
709 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
710 << ImpMethodDecl->getDeclName();
711 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
712 }
713}
714
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000715/// isPropertyReadonly - Return true if property is readonly, by searching
716/// for the property in the class and in its categories and implementations
717///
718bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
719 ObjCInterfaceDecl *IDecl) const {
720 // by far the most common case.
721 if (!PDecl->isReadOnly())
722 return false;
723 // Even if property is ready only, if interface has a user defined setter,
724 // it is not considered read only.
725 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
726 return false;
727
728 // Main class has the property as 'readonly'. Must search
729 // through the category list to see if the property's
730 // attribute has been over-ridden to 'readwrite'.
731 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
732 Category; Category = Category->getNextClassCategory()) {
733 // Even if property is ready only, if a category has a user defined setter,
734 // it is not considered read only.
735 if (Category->getInstanceMethod(PDecl->getSetterName()))
736 return false;
737 ObjCPropertyDecl *P =
738 Category->FindPropertyDeclaration(PDecl->getIdentifier());
739 if (P && !P->isReadOnly())
740 return false;
741 }
742
743 // Also, check for definition of a setter method in the implementation if
744 // all else failed.
745 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
746 if (ObjCImplementationDecl *IMD =
747 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
748 if (IMD->getInstanceMethod(PDecl->getSetterName()))
749 return false;
750 }
751 else if (ObjCCategoryImplDecl *CIMD =
752 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
753 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
754 return false;
755 }
756 }
757 return true;
758}
759
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000760/// FIXME: Type hierarchies in Objective-C can be deep. We could most
761/// likely improve the efficiency of selector lookups and type
762/// checking by associating with each protocol / interface / category
763/// the flattened instance tables. If we used an immutable set to keep
764/// the table then it wouldn't add significant memory cost and it
765/// would be handy for lookups.
766
Steve Naroffefe7f362008-02-08 22:06:17 +0000767/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000768/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000769void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
770 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000771 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000772 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000773 const llvm::DenseSet<Selector> &ClsMap,
774 ObjCInterfaceDecl *IDecl) {
775 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
776
777 // If a method lookup fails locally we still need to look and see if
778 // the method was implemented by a base class or an inherited
779 // protocol. This lookup is slow, but occurs rarely in correct code
780 // and otherwise would terminate in a warning.
781
Chris Lattner4d391482007-12-12 07:09:47 +0000782 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000783 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000784 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000785 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000786 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000787 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000788 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000789 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000790 }
791 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000792 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000793 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000794 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000795 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
796 !ClsMap.count(method->getSelector()) &&
797 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000798 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000799 }
Chris Lattner780f3292008-07-21 21:32:27 +0000800 // Check on this protocols's referenced protocols, recursively.
801 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
802 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000803 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000804}
805
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000806void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
807 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000808 llvm::DenseSet<Selector> InsMap;
809 // Check and see if instance methods in class interface have been
810 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000811 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000812 E = IMPDecl->instmeth_end(); I != E; ++I)
813 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000814
815 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000816 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000817 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000818 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000819 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000820 else {
Fariborz Jahaniande739412008-12-05 01:35:25 +0000821 ObjCMethodDecl *ImpMethodDecl =
822 IMPDecl->getInstanceMethod((*I)->getSelector());
823 ObjCMethodDecl *IntfMethodDecl =
824 IDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000825 assert(IntfMethodDecl &&
826 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
827 // ImpMethodDecl may be null as in a @dynamic property.
828 if (ImpMethodDecl)
829 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahaniande739412008-12-05 01:35:25 +0000830 }
Chris Lattner4c525092007-12-12 17:58:05 +0000831
Chris Lattner4d391482007-12-12 07:09:47 +0000832 llvm::DenseSet<Selector> ClsMap;
833 // Check and see if class methods in class interface have been
834 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000835 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000836 E = IMPDecl->classmeth_end(); I != E; ++I)
837 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000838
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000839 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000840 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000841 if (!ClsMap.count((*I)->getSelector()))
842 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000843 else {
844 ObjCMethodDecl *ImpMethodDecl =
845 IMPDecl->getClassMethod((*I)->getSelector());
846 ObjCMethodDecl *IntfMethodDecl =
847 IDecl->getClassMethod((*I)->getSelector());
848 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
849 }
850
Chris Lattner4d391482007-12-12 07:09:47 +0000851
852 // Check the protocol list for unimplemented methods in the @implementation
853 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000854 const ObjCList<ObjCProtocolDecl> &Protocols =
855 IDecl->getReferencedProtocols();
856 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
857 E = Protocols.end(); I != E; ++I)
858 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000859 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000860}
861
862/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000863/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000864void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
865 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000866 llvm::DenseSet<Selector> InsMap;
867 // Check and see if instance methods in category interface have been
868 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000869 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000870 E = CatImplDecl->instmeth_end(); I != E; ++I)
871 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000872
873 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000874 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000875 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000876 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000877 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000878 else {
879 ObjCMethodDecl *ImpMethodDecl =
880 CatImplDecl->getInstanceMethod((*I)->getSelector());
881 ObjCMethodDecl *IntfMethodDecl =
882 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000883 assert(IntfMethodDecl &&
884 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
885 // ImpMethodDecl may be null as in a @dynamic property.
886 if (ImpMethodDecl)
887 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000888 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000889
Chris Lattner4d391482007-12-12 07:09:47 +0000890 llvm::DenseSet<Selector> ClsMap;
891 // Check and see if class methods in category interface have been
892 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000893 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000894 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
895 I != E; ++I)
896 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000897
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000898 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000899 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000900 if (!ClsMap.count((*I)->getSelector()))
901 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000902 else {
903 ObjCMethodDecl *ImpMethodDecl =
904 CatImplDecl->getClassMethod((*I)->getSelector());
905 ObjCMethodDecl *IntfMethodDecl =
906 CatClassDecl->getClassMethod((*I)->getSelector());
907 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
908 }
Chris Lattner4d391482007-12-12 07:09:47 +0000909 // Check the protocol list for unimplemented methods in the @implementation
910 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000911 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
912 E = CatClassDecl->protocol_end(); PI != E; ++PI)
913 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000914 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000915}
916
917/// ActOnForwardClassDeclaration -
918Action::DeclTy *
919Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
920 IdentifierInfo **IdentList, unsigned NumElts)
921{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000922 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000923
924 for (unsigned i = 0; i != NumElts; ++i) {
925 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000926 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000927 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000928 // Maybe we will complain about the shadowed template parameter.
929 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
930 // Just pretend that we didn't see the previous declaration.
931 PrevDecl = 0;
932 }
933
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000934 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000935 // GCC apparently allows the following idiom:
936 //
937 // typedef NSObject < XCElementTogglerP > XCElementToggler;
938 // @class XCElementToggler;
939 //
940 // FIXME: Make an extension?
941 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
942 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000943 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000944 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000945 }
Chris Lattner4d391482007-12-12 07:09:47 +0000946 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000947 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000948 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +0000949 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
950 IdentList[i], SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000951 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000952
Douglas Gregord0434102009-01-09 00:49:46 +0000953 // FIXME: PushOnScopeChains?
Douglas Gregor482b77d2009-01-12 23:27:07 +0000954 CurContext->addDecl(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000955 // Remember that this needs to be removed when the scope is popped.
956 TUScope->AddDecl(IDecl);
957 }
958
959 Interfaces.push_back(IDecl);
960 }
961
Douglas Gregord0434102009-01-09 00:49:46 +0000962 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000963 &Interfaces[0],
964 Interfaces.size());
Douglas Gregor482b77d2009-01-12 23:27:07 +0000965 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000966 CheckObjCDeclScope(CDecl);
967 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000968}
969
970
971/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
972/// returns true, or false, accordingly.
973/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000974bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000975 const ObjCMethodDecl *PrevMethod,
976 bool matchBasedOnSizeAndAlignment) {
977 QualType T1 = Context.getCanonicalType(Method->getResultType());
978 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
979
980 if (T1 != T2) {
981 // The result types are different.
982 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000983 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000984 // Incomplete types don't have a size and alignment.
985 if (T1->isIncompleteType() || T2->isIncompleteType())
986 return false;
987 // Check is based on size and alignment.
988 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
989 return false;
990 }
991 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
992 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
993 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
994 if (T1 != T2) {
995 // The result types are different.
996 if (!matchBasedOnSizeAndAlignment)
997 return false;
998 // Incomplete types don't have a size and alignment.
999 if (T1->isIncompleteType() || T2->isIncompleteType())
1000 return false;
1001 // Check is based on size and alignment.
1002 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1003 return false;
1004 }
Chris Lattner4d391482007-12-12 07:09:47 +00001005 }
1006 return true;
1007}
1008
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001009void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
1010 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001011 if (!FirstMethod.Method) {
1012 // Haven't seen a method with this selector name yet - add it.
1013 FirstMethod.Method = Method;
1014 FirstMethod.Next = 0;
1015 } else {
1016 // We've seen a method with this name, now check the type signature(s).
1017 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1018
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001019 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001020 Next = Next->Next)
1021 match = MatchTwoMethodDeclarations(Method, Next->Method);
1022
1023 if (!match) {
1024 // We have a new signature for an existing method - add it.
1025 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +00001026 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +00001027 }
1028 }
1029}
1030
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001031// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001032ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1033 SourceRange R) {
1034 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001035 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001036
1037 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001038 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1039 // This checks if the methods differ by size & alignment.
1040 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1041 issueWarning = true;
1042 }
1043 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001044 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001045 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001046 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001047 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001048 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001049 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001050 }
1051 return MethList.Method;
1052}
1053
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001054void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1055 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001056 if (!FirstMethod.Method) {
1057 // Haven't seen a method with this selector name yet - add it.
1058 FirstMethod.Method = Method;
1059 FirstMethod.Next = 0;
1060 } else {
1061 // We've seen a method with this name, now check the type signature(s).
1062 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1063
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001064 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001065 Next = Next->Next)
1066 match = MatchTwoMethodDeclarations(Method, Next->Method);
1067
1068 if (!match) {
1069 // We have a new signature for an existing method - add it.
1070 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001071 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001072 FirstMethod.Next = OMI;
1073 }
1074 }
1075}
1076
Steve Naroff0701bbb2009-01-08 17:28:14 +00001077/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1078/// have the property type and issue diagnostics if they don't.
1079/// Also synthesize a getter/setter method if none exist (and update the
1080/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1081/// methods is the "right" thing to do.
1082void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1083 ObjCContainerDecl *CD) {
1084 ObjCMethodDecl *GetterMethod, *SetterMethod;
1085
1086 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1087 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1088
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001089 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001090 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001091 Diag(property->getLocation(),
1092 diag::err_accessor_property_type_mismatch)
1093 << property->getDeclName()
1094 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001095 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1096 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001097
1098 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001099 if (Context.getCanonicalType(SetterMethod->getResultType())
1100 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001101 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1102 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001103 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001104 Diag(property->getLocation(),
1105 diag::err_accessor_property_type_mismatch)
1106 << property->getDeclName()
1107 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001108 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1109 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001110 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001111
1112 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001113 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001114 // FIXME: The synthesized property we set here is misleading. We
1115 // almost always synthesize these methods unless the user explicitly
1116 // provided prototypes (which is odd, but allowed). Sema should be
1117 // typechecking that the declarations jive in that situation (which
1118 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001119 if (!GetterMethod) {
1120 // No instance method of same name as property getter name was found.
1121 // Declare a getter method and add it to the list of methods
1122 // for this class.
1123 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1124 property->getLocation(), property->getGetterName(),
1125 property->getType(), CD, true, false, true,
1126 (property->getPropertyImplementation() ==
1127 ObjCPropertyDecl::Optional) ?
1128 ObjCMethodDecl::Optional :
1129 ObjCMethodDecl::Required);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001130 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001131 } else
1132 // A user declared getter will be synthesize when @synthesize of
1133 // the property with the same name is seen in the @implementation
1134 GetterMethod->setIsSynthesized();
1135 property->setGetterMethodDecl(GetterMethod);
1136
1137 // Skip setter if property is read-only.
1138 if (!property->isReadOnly()) {
1139 // Find the default setter and if one not found, add one.
1140 if (!SetterMethod) {
1141 // No instance method of same name as property setter name was found.
1142 // Declare a setter method and add it to the list of methods
1143 // for this class.
1144 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1145 property->getLocation(),
1146 property->getSetterName(),
1147 Context.VoidTy, CD, true, false, true,
1148 (property->getPropertyImplementation() ==
1149 ObjCPropertyDecl::Optional) ?
1150 ObjCMethodDecl::Optional :
1151 ObjCMethodDecl::Required);
1152 // Invent the arguments for the setter. We don't bother making a
1153 // nice name for the argument.
1154 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1155 SourceLocation(),
1156 property->getIdentifier(),
1157 property->getType(),
1158 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001159 0);
Steve Naroff92f863b2009-01-08 20:15:03 +00001160 SetterMethod->setMethodParams(&Argument, 1);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001161 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001162 } else
1163 // A user declared setter will be synthesize when @synthesize of
1164 // the property with the same name is seen in the @implementation
1165 SetterMethod->setIsSynthesized();
1166 property->setSetterMethodDecl(SetterMethod);
1167 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001168 // Add any synthesized methods to the global pool. This allows us to
1169 // handle the following, which is supported by GCC (and part of the design).
1170 //
1171 // @interface Foo
1172 // @property double bar;
1173 // @end
1174 //
1175 // void thisIsUnfortunate() {
1176 // id foo;
1177 // double bar = [foo bar];
1178 // }
1179 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001180 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001181 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001182 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001183 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001184}
1185
Steve Naroffa56f6162007-12-18 01:30:32 +00001186// Note: For class/category implemenations, allMethods/allProperties is
1187// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001188void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1189 DeclTy **allMethods, unsigned allNum,
1190 DeclTy **allProperties, unsigned pNum) {
1191 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1192
Steve Naroffa56f6162007-12-18 01:30:32 +00001193 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1194 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001195 // should be true.
1196 if (!ClassDecl)
1197 return;
1198
Chris Lattner4d391482007-12-12 07:09:47 +00001199 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001200 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1201 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001202 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001203
Steve Naroff0701bbb2009-01-08 17:28:14 +00001204 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001205
1206 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1207 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1208 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1209
Chris Lattner4d391482007-12-12 07:09:47 +00001210 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001211 ObjCMethodDecl *Method =
1212 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001213
1214 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001215 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001216 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001217 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001218 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1219 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001220 if ((isInterfaceDeclKind && PrevMethod && !match)
1221 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001222 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001223 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001224 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001225 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001226 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001227 InsMap[Method->getSelector()] = Method;
1228 /// The following allows us to typecheck messages to "id".
1229 AddInstanceMethodToGlobalPool(Method);
1230 }
1231 }
1232 else {
1233 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001234 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001235 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1236 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001237 if ((isInterfaceDeclKind && PrevMethod && !match)
1238 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001239 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001240 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001241 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001242 } else {
Douglas Gregor482b77d2009-01-12 23:27:07 +00001243 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001244 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001245 /// The following allows us to typecheck messages to "Class".
1246 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001247 }
1248 }
1249 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001250 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001251 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001252 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001253 ComparePropertiesInBaseAndSuper(I);
1254 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001255 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001256 // Categories are used to extend the class by declaring new methods.
1257 // By the same token, they are also used to add new properties. No
1258 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001259
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001260 // Merge protocol properties into category
1261 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner4d391482007-12-12 07:09:47 +00001262 }
Steve Naroff09c47192009-01-09 15:36:25 +00001263 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1264 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1265 // user-defined setter/getter. It also synthesizes setter/getter methods
1266 // and adds them to the DeclContext and global method pools.
1267 for (ObjCContainerDecl::prop_iterator i = CDecl->prop_begin(),
1268 e = CDecl->prop_end(); i != e; ++i)
1269 ProcessPropertyDecl((*i), CDecl);
1270 CDecl->setAtEndLoc(AtEndLoc);
1271 }
1272 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001273 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001274 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001275 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001276 } else if (ObjCCategoryImplDecl* CatImplClass =
1277 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001278 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001279 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001280 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001281 // in this interface are implemented in the category @implementation.
Chris Lattner4d391482007-12-12 07:09:47 +00001282 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001283 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001284 Categories; Categories = Categories->getNextClassCategory()) {
1285 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1286 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1287 break;
1288 }
1289 }
1290 }
1291 }
1292}
1293
1294
1295/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1296/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001297static Decl::ObjCDeclQualifier
1298CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1299 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1300 if (PQTVal & ObjCDeclSpec::DQ_In)
1301 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1302 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1303 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1304 if (PQTVal & ObjCDeclSpec::DQ_Out)
1305 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1306 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1307 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1308 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1309 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1310 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1311 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001312
1313 return ret;
1314}
1315
1316Sema::DeclTy *Sema::ActOnMethodDeclaration(
1317 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001318 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001319 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001320 Selector Sel,
1321 // optional arguments. The number of types/arguments is obtained
1322 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001323 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001324 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001325 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1326 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001327 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001328
1329 // Make sure we can establish a context for the method.
1330 if (!ClassDecl) {
1331 Diag(MethodLoc, diag::error_missing_method_context);
1332 return 0;
1333 }
Chris Lattner4d391482007-12-12 07:09:47 +00001334 QualType resultDeclType;
1335
1336 if (ReturnType)
1337 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1338 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001339 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001340
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001341 ObjCMethodDecl* ObjCMethod =
1342 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001343 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001344 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001345 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001346 MethodDeclKind == tok::objc_optional ?
1347 ObjCMethodDecl::Optional :
1348 ObjCMethodDecl::Required);
1349
Chris Lattner0ed844b2008-04-04 06:12:32 +00001350 llvm::SmallVector<ParmVarDecl*, 16> Params;
1351
1352 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1353 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001354 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001355
Steve Naroff6082c622008-12-09 19:36:17 +00001356 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001357 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001358 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001359 if (argType->isArrayType()) { // (char *[]) -> (char **)
1360 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001361 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001362 }
Steve Naroff6082c622008-12-09 19:36:17 +00001363 else if (argType->isFunctionType())
1364 argType = Context.getPointerType(argType);
Fariborz Jahanian9bae5e72009-01-17 21:57:49 +00001365 else if (argType->isObjCInterfaceType()) {
1366 // FIXME! provide more precise location for the parameter
1367 Diag(MethodLoc, diag::err_object_as_method_param);
1368 return 0;
1369 }
Steve Naroff6082c622008-12-09 19:36:17 +00001370 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001371 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001372 ParmVarDecl* Param;
1373 if (originalArgType.isNull())
1374 Param = ParmVarDecl::Create(Context, ObjCMethod,
1375 SourceLocation(/*FIXME*/),
1376 ArgNames[i], argType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001377 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001378 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001379 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
1380 SourceLocation(/*FIXME*/),
1381 ArgNames[i], argType, originalArgType,
1382 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001383
Chris Lattner0ed844b2008-04-04 06:12:32 +00001384 Param->setObjCDeclQualifier(
1385 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1386 Params.push_back(Param);
1387 }
1388
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001389 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1390 ObjCMethod->setObjCDeclQualifier(
1391 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1392 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001393
1394 if (AttrList)
1395 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001396
1397 // For implementations (which can be very "coarse grain"), we add the
1398 // method now. This allows the AST to implement lookup methods that work
1399 // incrementally (without waiting until we parse the @end). It also allows
1400 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001401 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001402 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001403 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001404 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001405 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001406 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001407 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001408 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001409 }
1410 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001411 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001412 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001413 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001414 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001415 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001416 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001417 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001418 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001419 }
1420 }
1421 if (PrevMethod) {
1422 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001423 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001424 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001425 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001426 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001427 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001428}
1429
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001430void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1431 SourceLocation Loc,
1432 unsigned &Attributes) {
1433 // FIXME: Improve the reported location.
1434
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001435 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001436 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001437 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1438 ObjCDeclSpec::DQ_PR_assign |
1439 ObjCDeclSpec::DQ_PR_copy |
1440 ObjCDeclSpec::DQ_PR_retain))) {
1441 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1442 "readwrite" :
1443 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1444 "assign" :
1445 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1446 "copy" : "retain";
1447
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001448 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001449 diag::err_objc_property_attr_mutually_exclusive :
1450 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001451 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001452 }
1453
1454 // Check for copy or retain on non-object types.
1455 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1456 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001457 Diag(Loc, diag::err_objc_property_requires_object)
1458 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001459 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1460 }
1461
1462 // Check for more than one of { assign, copy, retain }.
1463 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1464 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001465 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1466 << "assign" << "copy";
1467 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001468 }
1469 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001470 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1471 << "assign" << "retain";
1472 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001473 }
1474 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1475 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001476 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1477 << "copy" << "retain";
1478 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001479 }
1480 }
1481
1482 // Warn if user supplied no assignment attribute, property is
1483 // readwrite, and this is an object type.
1484 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1485 ObjCDeclSpec::DQ_PR_retain)) &&
1486 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1487 Context.isObjCObjectPointerType(PropertyTy)) {
1488 // Skip this warning in gc-only mode.
1489 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1490 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1491
1492 // If non-gc code warn that this is likely inappropriate.
1493 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1494 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1495
1496 // FIXME: Implement warning dependent on NSCopying being
1497 // implemented. See also:
1498 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1499 // (please trim this list while you are at it).
1500 }
1501}
1502
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001503Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1504 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001505 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001506 Selector GetterSel,
1507 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001508 DeclTy *ClassCategory,
1509 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001510 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001511 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001512 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1513 // default is readwrite!
1514 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1515 // property is defaulted to 'assign' if it is readwrite and is
1516 // not retain or copy
1517 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1518 (isReadWrite &&
1519 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1520 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1521 QualType T = GetTypeForDeclarator(FD.D, S);
1522 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001523
1524 // May modify Attributes.
1525 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001526
1527 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1528 if (!CDecl->getIdentifier()) {
1529 // This is an anonymous category. property requires special
1530 // handling.
1531 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1532 if (ObjCPropertyDecl *PIDecl =
1533 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1534 // property 'PIDecl's readonly attribute will be over-ridden
1535 // with anonymous category's readwrite property attribute!
1536 unsigned PIkind = PIDecl->getPropertyAttributes();
1537 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001538 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001539 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1540 Diag(AtLoc, diag::warn_property_attr_mismatch);
1541 PIDecl->makeitReadWriteAttribute();
1542 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1543 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1544 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1545 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1546 PIDecl->setSetterName(SetterSel);
1547 // FIXME: use a common routine with addPropertyMethods.
1548 ObjCMethodDecl *SetterDecl =
1549 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1550 Context.VoidTy,
1551 ICDecl,
1552 true, false, true,
1553 ObjCMethodDecl::Required);
1554 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1555 SetterDecl,
1556 SourceLocation(),
1557 FD.D.getIdentifier(),
1558 T,
1559 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001560 0);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001561 SetterDecl->setMethodParams(&Argument, 1);
1562 PIDecl->setSetterMethodDecl(SetterDecl);
1563 }
1564 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001565 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001566 *isOverridingProperty = true;
1567 return 0;
1568 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001569 // No matching property found in the main class. Just fall thru
1570 // and add property to the anonymous category. It looks like
Ben Laurie9af5e672009-02-16 09:18:41 +00001571 // it works as is. This category becomes just like a category
1572 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001573 } else {
1574 Diag(CDecl->getLocation(), diag::err_continuation_class);
1575 *isOverridingProperty = true;
1576 return 0;
1577 }
1578 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001579
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001580 Type *t = T.getTypePtr();
1581 if (t->isArrayType() || t->isFunctionType())
1582 Diag(AtLoc, diag::err_property_type) << T;
1583
Steve Naroff93983f82009-01-11 12:47:58 +00001584 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1585 assert(DC && "ClassDecl is not a DeclContext");
1586 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001587 FD.D.getIdentifier(), T);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001588 DC->addDecl(PDecl);
Douglas Gregord0434102009-01-09 00:49:46 +00001589
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001590 // Regardless of setter/getter attribute, we save the default getter/setter
1591 // selector names in anticipation of declaration of setter/getter methods.
1592 PDecl->setGetterName(GetterSel);
1593 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001594
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001595 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001596 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001597
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001598 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001599 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001600
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001601 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001602 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001603
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001604 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001605 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001606
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001607 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001608 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001609
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001610 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001611 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001612
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001613 if (isAssign)
1614 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1615
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001616 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001617 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001618
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001619 if (MethodImplKind == tok::objc_required)
1620 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1621 else if (MethodImplKind == tok::objc_optional)
1622 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1623
Chris Lattner4d391482007-12-12 07:09:47 +00001624 return PDecl;
1625}
1626
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001627/// ActOnPropertyImplDecl - This routine performs semantic checks and
1628/// builds the AST node for a property implementation declaration; declared
1629/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001630///
1631Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1632 SourceLocation PropertyLoc,
1633 bool Synthesize,
1634 DeclTy *ClassCatImpDecl,
1635 IdentifierInfo *PropertyId,
1636 IdentifierInfo *PropertyIvar) {
1637 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1638 // Make sure we have a context for the property implementation declaration.
1639 if (!ClassImpDecl) {
1640 Diag(AtLoc, diag::error_missing_property_context);
1641 return 0;
1642 }
1643 ObjCPropertyDecl *property = 0;
1644 ObjCInterfaceDecl* IDecl = 0;
1645 // Find the class or category class where this property must have
1646 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001647 ObjCImplementationDecl *IC = 0;
1648 ObjCCategoryImplDecl* CatImplClass = 0;
1649 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001650 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001651 // We always synthesize an interface for an implementation
1652 // without an interface decl. So, IDecl is always non-zero.
1653 assert(IDecl &&
1654 "ActOnPropertyImplDecl - @implementation without @interface");
1655
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001656 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001657 property = IDecl->FindPropertyDeclaration(PropertyId);
1658 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001659 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001660 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001661 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001662 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001663 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001664 if (Synthesize) {
1665 Diag(AtLoc, diag::error_synthesize_category_decl);
1666 return 0;
1667 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001668 IDecl = CatImplClass->getClassInterface();
1669 if (!IDecl) {
1670 Diag(AtLoc, diag::error_missing_property_interface);
1671 return 0;
1672 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001673 ObjCCategoryDecl *Category =
1674 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1675
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001676 // If category for this implementation not found, it is an error which
1677 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001678 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001679 return 0;
1680 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001681 property = Category->FindPropertyDeclaration(PropertyId);
1682 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001683 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001684 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001685 return 0;
1686 }
1687 }
1688 else {
1689 Diag(AtLoc, diag::error_bad_property_context);
1690 return 0;
1691 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001692 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001693 // Check that we have a valid, previously declared ivar for @synthesize
1694 if (Synthesize) {
1695 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001696 if (!PropertyIvar)
1697 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001698 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001699 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001700 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001701 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001702 return 0;
1703 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001704 QualType PropType = Context.getCanonicalType(property->getType());
1705 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1706
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001707 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001708 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001709 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001710 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001711 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001712 return 0;
1713 }
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00001714 else {
1715 // FIXME! Rules for properties are somewhat different that those
1716 // for assignments. Use a new routine to consolidate all cases;
1717 // specifically for property redeclarations as well as for ivars.
1718 QualType lhsType =
1719 Context.getCanonicalType(PropType).getUnqualifiedType();
1720 QualType rhsType =
1721 Context.getCanonicalType(IvarType).getUnqualifiedType();
1722 if (lhsType != rhsType &&
1723 lhsType->isArithmeticType()) {
1724 Diag(PropertyLoc, diag::error_property_ivar_type)
1725 << property->getDeclName() << Ivar->getDeclName();
1726 return 0;
1727 }
1728 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001729 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001730 } else if (PropertyIvar) {
1731 // @dynamic
1732 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1733 return 0;
1734 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001735 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001736 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00001737 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1738 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001739 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001740 ObjCPropertyImplDecl::Synthesize
1741 : ObjCPropertyImplDecl::Dynamic),
1742 Ivar);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001743 CurContext->addDecl(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001744 if (IC) {
1745 if (Synthesize)
1746 if (ObjCPropertyImplDecl *PPIDecl =
1747 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1748 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1749 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1750 << PropertyIvar;
1751 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1752 }
1753
1754 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1755 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1756 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1757 return 0;
1758 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001759 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001760 }
1761 else {
1762 if (Synthesize)
1763 if (ObjCPropertyImplDecl *PPIDecl =
1764 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1765 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1766 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1767 << PropertyIvar;
1768 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1769 }
1770
1771 if (ObjCPropertyImplDecl *PPIDecl =
1772 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1773 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1774 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1775 return 0;
1776 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001777 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001778 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001779
1780 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001781}
Anders Carlsson15281452008-11-04 16:57:32 +00001782
Chris Lattnercc98eac2008-12-17 07:13:27 +00001783bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001784 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001785 return false;
1786
1787 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1788 D->setInvalidDecl();
1789
1790 return true;
1791}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001792
1793/// Collect the instance variables declared in an Objective-C object. Used in
1794/// the creation of structures from objects using the @defs directive.
1795/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1796/// part of the AST generation logic of @defs.
1797static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1798 ASTContext& Ctx,
1799 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1800 if (Class->getSuperClass())
1801 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1802
1803 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1804 for (ObjCInterfaceDecl::ivar_iterator
1805 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1806
1807 ObjCIvarDecl* ID = *I;
1808 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1809 ID->getLocation(),
1810 ID->getIdentifier(),
1811 ID->getType(),
1812 ID->getBitWidth()));
1813 }
1814}
1815
1816/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1817/// instance variables of ClassName into Decls.
1818void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1819 IdentifierInfo *ClassName,
1820 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1821 // Check that ClassName is a valid class
1822 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1823 if (!Class) {
1824 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1825 return;
1826 }
1827 // Collect the instance variables
1828 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1829
1830 // Introduce all of these fields into the appropriate scope.
1831 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1832 D != Decls.end(); ++D) {
1833 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1834 if (getLangOptions().CPlusPlus)
1835 PushOnScopeChains(cast<FieldDecl>(FD), S);
1836 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
Douglas Gregor482b77d2009-01-12 23:27:07 +00001837 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001838 }
1839}
1840