blob: d47f98af25f9acaee6a9d41476ee3702dea2e1f4 [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.
Douglas Gregor5d764842009-01-09 17:18:27 +000033 if (MDecl->isInstanceMethod())
Steve Narofffe9eb6a2007-12-18 01:30:32 +000034 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)
Steve Naroff451f83c2009-01-09 15:36:25 +0000323 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
324 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000325 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000326 // Does property in super class has declaration in current class?
Steve Naroff451f83c2009-01-09 15:36:25 +0000327 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
328 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000329 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 Jahanianacad6d12008-12-06 23:03:39 +0000343 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
344 if (!IDecl) {
345 // Category
346 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
347 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Steve Naroff451f83c2009-01-09 15:36:25 +0000348 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
349 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000350 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000351 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000352 // Is this property already in category's list of properties?
Steve Naroff451f83c2009-01-09 15:36:25 +0000353 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end();
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000354 CP != CE; ++CP)
355 if ((*CP)->getIdentifier() == Pr->getIdentifier())
356 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000357 if (CP != CE)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000358 // Property protocol already exist in class. Diagnose any mismatch.
359 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
360 }
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000361 return;
362 }
Steve Naroff451f83c2009-01-09 15:36:25 +0000363 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
364 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000365 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000366 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000367 // Is this property already in class's list of properties?
Steve Naroff451f83c2009-01-09 15:36:25 +0000368 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end();
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000369 CP != CE; ++CP)
370 if ((*CP)->getIdentifier() == Pr->getIdentifier())
371 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000372 if (CP != CE)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000373 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000374 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000375 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000376}
377
378/// MergeProtocolPropertiesIntoClass - This routine merges properties
379/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000380/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000381///
382
383void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000384Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000385 DeclTy *MergeItsProtocols) {
386 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000387 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
388
389 if (!IDecl) {
390 // Category
391 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
392 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
393 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
394 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
395 E = MDecl->protocol_end(); P != E; ++P)
396 // Merge properties of category (*P) into IDECL's
397 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
398
399 // Go thru the list of protocols for this category and recursively merge
400 // their properties into this class as well.
401 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
402 E = CatDecl->protocol_end(); P != E; ++P)
403 MergeProtocolPropertiesIntoClass(CatDecl, *P);
404 } else {
405 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
406 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
407 E = MD->protocol_end(); P != E; ++P)
408 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
409 }
410 return;
411 }
412
Chris Lattner5cece462008-07-21 07:06:49 +0000413 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000414 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
415 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000416 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000417 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
418
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000419 // Go thru the list of protocols for this class and recursively merge
420 // their properties into this class as well.
421 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
422 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000423 MergeProtocolPropertiesIntoClass(IDecl, *P);
424 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000425 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
426 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
427 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000428 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000429 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000430}
431
Chris Lattner855e51f2007-12-12 07:09:47 +0000432/// ActOnForwardProtocolDeclaration -
433Action::DeclTy *
434Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000435 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000436 unsigned NumElts,
437 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000438 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000439
440 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000441 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000442 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000443 if (PDecl == 0) { // Not already seen?
444 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
445 IdentList[i].second, Ident);
446 // FIXME: PushOnScopeChains?
447 CurContext->addDecl(Context, PDecl);
448 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000449 if (attrList)
450 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000451 Protocols.push_back(PDecl);
452 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000453
454 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000455 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000456 &Protocols[0], Protocols.size());
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000457 CurContext->addDecl(Context, PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000458 CheckObjCDeclScope(PDecl);
459 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000460}
461
Chris Lattnere705e5e2008-07-21 22:17:28 +0000462Sema::DeclTy *Sema::
463ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
464 IdentifierInfo *ClassName, SourceLocation ClassLoc,
465 IdentifierInfo *CategoryName,
466 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000467 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000468 unsigned NumProtoRefs,
469 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000470 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000471
Chris Lattnere29dc832008-03-16 20:34:23 +0000472 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000473 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
474 // FIXME: PushOnScopeChains?
475 CurContext->addDecl(Context, CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000476 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000477
478 /// Check that class of this category is already completely declared.
479 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000480 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffac0580b2008-06-05 15:03:27 +0000481 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000482 /// Check for duplicate interface declaration for this category
483 ObjCCategoryDecl *CDeclChain;
484 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
485 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000486 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000487 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner65cae292008-11-19 08:23:25 +0000488 << ClassName << CategoryName;
Chris Lattnerde1bd982008-11-23 22:38:38 +0000489 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000490 break;
491 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000492 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000493 if (!CDeclChain)
494 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000495 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000496
497 if (NumProtoRefs) {
Chris Lattner45142b92008-07-26 04:07:02 +0000498 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
499 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000500 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000501
502 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000503 return CDecl;
504}
505
506/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000507/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000508/// object.
509Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
510 SourceLocation AtCatImplLoc,
511 IdentifierInfo *ClassName, SourceLocation ClassLoc,
512 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000513 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000514 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000515 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
516 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000517 /// Check that class of this category is already completely declared.
518 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000519 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000520
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000521 // FIXME: PushOnScopeChains?
522 CurContext->addDecl(Context, CDecl);
523
Chris Lattner855e51f2007-12-12 07:09:47 +0000524 /// TODO: Check that CatName, category name, is not used in another
525 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000526 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000527
528 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000529 return CDecl;
530}
531
532Sema::DeclTy *Sema::ActOnStartClassImplementation(
533 SourceLocation AtClassImplLoc,
534 IdentifierInfo *ClassName, SourceLocation ClassLoc,
535 IdentifierInfo *SuperClassname,
536 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000537 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000538 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000539 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000540 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000541 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000542 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000543 }
544 else {
545 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000546 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000547 if (!IDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000548 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000549 }
550
551 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000552 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000553 if (SuperClassname) {
554 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000555 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000556 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000557 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
558 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000559 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000560 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000561 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000562 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000563 Diag(SuperClassLoc, diag::err_undef_superclass)
564 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000565 else if (IDecl && IDecl->getSuperClass() != SDecl) {
566 // This implementation and its interface do not have the same
567 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000568 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000569 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000570 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000571 }
572 }
573 }
574
575 if (!IDecl) {
576 // Legacy case of @implementation with no corresponding @interface.
577 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000578
579 // FIXME: Do we support attributes on the @implementation? If so
580 // we should copy them over.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000581 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
582 ClassName, ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000583 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000584 IDecl->setSuperClass(SDecl);
585 IDecl->setLocEnd(ClassLoc);
586
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000587 // FIXME: PushOnScopeChains?
588 CurContext->addDecl(Context, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000589 // Remember that this needs to be removed when the scope is popped.
590 TUScope->AddDecl(IDecl);
591 }
592
Ted Kremenek42730c52008-01-07 19:49:32 +0000593 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000594 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
595 ClassName, IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000596
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000597 // FIXME: PushOnScopeChains?
598 CurContext->addDecl(Context, IMPDecl);
599
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000600 if (CheckObjCDeclScope(IMPDecl))
601 return IMPDecl;
602
Chris Lattner855e51f2007-12-12 07:09:47 +0000603 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000604 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000605 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000606 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000607 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000608 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000609 return IMPDecl;
610}
611
Ted Kremenek42730c52008-01-07 19:49:32 +0000612void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
613 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000614 SourceLocation RBrace) {
615 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000616 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000617 if (!IDecl)
618 return;
619 /// Check case of non-existing @interface decl.
620 /// (legacy objective-c @implementation decl without an @interface decl).
621 /// Add implementations's ivar to the synthesize class's ivar list.
622 if (IDecl->ImplicitInterfaceDecl()) {
623 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
624 return;
625 }
626 // If implementation has empty ivar list, just return.
627 if (numIvars == 0)
628 return;
629
630 assert(ivars && "missing @implementation ivars");
631
632 // Check interface's Ivar list against those in the implementation.
633 // names and types must match.
634 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000635 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000636 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000637 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
638 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000639 ObjCIvarDecl* ImplIvar = ivars[j++];
640 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000641 assert (ImplIvar && "missing implementation ivar");
642 assert (ClsIvar && "missing class ivar");
Chris Lattnerb5457862008-07-27 00:05:05 +0000643 if (Context.getCanonicalType(ImplIvar->getType()) !=
644 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000645 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000646 << ImplIvar->getIdentifier()
647 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000648 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000649 }
650 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
651 // as error.
652 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000653 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000654 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000655 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000656 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000657 }
658 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000659 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000660
661 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000662 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000663 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000664 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000665}
666
Steve Naroffb4f48512008-02-10 21:38:56 +0000667void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
668 bool &IncompleteImpl) {
669 if (!IncompleteImpl) {
670 Diag(ImpLoc, diag::warn_incomplete_impl);
671 IncompleteImpl = true;
672 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000673 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000674}
675
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000676void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
677 ObjCMethodDecl *IntfMethodDecl) {
678 bool err = false;
679 QualType ImpMethodQType =
680 Context.getCanonicalType(ImpMethodDecl->getResultType());
681 QualType IntfMethodQType =
682 Context.getCanonicalType(IntfMethodDecl->getResultType());
683 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
684 err = true;
685 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
686 IF=IntfMethodDecl->param_begin(),
687 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
688 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
689 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
690 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
691 err = true;
692 break;
693 }
694 }
695 if (err) {
696 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
697 << ImpMethodDecl->getDeclName();
698 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
699 }
700}
701
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000702/// FIXME: Type hierarchies in Objective-C can be deep. We could most
703/// likely improve the efficiency of selector lookups and type
704/// checking by associating with each protocol / interface / category
705/// the flattened instance tables. If we used an immutable set to keep
706/// the table then it wouldn't add significant memory cost and it
707/// would be handy for lookups.
708
Steve Naroffb268d2a2008-02-08 22:06:17 +0000709/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000710/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000711void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
712 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000713 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000714 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000715 const llvm::DenseSet<Selector> &ClsMap,
716 ObjCInterfaceDecl *IDecl) {
717 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
718
719 // If a method lookup fails locally we still need to look and see if
720 // the method was implemented by a base class or an inherited
721 // protocol. This lookup is slow, but occurs rarely in correct code
722 // and otherwise would terminate in a warning.
723
Chris Lattner855e51f2007-12-12 07:09:47 +0000724 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000725 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000726 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000727 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000728 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000729 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000730 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000731 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000732 }
733 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000734 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000735 E = PDecl->classmeth_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 &&
738 !ClsMap.count(method->getSelector()) &&
739 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000740 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000741 }
Chris Lattner0be08822008-07-21 21:32:27 +0000742 // Check on this protocols's referenced protocols, recursively.
743 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
744 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000745 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000746}
747
Ted Kremenek42730c52008-01-07 19:49:32 +0000748void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
749 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000750 llvm::DenseSet<Selector> InsMap;
751 // Check and see if instance methods in class interface have been
752 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000753 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000754 E = IMPDecl->instmeth_end(); I != E; ++I)
755 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000756
757 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000758 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000759 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000760 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000761 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian42d0bf92008-12-22 19:05:31 +0000762 else {
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000763 ObjCMethodDecl *ImpMethodDecl =
764 IMPDecl->getInstanceMethod((*I)->getSelector());
765 ObjCMethodDecl *IntfMethodDecl =
766 IDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian42d0bf92008-12-22 19:05:31 +0000767 assert(IntfMethodDecl &&
768 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
769 // ImpMethodDecl may be null as in a @dynamic property.
770 if (ImpMethodDecl)
771 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000772 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000773
Chris Lattner855e51f2007-12-12 07:09:47 +0000774 llvm::DenseSet<Selector> ClsMap;
775 // Check and see if class methods in class interface have been
776 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000777 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000778 E = IMPDecl->classmeth_end(); I != E; ++I)
779 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000780
Ted Kremenek42730c52008-01-07 19:49:32 +0000781 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000782 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000783 if (!ClsMap.count((*I)->getSelector()))
784 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000785 else {
786 ObjCMethodDecl *ImpMethodDecl =
787 IMPDecl->getClassMethod((*I)->getSelector());
788 ObjCMethodDecl *IntfMethodDecl =
789 IDecl->getClassMethod((*I)->getSelector());
790 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
791 }
792
Chris Lattner855e51f2007-12-12 07:09:47 +0000793
794 // Check the protocol list for unimplemented methods in the @implementation
795 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000796 const ObjCList<ObjCProtocolDecl> &Protocols =
797 IDecl->getReferencedProtocols();
798 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
799 E = Protocols.end(); I != E; ++I)
800 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000801 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000802}
803
804/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000805/// category interface are implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000806void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
807 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000808 llvm::DenseSet<Selector> InsMap;
809 // Check and see if instance methods in category interface have been
810 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000811 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000812 E = CatImplDecl->instmeth_end(); I != E; ++I)
813 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000814
815 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000816 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000817 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian42d0bf92008-12-22 19:05:31 +0000818 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000819 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000820 else {
821 ObjCMethodDecl *ImpMethodDecl =
822 CatImplDecl->getInstanceMethod((*I)->getSelector());
823 ObjCMethodDecl *IntfMethodDecl =
824 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian42d0bf92008-12-22 19:05:31 +0000825 assert(IntfMethodDecl &&
826 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
827 // ImpMethodDecl may be null as in a @dynamic property.
828 if (ImpMethodDecl)
829 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000830 }
Steve Naroffb4f48512008-02-10 21:38:56 +0000831
Chris Lattner855e51f2007-12-12 07:09:47 +0000832 llvm::DenseSet<Selector> ClsMap;
833 // Check and see if class methods in category interface have been
834 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000835 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000836 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
837 I != E; ++I)
838 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000839
Ted Kremenek42730c52008-01-07 19:49:32 +0000840 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000841 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000842 if (!ClsMap.count((*I)->getSelector()))
843 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000844 else {
845 ObjCMethodDecl *ImpMethodDecl =
846 CatImplDecl->getClassMethod((*I)->getSelector());
847 ObjCMethodDecl *IntfMethodDecl =
848 CatClassDecl->getClassMethod((*I)->getSelector());
849 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
850 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000851 // Check the protocol list for unimplemented methods in the @implementation
852 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000853 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
854 E = CatClassDecl->protocol_end(); PI != E; ++PI)
855 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000856 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +0000857}
858
859/// ActOnForwardClassDeclaration -
860Action::DeclTy *
861Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
862 IdentifierInfo **IdentList, unsigned NumElts)
863{
Ted Kremenek42730c52008-01-07 19:49:32 +0000864 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000865
866 for (unsigned i = 0; i != NumElts; ++i) {
867 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000868 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000869 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +0000870 // Maybe we will complain about the shadowed template parameter.
871 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
872 // Just pretend that we didn't see the previous declaration.
873 PrevDecl = 0;
874 }
875
Ted Kremenek42730c52008-01-07 19:49:32 +0000876 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000877 // GCC apparently allows the following idiom:
878 //
879 // typedef NSObject < XCElementTogglerP > XCElementToggler;
880 // @class XCElementToggler;
881 //
882 // FIXME: Make an extension?
883 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
884 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +0000885 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +0000886 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +0000887 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000888 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000889 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000890 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000891 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
892 IdentList[i], SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000893 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000894
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000895 // FIXME: PushOnScopeChains?
896 CurContext->addDecl(Context, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000897 // Remember that this needs to be removed when the scope is popped.
898 TUScope->AddDecl(IDecl);
899 }
900
901 Interfaces.push_back(IDecl);
902 }
903
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000904 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000905 &Interfaces[0],
906 Interfaces.size());
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000907 CurContext->addDecl(Context, CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000908 CheckObjCDeclScope(CDecl);
909 return CDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000910}
911
912
913/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
914/// returns true, or false, accordingly.
915/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000916bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +0000917 const ObjCMethodDecl *PrevMethod,
918 bool matchBasedOnSizeAndAlignment) {
919 QualType T1 = Context.getCanonicalType(Method->getResultType());
920 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
921
922 if (T1 != T2) {
923 // The result types are different.
924 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +0000925 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +0000926 // Incomplete types don't have a size and alignment.
927 if (T1->isIncompleteType() || T2->isIncompleteType())
928 return false;
929 // Check is based on size and alignment.
930 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
931 return false;
932 }
933 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
934 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
935 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
936 if (T1 != T2) {
937 // The result types are different.
938 if (!matchBasedOnSizeAndAlignment)
939 return false;
940 // Incomplete types don't have a size and alignment.
941 if (T1->isIncompleteType() || T2->isIncompleteType())
942 return false;
943 // Check is based on size and alignment.
944 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
945 return false;
946 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000947 }
948 return true;
949}
950
Ted Kremenek42730c52008-01-07 19:49:32 +0000951void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
952 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000953 if (!FirstMethod.Method) {
954 // Haven't seen a method with this selector name yet - add it.
955 FirstMethod.Method = Method;
956 FirstMethod.Next = 0;
957 } else {
958 // We've seen a method with this name, now check the type signature(s).
959 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
960
Ted Kremenek42730c52008-01-07 19:49:32 +0000961 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000962 Next = Next->Next)
963 match = MatchTwoMethodDeclarations(Method, Next->Method);
964
965 if (!match) {
966 // We have a new signature for an existing method - add it.
967 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner3a8f2942008-11-24 03:33:13 +0000968 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner855e51f2007-12-12 07:09:47 +0000969 }
970 }
971}
972
Steve Naroffec7e0882008-10-21 10:50:19 +0000973// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +0000974ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
975 SourceRange R) {
976 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +0000977 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +0000978
979 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +0000980 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
981 // This checks if the methods differ by size & alignment.
982 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
983 issueWarning = true;
984 }
985 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +0000986 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +0000987 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000988 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +0000989 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +0000990 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000991 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +0000992 }
993 return MethList.Method;
994}
995
Ted Kremenek42730c52008-01-07 19:49:32 +0000996void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
997 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000998 if (!FirstMethod.Method) {
999 // Haven't seen a method with this selector name yet - add it.
1000 FirstMethod.Method = Method;
1001 FirstMethod.Next = 0;
1002 } else {
1003 // We've seen a method with this name, now check the type signature(s).
1004 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1005
Ted Kremenek42730c52008-01-07 19:49:32 +00001006 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001007 Next = Next->Next)
1008 match = MatchTwoMethodDeclarations(Method, Next->Method);
1009
1010 if (!match) {
1011 // We have a new signature for an existing method - add it.
1012 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001013 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001014 FirstMethod.Next = OMI;
1015 }
1016 }
1017}
1018
Steve Naroffab63fd62009-01-08 17:28:14 +00001019/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1020/// have the property type and issue diagnostics if they don't.
1021/// Also synthesize a getter/setter method if none exist (and update the
1022/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1023/// methods is the "right" thing to do.
1024void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1025 ObjCContainerDecl *CD) {
1026 ObjCMethodDecl *GetterMethod, *SetterMethod;
1027
1028 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1029 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1030
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001031 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001032 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001033 Diag(property->getLocation(),
1034 diag::err_accessor_property_type_mismatch)
1035 << property->getDeclName()
1036 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001037 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1038 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001039
1040 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001041 if (Context.getCanonicalType(SetterMethod->getResultType())
1042 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001043 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1044 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001045 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001046 Diag(property->getLocation(),
1047 diag::err_accessor_property_type_mismatch)
1048 << property->getDeclName()
1049 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001050 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1051 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001052 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001053
1054 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001055 // Find the default getter and if one not found, add one.
Steve Naroff4cf0d3f2009-01-08 20:17:34 +00001056 // FIXME: The synthesized property we set here is misleading. We
1057 // almost always synthesize these methods unless the user explicitly
1058 // provided prototypes (which is odd, but allowed). Sema should be
1059 // typechecking that the declarations jive in that situation (which
1060 // it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001061 if (!GetterMethod) {
1062 // No instance method of same name as property getter name was found.
1063 // Declare a getter method and add it to the list of methods
1064 // for this class.
1065 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1066 property->getLocation(), property->getGetterName(),
1067 property->getType(), CD, true, false, true,
1068 (property->getPropertyImplementation() ==
1069 ObjCPropertyDecl::Optional) ?
1070 ObjCMethodDecl::Optional :
1071 ObjCMethodDecl::Required);
Douglas Gregord1675382009-01-09 19:42:16 +00001072 CD->addDecl(Context, GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001073 } else
1074 // A user declared getter will be synthesize when @synthesize of
1075 // the property with the same name is seen in the @implementation
1076 GetterMethod->setIsSynthesized();
1077 property->setGetterMethodDecl(GetterMethod);
1078
1079 // Skip setter if property is read-only.
1080 if (!property->isReadOnly()) {
1081 // Find the default setter and if one not found, add one.
1082 if (!SetterMethod) {
1083 // No instance method of same name as property setter name was found.
1084 // Declare a setter method and add it to the list of methods
1085 // for this class.
1086 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1087 property->getLocation(),
1088 property->getSetterName(),
1089 Context.VoidTy, CD, true, false, true,
1090 (property->getPropertyImplementation() ==
1091 ObjCPropertyDecl::Optional) ?
1092 ObjCMethodDecl::Optional :
1093 ObjCMethodDecl::Required);
1094 // Invent the arguments for the setter. We don't bother making a
1095 // nice name for the argument.
1096 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1097 SourceLocation(),
1098 property->getIdentifier(),
1099 property->getType(),
1100 VarDecl::None,
1101 0, 0);
1102 SetterMethod->setMethodParams(&Argument, 1);
Douglas Gregord1675382009-01-09 19:42:16 +00001103 CD->addDecl(Context, SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001104 } else
1105 // A user declared setter will be synthesize when @synthesize of
1106 // the property with the same name is seen in the @implementation
1107 SetterMethod->setIsSynthesized();
1108 property->setSetterMethodDecl(SetterMethod);
1109 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001110 // Add any synthesized methods to the global pool. This allows us to
1111 // handle the following, which is supported by GCC (and part of the design).
1112 //
1113 // @interface Foo
1114 // @property double bar;
1115 // @end
1116 //
1117 // void thisIsUnfortunate() {
1118 // id foo;
1119 // double bar = [foo bar];
1120 // }
1121 //
Douglas Gregord1675382009-01-09 19:42:16 +00001122 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001123 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001124 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001125 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001126}
1127
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001128// Note: For class/category implemenations, allMethods/allProperties is
1129// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +00001130void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1131 DeclTy **allMethods, unsigned allNum,
1132 DeclTy **allProperties, unsigned pNum) {
1133 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1134
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001135 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1136 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001137 // should be true.
1138 if (!ClassDecl)
1139 return;
1140
Chris Lattner855e51f2007-12-12 07:09:47 +00001141 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001142 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1143 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001144 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001145
Seo Sanghyeon0473be42008-07-05 02:01:25 +00001146 if (pNum != 0) {
Steve Naroff451f83c2009-01-09 15:36:25 +00001147 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl))
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +00001148 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +00001149 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +00001150 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +00001151 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001152 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1153 assert(DC && "Missing DeclContext");
1154
1155 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1156 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1157 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1158
Chris Lattner855e51f2007-12-12 07:09:47 +00001159 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001160 ObjCMethodDecl *Method =
1161 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +00001162
1163 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001164 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001165 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001166 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001167 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1168 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001169 if ((isInterfaceDeclKind && PrevMethod && !match)
1170 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001171 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001172 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001173 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001174 } else {
Steve Naroffab63fd62009-01-08 17:28:14 +00001175 DC->addDecl(Context, Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001176 InsMap[Method->getSelector()] = Method;
1177 /// The following allows us to typecheck messages to "id".
1178 AddInstanceMethodToGlobalPool(Method);
1179 }
1180 }
1181 else {
1182 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001183 const ObjCMethodDecl *&PrevMethod = ClsMap[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 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001194 /// The following allows us to typecheck messages to "Class".
1195 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001196 }
1197 }
1198 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001199 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001200 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001201 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001202 ComparePropertiesInBaseAndSuper(I);
1203 MergeProtocolPropertiesIntoClass(I, I);
Steve Naroff451f83c2009-01-09 15:36:25 +00001204 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001205 // Categories are used to extend the class by declaring new methods.
1206 // By the same token, they are also used to add new properties. No
1207 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001208
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001209 // Merge protocol properties into category
1210 MergeProtocolPropertiesIntoClass(C, C);
Chris Lattner855e51f2007-12-12 07:09:47 +00001211 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001212 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1213 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1214 // user-defined setter/getter. It also synthesizes setter/getter methods
1215 // and adds them to the DeclContext and global method pools.
1216 for (ObjCContainerDecl::prop_iterator i = CDecl->prop_begin(),
1217 e = CDecl->prop_end(); i != e; ++i)
1218 ProcessPropertyDecl((*i), CDecl);
1219 CDecl->setAtEndLoc(AtEndLoc);
1220 }
1221 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001222 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001223 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +00001224 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001225 } else if (ObjCCategoryImplDecl* CatImplClass =
1226 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001227 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001228 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +00001229 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001230 // in this interface are implemented in the category @implementation.
Chris Lattner855e51f2007-12-12 07:09:47 +00001231 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001232 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001233 Categories; Categories = Categories->getNextClassCategory()) {
1234 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1235 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1236 break;
1237 }
1238 }
1239 }
1240 }
1241}
1242
1243
1244/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1245/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001246static Decl::ObjCDeclQualifier
1247CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1248 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1249 if (PQTVal & ObjCDeclSpec::DQ_In)
1250 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1251 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1252 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1253 if (PQTVal & ObjCDeclSpec::DQ_Out)
1254 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1255 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1256 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1257 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1258 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1259 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1260 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001261
1262 return ret;
1263}
1264
1265Sema::DeclTy *Sema::ActOnMethodDeclaration(
1266 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +00001267 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001268 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001269 Selector Sel,
1270 // optional arguments. The number of types/arguments is obtained
1271 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001272 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001273 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001274 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1275 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +00001276 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +00001277
1278 // Make sure we can establish a context for the method.
1279 if (!ClassDecl) {
1280 Diag(MethodLoc, diag::error_missing_method_context);
1281 return 0;
1282 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001283 QualType resultDeclType;
1284
1285 if (ReturnType)
1286 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1287 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001288 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001289
Chris Lattner114add62008-03-16 00:49:28 +00001290 ObjCMethodDecl* ObjCMethod =
1291 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroffab63fd62009-01-08 17:28:14 +00001292 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001293 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001294 false,
Chris Lattner114add62008-03-16 00:49:28 +00001295 MethodDeclKind == tok::objc_optional ?
1296 ObjCMethodDecl::Optional :
1297 ObjCMethodDecl::Required);
1298
Chris Lattnereee57c02008-04-04 06:12:32 +00001299 llvm::SmallVector<ParmVarDecl*, 16> Params;
1300
1301 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1302 // FIXME: arg->AttrList must be stored too!
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001303 QualType argType, originalArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001304
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001305 if (ArgTypes[i]) {
Chris Lattnereee57c02008-04-04 06:12:32 +00001306 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001307 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001308 if (argType->isArrayType()) { // (char *[]) -> (char **)
1309 originalArgType = argType;
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001310 argType = Context.getArrayDecayedType(argType);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001311 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001312 else if (argType->isFunctionType())
1313 argType = Context.getPointerType(argType);
1314 } else
Chris Lattnereee57c02008-04-04 06:12:32 +00001315 argType = Context.getObjCIdType();
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001316 ParmVarDecl* Param;
1317 if (originalArgType.isNull())
1318 Param = ParmVarDecl::Create(Context, ObjCMethod,
1319 SourceLocation(/*FIXME*/),
1320 ArgNames[i], argType,
1321 VarDecl::None, 0, 0);
1322 else
1323 Param = ParmVarWithOriginalTypeDecl::Create(Context, ObjCMethod,
1324 SourceLocation(/*FIXME*/),
1325 ArgNames[i], argType, originalArgType,
1326 VarDecl::None, 0, 0);
1327
Chris Lattnereee57c02008-04-04 06:12:32 +00001328 Param->setObjCDeclQualifier(
1329 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1330 Params.push_back(Param);
1331 }
1332
Ted Kremenek42730c52008-01-07 19:49:32 +00001333 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1334 ObjCMethod->setObjCDeclQualifier(
1335 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1336 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001337
1338 if (AttrList)
1339 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001340
1341 // For implementations (which can be very "coarse grain"), we add the
1342 // method now. This allows the AST to implement lookup methods that work
1343 // incrementally (without waiting until we parse the @end). It also allows
1344 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001345 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001346 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001347 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001348 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001349 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001350 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001351 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001352 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001353 }
1354 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001355 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001356 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001357 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001358 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001359 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001360 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001361 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001362 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001363 }
1364 }
1365 if (PrevMethod) {
1366 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001367 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001368 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001369 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001370 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001371 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001372}
1373
Daniel Dunbar540ff472008-09-23 21:53:23 +00001374void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1375 SourceLocation Loc,
1376 unsigned &Attributes) {
1377 // FIXME: Improve the reported location.
1378
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001379 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001380 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001381 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1382 ObjCDeclSpec::DQ_PR_assign |
1383 ObjCDeclSpec::DQ_PR_copy |
1384 ObjCDeclSpec::DQ_PR_retain))) {
1385 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1386 "readwrite" :
1387 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1388 "assign" :
1389 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1390 "copy" : "retain";
1391
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001392 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1393 diag::err_objc_property_attr_mutually_exclusive :
1394 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001395 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001396 }
1397
1398 // Check for copy or retain on non-object types.
1399 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1400 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001401 Diag(Loc, diag::err_objc_property_requires_object)
1402 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001403 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1404 }
1405
1406 // Check for more than one of { assign, copy, retain }.
1407 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1408 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001409 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1410 << "assign" << "copy";
1411 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001412 }
1413 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001414 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1415 << "assign" << "retain";
1416 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001417 }
1418 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1419 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001420 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1421 << "copy" << "retain";
1422 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001423 }
1424 }
1425
1426 // Warn if user supplied no assignment attribute, property is
1427 // readwrite, and this is an object type.
1428 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1429 ObjCDeclSpec::DQ_PR_retain)) &&
1430 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1431 Context.isObjCObjectPointerType(PropertyTy)) {
1432 // Skip this warning in gc-only mode.
1433 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1434 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1435
1436 // If non-gc code warn that this is likely inappropriate.
1437 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1438 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1439
1440 // FIXME: Implement warning dependent on NSCopying being
1441 // implemented. See also:
1442 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1443 // (please trim this list while you are at it).
1444 }
1445}
1446
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001447Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1448 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001449 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001450 Selector GetterSel,
1451 Selector SetterSel,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001452 DeclTy *ClassCategory,
1453 bool *isOverridingProperty,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001454 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001455 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001456 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1457 // default is readwrite!
1458 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1459 // property is defaulted to 'assign' if it is readwrite and is
1460 // not retain or copy
1461 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1462 (isReadWrite &&
1463 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1464 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1465 QualType T = GetTypeForDeclarator(FD.D, S);
1466 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001467
1468 // May modify Attributes.
1469 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001470
1471 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1472 if (!CDecl->getIdentifier()) {
1473 // This is an anonymous category. property requires special
1474 // handling.
1475 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1476 if (ObjCPropertyDecl *PIDecl =
1477 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1478 // property 'PIDecl's readonly attribute will be over-ridden
1479 // with anonymous category's readwrite property attribute!
1480 unsigned PIkind = PIDecl->getPropertyAttributes();
1481 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001482 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001483 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1484 Diag(AtLoc, diag::warn_property_attr_mismatch);
1485 PIDecl->makeitReadWriteAttribute();
1486 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1487 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1488 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1489 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1490 PIDecl->setSetterName(SetterSel);
1491 // FIXME: use a common routine with addPropertyMethods.
1492 ObjCMethodDecl *SetterDecl =
1493 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1494 Context.VoidTy,
1495 ICDecl,
1496 true, false, true,
1497 ObjCMethodDecl::Required);
1498 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1499 SetterDecl,
1500 SourceLocation(),
1501 FD.D.getIdentifier(),
1502 T,
1503 VarDecl::None,
1504 0, 0);
1505 SetterDecl->setMethodParams(&Argument, 1);
1506 PIDecl->setSetterMethodDecl(SetterDecl);
1507 }
1508 else
Fariborz Jahanian3d7aa252008-12-04 22:56:16 +00001509 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001510 *isOverridingProperty = true;
1511 return 0;
1512 }
Fariborz Jahanianc1235662008-11-26 20:33:54 +00001513 // No matching property found in the main class. Just fall thru
1514 // and add property to the anonymous category. It looks like
1515 // it works as is. This category becomes just like a category
1516 // for its primary class.
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001517 } else {
1518 Diag(CDecl->getLocation(), diag::err_continuation_class);
1519 *isOverridingProperty = true;
1520 return 0;
1521 }
1522 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001523
Fariborz Jahanianf8bfa0c2008-12-16 17:51:01 +00001524 Type *t = T.getTypePtr();
1525 if (t->isArrayType() || t->isFunctionType())
1526 Diag(AtLoc, diag::err_property_type) << T;
1527
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001528 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, CurContext, AtLoc,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001529 FD.D.getIdentifier(), T);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001530 // FIXME: PushOnScopeChains?
1531 CurContext->addDecl(Context, PDecl);
1532
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001533 // Regardless of setter/getter attribute, we save the default getter/setter
1534 // selector names in anticipation of declaration of setter/getter methods.
1535 PDecl->setGetterName(GetterSel);
1536 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001537
Daniel Dunbar540ff472008-09-23 21:53:23 +00001538 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001539 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001540
Daniel Dunbar540ff472008-09-23 21:53:23 +00001541 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001542 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001543
Daniel Dunbar540ff472008-09-23 21:53:23 +00001544 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001545 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001546
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001547 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001548 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001549
Daniel Dunbar540ff472008-09-23 21:53:23 +00001550 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001551 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001552
Daniel Dunbar540ff472008-09-23 21:53:23 +00001553 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001554 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001555
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001556 if (isAssign)
1557 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1558
Daniel Dunbar540ff472008-09-23 21:53:23 +00001559 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001560 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001561
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001562 if (MethodImplKind == tok::objc_required)
1563 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1564 else if (MethodImplKind == tok::objc_optional)
1565 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1566
Chris Lattner855e51f2007-12-12 07:09:47 +00001567 return PDecl;
1568}
1569
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001570/// ActOnPropertyImplDecl - This routine performs semantic checks and
1571/// builds the AST node for a property implementation declaration; declared
1572/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001573///
1574Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1575 SourceLocation PropertyLoc,
1576 bool Synthesize,
1577 DeclTy *ClassCatImpDecl,
1578 IdentifierInfo *PropertyId,
1579 IdentifierInfo *PropertyIvar) {
1580 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1581 // Make sure we have a context for the property implementation declaration.
1582 if (!ClassImpDecl) {
1583 Diag(AtLoc, diag::error_missing_property_context);
1584 return 0;
1585 }
1586 ObjCPropertyDecl *property = 0;
1587 ObjCInterfaceDecl* IDecl = 0;
1588 // Find the class or category class where this property must have
1589 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001590 ObjCImplementationDecl *IC = 0;
1591 ObjCCategoryImplDecl* CatImplClass = 0;
1592 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001593 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001594 // We always synthesize an interface for an implementation
1595 // without an interface decl. So, IDecl is always non-zero.
1596 assert(IDecl &&
1597 "ActOnPropertyImplDecl - @implementation without @interface");
1598
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001599 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001600 property = IDecl->FindPropertyDeclaration(PropertyId);
1601 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001602 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001603 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001604 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001605 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001606 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001607 if (Synthesize) {
1608 Diag(AtLoc, diag::error_synthesize_category_decl);
1609 return 0;
1610 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001611 IDecl = CatImplClass->getClassInterface();
1612 if (!IDecl) {
1613 Diag(AtLoc, diag::error_missing_property_interface);
1614 return 0;
1615 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001616 ObjCCategoryDecl *Category =
1617 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1618
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001619 // If category for this implementation not found, it is an error which
1620 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001621 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001622 return 0;
1623 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001624 property = Category->FindPropertyDeclaration(PropertyId);
1625 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001626 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001627 << Category->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001628 return 0;
1629 }
1630 }
1631 else {
1632 Diag(AtLoc, diag::error_bad_property_context);
1633 return 0;
1634 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001635 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001636 // Check that we have a valid, previously declared ivar for @synthesize
1637 if (Synthesize) {
1638 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001639 if (!PropertyIvar)
1640 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001641 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001642 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001643 if (!Ivar) {
Chris Lattner65cae292008-11-19 08:23:25 +00001644 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001645 return 0;
1646 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001647 QualType PropType = Context.getCanonicalType(property->getType());
1648 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1649
Steve Naroff631e3922008-09-30 00:24:17 +00001650 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001651 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001652 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001653 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001654 << property->getDeclName() << Ivar->getDeclName();
Steve Naroffc32e45c2008-09-30 10:07:56 +00001655 return 0;
1656 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001657 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001658 } else if (PropertyIvar) {
1659 // @dynamic
1660 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1661 return 0;
1662 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001663 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001664 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001665 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1666 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001667 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001668 ObjCPropertyImplDecl::Synthesize
1669 : ObjCPropertyImplDecl::Dynamic),
1670 Ivar);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001671 CurContext->addDecl(Context, PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001672 if (IC) {
1673 if (Synthesize)
1674 if (ObjCPropertyImplDecl *PPIDecl =
1675 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1676 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1677 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1678 << PropertyIvar;
1679 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1680 }
1681
1682 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1683 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1684 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1685 return 0;
1686 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001687 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001688 }
1689 else {
1690 if (Synthesize)
1691 if (ObjCPropertyImplDecl *PPIDecl =
1692 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1693 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1694 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1695 << PropertyIvar;
1696 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1697 }
1698
1699 if (ObjCPropertyImplDecl *PPIDecl =
1700 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1701 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1702 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1703 return 0;
1704 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001705 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001706 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001707
1708 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001709}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001710
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001711bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00001712 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001713 return false;
1714
1715 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1716 D->setInvalidDecl();
1717
1718 return true;
1719}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001720
1721/// Collect the instance variables declared in an Objective-C object. Used in
1722/// the creation of structures from objects using the @defs directive.
1723/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1724/// part of the AST generation logic of @defs.
1725static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1726 ASTContext& Ctx,
1727 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1728 if (Class->getSuperClass())
1729 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1730
1731 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1732 for (ObjCInterfaceDecl::ivar_iterator
1733 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1734
1735 ObjCIvarDecl* ID = *I;
1736 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1737 ID->getLocation(),
1738 ID->getIdentifier(),
1739 ID->getType(),
1740 ID->getBitWidth()));
1741 }
1742}
1743
1744/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1745/// instance variables of ClassName into Decls.
1746void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1747 IdentifierInfo *ClassName,
1748 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1749 // Check that ClassName is a valid class
1750 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1751 if (!Class) {
1752 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1753 return;
1754 }
1755 // Collect the instance variables
1756 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1757
1758 // Introduce all of these fields into the appropriate scope.
1759 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1760 D != Decls.end(); ++D) {
1761 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1762 if (getLangOptions().CPlusPlus)
1763 PushOnScopeChains(cast<FieldDecl>(FD), S);
1764 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
1765 Record->addDecl(Context, FD);
1766 }
1767}
1768