blob: f71f4f4b786d0451ac7871933c9a81c895f8af18 [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"
17#include "clang/Parse/Scope.h"
18
19using namespace clang;
20
Ted Kremenek42730c52008-01-07 19:49:32 +000021/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Ted Kremenek42730c52008-01-07 19:49:32 +000023void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Ted Kremenek42730c52008-01-07 19:49:32 +000025 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Chris Lattner855e51f2007-12-12 07:09:47 +000026 assert(MDecl != 0 && "Not a method declarator!");
Steve Narofffe9eb6a2007-12-18 01:30:32 +000027
28 // Allow the rest of sema to find private method decl implementations.
29 if (MDecl->isInstance())
30 AddInstanceMethodToGlobalPool(MDecl);
31 else
32 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000033
34 // Allow all of Sema to see that we are entering a method definition.
Chris Lattnerf3874bc2008-04-06 04:47:34 +000035 PushDeclContext(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000036
37 // Create Decl objects for each parameter, entrring them in the scope for
38 // binding to their use.
39 struct DeclaratorChunk::ParamInfo PI;
40
41 // Insert the invisible arguments, self and _cmd!
42 PI.Ident = &Context.Idents.get("self");
43 PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
Steve Naroff1a5027c2008-06-05 14:49:39 +000044 QualType selfTy;
Chris Lattner855e51f2007-12-12 07:09:47 +000045 if (MDecl->isInstance()) {
Steve Naroff1a5027c2008-06-05 14:49:39 +000046 selfTy = Context.getObjCIdType();
Gabor Greif9b2e0f92008-02-29 20:35:55 +000047 if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) {
48 // There may be no interface context due to error in declaration of the
49 // interface (which has been reported). Recover gracefully
Chris Lattner3e254fb2008-04-08 04:40:51 +000050 selfTy = Context.getObjCInterfaceType(OID);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000051 selfTy = Context.getPointerType(selfTy);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000052 }
Steve Naroff1a5027c2008-06-05 14:49:39 +000053 } else // we have a factory method.
54 selfTy = Context.getObjCClassType();
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000055 getCurMethodDecl()->setSelfDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner8c7c6a12008-06-17 18:05:57 +000056 PI.Ident, PI.IdentLoc, selfTy));
Chris Lattner855e51f2007-12-12 07:09:47 +000057
58 PI.Ident = &Context.Idents.get("_cmd");
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000059 getCurMethodDecl()->setCmdDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner8c7c6a12008-06-17 18:05:57 +000060 PI.Ident, PI.IdentLoc, Context.getObjCSelType()));
Chris Lattner3e254fb2008-04-08 04:40:51 +000061
Chris Lattner97316c02008-04-10 02:22:51 +000062 // Introduce all of the other parameters into this scope.
Chris Lattner685d7922008-03-16 01:07:14 +000063 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000064 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +000065 IdentifierInfo *II = PDecl->getIdentifier();
Argiris Kirtzidis43ce0be2008-04-27 13:30:35 +000066 if (II)
67 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000068 }
69}
70
Chris Lattnere705e5e2008-07-21 22:17:28 +000071Sema::DeclTy *Sema::
72ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
73 IdentifierInfo *ClassName, SourceLocation ClassLoc,
74 IdentifierInfo *SuperName, SourceLocation SuperLoc,
75 const IdentifierLocPair *ProtocolNames,
76 unsigned NumProtocols,
77 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000078 assert(ClassName && "Missing class identifier");
79
80 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +000081 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000082 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +000083 Diag(ClassLoc, diag::err_redefinition_different_kind,
84 ClassName->getName());
85 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
86 }
87
Ted Kremenek42730c52008-01-07 19:49:32 +000088 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000089 if (IDecl) {
90 // Class already seen. Is it a forward declaration?
91 if (!IDecl->isForwardDecl())
92 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
93 else {
94 IDecl->setLocation(AtInterfaceLoc);
95 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000096 }
Chris Lattner5cece462008-07-21 07:06:49 +000097 } else {
98 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000099 ClassName, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000100
Steve Naroff15208162008-04-02 18:30:49 +0000101 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000102 // Remember that this needs to be removed when the scope is popped.
103 TUScope->AddDecl(IDecl);
104 }
105
106 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000107 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000108 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000109 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000111 Diag(SuperLoc, diag::err_redefinition_different_kind,
112 SuperName->getName());
113 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
114 }
115 else {
116 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000117 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000118
119 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Chris Lattner5cece462008-07-21 07:06:49 +0000120 Diag(SuperLoc, diag::err_undef_superclass,
Chris Lattner855e51f2007-12-12 07:09:47 +0000121 SuperClassEntry ? SuperClassEntry->getName()
122 : SuperName->getName(),
Chris Lattner5cece462008-07-21 07:06:49 +0000123 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc));
Chris Lattner855e51f2007-12-12 07:09:47 +0000124 }
125 }
126 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000127 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000128 IDecl->setLocEnd(SuperLoc);
129 } else { // we have a root class.
130 IDecl->setLocEnd(ClassLoc);
131 }
132
133 /// Check then save referenced protocols
134 if (NumProtocols) {
Chris Lattner5cece462008-07-21 07:06:49 +0000135 llvm::SmallVector<ObjCProtocolDecl*, 8> RefProtos;
Chris Lattner855e51f2007-12-12 07:09:47 +0000136 for (unsigned int i = 0; i != NumProtocols; i++) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000137 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i].first];
Chris Lattner8779c632008-07-21 18:34:02 +0000138 if (!RefPDecl)
Chris Lattnere705e5e2008-07-21 22:17:28 +0000139 Diag(ProtocolNames[i].second, diag::err_undef_protocolref,
140 ProtocolNames[i].first->getName(), ClassName->getName());
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000141 else {
142 if (RefPDecl->isForwardDecl())
Chris Lattnere705e5e2008-07-21 22:17:28 +0000143 Diag(ProtocolNames[i].second, diag::warn_undef_protocolref,
144 ProtocolNames[i].first->getName(), ClassName->getName());
Chris Lattner5cece462008-07-21 07:06:49 +0000145 RefProtos.push_back(RefPDecl);
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000146 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000147 }
Chris Lattner5cece462008-07-21 07:06:49 +0000148 if (!RefProtos.empty())
149 IDecl->addReferencedProtocols(&RefProtos[0], RefProtos.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000150 IDecl->setLocEnd(EndProtoLoc);
151 }
152 return IDecl;
153}
154
155/// ActOnCompatiblityAlias - this action is called after complete parsing of
156/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000157Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
158 IdentifierInfo *AliasName,
159 SourceLocation AliasLocation,
160 IdentifierInfo *ClassName,
161 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000162 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000163 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000164 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000165 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000166 Diag(AliasLocation, diag::warn_previous_alias_decl);
167 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
168 }
169 else {
170 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
171 AliasName->getName());
172 Diag(ADecl->getLocation(), diag::err_previous_declaration);
173 }
174 return 0;
175 }
176 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000177 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000178 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
179 if (CDecl == 0) {
180 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
181 if (CDeclU)
182 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000183 return 0;
184 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000185
186 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000187 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000188 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
189
190 ObjCAliasDecls[AliasName] = AliasDecl;
191 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000192 return AliasDecl;
193}
194
195Sema::DeclTy *Sema::ActOnStartProtocolInterface(
196 SourceLocation AtProtoInterfaceLoc,
197 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000198 const IdentifierLocPair *ProtoRefNames, unsigned NumProtoRefs,
Chris Lattner855e51f2007-12-12 07:09:47 +0000199 SourceLocation EndProtoLoc) {
200 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000201 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000202 if (PDecl) {
203 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000204 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000205 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
206 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000207 // Just return the protocol we already had.
208 // FIXME: don't leak the objects passed in!
209 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000210 }
Chris Lattnerc1881852008-03-16 01:25:17 +0000211
212 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000213 } else {
Chris Lattner0be08822008-07-21 21:32:27 +0000214 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000215 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000216 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000217 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000218
219 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000220 /// Check then save referenced protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000221 llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000222 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000223 ObjCProtocolDecl *RefPDecl = ObjCProtocols[ProtoRefNames[i].first];
Chris Lattner8779c632008-07-21 18:34:02 +0000224 if (!RefPDecl)
Chris Lattnere705e5e2008-07-21 22:17:28 +0000225 Diag(ProtoRefNames[i].second, diag::err_undef_protocolref,
226 ProtoRefNames[i].first->getName(), ProtocolName->getName());
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000227 else {
228 if (RefPDecl->isForwardDecl())
Chris Lattnere705e5e2008-07-21 22:17:28 +0000229 Diag(ProtoRefNames[i].second, diag::warn_undef_protocolref,
230 ProtoRefNames[i].first->getName(), ProtocolName->getName());
Chris Lattner0be08822008-07-21 21:32:27 +0000231 Protocols.push_back(RefPDecl);
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000232 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000233 }
Chris Lattner0be08822008-07-21 21:32:27 +0000234 if (!Protocols.empty())
235 PDecl->addReferencedProtocols(&Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000236 PDecl->setLocEnd(EndProtoLoc);
237 }
238 return PDecl;
239}
240
241/// FindProtocolDeclaration - This routine looks up protocols and
242/// issuer error if they are not declared. It returns list of protocol
243/// declarations in its 'Protocols' argument.
244void
245Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000246 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000247 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000248 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000249 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000250 if (ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first])
Chris Lattner855e51f2007-12-12 07:09:47 +0000251 Protocols.push_back(PDecl);
Chris Lattnere705e5e2008-07-21 22:17:28 +0000252 else
253 Diag(ProtocolId[i].second, diag::err_undeclared_protocol,
254 ProtocolId[i].first->getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000255 }
256}
257
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000258/// DiagnosePropertyMismatch - Compares two properties for their
259/// attributes and types and warns on a variety of inconsistancies.
260///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000261void
262Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
263 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000264 const char *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000265 ObjCPropertyDecl::PropertyAttributeKind CAttr =
266 Property->getPropertyAttributes();
267 ObjCPropertyDecl::PropertyAttributeKind SAttr =
268 SuperProperty->getPropertyAttributes();
269 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
270 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000271 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000272 Property->getName(), inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000273 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
274 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000275 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000276 Property->getName(), "copy", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000277 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000278 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
279 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000280 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000281 Property->getName(), "retain", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000282 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000283
284 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
285 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000286 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000287 Property->getName(), "atomic", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000288 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000289 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000290 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000291 Property->getName(), "setter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000292 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000293 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000294 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000295 Property->getName(), "getter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000296 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000297
Fariborz Jahanian513e30a2008-05-01 18:05:01 +0000298 if (Property->getCanonicalType() != SuperProperty->getCanonicalType())
299 Diag(Property->getLocation(), diag::warn_property_type,
300 Property->getType().getAsString(),
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000301 inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000302
303}
304
305/// ComparePropertiesInBaseAndSuper - This routine compares property
306/// declarations in base and its super class, if any, and issues
307/// diagnostics in a variety of inconsistant situations.
308///
309void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000310Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000311 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
312 if (!SDecl)
313 return;
Fariborz Jahanianed986602008-05-01 00:03:38 +0000314 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
315 E = SDecl->classprop_end(); S != E; ++S) {
316 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000317 // Does property in super class has declaration in current class?
318 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
319 E = IDecl->classprop_end(); I != E; ++I) {
320 ObjCPropertyDecl *PDecl = (*I);
321 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000322 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000323 }
324 }
325}
326
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000327/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
328/// of properties declared in a protocol and adds them to the list
329/// of properties for current class if it is not there already.
330void
331Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
332 ObjCProtocolDecl *PDecl)
333{
334 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
335 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
336 E = PDecl->classprop_end(); P != E; ++P) {
337 ObjCPropertyDecl *Pr = (*P);
338 ObjCInterfaceDecl::classprop_iterator CP, CE;
339 // Is this property already in class's list of properties?
340 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
341 CP != CE; ++CP)
342 if ((*CP)->getIdentifier() == Pr->getIdentifier())
343 break;
344 if (CP == CE)
345 // Add this property to list of properties for thie class.
346 mergeProperties.push_back(Pr);
347 else
348 // Property protocol already exist in class. Diagnose any mismatch.
349 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
350 }
351 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
352}
353
354/// MergeProtocolPropertiesIntoClass - This routine merges properties
355/// declared in 'MergeItsProtocols' objects (which can be a class or an
356/// inherited protocol into the list of properties for class 'IDecl'
357///
358
359void
360Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
361 DeclTy *MergeItsProtocols) {
362 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000363 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000364 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
365 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000366 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000367 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
368
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000369 // Go thru the list of protocols for this class and recursively merge
370 // their properties into this class as well.
371 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
372 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000373 MergeProtocolPropertiesIntoClass(IDecl, *P);
374 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000375 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
376 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
377 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000378 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000379 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000380}
381
Chris Lattner855e51f2007-12-12 07:09:47 +0000382/// ActOnForwardProtocolDeclaration -
383Action::DeclTy *
384Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000385 const IdentifierLocPair *IdentList,
386 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000387 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000388
389 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000390 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000391 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattnere705e5e2008-07-21 22:17:28 +0000392 if (PDecl == 0) // Not already seen?
393 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000394
395 Protocols.push_back(PDecl);
396 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000397 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
398 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000399}
400
Chris Lattnere705e5e2008-07-21 22:17:28 +0000401Sema::DeclTy *Sema::
402ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
403 IdentifierInfo *ClassName, SourceLocation ClassLoc,
404 IdentifierInfo *CategoryName,
405 SourceLocation CategoryLoc,
406 const IdentifierLocPair *ProtoRefNames,
407 unsigned NumProtoRefs,
408 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000409 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000410
Chris Lattnere29dc832008-03-16 20:34:23 +0000411 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000412 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000413 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000414
415 /// Check that class of this category is already completely declared.
416 if (!IDecl || IDecl->isForwardDecl())
417 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffac0580b2008-06-05 15:03:27 +0000418 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000419 /// Check for duplicate interface declaration for this category
420 ObjCCategoryDecl *CDeclChain;
421 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
422 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000423 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff0a7149f2008-06-05 04:33:44 +0000424 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000425 CategoryName->getName());
426 break;
427 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000428 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000429 if (!CDeclChain)
430 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000431 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000432
433 if (NumProtoRefs) {
Chris Lattner321b5d12008-03-16 20:47:45 +0000434 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
435 /// Check and then save the referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000436 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000437 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i].first];
Chris Lattner8779c632008-07-21 18:34:02 +0000438 if (!RefPDecl)
Chris Lattnere705e5e2008-07-21 22:17:28 +0000439 Diag(ProtoRefNames[i].second, diag::err_undef_protocolref,
440 ProtoRefNames[i].first->getName(), CategoryName->getName());
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000441 else {
442 if (RefPDecl->isForwardDecl())
Chris Lattnere705e5e2008-07-21 22:17:28 +0000443 Diag(ProtoRefNames[i].second, diag::warn_undef_protocolref,
444 ProtoRefNames[i].first->getName(), CategoryName->getName());
Chris Lattner321b5d12008-03-16 20:47:45 +0000445 RefProtocols.push_back(RefPDecl);
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000446 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000447 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000448 if (!RefProtocols.empty())
Chris Lattnerc72eaaf2008-07-21 17:23:15 +0000449 CDecl->addReferencedProtocols(&RefProtocols[0], RefProtocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000450 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000451 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000452 return CDecl;
453}
454
455/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000456/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000457/// object.
458Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
459 SourceLocation AtCatImplLoc,
460 IdentifierInfo *ClassName, SourceLocation ClassLoc,
461 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000462 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000463 ObjCCategoryImplDecl *CDecl =
464 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000465 /// Check that class of this category is already completely declared.
466 if (!IDecl || IDecl->isForwardDecl())
467 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
468
469 /// TODO: Check that CatName, category name, is not used in another
470 // implementation.
471 return CDecl;
472}
473
474Sema::DeclTy *Sema::ActOnStartClassImplementation(
475 SourceLocation AtClassImplLoc,
476 IdentifierInfo *ClassName, SourceLocation ClassLoc,
477 IdentifierInfo *SuperClassname,
478 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000479 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000480 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000481 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000482 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000483 Diag(ClassLoc, diag::err_redefinition_different_kind,
484 ClassName->getName());
485 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
486 }
487 else {
488 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000489 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000490 if (!IDecl)
491 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
492 }
493
494 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000495 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000496 if (SuperClassname) {
497 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000498 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000499 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000500 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
501 SuperClassname->getName());
502 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
503 }
504 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000505 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000506 if (!SDecl)
507 Diag(SuperClassLoc, diag::err_undef_superclass,
508 SuperClassname->getName(), ClassName->getName());
509 else if (IDecl && IDecl->getSuperClass() != SDecl) {
510 // This implementation and its interface do not have the same
511 // super class.
512 Diag(SuperClassLoc, diag::err_conflicting_super_class,
513 SDecl->getName());
514 Diag(SDecl->getLocation(), diag::err_previous_definition);
515 }
516 }
517 }
518
519 if (!IDecl) {
520 // Legacy case of @implementation with no corresponding @interface.
521 // Build, chain & install the interface decl into the identifier.
Chris Lattner5cece462008-07-21 07:06:49 +0000522 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000523 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000524 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000525 IDecl->setSuperClass(SDecl);
526 IDecl->setLocEnd(ClassLoc);
527
528 // Remember that this needs to be removed when the scope is popped.
529 TUScope->AddDecl(IDecl);
530 }
531
Ted Kremenek42730c52008-01-07 19:49:32 +0000532 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000533 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
534 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000535
536 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000537 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000538 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000539 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
540 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000541 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000542 return IMPDecl;
543}
544
Ted Kremenek42730c52008-01-07 19:49:32 +0000545void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
546 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000547 SourceLocation RBrace) {
548 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000549 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000550 if (!IDecl)
551 return;
552 /// Check case of non-existing @interface decl.
553 /// (legacy objective-c @implementation decl without an @interface decl).
554 /// Add implementations's ivar to the synthesize class's ivar list.
555 if (IDecl->ImplicitInterfaceDecl()) {
556 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
557 return;
558 }
559 // If implementation has empty ivar list, just return.
560 if (numIvars == 0)
561 return;
562
563 assert(ivars && "missing @implementation ivars");
564
565 // Check interface's Ivar list against those in the implementation.
566 // names and types must match.
567 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000568 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000569 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000570 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
571 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000572 ObjCIvarDecl* ImplIvar = ivars[j++];
573 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000574 assert (ImplIvar && "missing implementation ivar");
575 assert (ClsIvar && "missing class ivar");
576 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
577 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
578 ImplIvar->getIdentifier()->getName());
579 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
580 ClsIvar->getIdentifier()->getName());
581 }
582 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
583 // as error.
584 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
585 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
586 ImplIvar->getIdentifier()->getName());
587 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
588 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000589 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000590 }
591 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000592 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000593
594 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000595 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000596 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000597 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000598}
599
Steve Naroffb4f48512008-02-10 21:38:56 +0000600void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
601 bool &IncompleteImpl) {
602 if (!IncompleteImpl) {
603 Diag(ImpLoc, diag::warn_incomplete_impl);
604 IncompleteImpl = true;
605 }
606 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
607}
608
Steve Naroffb268d2a2008-02-08 22:06:17 +0000609/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000610/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000611void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
612 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000613 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000614 const llvm::DenseSet<Selector> &InsMap,
615 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000616 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000617 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000618 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000619 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000620 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000621 method->getImplementationControl() != ObjCMethodDecl::Optional)
622 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000623 }
624 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000625 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000626 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000627 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000628 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000629 method->getImplementationControl() != ObjCMethodDecl::Optional)
630 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000631 }
Chris Lattner0be08822008-07-21 21:32:27 +0000632 // Check on this protocols's referenced protocols, recursively.
633 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
634 E = PDecl->protocol_end(); PI != E; ++PI)
635 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000636}
637
Ted Kremenek42730c52008-01-07 19:49:32 +0000638void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
639 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000640 llvm::DenseSet<Selector> InsMap;
641 // Check and see if instance methods in class interface have been
642 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000643 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000644 E = IMPDecl->instmeth_end(); I != E; ++I)
645 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000646
647 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000648 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000649 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000650 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000651 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000652
Chris Lattner855e51f2007-12-12 07:09:47 +0000653 llvm::DenseSet<Selector> ClsMap;
654 // Check and see if class methods in class interface have been
655 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000656 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000657 E = IMPDecl->classmeth_end(); I != E; ++I)
658 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000659
Ted Kremenek42730c52008-01-07 19:49:32 +0000660 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000661 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000662 if (!ClsMap.count((*I)->getSelector()))
663 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000664
665 // Check the protocol list for unimplemented methods in the @implementation
666 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000667 const ObjCList<ObjCProtocolDecl> &Protocols =
668 IDecl->getReferencedProtocols();
669 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
670 E = Protocols.end(); I != E; ++I)
671 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000672 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000673}
674
675/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
676/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000677void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
678 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000679 llvm::DenseSet<Selector> InsMap;
680 // Check and see if instance methods in category interface have been
681 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000682 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000683 E = CatImplDecl->instmeth_end(); I != E; ++I)
684 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000685
686 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000687 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000688 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000689 if (!InsMap.count((*I)->getSelector()))
690 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
691
Chris Lattner855e51f2007-12-12 07:09:47 +0000692 llvm::DenseSet<Selector> ClsMap;
693 // Check and see if class methods in category interface have been
694 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000695 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000696 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
697 I != E; ++I)
698 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000699
Ted Kremenek42730c52008-01-07 19:49:32 +0000700 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000701 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000702 if (!ClsMap.count((*I)->getSelector()))
703 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000704
705 // Check the protocol list for unimplemented methods in the @implementation
706 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000707 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
708 E = CatClassDecl->protocol_end(); PI != E; ++PI)
709 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000710 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000711}
712
713/// ActOnForwardClassDeclaration -
714Action::DeclTy *
715Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
716 IdentifierInfo **IdentList, unsigned NumElts)
717{
Ted Kremenek42730c52008-01-07 19:49:32 +0000718 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000719
720 for (unsigned i = 0; i != NumElts; ++i) {
721 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000722 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000723 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000724 // GCC apparently allows the following idiom:
725 //
726 // typedef NSObject < XCElementTogglerP > XCElementToggler;
727 // @class XCElementToggler;
728 //
729 // FIXME: Make an extension?
730 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
731 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
732 Diag(AtClassLoc, diag::err_redefinition_different_kind,
733 IdentList[i]->getName());
734 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
735 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000736 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000737 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000738 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000739 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000740 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000741 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000742
743 // Remember that this needs to be removed when the scope is popped.
744 TUScope->AddDecl(IDecl);
745 }
746
747 Interfaces.push_back(IDecl);
748 }
749
Chris Lattnere29dc832008-03-16 20:34:23 +0000750 return ObjCClassDecl::Create(Context, AtClassLoc,
751 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000752}
753
754
755/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
756/// returns true, or false, accordingly.
757/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000758bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
759 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000760 if (Method->getResultType().getCanonicalType() !=
761 PrevMethod->getResultType().getCanonicalType())
762 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000763 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000764 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
765 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000766 if (Context.getCanonicalType(ParamDecl->getType()) !=
767 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000768 return false;
769 }
770 return true;
771}
772
Ted Kremenek42730c52008-01-07 19:49:32 +0000773void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
774 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000775 if (!FirstMethod.Method) {
776 // Haven't seen a method with this selector name yet - add it.
777 FirstMethod.Method = Method;
778 FirstMethod.Next = 0;
779 } else {
780 // We've seen a method with this name, now check the type signature(s).
781 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
782
Ted Kremenek42730c52008-01-07 19:49:32 +0000783 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000784 Next = Next->Next)
785 match = MatchTwoMethodDeclarations(Method, Next->Method);
786
787 if (!match) {
788 // We have a new signature for an existing method - add it.
789 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000790 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000791 FirstMethod.Next = OMI;
792 }
793 }
794}
795
Ted Kremenek42730c52008-01-07 19:49:32 +0000796void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
797 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000798 if (!FirstMethod.Method) {
799 // Haven't seen a method with this selector name yet - add it.
800 FirstMethod.Method = Method;
801 FirstMethod.Next = 0;
802 } else {
803 // We've seen a method with this name, now check the type signature(s).
804 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
805
Ted Kremenek42730c52008-01-07 19:49:32 +0000806 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000807 Next = Next->Next)
808 match = MatchTwoMethodDeclarations(Method, Next->Method);
809
810 if (!match) {
811 // We have a new signature for an existing method - add it.
812 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000813 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000814 FirstMethod.Next = OMI;
815 }
816 }
817}
818
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000819// Note: For class/category implemenations, allMethods/allProperties is
820// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000821void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
822 DeclTy **allMethods, unsigned allNum,
823 DeclTy **allProperties, unsigned pNum) {
824 Decl *ClassDecl = static_cast<Decl *>(classDecl);
825
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000826 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
827 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000828 // should be true.
829 if (!ClassDecl)
830 return;
831
Ted Kremenek42730c52008-01-07 19:49:32 +0000832 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
833 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000834
Ted Kremenek42730c52008-01-07 19:49:32 +0000835 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
836 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000837
838 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000839 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
840 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000841 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000842
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000843 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000844 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
845 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000846 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
847 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
848 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
849 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000850 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000851 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000852 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000853
854 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000855 ObjCMethodDecl *Method =
856 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000857
858 if (!Method) continue; // Already issued a diagnostic.
859 if (Method->isInstance()) {
860 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000861 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000862 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
863 : false;
864 if (isInterfaceDeclKind && PrevMethod && !match
865 || checkIdenticalMethods && match) {
866 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
867 Method->getSelector().getName());
868 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
869 } else {
870 insMethods.push_back(Method);
871 InsMap[Method->getSelector()] = Method;
872 /// The following allows us to typecheck messages to "id".
873 AddInstanceMethodToGlobalPool(Method);
874 }
875 }
876 else {
877 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000878 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000879 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
880 : false;
881 if (isInterfaceDeclKind && PrevMethod && !match
882 || checkIdenticalMethods && match) {
883 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
884 Method->getSelector().getName());
885 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
886 } else {
887 clsMethods.push_back(Method);
888 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000889 /// The following allows us to typecheck messages to "Class".
890 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000891 }
892 }
893 }
894
Ted Kremenek42730c52008-01-07 19:49:32 +0000895 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000896 // Compares properties declaraed in this class to those of its
897 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000898 ComparePropertiesInBaseAndSuper(I);
899 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000900 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
901 E = I->classprop_end(); P != E; ++P) {
Steve Naroff638d6a42008-05-22 23:24:08 +0000902 // FIXME: It would be really nice if we could avoid this. Injecting
903 // methods into the interface makes it hard to distinguish "real" methods
904 // from synthesized "property" methods (that aren't in the source).
905 // This complicicates the rewriter's life.
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000906 I->addPropertyMethods(Context, *P, insMethods);
907 }
908 I->addMethods(&insMethods[0], insMethods.size(),
909 &clsMethods[0], clsMethods.size(), AtEndLoc);
910
Ted Kremenek42730c52008-01-07 19:49:32 +0000911 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000912 P->addMethods(&insMethods[0], insMethods.size(),
913 &clsMethods[0], clsMethods.size(), AtEndLoc);
914 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000915 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000916 C->addMethods(&insMethods[0], insMethods.size(),
917 &clsMethods[0], clsMethods.size(), AtEndLoc);
918 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000919 else if (ObjCImplementationDecl *IC =
920 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000921 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000922 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000923 ImplMethodsVsClassMethods(IC, IDecl);
924 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000925 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000926 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000927 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000928 // Find category interface decl and then check that all methods declared
929 // in this interface is implemented in the category @implementation.
930 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000931 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000932 Categories; Categories = Categories->getNextClassCategory()) {
933 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
934 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
935 break;
936 }
937 }
938 }
939 }
940}
941
942
943/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
944/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000945static Decl::ObjCDeclQualifier
946CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
947 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
948 if (PQTVal & ObjCDeclSpec::DQ_In)
949 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
950 if (PQTVal & ObjCDeclSpec::DQ_Inout)
951 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
952 if (PQTVal & ObjCDeclSpec::DQ_Out)
953 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
954 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
955 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
956 if (PQTVal & ObjCDeclSpec::DQ_Byref)
957 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
958 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
959 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000960
961 return ret;
962}
963
964Sema::DeclTy *Sema::ActOnMethodDeclaration(
965 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000966 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000967 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000968 Selector Sel,
969 // optional arguments. The number of types/arguments is obtained
970 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000971 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000972 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
973 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000974 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000975
976 // Make sure we can establish a context for the method.
977 if (!ClassDecl) {
978 Diag(MethodLoc, diag::error_missing_method_context);
979 return 0;
980 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000981 QualType resultDeclType;
982
983 if (ReturnType)
984 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
985 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000986 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000987
Chris Lattner114add62008-03-16 00:49:28 +0000988 ObjCMethodDecl* ObjCMethod =
989 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000990 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000991 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000992 false,
Chris Lattner114add62008-03-16 00:49:28 +0000993 MethodDeclKind == tok::objc_optional ?
994 ObjCMethodDecl::Optional :
995 ObjCMethodDecl::Required);
996
Chris Lattnereee57c02008-04-04 06:12:32 +0000997 llvm::SmallVector<ParmVarDecl*, 16> Params;
998
999 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1000 // FIXME: arg->AttrList must be stored too!
1001 QualType argType;
1002
1003 if (ArgTypes[i])
1004 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1005 else
1006 argType = Context.getObjCIdType();
1007 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1008 SourceLocation(/*FIXME*/),
1009 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001010 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +00001011 Param->setObjCDeclQualifier(
1012 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1013 Params.push_back(Param);
1014 }
1015
Ted Kremenek42730c52008-01-07 19:49:32 +00001016 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1017 ObjCMethod->setObjCDeclQualifier(
1018 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1019 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +00001020
1021 // For implementations (which can be very "coarse grain"), we add the
1022 // method now. This allows the AST to implement lookup methods that work
1023 // incrementally (without waiting until we parse the @end). It also allows
1024 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001025 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001026 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001027 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001028 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001029 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001030 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001031 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001032 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001033 }
1034 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001035 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001036 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001037 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001038 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001039 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001040 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001041 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001042 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001043 }
1044 }
1045 if (PrevMethod) {
1046 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001047 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1048 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001049 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1050 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001051 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001052}
1053
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001054Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1055 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001056 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001057 Selector GetterSel,
1058 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001059 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001060 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001061 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1062 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001063 // Regardless of setter/getter attribute, we save the default getter/setter
1064 // selector names in anticipation of declaration of setter/getter methods.
1065 PDecl->setGetterName(GetterSel);
1066 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001067
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001068 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001069 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001070
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001071 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001072 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001073
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001074 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001075 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001076
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001077 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001078 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001079
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001080 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001081 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001082
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001083 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001084 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001085
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001086 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001087 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001088
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001089 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001090 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001091
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001092 if (MethodImplKind == tok::objc_required)
1093 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1094 else if (MethodImplKind == tok::objc_optional)
1095 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1096
Chris Lattner855e51f2007-12-12 07:09:47 +00001097 return PDecl;
1098}
1099
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001100/// ActOnPropertyImplDecl - This routine performs semantic checks and
1101/// builds the AST node for a property implementation declaration; declared
1102/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001103///
1104Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1105 SourceLocation PropertyLoc,
1106 bool Synthesize,
1107 DeclTy *ClassCatImpDecl,
1108 IdentifierInfo *PropertyId,
1109 IdentifierInfo *PropertyIvar) {
1110 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1111 // Make sure we have a context for the property implementation declaration.
1112 if (!ClassImpDecl) {
1113 Diag(AtLoc, diag::error_missing_property_context);
1114 return 0;
1115 }
1116 ObjCPropertyDecl *property = 0;
1117 ObjCInterfaceDecl* IDecl = 0;
1118 // Find the class or category class where this property must have
1119 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001120 ObjCImplementationDecl *IC = 0;
1121 ObjCCategoryImplDecl* CatImplClass = 0;
1122 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001123 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001124 // We always synthesize an interface for an implementation
1125 // without an interface decl. So, IDecl is always non-zero.
1126 assert(IDecl &&
1127 "ActOnPropertyImplDecl - @implementation without @interface");
1128
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001129 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001130 property = IDecl->FindPropertyDeclaration(PropertyId);
1131 if (!property) {
1132 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001133 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001134 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001135 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001136 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001137 if (Synthesize) {
1138 Diag(AtLoc, diag::error_synthesize_category_decl);
1139 return 0;
1140 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001141 IDecl = CatImplClass->getClassInterface();
1142 if (!IDecl) {
1143 Diag(AtLoc, diag::error_missing_property_interface);
1144 return 0;
1145 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001146 ObjCCategoryDecl *Category =
1147 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1148
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001149 // If category for this implementation not found, it is an error which
1150 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001151 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001152 return 0;
1153 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001154 property = Category->FindPropertyDeclaration(PropertyId);
1155 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001156 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001157 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001158 return 0;
1159 }
1160 }
1161 else {
1162 Diag(AtLoc, diag::error_bad_property_context);
1163 return 0;
1164 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001165 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001166 // Check that we have a valid, previously declared ivar for @synthesize
1167 if (Synthesize) {
1168 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001169 if (!PropertyIvar)
1170 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001171 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001172 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001173 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001174 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1175 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001176 return 0;
1177 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001178 // Check that type of property and its ivar match.
1179 if (Ivar->getCanonicalType() != property->getCanonicalType()) {
1180 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1181 Ivar->getName());
1182 return 0;
1183 }
1184
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001185 } else if (PropertyIvar) {
1186 // @dynamic
1187 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1188 return 0;
1189 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001190 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001191 ObjCPropertyImplDecl *PIDecl =
1192 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1193 (Synthesize ?
1194 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1195 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1196 Ivar);
1197 if (IC)
1198 IC->addPropertyImplementation(PIDecl);
1199 else
1200 CatImplClass->addPropertyImplementation(PIDecl);
1201
1202 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001203}