blob: 5f191a2f3cbfcc5f8254d6f00ec3b347be652bb5 [file] [log] [blame]
Chris Lattner855e51f2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner855e51f2007-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 Dunbar8d03cbe2008-08-11 03:27:53 +000017#include "clang/Basic/Diagnostic.h"
18#include "clang/Parse/DeclSpec.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000019
20using namespace clang;
21
Ted Kremenek42730c52008-01-07 19:49:32 +000022/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000023/// and user declared, in the method definition's AST.
Ted Kremenek42730c52008-01-07 19:49:32 +000024void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000025 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff3ac43f92008-07-25 17:57:26 +000026 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
27
28 // If we don't have a valid method decl, simply return.
29 if (!MDecl)
30 return;
Steve Narofffe9eb6a2007-12-18 01:30:32 +000031
32 // Allow the rest of sema to find private method decl implementations.
33 if (MDecl->isInstance())
34 AddInstanceMethodToGlobalPool(MDecl);
35 else
36 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000037
38 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor8acb7272008-12-11 16:49:14 +000039 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000040
41 // Create Decl objects for each parameter, entrring them in the scope for
42 // binding to their use.
Chris Lattner855e51f2007-12-12 07:09:47 +000043
44 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +000045 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +000046
Daniel Dunbareaf91c32008-08-26 06:07:48 +000047 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
48 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000049
Chris Lattner97316c02008-04-10 02:22:51 +000050 // Introduce all of the other parameters into this scope.
Chris Lattner685d7922008-03-16 01:07:14 +000051 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000052 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +000053 IdentifierInfo *II = PDecl->getIdentifier();
Argiris Kirtzidis43ce0be2008-04-27 13:30:35 +000054 if (II)
55 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000056 }
57}
58
Chris Lattnere705e5e2008-07-21 22:17:28 +000059Sema::DeclTy *Sema::
60ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerae1ae492008-07-26 04:13:19 +000063 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000064 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000065 assert(ClassName && "Missing class identifier");
66
67 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +000068 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Douglas Gregor2715a1f2008-12-08 18:40:42 +000069 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +000070 // Maybe we will complain about the shadowed template parameter.
71 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
72 // Just pretend that we didn't see the previous declaration.
73 PrevDecl = 0;
74 }
75
Ted Kremenek42730c52008-01-07 19:49:32 +000076 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000077 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +000078 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +000079 }
80
Ted Kremenek42730c52008-01-07 19:49:32 +000081 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000082 if (IDecl) {
83 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +000084 if (!IDecl->isForwardDecl()) {
Chris Lattner271d4c22008-11-24 05:29:24 +000085 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattner5b250652008-11-23 22:46:27 +000086 Diag(IDecl->getLocation(), diag::note_previous_definition);
87
Steve Naroff1422a622008-11-18 19:15:30 +000088 // Return the previous class interface.
89 // FIXME: don't leak the objects passed in!
90 return IDecl;
91 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +000092 IDecl->setLocation(AtInterfaceLoc);
93 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000094 }
Chris Lattner5cece462008-07-21 07:06:49 +000095 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000096 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000097 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000098 if (AttrList)
99 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000100
Steve Naroff15208162008-04-02 18:30:49 +0000101 ObjCInterfaceDecls[ClassName] = IDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000102 // FIXME: PushOnScopeChains
103 CurContext->addDecl(Context, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000104 // Remember that this needs to be removed when the scope is popped.
105 TUScope->AddDecl(IDecl);
106 }
107
108 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000110 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000111 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000112 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000113 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000114 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000115 }
116 else {
117 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000118 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner65cae292008-11-19 08:23:25 +0000119
120 if (!SuperClassEntry)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000121 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner65cae292008-11-19 08:23:25 +0000122 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
123 else if (SuperClassEntry->isForwardDecl())
124 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattnerb1753422008-11-23 21:45:46 +0000125 << SuperClassEntry->getDeclName() << ClassName
Chris Lattner65cae292008-11-19 08:23:25 +0000126 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000127 }
128 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000129 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000130 IDecl->setLocEnd(SuperLoc);
131 } else { // we have a root class.
132 IDecl->setLocEnd(ClassLoc);
133 }
134
Steve Naroff1422a622008-11-18 19:15:30 +0000135 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000136 if (NumProtoRefs) {
137 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000138 IDecl->setLocEnd(EndProtoLoc);
139 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000140
141 CheckObjCDeclScope(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000142 return IDecl;
143}
144
145/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000146/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000147Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
148 IdentifierInfo *AliasName,
149 SourceLocation AliasLocation,
150 IdentifierInfo *ClassName,
151 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000152 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000153 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000154 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000155 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000156 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000157 else
Chris Lattner65cae292008-11-19 08:23:25 +0000158 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000159 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000160 return 0;
161 }
162 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000163 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000164 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
165 QualType T = TDecl->getUnderlyingType();
166 if (T->isObjCInterfaceType()) {
167 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
168 ClassName = IDecl->getIdentifier();
169 CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
170 }
171 }
172 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000173 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
174 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000175 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000176 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000177 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000178 return 0;
179 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000180
181 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000182 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000183 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe57c21a2008-04-01 23:04:06 +0000184
185 ObjCAliasDecls[AliasName] = AliasDecl;
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000186
187 // FIXME: PushOnScopeChains?
188 CurContext->addDecl(Context, AliasDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000189 if (!CheckObjCDeclScope(AliasDecl))
190 TUScope->AddDecl(AliasDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000191
Chris Lattner855e51f2007-12-12 07:09:47 +0000192 return AliasDecl;
193}
194
Chris Lattner2bdedd62008-07-26 04:03:38 +0000195Sema::DeclTy *
196Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
197 IdentifierInfo *ProtocolName,
198 SourceLocation ProtocolLoc,
199 DeclTy * const *ProtoRefs,
200 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000201 SourceLocation EndProtoLoc,
202 AttributeList *AttrList) {
203 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000204 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000205 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000206 if (PDecl) {
207 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000208 if (!PDecl->isForwardDecl()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000209 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000210 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-03-16 01:25:17 +0000211 // Just return the protocol we already had.
212 // FIXME: don't leak the objects passed in!
213 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000214 }
Steve Naroff9a9e5212008-08-13 16:39:22 +0000215 // Make sure the cached decl gets a valid start location.
216 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000217 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000218 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000219 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
220 AtProtoInterfaceLoc,ProtocolName);
221 // FIXME: PushOnScopeChains?
222 CurContext->addDecl(Context, PDecl);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000223 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000224 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000225 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000226 if (AttrList)
227 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000228 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000229 /// Check then save referenced protocols.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000230 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000231 PDecl->setLocEnd(EndProtoLoc);
232 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000233
234 CheckObjCDeclScope(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000235 return PDecl;
236}
237
238/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000239/// issues an error if they are not declared. It returns list of
240/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000241void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000242Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000243 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000244 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000245 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000246 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000247 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
248 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000249 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000250 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000251 continue;
252 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000253 for (const Attr *attr = PDecl->getAttrs(); attr; attr = attr->getNext()) {
Fariborz Jahanianf463b9a2008-12-29 19:57:17 +0000254 if (attr->getKind() == Attr::Unavailable)
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000255 Diag(ProtocolId[i].second, diag::warn_unavailable) <<
256 PDecl->getDeclName();
Fariborz Jahanianf463b9a2008-12-29 19:57:17 +0000257 if (attr->getKind() == Attr::Deprecated)
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000258 Diag(ProtocolId[i].second, diag::warn_deprecated) <<
259 PDecl->getDeclName();
260 }
Chris Lattner17d50a92008-07-26 03:47:43 +0000261
262 // If this is a forward declaration and we are supposed to warn in this
263 // case, do it.
264 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000265 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000266 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000267 Protocols.push_back(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000268 }
269}
270
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000271/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000272/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000273///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000274void
275Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
276 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000277 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000278 ObjCPropertyDecl::PropertyAttributeKind CAttr =
279 Property->getPropertyAttributes();
280 ObjCPropertyDecl::PropertyAttributeKind SAttr =
281 SuperProperty->getPropertyAttributes();
282 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
283 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000284 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000285 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000286 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
287 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000288 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000289 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000290 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
291 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000292 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000293 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000294
295 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
296 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000297 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000298 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000299 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000300 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000301 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000302 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000303 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000304 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000305
Chris Lattnerc5cff302008-07-26 20:50:02 +0000306 if (Context.getCanonicalType(Property->getType()) !=
307 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattner70b93d82008-11-18 22:52:51 +0000308 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000309 << Property->getType() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000310
311}
312
313/// ComparePropertiesInBaseAndSuper - This routine compares property
314/// declarations in base and its super class, if any, and issues
315/// diagnostics in a variety of inconsistant situations.
316///
317void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000318Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000319 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
320 if (!SDecl)
321 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000322 // FIXME: O(N^2)
Fariborz Jahanianed986602008-05-01 00:03:38 +0000323 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
324 E = SDecl->classprop_end(); S != E; ++S) {
325 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000326 // Does property in super class has declaration in current class?
327 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
328 E = IDecl->classprop_end(); I != E; ++I) {
329 ObjCPropertyDecl *PDecl = (*I);
330 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000331 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000332 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000333 }
334 }
335}
336
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000337/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
338/// of properties declared in a protocol and adds them to the list
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000339/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000340void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000341Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000342 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000343 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000344 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
345 if (!IDecl) {
346 // Category
347 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
348 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
349 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
350 E = PDecl->classprop_end(); P != E; ++P) {
351 ObjCPropertyDecl *Pr = (*P);
352 ObjCCategoryDecl::classprop_iterator CP, CE;
353 // Is this property already in category's list of properties?
354 for (CP = CatDecl->classprop_begin(), CE = CatDecl->classprop_end();
355 CP != CE; ++CP)
356 if ((*CP)->getIdentifier() == Pr->getIdentifier())
357 break;
358 if (CP == CE)
359 // Add this property to list of properties for thie class.
360 mergeProperties.push_back(Pr);
361 else
362 // Property protocol already exist in class. Diagnose any mismatch.
363 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
364 }
365 CatDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
366 return;
367 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000368 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
369 E = PDecl->classprop_end(); P != E; ++P) {
370 ObjCPropertyDecl *Pr = (*P);
371 ObjCInterfaceDecl::classprop_iterator CP, CE;
372 // Is this property already in class's list of properties?
373 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
374 CP != CE; ++CP)
375 if ((*CP)->getIdentifier() == Pr->getIdentifier())
376 break;
377 if (CP == CE)
378 // Add this property to list of properties for thie class.
379 mergeProperties.push_back(Pr);
380 else
381 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000382 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000383 }
384 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
385}
386
387/// MergeProtocolPropertiesIntoClass - This routine merges properties
388/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000389/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000390///
391
392void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000393Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000394 DeclTy *MergeItsProtocols) {
395 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000396 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
397
398 if (!IDecl) {
399 // Category
400 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
401 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
402 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
403 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
404 E = MDecl->protocol_end(); P != E; ++P)
405 // Merge properties of category (*P) into IDECL's
406 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
407
408 // Go thru the list of protocols for this category and recursively merge
409 // their properties into this class as well.
410 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
411 E = CatDecl->protocol_end(); P != E; ++P)
412 MergeProtocolPropertiesIntoClass(CatDecl, *P);
413 } else {
414 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
415 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
416 E = MD->protocol_end(); P != E; ++P)
417 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
418 }
419 return;
420 }
421
Chris Lattner5cece462008-07-21 07:06:49 +0000422 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000423 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
424 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000425 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000426 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
427
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000428 // Go thru the list of protocols for this class and recursively merge
429 // their properties into this class as well.
430 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
431 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000432 MergeProtocolPropertiesIntoClass(IDecl, *P);
433 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000434 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
435 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
436 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000437 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000438 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000439}
440
Chris Lattner855e51f2007-12-12 07:09:47 +0000441/// ActOnForwardProtocolDeclaration -
442Action::DeclTy *
443Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000444 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000445 unsigned NumElts,
446 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000447 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000448
449 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000450 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000451 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000452 if (PDecl == 0) { // Not already seen?
453 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
454 IdentList[i].second, Ident);
455 // FIXME: PushOnScopeChains?
456 CurContext->addDecl(Context, PDecl);
457 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000458 if (attrList)
459 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000460 Protocols.push_back(PDecl);
461 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000462
463 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000464 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000465 &Protocols[0], Protocols.size());
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000466 CurContext->addDecl(Context, PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000467 CheckObjCDeclScope(PDecl);
468 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000469}
470
Chris Lattnere705e5e2008-07-21 22:17:28 +0000471Sema::DeclTy *Sema::
472ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
473 IdentifierInfo *ClassName, SourceLocation ClassLoc,
474 IdentifierInfo *CategoryName,
475 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000476 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000477 unsigned NumProtoRefs,
478 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000479 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000480
Chris Lattnere29dc832008-03-16 20:34:23 +0000481 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000482 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
483 // FIXME: PushOnScopeChains?
484 CurContext->addDecl(Context, CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000485 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000486
487 /// Check that class of this category is already completely declared.
488 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000489 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffac0580b2008-06-05 15:03:27 +0000490 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000491 /// Check for duplicate interface declaration for this category
492 ObjCCategoryDecl *CDeclChain;
493 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
494 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000495 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000496 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner65cae292008-11-19 08:23:25 +0000497 << ClassName << CategoryName;
Chris Lattnerde1bd982008-11-23 22:38:38 +0000498 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000499 break;
500 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000501 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000502 if (!CDeclChain)
503 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000504 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000505
506 if (NumProtoRefs) {
Chris Lattner45142b92008-07-26 04:07:02 +0000507 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
508 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000509 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000510
511 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000512 return CDecl;
513}
514
515/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000516/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000517/// object.
518Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
519 SourceLocation AtCatImplLoc,
520 IdentifierInfo *ClassName, SourceLocation ClassLoc,
521 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000522 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000523 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000524 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
525 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000526 /// Check that class of this category is already completely declared.
527 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000528 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000529
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000530 // FIXME: PushOnScopeChains?
531 CurContext->addDecl(Context, CDecl);
532
Chris Lattner855e51f2007-12-12 07:09:47 +0000533 /// TODO: Check that CatName, category name, is not used in another
534 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000535 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000536
537 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000538 return CDecl;
539}
540
541Sema::DeclTy *Sema::ActOnStartClassImplementation(
542 SourceLocation AtClassImplLoc,
543 IdentifierInfo *ClassName, SourceLocation ClassLoc,
544 IdentifierInfo *SuperClassname,
545 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000546 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000547 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000548 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000549 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000550 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000551 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000552 }
553 else {
554 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000555 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000556 if (!IDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000557 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000558 }
559
560 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000561 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000562 if (SuperClassname) {
563 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000564 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000565 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000566 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
567 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000568 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000569 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000570 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000571 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000572 Diag(SuperClassLoc, diag::err_undef_superclass)
573 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000574 else if (IDecl && IDecl->getSuperClass() != SDecl) {
575 // This implementation and its interface do not have the same
576 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000577 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000578 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000579 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000580 }
581 }
582 }
583
584 if (!IDecl) {
585 // Legacy case of @implementation with no corresponding @interface.
586 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000587
588 // FIXME: Do we support attributes on the @implementation? If so
589 // we should copy them over.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000590 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
591 ClassName, ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000592 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000593 IDecl->setSuperClass(SDecl);
594 IDecl->setLocEnd(ClassLoc);
595
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000596 // FIXME: PushOnScopeChains?
597 CurContext->addDecl(Context, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000598 // Remember that this needs to be removed when the scope is popped.
599 TUScope->AddDecl(IDecl);
600 }
601
Ted Kremenek42730c52008-01-07 19:49:32 +0000602 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000603 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
604 ClassName, IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000605
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000606 // FIXME: PushOnScopeChains?
607 CurContext->addDecl(Context, IMPDecl);
608
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000609 if (CheckObjCDeclScope(IMPDecl))
610 return IMPDecl;
611
Chris Lattner855e51f2007-12-12 07:09:47 +0000612 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000613 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000614 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000615 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000616 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000617 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000618 return IMPDecl;
619}
620
Ted Kremenek42730c52008-01-07 19:49:32 +0000621void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
622 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000623 SourceLocation RBrace) {
624 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000625 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000626 if (!IDecl)
627 return;
628 /// Check case of non-existing @interface decl.
629 /// (legacy objective-c @implementation decl without an @interface decl).
630 /// Add implementations's ivar to the synthesize class's ivar list.
631 if (IDecl->ImplicitInterfaceDecl()) {
632 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
633 return;
634 }
635 // If implementation has empty ivar list, just return.
636 if (numIvars == 0)
637 return;
638
639 assert(ivars && "missing @implementation ivars");
640
641 // Check interface's Ivar list against those in the implementation.
642 // names and types must match.
643 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000644 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000645 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000646 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
647 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000648 ObjCIvarDecl* ImplIvar = ivars[j++];
649 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000650 assert (ImplIvar && "missing implementation ivar");
651 assert (ClsIvar && "missing class ivar");
Chris Lattnerb5457862008-07-27 00:05:05 +0000652 if (Context.getCanonicalType(ImplIvar->getType()) !=
653 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000654 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000655 << ImplIvar->getIdentifier()
656 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000657 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000658 }
659 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
660 // as error.
661 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000662 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000663 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000664 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000665 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000666 }
667 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000668 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000669
670 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000671 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000672 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000673 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000674}
675
Steve Naroffb4f48512008-02-10 21:38:56 +0000676void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
677 bool &IncompleteImpl) {
678 if (!IncompleteImpl) {
679 Diag(ImpLoc, diag::warn_incomplete_impl);
680 IncompleteImpl = true;
681 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000682 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000683}
684
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000685void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
686 ObjCMethodDecl *IntfMethodDecl) {
687 bool err = false;
688 QualType ImpMethodQType =
689 Context.getCanonicalType(ImpMethodDecl->getResultType());
690 QualType IntfMethodQType =
691 Context.getCanonicalType(IntfMethodDecl->getResultType());
692 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
693 err = true;
694 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
695 IF=IntfMethodDecl->param_begin(),
696 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
697 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
698 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
699 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
700 err = true;
701 break;
702 }
703 }
704 if (err) {
705 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
706 << ImpMethodDecl->getDeclName();
707 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
708 }
709}
710
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000711/// FIXME: Type hierarchies in Objective-C can be deep. We could most
712/// likely improve the efficiency of selector lookups and type
713/// checking by associating with each protocol / interface / category
714/// the flattened instance tables. If we used an immutable set to keep
715/// the table then it wouldn't add significant memory cost and it
716/// would be handy for lookups.
717
Steve Naroffb268d2a2008-02-08 22:06:17 +0000718/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000719/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000720void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
721 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000722 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000723 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000724 const llvm::DenseSet<Selector> &ClsMap,
725 ObjCInterfaceDecl *IDecl) {
726 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
727
728 // If a method lookup fails locally we still need to look and see if
729 // the method was implemented by a base class or an inherited
730 // protocol. This lookup is slow, but occurs rarely in correct code
731 // and otherwise would terminate in a warning.
732
Chris Lattner855e51f2007-12-12 07:09:47 +0000733 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000734 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000735 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000736 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000737 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000738 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000739 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000740 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000741 }
742 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000743 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000744 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000745 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000746 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
747 !ClsMap.count(method->getSelector()) &&
748 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000749 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000750 }
Chris Lattner0be08822008-07-21 21:32:27 +0000751 // Check on this protocols's referenced protocols, recursively.
752 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
753 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000754 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000755}
756
Ted Kremenek42730c52008-01-07 19:49:32 +0000757void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
758 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000759 llvm::DenseSet<Selector> InsMap;
760 // Check and see if instance methods in class interface have been
761 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000762 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000763 E = IMPDecl->instmeth_end(); I != E; ++I)
764 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000765
766 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000767 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000768 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000769 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000770 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian42d0bf92008-12-22 19:05:31 +0000771 else {
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000772 ObjCMethodDecl *ImpMethodDecl =
773 IMPDecl->getInstanceMethod((*I)->getSelector());
774 ObjCMethodDecl *IntfMethodDecl =
775 IDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian42d0bf92008-12-22 19:05:31 +0000776 assert(IntfMethodDecl &&
777 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
778 // ImpMethodDecl may be null as in a @dynamic property.
779 if (ImpMethodDecl)
780 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000781 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000782
Chris Lattner855e51f2007-12-12 07:09:47 +0000783 llvm::DenseSet<Selector> ClsMap;
784 // Check and see if class methods in class interface have been
785 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000786 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000787 E = IMPDecl->classmeth_end(); I != E; ++I)
788 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000789
Ted Kremenek42730c52008-01-07 19:49:32 +0000790 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000791 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000792 if (!ClsMap.count((*I)->getSelector()))
793 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000794 else {
795 ObjCMethodDecl *ImpMethodDecl =
796 IMPDecl->getClassMethod((*I)->getSelector());
797 ObjCMethodDecl *IntfMethodDecl =
798 IDecl->getClassMethod((*I)->getSelector());
799 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
800 }
801
Chris Lattner855e51f2007-12-12 07:09:47 +0000802
803 // Check the protocol list for unimplemented methods in the @implementation
804 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000805 const ObjCList<ObjCProtocolDecl> &Protocols =
806 IDecl->getReferencedProtocols();
807 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
808 E = Protocols.end(); I != E; ++I)
809 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000810 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000811}
812
813/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000814/// category interface are implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000815void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
816 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000817 llvm::DenseSet<Selector> InsMap;
818 // Check and see if instance methods in category interface have been
819 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000820 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000821 E = CatImplDecl->instmeth_end(); I != E; ++I)
822 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000823
824 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000825 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000826 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian42d0bf92008-12-22 19:05:31 +0000827 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000828 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000829 else {
830 ObjCMethodDecl *ImpMethodDecl =
831 CatImplDecl->getInstanceMethod((*I)->getSelector());
832 ObjCMethodDecl *IntfMethodDecl =
833 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian42d0bf92008-12-22 19:05:31 +0000834 assert(IntfMethodDecl &&
835 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
836 // ImpMethodDecl may be null as in a @dynamic property.
837 if (ImpMethodDecl)
838 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000839 }
Steve Naroffb4f48512008-02-10 21:38:56 +0000840
Chris Lattner855e51f2007-12-12 07:09:47 +0000841 llvm::DenseSet<Selector> ClsMap;
842 // Check and see if class methods in category interface have been
843 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000844 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000845 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
846 I != E; ++I)
847 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000848
Ted Kremenek42730c52008-01-07 19:49:32 +0000849 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000850 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000851 if (!ClsMap.count((*I)->getSelector()))
852 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000853 else {
854 ObjCMethodDecl *ImpMethodDecl =
855 CatImplDecl->getClassMethod((*I)->getSelector());
856 ObjCMethodDecl *IntfMethodDecl =
857 CatClassDecl->getClassMethod((*I)->getSelector());
858 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
859 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000860 // Check the protocol list for unimplemented methods in the @implementation
861 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000862 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
863 E = CatClassDecl->protocol_end(); PI != E; ++PI)
864 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000865 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +0000866}
867
868/// ActOnForwardClassDeclaration -
869Action::DeclTy *
870Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
871 IdentifierInfo **IdentList, unsigned NumElts)
872{
Ted Kremenek42730c52008-01-07 19:49:32 +0000873 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000874
875 for (unsigned i = 0; i != NumElts; ++i) {
876 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000877 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000878 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +0000879 // Maybe we will complain about the shadowed template parameter.
880 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
881 // Just pretend that we didn't see the previous declaration.
882 PrevDecl = 0;
883 }
884
Ted Kremenek42730c52008-01-07 19:49:32 +0000885 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000886 // GCC apparently allows the following idiom:
887 //
888 // typedef NSObject < XCElementTogglerP > XCElementToggler;
889 // @class XCElementToggler;
890 //
891 // FIXME: Make an extension?
892 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
893 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +0000894 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +0000895 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +0000896 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000897 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000898 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000899 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000900 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
901 IdentList[i], SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000902 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000903
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000904 // FIXME: PushOnScopeChains?
905 CurContext->addDecl(Context, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000906 // Remember that this needs to be removed when the scope is popped.
907 TUScope->AddDecl(IDecl);
908 }
909
910 Interfaces.push_back(IDecl);
911 }
912
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000913 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000914 &Interfaces[0],
915 Interfaces.size());
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000916 CurContext->addDecl(Context, CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000917 CheckObjCDeclScope(CDecl);
918 return CDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000919}
920
921
922/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
923/// returns true, or false, accordingly.
924/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000925bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +0000926 const ObjCMethodDecl *PrevMethod,
927 bool matchBasedOnSizeAndAlignment) {
928 QualType T1 = Context.getCanonicalType(Method->getResultType());
929 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
930
931 if (T1 != T2) {
932 // The result types are different.
933 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +0000934 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +0000935 // Incomplete types don't have a size and alignment.
936 if (T1->isIncompleteType() || T2->isIncompleteType())
937 return false;
938 // Check is based on size and alignment.
939 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
940 return false;
941 }
942 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
943 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
944 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
945 if (T1 != T2) {
946 // The result types are different.
947 if (!matchBasedOnSizeAndAlignment)
948 return false;
949 // Incomplete types don't have a size and alignment.
950 if (T1->isIncompleteType() || T2->isIncompleteType())
951 return false;
952 // Check is based on size and alignment.
953 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
954 return false;
955 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000956 }
957 return true;
958}
959
Ted Kremenek42730c52008-01-07 19:49:32 +0000960void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
961 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000962 if (!FirstMethod.Method) {
963 // Haven't seen a method with this selector name yet - add it.
964 FirstMethod.Method = Method;
965 FirstMethod.Next = 0;
966 } else {
967 // We've seen a method with this name, now check the type signature(s).
968 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
969
Ted Kremenek42730c52008-01-07 19:49:32 +0000970 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000971 Next = Next->Next)
972 match = MatchTwoMethodDeclarations(Method, Next->Method);
973
974 if (!match) {
975 // We have a new signature for an existing method - add it.
976 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner3a8f2942008-11-24 03:33:13 +0000977 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner855e51f2007-12-12 07:09:47 +0000978 }
979 }
980}
981
Steve Naroffec7e0882008-10-21 10:50:19 +0000982// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +0000983ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
984 SourceRange R) {
985 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +0000986 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +0000987
988 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +0000989 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
990 // This checks if the methods differ by size & alignment.
991 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
992 issueWarning = true;
993 }
994 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +0000995 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +0000996 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000997 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +0000998 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +0000999 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001000 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001001 }
1002 return MethList.Method;
1003}
1004
Ted Kremenek42730c52008-01-07 19:49:32 +00001005void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1006 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001007 if (!FirstMethod.Method) {
1008 // Haven't seen a method with this selector name yet - add it.
1009 FirstMethod.Method = Method;
1010 FirstMethod.Next = 0;
1011 } else {
1012 // We've seen a method with this name, now check the type signature(s).
1013 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1014
Ted Kremenek42730c52008-01-07 19:49:32 +00001015 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001016 Next = Next->Next)
1017 match = MatchTwoMethodDeclarations(Method, Next->Method);
1018
1019 if (!match) {
1020 // We have a new signature for an existing method - add it.
1021 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001022 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001023 FirstMethod.Next = OMI;
1024 }
1025 }
1026}
1027
Steve Naroffab63fd62009-01-08 17:28:14 +00001028/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1029/// have the property type and issue diagnostics if they don't.
1030/// Also synthesize a getter/setter method if none exist (and update the
1031/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1032/// methods is the "right" thing to do.
1033void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1034 ObjCContainerDecl *CD) {
1035 ObjCMethodDecl *GetterMethod, *SetterMethod;
1036
1037 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1038 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1039
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001040 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001041 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001042 Diag(property->getLocation(),
1043 diag::err_accessor_property_type_mismatch)
1044 << property->getDeclName()
1045 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001046 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1047 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001048
1049 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001050 if (Context.getCanonicalType(SetterMethod->getResultType())
1051 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001052 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1053 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001054 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001055 Diag(property->getLocation(),
1056 diag::err_accessor_property_type_mismatch)
1057 << property->getDeclName()
1058 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001059 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1060 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001061 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001062
1063 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001064 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001065 // FIXME: The synthesized property we set here is misleading. We
1066 // almost always synthesize these methods unless the user explicitly
1067 // provided prototypes (which is odd, but allowed). Sema should be
1068 // typechecking that the declarations jive in that situation (which
1069 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001070 if (!GetterMethod) {
1071 // No instance method of same name as property getter name was found.
1072 // Declare a getter method and add it to the list of methods
1073 // for this class.
1074 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1075 property->getLocation(), property->getGetterName(),
1076 property->getType(), CD, true, false, true,
1077 (property->getPropertyImplementation() ==
1078 ObjCPropertyDecl::Optional) ?
1079 ObjCMethodDecl::Optional :
1080 ObjCMethodDecl::Required);
1081 } else
1082 // A user declared getter will be synthesize when @synthesize of
1083 // the property with the same name is seen in the @implementation
1084 GetterMethod->setIsSynthesized();
1085 property->setGetterMethodDecl(GetterMethod);
1086
1087 // Skip setter if property is read-only.
1088 if (!property->isReadOnly()) {
1089 // Find the default setter and if one not found, add one.
1090 if (!SetterMethod) {
1091 // No instance method of same name as property setter name was found.
1092 // Declare a setter method and add it to the list of methods
1093 // for this class.
1094 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1095 property->getLocation(),
1096 property->getSetterName(),
1097 Context.VoidTy, CD, true, false, true,
1098 (property->getPropertyImplementation() ==
1099 ObjCPropertyDecl::Optional) ?
1100 ObjCMethodDecl::Optional :
1101 ObjCMethodDecl::Required);
1102 // Invent the arguments for the setter. We don't bother making a
1103 // nice name for the argument.
1104 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1105 SourceLocation(),
1106 property->getIdentifier(),
1107 property->getType(),
1108 VarDecl::None,
1109 0, 0);
1110 SetterMethod->setMethodParams(&Argument, 1);
1111 } else
1112 // A user declared setter will be synthesize when @synthesize of
1113 // the property with the same name is seen in the @implementation
1114 SetterMethod->setIsSynthesized();
1115 property->setSetterMethodDecl(SetterMethod);
1116 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001117 // Add any synthesized methods to the global pool. This allows us to
1118 // handle the following, which is supported by GCC (and part of the design).
1119 //
1120 // @interface Foo
1121 // @property double bar;
1122 // @end
1123 //
1124 // void thisIsUnfortunate() {
1125 // id foo;
1126 // double bar = [foo bar];
1127 // }
1128 //
Steve Naroffab63fd62009-01-08 17:28:14 +00001129 if (GetterMethod) {
1130 CD->addDecl(Context, GetterMethod);
1131 AddInstanceMethodToGlobalPool(GetterMethod);
1132 }
1133 if (SetterMethod) {
1134 CD->addDecl(Context, SetterMethod);
1135 AddInstanceMethodToGlobalPool(SetterMethod);
1136 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001137}
1138
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001139// Note: For class/category implemenations, allMethods/allProperties is
1140// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +00001141void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1142 DeclTy **allMethods, unsigned allNum,
1143 DeclTy **allProperties, unsigned pNum) {
1144 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1145
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001146 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1147 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001148 // should be true.
1149 if (!ClassDecl)
1150 return;
1151
Chris Lattner855e51f2007-12-12 07:09:47 +00001152 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001153 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1154 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001155 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001156
Steve Naroffab63fd62009-01-08 17:28:14 +00001157
Seo Sanghyeon0473be42008-07-05 02:01:25 +00001158 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +00001159 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
1160 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +00001161 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1162 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
1163 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001164 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +00001165 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +00001166 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +00001167 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001168
1169 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1170 assert(DC && "Missing DeclContext");
1171
1172 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1173 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1174 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1175
Chris Lattner855e51f2007-12-12 07:09:47 +00001176 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001177 ObjCMethodDecl *Method =
1178 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +00001179
1180 if (!Method) continue; // Already issued a diagnostic.
1181 if (Method->isInstance()) {
1182 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001183 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001184 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1185 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001186 if ((isInterfaceDeclKind && PrevMethod && !match)
1187 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001188 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001189 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001190 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001191 } else {
Steve Naroffab63fd62009-01-08 17:28:14 +00001192 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001193 InsMap[Method->getSelector()] = Method;
1194 /// The following allows us to typecheck messages to "id".
1195 AddInstanceMethodToGlobalPool(Method);
1196 }
1197 }
1198 else {
1199 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001200 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001201 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1202 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001203 if ((isInterfaceDeclKind && PrevMethod && !match)
1204 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001205 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001206 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001207 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001208 } else {
Steve Naroffab63fd62009-01-08 17:28:14 +00001209 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001210 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001211 /// The following allows us to typecheck messages to "Class".
1212 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001213 }
1214 }
1215 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001216 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001217 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001218 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001219 ComparePropertiesInBaseAndSuper(I);
1220 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001221 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001222 e = I->classprop_end(); i != e; ++i) {
Steve Naroffab63fd62009-01-08 17:28:14 +00001223 ProcessPropertyDecl((*i), I);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001224 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001225 I->setAtEndLoc(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001226 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001227 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001228 e = P->classprop_end(); i != e; ++i) {
Steve Naroffab63fd62009-01-08 17:28:14 +00001229 ProcessPropertyDecl((*i), P);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001230 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001231 P->setAtEndLoc(AtEndLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +00001232 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001233 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001234 // Categories are used to extend the class by declaring new methods.
1235 // By the same token, they are also used to add new properties. No
1236 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001237
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001238 // Merge protocol properties into category
1239 MergeProtocolPropertiesIntoClass(C, C);
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001240 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001241 e = C->classprop_end(); i != e; ++i) {
Steve Naroffab63fd62009-01-08 17:28:14 +00001242 ProcessPropertyDecl((*i), C);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001243 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001244 C->setAtEndLoc(AtEndLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +00001245 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001246 else if (ObjCImplementationDecl *IC =
1247 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001248 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001249 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +00001250 ImplMethodsVsClassMethods(IC, IDecl);
1251 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +00001252 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001253 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001254 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +00001255 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001256 // in this interface are implemented in the category @implementation.
Chris Lattner855e51f2007-12-12 07:09:47 +00001257 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001258 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001259 Categories; Categories = Categories->getNextClassCategory()) {
1260 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1261 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1262 break;
1263 }
1264 }
1265 }
1266 }
1267}
1268
1269
1270/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1271/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001272static Decl::ObjCDeclQualifier
1273CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1274 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1275 if (PQTVal & ObjCDeclSpec::DQ_In)
1276 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1277 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1278 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1279 if (PQTVal & ObjCDeclSpec::DQ_Out)
1280 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1281 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1282 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1283 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1284 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1285 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1286 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001287
1288 return ret;
1289}
1290
1291Sema::DeclTy *Sema::ActOnMethodDeclaration(
1292 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +00001293 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001294 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001295 Selector Sel,
1296 // optional arguments. The number of types/arguments is obtained
1297 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001298 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001299 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001300 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1301 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +00001302 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +00001303
1304 // Make sure we can establish a context for the method.
1305 if (!ClassDecl) {
1306 Diag(MethodLoc, diag::error_missing_method_context);
1307 return 0;
1308 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001309 QualType resultDeclType;
1310
1311 if (ReturnType)
1312 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1313 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001314 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001315
Chris Lattner114add62008-03-16 00:49:28 +00001316 ObjCMethodDecl* ObjCMethod =
1317 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroffab63fd62009-01-08 17:28:14 +00001318 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001319 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001320 false,
Chris Lattner114add62008-03-16 00:49:28 +00001321 MethodDeclKind == tok::objc_optional ?
1322 ObjCMethodDecl::Optional :
1323 ObjCMethodDecl::Required);
1324
Chris Lattnereee57c02008-04-04 06:12:32 +00001325 llvm::SmallVector<ParmVarDecl*, 16> Params;
1326
1327 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1328 // FIXME: arg->AttrList must be stored too!
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001329 QualType argType, originalArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001330
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001331 if (ArgTypes[i]) {
Chris Lattnereee57c02008-04-04 06:12:32 +00001332 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001333 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001334 if (argType->isArrayType()) { // (char *[]) -> (char **)
1335 originalArgType = argType;
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001336 argType = Context.getArrayDecayedType(argType);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001337 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001338 else if (argType->isFunctionType())
1339 argType = Context.getPointerType(argType);
1340 } else
Chris Lattnereee57c02008-04-04 06:12:32 +00001341 argType = Context.getObjCIdType();
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001342 ParmVarDecl* Param;
1343 if (originalArgType.isNull())
1344 Param = ParmVarDecl::Create(Context, ObjCMethod,
1345 SourceLocation(/*FIXME*/),
1346 ArgNames[i], argType,
1347 VarDecl::None, 0, 0);
1348 else
1349 Param = ParmVarWithOriginalTypeDecl::Create(Context, ObjCMethod,
1350 SourceLocation(/*FIXME*/),
1351 ArgNames[i], argType, originalArgType,
1352 VarDecl::None, 0, 0);
1353
Chris Lattnereee57c02008-04-04 06:12:32 +00001354 Param->setObjCDeclQualifier(
1355 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1356 Params.push_back(Param);
1357 }
1358
Ted Kremenek42730c52008-01-07 19:49:32 +00001359 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1360 ObjCMethod->setObjCDeclQualifier(
1361 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1362 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001363
1364 if (AttrList)
1365 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001366
1367 // For implementations (which can be very "coarse grain"), we add the
1368 // method now. This allows the AST to implement lookup methods that work
1369 // incrementally (without waiting until we parse the @end). It also allows
1370 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001371 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001372 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001373 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001374 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001375 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001376 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001377 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001378 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001379 }
1380 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001381 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001382 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001383 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001384 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001385 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001386 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001387 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001388 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001389 }
1390 }
1391 if (PrevMethod) {
1392 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001393 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001394 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001395 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001396 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001397 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001398}
1399
Daniel Dunbar540ff472008-09-23 21:53:23 +00001400void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1401 SourceLocation Loc,
1402 unsigned &Attributes) {
1403 // FIXME: Improve the reported location.
1404
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001405 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001406 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001407 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1408 ObjCDeclSpec::DQ_PR_assign |
1409 ObjCDeclSpec::DQ_PR_copy |
1410 ObjCDeclSpec::DQ_PR_retain))) {
1411 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1412 "readwrite" :
1413 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1414 "assign" :
1415 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1416 "copy" : "retain";
1417
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001418 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1419 diag::err_objc_property_attr_mutually_exclusive :
1420 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001421 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001422 }
1423
1424 // Check for copy or retain on non-object types.
1425 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1426 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001427 Diag(Loc, diag::err_objc_property_requires_object)
1428 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001429 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1430 }
1431
1432 // Check for more than one of { assign, copy, retain }.
1433 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1434 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001435 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1436 << "assign" << "copy";
1437 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001438 }
1439 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001440 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1441 << "assign" << "retain";
1442 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001443 }
1444 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1445 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001446 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1447 << "copy" << "retain";
1448 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001449 }
1450 }
1451
1452 // Warn if user supplied no assignment attribute, property is
1453 // readwrite, and this is an object type.
1454 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1455 ObjCDeclSpec::DQ_PR_retain)) &&
1456 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1457 Context.isObjCObjectPointerType(PropertyTy)) {
1458 // Skip this warning in gc-only mode.
1459 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1460 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1461
1462 // If non-gc code warn that this is likely inappropriate.
1463 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1464 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1465
1466 // FIXME: Implement warning dependent on NSCopying being
1467 // implemented. See also:
1468 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1469 // (please trim this list while you are at it).
1470 }
1471}
1472
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001473Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1474 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001475 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001476 Selector GetterSel,
1477 Selector SetterSel,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001478 DeclTy *ClassCategory,
1479 bool *isOverridingProperty,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001480 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001481 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001482 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1483 // default is readwrite!
1484 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1485 // property is defaulted to 'assign' if it is readwrite and is
1486 // not retain or copy
1487 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1488 (isReadWrite &&
1489 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1490 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1491 QualType T = GetTypeForDeclarator(FD.D, S);
1492 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001493
1494 // May modify Attributes.
1495 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001496
1497 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1498 if (!CDecl->getIdentifier()) {
1499 // This is an anonymous category. property requires special
1500 // handling.
1501 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1502 if (ObjCPropertyDecl *PIDecl =
1503 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1504 // property 'PIDecl's readonly attribute will be over-ridden
1505 // with anonymous category's readwrite property attribute!
1506 unsigned PIkind = PIDecl->getPropertyAttributes();
1507 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001508 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001509 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1510 Diag(AtLoc, diag::warn_property_attr_mismatch);
1511 PIDecl->makeitReadWriteAttribute();
1512 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1513 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1514 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1515 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1516 PIDecl->setSetterName(SetterSel);
1517 // FIXME: use a common routine with addPropertyMethods.
1518 ObjCMethodDecl *SetterDecl =
1519 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1520 Context.VoidTy,
1521 ICDecl,
1522 true, false, true,
1523 ObjCMethodDecl::Required);
1524 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1525 SetterDecl,
1526 SourceLocation(),
1527 FD.D.getIdentifier(),
1528 T,
1529 VarDecl::None,
1530 0, 0);
1531 SetterDecl->setMethodParams(&Argument, 1);
1532 PIDecl->setSetterMethodDecl(SetterDecl);
1533 }
1534 else
Fariborz Jahanian3d7aa252008-12-04 22:56:16 +00001535 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001536 *isOverridingProperty = true;
1537 return 0;
1538 }
Fariborz Jahanianc1235662008-11-26 20:33:54 +00001539 // No matching property found in the main class. Just fall thru
1540 // and add property to the anonymous category. It looks like
1541 // it works as is. This category becomes just like a category
1542 // for its primary class.
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001543 } else {
1544 Diag(CDecl->getLocation(), diag::err_continuation_class);
1545 *isOverridingProperty = true;
1546 return 0;
1547 }
1548 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001549
Fariborz Jahanianf8bfa0c2008-12-16 17:51:01 +00001550 Type *t = T.getTypePtr();
1551 if (t->isArrayType() || t->isFunctionType())
1552 Diag(AtLoc, diag::err_property_type) << T;
1553
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001554 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, CurContext, AtLoc,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001555 FD.D.getIdentifier(), T);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001556 // FIXME: PushOnScopeChains?
1557 CurContext->addDecl(Context, PDecl);
1558
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001559 // Regardless of setter/getter attribute, we save the default getter/setter
1560 // selector names in anticipation of declaration of setter/getter methods.
1561 PDecl->setGetterName(GetterSel);
1562 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001563
Daniel Dunbar540ff472008-09-23 21:53:23 +00001564 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001565 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001566
Daniel Dunbar540ff472008-09-23 21:53:23 +00001567 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001568 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001569
Daniel Dunbar540ff472008-09-23 21:53:23 +00001570 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001571 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001572
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001573 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001574 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001575
Daniel Dunbar540ff472008-09-23 21:53:23 +00001576 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001577 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001578
Daniel Dunbar540ff472008-09-23 21:53:23 +00001579 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001580 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001581
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001582 if (isAssign)
1583 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1584
Daniel Dunbar540ff472008-09-23 21:53:23 +00001585 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001586 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001587
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001588 if (MethodImplKind == tok::objc_required)
1589 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1590 else if (MethodImplKind == tok::objc_optional)
1591 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1592
Chris Lattner855e51f2007-12-12 07:09:47 +00001593 return PDecl;
1594}
1595
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001596/// ActOnPropertyImplDecl - This routine performs semantic checks and
1597/// builds the AST node for a property implementation declaration; declared
1598/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001599///
1600Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1601 SourceLocation PropertyLoc,
1602 bool Synthesize,
1603 DeclTy *ClassCatImpDecl,
1604 IdentifierInfo *PropertyId,
1605 IdentifierInfo *PropertyIvar) {
1606 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1607 // Make sure we have a context for the property implementation declaration.
1608 if (!ClassImpDecl) {
1609 Diag(AtLoc, diag::error_missing_property_context);
1610 return 0;
1611 }
1612 ObjCPropertyDecl *property = 0;
1613 ObjCInterfaceDecl* IDecl = 0;
1614 // Find the class or category class where this property must have
1615 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001616 ObjCImplementationDecl *IC = 0;
1617 ObjCCategoryImplDecl* CatImplClass = 0;
1618 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001619 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001620 // We always synthesize an interface for an implementation
1621 // without an interface decl. So, IDecl is always non-zero.
1622 assert(IDecl &&
1623 "ActOnPropertyImplDecl - @implementation without @interface");
1624
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001625 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001626 property = IDecl->FindPropertyDeclaration(PropertyId);
1627 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001628 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001629 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001630 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001631 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001632 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001633 if (Synthesize) {
1634 Diag(AtLoc, diag::error_synthesize_category_decl);
1635 return 0;
1636 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001637 IDecl = CatImplClass->getClassInterface();
1638 if (!IDecl) {
1639 Diag(AtLoc, diag::error_missing_property_interface);
1640 return 0;
1641 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001642 ObjCCategoryDecl *Category =
1643 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1644
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001645 // If category for this implementation not found, it is an error which
1646 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001647 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001648 return 0;
1649 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001650 property = Category->FindPropertyDeclaration(PropertyId);
1651 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001652 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001653 << Category->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001654 return 0;
1655 }
1656 }
1657 else {
1658 Diag(AtLoc, diag::error_bad_property_context);
1659 return 0;
1660 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001661 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001662 // Check that we have a valid, previously declared ivar for @synthesize
1663 if (Synthesize) {
1664 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001665 if (!PropertyIvar)
1666 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001667 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001668 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001669 if (!Ivar) {
Chris Lattner65cae292008-11-19 08:23:25 +00001670 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001671 return 0;
1672 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001673 QualType PropType = Context.getCanonicalType(property->getType());
1674 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1675
Steve Naroff631e3922008-09-30 00:24:17 +00001676 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001677 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001678 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001679 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001680 << property->getDeclName() << Ivar->getDeclName();
Steve Naroffc32e45c2008-09-30 10:07:56 +00001681 return 0;
1682 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001683 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001684 } else if (PropertyIvar) {
1685 // @dynamic
1686 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1687 return 0;
1688 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001689 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001690 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001691 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1692 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001693 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001694 ObjCPropertyImplDecl::Synthesize
1695 : ObjCPropertyImplDecl::Dynamic),
1696 Ivar);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001697 CurContext->addDecl(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001698 if (IC) {
1699 if (Synthesize)
1700 if (ObjCPropertyImplDecl *PPIDecl =
1701 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1702 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1703 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1704 << PropertyIvar;
1705 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1706 }
1707
1708 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1709 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1710 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1711 return 0;
1712 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001713 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001714 }
1715 else {
1716 if (Synthesize)
1717 if (ObjCPropertyImplDecl *PPIDecl =
1718 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1719 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1720 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1721 << PropertyIvar;
1722 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1723 }
1724
1725 if (ObjCPropertyImplDecl *PPIDecl =
1726 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1727 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1728 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1729 return 0;
1730 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001731 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001732 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001733
1734 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001735}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001736
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001737bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00001738 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001739 return false;
1740
1741 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1742 D->setInvalidDecl();
1743
1744 return true;
1745}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001746
1747/// Collect the instance variables declared in an Objective-C object. Used in
1748/// the creation of structures from objects using the @defs directive.
1749/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1750/// part of the AST generation logic of @defs.
1751static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1752 ASTContext& Ctx,
1753 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1754 if (Class->getSuperClass())
1755 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1756
1757 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1758 for (ObjCInterfaceDecl::ivar_iterator
1759 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1760
1761 ObjCIvarDecl* ID = *I;
1762 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1763 ID->getLocation(),
1764 ID->getIdentifier(),
1765 ID->getType(),
1766 ID->getBitWidth()));
1767 }
1768}
1769
1770/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1771/// instance variables of ClassName into Decls.
1772void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1773 IdentifierInfo *ClassName,
1774 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1775 // Check that ClassName is a valid class
1776 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1777 if (!Class) {
1778 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1779 return;
1780 }
1781 // Collect the instance variables
1782 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1783
1784 // Introduce all of these fields into the appropriate scope.
1785 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1786 D != Decls.end(); ++D) {
1787 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1788 if (getLangOptions().CPlusPlus)
1789 PushOnScopeChains(cast<FieldDecl>(FD), S);
1790 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
1791 Record->addDecl(Context, FD);
1792 }
1793}
1794