blob: 79f1e2d682372724cc47194f9e525e4ee9a7d7d1 [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");
Steve Naroff3ac43f92008-07-25 17:57:26 +000025 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
26
27 // If we don't have a valid method decl, simply return.
28 if (!MDecl)
29 return;
Steve Narofffe9eb6a2007-12-18 01:30:32 +000030
31 // Allow the rest of sema to find private method decl implementations.
32 if (MDecl->isInstance())
33 AddInstanceMethodToGlobalPool(MDecl);
34 else
35 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000036
37 // Allow all of Sema to see that we are entering a method definition.
Chris Lattnerf3874bc2008-04-06 04:47:34 +000038 PushDeclContext(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000039
40 // Create Decl objects for each parameter, entrring them in the scope for
41 // binding to their use.
42 struct DeclaratorChunk::ParamInfo PI;
43
44 // Insert the invisible arguments, self and _cmd!
45 PI.Ident = &Context.Idents.get("self");
46 PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
Steve Naroff1a5027c2008-06-05 14:49:39 +000047 QualType selfTy;
Chris Lattner855e51f2007-12-12 07:09:47 +000048 if (MDecl->isInstance()) {
Steve Naroff1a5027c2008-06-05 14:49:39 +000049 selfTy = Context.getObjCIdType();
Gabor Greif9b2e0f92008-02-29 20:35:55 +000050 if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) {
51 // There may be no interface context due to error in declaration of the
52 // interface (which has been reported). Recover gracefully
Chris Lattner3e254fb2008-04-08 04:40:51 +000053 selfTy = Context.getObjCInterfaceType(OID);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000054 selfTy = Context.getPointerType(selfTy);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000055 }
Steve Naroff1a5027c2008-06-05 14:49:39 +000056 } else // we have a factory method.
57 selfTy = Context.getObjCClassType();
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000058 getCurMethodDecl()->setSelfDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner8c7c6a12008-06-17 18:05:57 +000059 PI.Ident, PI.IdentLoc, selfTy));
Chris Lattner855e51f2007-12-12 07:09:47 +000060
61 PI.Ident = &Context.Idents.get("_cmd");
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000062 getCurMethodDecl()->setCmdDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner8c7c6a12008-06-17 18:05:57 +000063 PI.Ident, PI.IdentLoc, Context.getObjCSelType()));
Chris Lattner3e254fb2008-04-08 04:40:51 +000064
Chris Lattner97316c02008-04-10 02:22:51 +000065 // Introduce all of the other parameters into this scope.
Chris Lattner685d7922008-03-16 01:07:14 +000066 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000067 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +000068 IdentifierInfo *II = PDecl->getIdentifier();
Argiris Kirtzidis43ce0be2008-04-27 13:30:35 +000069 if (II)
70 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000071 }
72}
73
Chris Lattnere705e5e2008-07-21 22:17:28 +000074Sema::DeclTy *Sema::
75ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
76 IdentifierInfo *ClassName, SourceLocation ClassLoc,
77 IdentifierInfo *SuperName, SourceLocation SuperLoc,
78 const IdentifierLocPair *ProtocolNames,
79 unsigned NumProtocols,
80 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000081 assert(ClassName && "Missing class identifier");
82
83 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +000084 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000085 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +000086 Diag(ClassLoc, diag::err_redefinition_different_kind,
87 ClassName->getName());
88 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
89 }
90
Ted Kremenek42730c52008-01-07 19:49:32 +000091 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000092 if (IDecl) {
93 // Class already seen. Is it a forward declaration?
94 if (!IDecl->isForwardDecl())
95 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
96 else {
97 IDecl->setLocation(AtInterfaceLoc);
98 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000099 }
Chris Lattner5cece462008-07-21 07:06:49 +0000100 } else {
101 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +0000102 ClassName, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000103
Steve Naroff15208162008-04-02 18:30:49 +0000104 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000105 // Remember that this needs to be removed when the scope is popped.
106 TUScope->AddDecl(IDecl);
107 }
108
109 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000111 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000112 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000113 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000114 Diag(SuperLoc, diag::err_redefinition_different_kind,
115 SuperName->getName());
116 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
117 }
118 else {
119 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000120 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000121
122 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Chris Lattner5cece462008-07-21 07:06:49 +0000123 Diag(SuperLoc, diag::err_undef_superclass,
Chris Lattner855e51f2007-12-12 07:09:47 +0000124 SuperClassEntry ? SuperClassEntry->getName()
125 : SuperName->getName(),
Chris Lattner5cece462008-07-21 07:06:49 +0000126 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc));
Chris Lattner855e51f2007-12-12 07:09:47 +0000127 }
128 }
129 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000130 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000131 IDecl->setLocEnd(SuperLoc);
132 } else { // we have a root class.
133 IDecl->setLocEnd(ClassLoc);
134 }
135
136 /// Check then save referenced protocols
137 if (NumProtocols) {
Chris Lattner5cece462008-07-21 07:06:49 +0000138 llvm::SmallVector<ObjCProtocolDecl*, 8> RefProtos;
Chris Lattner855e51f2007-12-12 07:09:47 +0000139 for (unsigned int i = 0; i != NumProtocols; i++) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000140 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i].first];
Chris Lattner8779c632008-07-21 18:34:02 +0000141 if (!RefPDecl)
Chris Lattnere705e5e2008-07-21 22:17:28 +0000142 Diag(ProtocolNames[i].second, diag::err_undef_protocolref,
143 ProtocolNames[i].first->getName(), ClassName->getName());
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000144 else {
145 if (RefPDecl->isForwardDecl())
Chris Lattnere705e5e2008-07-21 22:17:28 +0000146 Diag(ProtocolNames[i].second, diag::warn_undef_protocolref,
147 ProtocolNames[i].first->getName(), ClassName->getName());
Chris Lattner5cece462008-07-21 07:06:49 +0000148 RefProtos.push_back(RefPDecl);
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000149 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000150 }
Chris Lattner5cece462008-07-21 07:06:49 +0000151 if (!RefProtos.empty())
152 IDecl->addReferencedProtocols(&RefProtos[0], RefProtos.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000153 IDecl->setLocEnd(EndProtoLoc);
154 }
155 return IDecl;
156}
157
158/// ActOnCompatiblityAlias - this action is called after complete parsing of
159/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000160Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
161 IdentifierInfo *AliasName,
162 SourceLocation AliasLocation,
163 IdentifierInfo *ClassName,
164 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000165 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000166 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000167 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000168 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000169 Diag(AliasLocation, diag::warn_previous_alias_decl);
170 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
171 }
172 else {
173 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
174 AliasName->getName());
175 Diag(ADecl->getLocation(), diag::err_previous_declaration);
176 }
177 return 0;
178 }
179 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000180 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000181 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
182 if (CDecl == 0) {
183 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
184 if (CDeclU)
185 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000186 return 0;
187 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000188
189 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000190 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000191 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
192
193 ObjCAliasDecls[AliasName] = AliasDecl;
194 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000195 return AliasDecl;
196}
197
198Sema::DeclTy *Sema::ActOnStartProtocolInterface(
199 SourceLocation AtProtoInterfaceLoc,
200 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000201 const IdentifierLocPair *ProtoRefNames, unsigned NumProtoRefs,
Chris Lattner855e51f2007-12-12 07:09:47 +0000202 SourceLocation EndProtoLoc) {
203 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000204 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000205 if (PDecl) {
206 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000207 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000208 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
209 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000210 // Just return the protocol we already had.
211 // FIXME: don't leak the objects passed in!
212 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000213 }
Chris Lattnerc1881852008-03-16 01:25:17 +0000214
215 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000216 } else {
Chris Lattner0be08822008-07-21 21:32:27 +0000217 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000218 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000219 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000220 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000221
222 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000223 /// Check then save referenced protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000224 llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000225 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000226 ObjCProtocolDecl *RefPDecl = ObjCProtocols[ProtoRefNames[i].first];
Chris Lattner8779c632008-07-21 18:34:02 +0000227 if (!RefPDecl)
Chris Lattnere705e5e2008-07-21 22:17:28 +0000228 Diag(ProtoRefNames[i].second, diag::err_undef_protocolref,
229 ProtoRefNames[i].first->getName(), ProtocolName->getName());
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000230 else {
231 if (RefPDecl->isForwardDecl())
Chris Lattnere705e5e2008-07-21 22:17:28 +0000232 Diag(ProtoRefNames[i].second, diag::warn_undef_protocolref,
233 ProtoRefNames[i].first->getName(), ProtocolName->getName());
Chris Lattner0be08822008-07-21 21:32:27 +0000234 Protocols.push_back(RefPDecl);
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000235 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000236 }
Chris Lattner0be08822008-07-21 21:32:27 +0000237 if (!Protocols.empty())
238 PDecl->addReferencedProtocols(&Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000239 PDecl->setLocEnd(EndProtoLoc);
240 }
241 return PDecl;
242}
243
244/// FindProtocolDeclaration - This routine looks up protocols and
245/// issuer error if they are not declared. It returns list of protocol
246/// declarations in its 'Protocols' argument.
247void
248Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000249 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000250 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000251 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000252 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000253 if (ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first])
Chris Lattner855e51f2007-12-12 07:09:47 +0000254 Protocols.push_back(PDecl);
Chris Lattnere705e5e2008-07-21 22:17:28 +0000255 else
256 Diag(ProtocolId[i].second, diag::err_undeclared_protocol,
257 ProtocolId[i].first->getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000258 }
259}
260
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000261/// DiagnosePropertyMismatch - Compares two properties for their
262/// attributes and types and warns on a variety of inconsistancies.
263///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000264void
265Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
266 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000267 const char *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000268 ObjCPropertyDecl::PropertyAttributeKind CAttr =
269 Property->getPropertyAttributes();
270 ObjCPropertyDecl::PropertyAttributeKind SAttr =
271 SuperProperty->getPropertyAttributes();
272 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
273 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000274 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000275 Property->getName(), inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000276 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
277 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000278 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000279 Property->getName(), "copy", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000280 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000281 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
282 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000283 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000284 Property->getName(), "retain", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000285 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000286
287 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
288 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000289 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000290 Property->getName(), "atomic", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000291 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000292 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000293 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000294 Property->getName(), "setter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000295 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000296 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000297 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000298 Property->getName(), "getter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000299 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000300
Fariborz Jahanian513e30a2008-05-01 18:05:01 +0000301 if (Property->getCanonicalType() != SuperProperty->getCanonicalType())
302 Diag(Property->getLocation(), diag::warn_property_type,
303 Property->getType().getAsString(),
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000304 inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000305
306}
307
308/// ComparePropertiesInBaseAndSuper - This routine compares property
309/// declarations in base and its super class, if any, and issues
310/// diagnostics in a variety of inconsistant situations.
311///
312void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000313Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000314 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
315 if (!SDecl)
316 return;
Fariborz Jahanianed986602008-05-01 00:03:38 +0000317 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
318 E = SDecl->classprop_end(); S != E; ++S) {
319 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000320 // Does property in super class has declaration in current class?
321 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
322 E = IDecl->classprop_end(); I != E; ++I) {
323 ObjCPropertyDecl *PDecl = (*I);
324 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000325 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000326 }
327 }
328}
329
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000330/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
331/// of properties declared in a protocol and adds them to the list
332/// of properties for current class if it is not there already.
333void
334Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
335 ObjCProtocolDecl *PDecl)
336{
337 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
338 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
339 E = PDecl->classprop_end(); P != E; ++P) {
340 ObjCPropertyDecl *Pr = (*P);
341 ObjCInterfaceDecl::classprop_iterator CP, CE;
342 // Is this property already in class's list of properties?
343 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
344 CP != CE; ++CP)
345 if ((*CP)->getIdentifier() == Pr->getIdentifier())
346 break;
347 if (CP == CE)
348 // Add this property to list of properties for thie class.
349 mergeProperties.push_back(Pr);
350 else
351 // Property protocol already exist in class. Diagnose any mismatch.
352 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
353 }
354 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
355}
356
357/// MergeProtocolPropertiesIntoClass - This routine merges properties
358/// declared in 'MergeItsProtocols' objects (which can be a class or an
359/// inherited protocol into the list of properties for class 'IDecl'
360///
361
362void
363Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
364 DeclTy *MergeItsProtocols) {
365 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000366 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000367 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
368 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000369 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000370 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
371
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000372 // Go thru the list of protocols for this class and recursively merge
373 // their properties into this class as well.
374 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
375 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000376 MergeProtocolPropertiesIntoClass(IDecl, *P);
377 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000378 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
379 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
380 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000381 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000382 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000383}
384
Chris Lattner855e51f2007-12-12 07:09:47 +0000385/// ActOnForwardProtocolDeclaration -
386Action::DeclTy *
387Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000388 const IdentifierLocPair *IdentList,
389 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000390 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000391
392 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000393 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000394 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattnere705e5e2008-07-21 22:17:28 +0000395 if (PDecl == 0) // Not already seen?
396 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000397
398 Protocols.push_back(PDecl);
399 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000400 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
401 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000402}
403
Chris Lattnere705e5e2008-07-21 22:17:28 +0000404Sema::DeclTy *Sema::
405ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
406 IdentifierInfo *ClassName, SourceLocation ClassLoc,
407 IdentifierInfo *CategoryName,
408 SourceLocation CategoryLoc,
409 const IdentifierLocPair *ProtoRefNames,
410 unsigned NumProtoRefs,
411 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000412 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000413
Chris Lattnere29dc832008-03-16 20:34:23 +0000414 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000415 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000416 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000417
418 /// Check that class of this category is already completely declared.
419 if (!IDecl || IDecl->isForwardDecl())
420 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffac0580b2008-06-05 15:03:27 +0000421 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000422 /// Check for duplicate interface declaration for this category
423 ObjCCategoryDecl *CDeclChain;
424 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
425 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000426 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff0a7149f2008-06-05 04:33:44 +0000427 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000428 CategoryName->getName());
429 break;
430 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000431 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000432 if (!CDeclChain)
433 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000434 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000435
436 if (NumProtoRefs) {
Chris Lattner321b5d12008-03-16 20:47:45 +0000437 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
438 /// Check and then save the referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000439 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000440 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i].first];
Chris Lattner8779c632008-07-21 18:34:02 +0000441 if (!RefPDecl)
Chris Lattnere705e5e2008-07-21 22:17:28 +0000442 Diag(ProtoRefNames[i].second, diag::err_undef_protocolref,
443 ProtoRefNames[i].first->getName(), CategoryName->getName());
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000444 else {
445 if (RefPDecl->isForwardDecl())
Chris Lattnere705e5e2008-07-21 22:17:28 +0000446 Diag(ProtoRefNames[i].second, diag::warn_undef_protocolref,
447 ProtoRefNames[i].first->getName(), CategoryName->getName());
Chris Lattner321b5d12008-03-16 20:47:45 +0000448 RefProtocols.push_back(RefPDecl);
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000449 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000450 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000451 if (!RefProtocols.empty())
Chris Lattnerc72eaaf2008-07-21 17:23:15 +0000452 CDecl->addReferencedProtocols(&RefProtocols[0], RefProtocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000453 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000454 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000455 return CDecl;
456}
457
458/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000459/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000460/// object.
461Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
462 SourceLocation AtCatImplLoc,
463 IdentifierInfo *ClassName, SourceLocation ClassLoc,
464 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000465 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000466 ObjCCategoryImplDecl *CDecl =
467 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000468 /// Check that class of this category is already completely declared.
469 if (!IDecl || IDecl->isForwardDecl())
470 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
471
472 /// TODO: Check that CatName, category name, is not used in another
473 // implementation.
474 return CDecl;
475}
476
477Sema::DeclTy *Sema::ActOnStartClassImplementation(
478 SourceLocation AtClassImplLoc,
479 IdentifierInfo *ClassName, SourceLocation ClassLoc,
480 IdentifierInfo *SuperClassname,
481 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000482 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000483 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000484 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000485 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000486 Diag(ClassLoc, diag::err_redefinition_different_kind,
487 ClassName->getName());
488 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
489 }
490 else {
491 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000492 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000493 if (!IDecl)
494 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
495 }
496
497 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000498 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000499 if (SuperClassname) {
500 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000501 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000502 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000503 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
504 SuperClassname->getName());
505 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
506 }
507 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000508 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000509 if (!SDecl)
510 Diag(SuperClassLoc, diag::err_undef_superclass,
511 SuperClassname->getName(), ClassName->getName());
512 else if (IDecl && IDecl->getSuperClass() != SDecl) {
513 // This implementation and its interface do not have the same
514 // super class.
515 Diag(SuperClassLoc, diag::err_conflicting_super_class,
516 SDecl->getName());
517 Diag(SDecl->getLocation(), diag::err_previous_definition);
518 }
519 }
520 }
521
522 if (!IDecl) {
523 // Legacy case of @implementation with no corresponding @interface.
524 // Build, chain & install the interface decl into the identifier.
Chris Lattner5cece462008-07-21 07:06:49 +0000525 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000526 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000527 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000528 IDecl->setSuperClass(SDecl);
529 IDecl->setLocEnd(ClassLoc);
530
531 // Remember that this needs to be removed when the scope is popped.
532 TUScope->AddDecl(IDecl);
533 }
534
Ted Kremenek42730c52008-01-07 19:49:32 +0000535 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000536 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
537 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000538
539 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000540 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000541 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000542 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
543 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000544 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000545 return IMPDecl;
546}
547
Ted Kremenek42730c52008-01-07 19:49:32 +0000548void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
549 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000550 SourceLocation RBrace) {
551 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000552 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000553 if (!IDecl)
554 return;
555 /// Check case of non-existing @interface decl.
556 /// (legacy objective-c @implementation decl without an @interface decl).
557 /// Add implementations's ivar to the synthesize class's ivar list.
558 if (IDecl->ImplicitInterfaceDecl()) {
559 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
560 return;
561 }
562 // If implementation has empty ivar list, just return.
563 if (numIvars == 0)
564 return;
565
566 assert(ivars && "missing @implementation ivars");
567
568 // Check interface's Ivar list against those in the implementation.
569 // names and types must match.
570 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000571 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000572 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000573 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
574 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000575 ObjCIvarDecl* ImplIvar = ivars[j++];
576 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000577 assert (ImplIvar && "missing implementation ivar");
578 assert (ClsIvar && "missing class ivar");
579 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
580 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
581 ImplIvar->getIdentifier()->getName());
582 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
583 ClsIvar->getIdentifier()->getName());
584 }
585 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
586 // as error.
587 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
588 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
589 ImplIvar->getIdentifier()->getName());
590 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
591 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000592 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000593 }
594 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000595 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000596
597 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000598 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000599 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000600 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000601}
602
Steve Naroffb4f48512008-02-10 21:38:56 +0000603void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
604 bool &IncompleteImpl) {
605 if (!IncompleteImpl) {
606 Diag(ImpLoc, diag::warn_incomplete_impl);
607 IncompleteImpl = true;
608 }
609 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
610}
611
Steve Naroffb268d2a2008-02-08 22:06:17 +0000612/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000613/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000614void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
615 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000616 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000617 const llvm::DenseSet<Selector> &InsMap,
618 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000619 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000620 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000621 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000622 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000623 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000624 method->getImplementationControl() != ObjCMethodDecl::Optional)
625 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000626 }
627 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000628 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000629 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000630 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000631 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000632 method->getImplementationControl() != ObjCMethodDecl::Optional)
633 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000634 }
Chris Lattner0be08822008-07-21 21:32:27 +0000635 // Check on this protocols's referenced protocols, recursively.
636 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
637 E = PDecl->protocol_end(); PI != E; ++PI)
638 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000639}
640
Ted Kremenek42730c52008-01-07 19:49:32 +0000641void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
642 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000643 llvm::DenseSet<Selector> InsMap;
644 // Check and see if instance methods in class interface have been
645 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000646 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000647 E = IMPDecl->instmeth_end(); I != E; ++I)
648 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000649
650 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000651 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000652 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000653 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000654 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000655
Chris Lattner855e51f2007-12-12 07:09:47 +0000656 llvm::DenseSet<Selector> ClsMap;
657 // Check and see if class methods in class interface have been
658 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000659 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000660 E = IMPDecl->classmeth_end(); I != E; ++I)
661 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000662
Ted Kremenek42730c52008-01-07 19:49:32 +0000663 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000664 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000665 if (!ClsMap.count((*I)->getSelector()))
666 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000667
668 // Check the protocol list for unimplemented methods in the @implementation
669 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000670 const ObjCList<ObjCProtocolDecl> &Protocols =
671 IDecl->getReferencedProtocols();
672 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
673 E = Protocols.end(); I != E; ++I)
674 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000675 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000676}
677
678/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
679/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000680void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
681 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000682 llvm::DenseSet<Selector> InsMap;
683 // Check and see if instance methods in category interface have been
684 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000685 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000686 E = CatImplDecl->instmeth_end(); I != E; ++I)
687 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000688
689 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000690 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000691 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000692 if (!InsMap.count((*I)->getSelector()))
693 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
694
Chris Lattner855e51f2007-12-12 07:09:47 +0000695 llvm::DenseSet<Selector> ClsMap;
696 // Check and see if class methods in category interface have been
697 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000698 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000699 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
700 I != E; ++I)
701 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000702
Ted Kremenek42730c52008-01-07 19:49:32 +0000703 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000704 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000705 if (!ClsMap.count((*I)->getSelector()))
706 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000707
708 // Check the protocol list for unimplemented methods in the @implementation
709 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000710 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
711 E = CatClassDecl->protocol_end(); PI != E; ++PI)
712 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000713 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000714}
715
716/// ActOnForwardClassDeclaration -
717Action::DeclTy *
718Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
719 IdentifierInfo **IdentList, unsigned NumElts)
720{
Ted Kremenek42730c52008-01-07 19:49:32 +0000721 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000722
723 for (unsigned i = 0; i != NumElts; ++i) {
724 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000725 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000726 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000727 // GCC apparently allows the following idiom:
728 //
729 // typedef NSObject < XCElementTogglerP > XCElementToggler;
730 // @class XCElementToggler;
731 //
732 // FIXME: Make an extension?
733 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
734 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
735 Diag(AtClassLoc, diag::err_redefinition_different_kind,
736 IdentList[i]->getName());
737 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
738 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000739 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000740 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000741 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000742 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000743 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000744 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000745
746 // Remember that this needs to be removed when the scope is popped.
747 TUScope->AddDecl(IDecl);
748 }
749
750 Interfaces.push_back(IDecl);
751 }
752
Chris Lattnere29dc832008-03-16 20:34:23 +0000753 return ObjCClassDecl::Create(Context, AtClassLoc,
754 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000755}
756
757
758/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
759/// returns true, or false, accordingly.
760/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000761bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
762 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000763 if (Method->getResultType().getCanonicalType() !=
764 PrevMethod->getResultType().getCanonicalType())
765 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000766 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000767 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
768 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000769 if (Context.getCanonicalType(ParamDecl->getType()) !=
770 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000771 return false;
772 }
773 return true;
774}
775
Ted Kremenek42730c52008-01-07 19:49:32 +0000776void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
777 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000778 if (!FirstMethod.Method) {
779 // Haven't seen a method with this selector name yet - add it.
780 FirstMethod.Method = Method;
781 FirstMethod.Next = 0;
782 } else {
783 // We've seen a method with this name, now check the type signature(s).
784 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
785
Ted Kremenek42730c52008-01-07 19:49:32 +0000786 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000787 Next = Next->Next)
788 match = MatchTwoMethodDeclarations(Method, Next->Method);
789
790 if (!match) {
791 // We have a new signature for an existing method - add it.
792 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000793 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000794 FirstMethod.Next = OMI;
795 }
796 }
797}
798
Ted Kremenek42730c52008-01-07 19:49:32 +0000799void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
800 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000801 if (!FirstMethod.Method) {
802 // Haven't seen a method with this selector name yet - add it.
803 FirstMethod.Method = Method;
804 FirstMethod.Next = 0;
805 } else {
806 // We've seen a method with this name, now check the type signature(s).
807 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
808
Ted Kremenek42730c52008-01-07 19:49:32 +0000809 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000810 Next = Next->Next)
811 match = MatchTwoMethodDeclarations(Method, Next->Method);
812
813 if (!match) {
814 // We have a new signature for an existing method - add it.
815 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000816 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000817 FirstMethod.Next = OMI;
818 }
819 }
820}
821
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000822// Note: For class/category implemenations, allMethods/allProperties is
823// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000824void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
825 DeclTy **allMethods, unsigned allNum,
826 DeclTy **allProperties, unsigned pNum) {
827 Decl *ClassDecl = static_cast<Decl *>(classDecl);
828
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000829 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
830 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000831 // should be true.
832 if (!ClassDecl)
833 return;
834
Ted Kremenek42730c52008-01-07 19:49:32 +0000835 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
836 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000837
Ted Kremenek42730c52008-01-07 19:49:32 +0000838 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
839 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000840
841 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000842 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
843 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000844 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000845
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000846 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000847 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
848 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000849 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
850 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
851 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
852 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000853 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000854 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000855 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000856
857 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000858 ObjCMethodDecl *Method =
859 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000860
861 if (!Method) continue; // Already issued a diagnostic.
862 if (Method->isInstance()) {
863 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000864 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000865 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
866 : false;
867 if (isInterfaceDeclKind && PrevMethod && !match
868 || checkIdenticalMethods && match) {
869 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
870 Method->getSelector().getName());
871 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
872 } else {
873 insMethods.push_back(Method);
874 InsMap[Method->getSelector()] = Method;
875 /// The following allows us to typecheck messages to "id".
876 AddInstanceMethodToGlobalPool(Method);
877 }
878 }
879 else {
880 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000881 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000882 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
883 : false;
884 if (isInterfaceDeclKind && PrevMethod && !match
885 || checkIdenticalMethods && match) {
886 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
887 Method->getSelector().getName());
888 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
889 } else {
890 clsMethods.push_back(Method);
891 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000892 /// The following allows us to typecheck messages to "Class".
893 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000894 }
895 }
896 }
897
Ted Kremenek42730c52008-01-07 19:49:32 +0000898 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000899 // Compares properties declaraed in this class to those of its
900 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000901 ComparePropertiesInBaseAndSuper(I);
902 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000903 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
904 E = I->classprop_end(); P != E; ++P) {
Steve Naroff638d6a42008-05-22 23:24:08 +0000905 // FIXME: It would be really nice if we could avoid this. Injecting
906 // methods into the interface makes it hard to distinguish "real" methods
907 // from synthesized "property" methods (that aren't in the source).
908 // This complicicates the rewriter's life.
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000909 I->addPropertyMethods(Context, *P, insMethods);
910 }
911 I->addMethods(&insMethods[0], insMethods.size(),
912 &clsMethods[0], clsMethods.size(), AtEndLoc);
913
Ted Kremenek42730c52008-01-07 19:49:32 +0000914 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000915 P->addMethods(&insMethods[0], insMethods.size(),
916 &clsMethods[0], clsMethods.size(), AtEndLoc);
917 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000918 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000919 C->addMethods(&insMethods[0], insMethods.size(),
920 &clsMethods[0], clsMethods.size(), AtEndLoc);
921 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000922 else if (ObjCImplementationDecl *IC =
923 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000924 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000925 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000926 ImplMethodsVsClassMethods(IC, IDecl);
927 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000928 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000929 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000930 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000931 // Find category interface decl and then check that all methods declared
932 // in this interface is implemented in the category @implementation.
933 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000934 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000935 Categories; Categories = Categories->getNextClassCategory()) {
936 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
937 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
938 break;
939 }
940 }
941 }
942 }
943}
944
945
946/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
947/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000948static Decl::ObjCDeclQualifier
949CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
950 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
951 if (PQTVal & ObjCDeclSpec::DQ_In)
952 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
953 if (PQTVal & ObjCDeclSpec::DQ_Inout)
954 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
955 if (PQTVal & ObjCDeclSpec::DQ_Out)
956 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
957 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
958 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
959 if (PQTVal & ObjCDeclSpec::DQ_Byref)
960 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
961 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
962 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000963
964 return ret;
965}
966
967Sema::DeclTy *Sema::ActOnMethodDeclaration(
968 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000969 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000970 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000971 Selector Sel,
972 // optional arguments. The number of types/arguments is obtained
973 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000974 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000975 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
976 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000977 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000978
979 // Make sure we can establish a context for the method.
980 if (!ClassDecl) {
981 Diag(MethodLoc, diag::error_missing_method_context);
982 return 0;
983 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000984 QualType resultDeclType;
985
986 if (ReturnType)
987 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
988 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000989 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000990
Chris Lattner114add62008-03-16 00:49:28 +0000991 ObjCMethodDecl* ObjCMethod =
992 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000993 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000994 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000995 false,
Chris Lattner114add62008-03-16 00:49:28 +0000996 MethodDeclKind == tok::objc_optional ?
997 ObjCMethodDecl::Optional :
998 ObjCMethodDecl::Required);
999
Chris Lattnereee57c02008-04-04 06:12:32 +00001000 llvm::SmallVector<ParmVarDecl*, 16> Params;
1001
1002 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1003 // FIXME: arg->AttrList must be stored too!
1004 QualType argType;
1005
1006 if (ArgTypes[i])
1007 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1008 else
1009 argType = Context.getObjCIdType();
1010 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1011 SourceLocation(/*FIXME*/),
1012 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001013 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +00001014 Param->setObjCDeclQualifier(
1015 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1016 Params.push_back(Param);
1017 }
1018
Ted Kremenek42730c52008-01-07 19:49:32 +00001019 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1020 ObjCMethod->setObjCDeclQualifier(
1021 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1022 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +00001023
1024 // For implementations (which can be very "coarse grain"), we add the
1025 // method now. This allows the AST to implement lookup methods that work
1026 // incrementally (without waiting until we parse the @end). It also allows
1027 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001028 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001029 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001030 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001031 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001032 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001033 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001034 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001035 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001036 }
1037 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001038 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001039 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001040 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001041 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001042 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001043 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001044 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001045 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001046 }
1047 }
1048 if (PrevMethod) {
1049 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001050 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1051 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001052 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1053 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001054 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001055}
1056
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001057Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1058 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001059 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001060 Selector GetterSel,
1061 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001062 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001063 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001064 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1065 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001066 // Regardless of setter/getter attribute, we save the default getter/setter
1067 // selector names in anticipation of declaration of setter/getter methods.
1068 PDecl->setGetterName(GetterSel);
1069 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001070
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001071 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001072 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001073
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001074 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001075 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001076
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001077 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001078 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001079
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001080 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001081 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001082
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001083 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001084 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001085
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001086 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001087 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001088
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001089 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001090 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001091
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001092 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001093 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001094
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001095 if (MethodImplKind == tok::objc_required)
1096 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1097 else if (MethodImplKind == tok::objc_optional)
1098 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1099
Chris Lattner855e51f2007-12-12 07:09:47 +00001100 return PDecl;
1101}
1102
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001103/// ActOnPropertyImplDecl - This routine performs semantic checks and
1104/// builds the AST node for a property implementation declaration; declared
1105/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001106///
1107Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1108 SourceLocation PropertyLoc,
1109 bool Synthesize,
1110 DeclTy *ClassCatImpDecl,
1111 IdentifierInfo *PropertyId,
1112 IdentifierInfo *PropertyIvar) {
1113 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1114 // Make sure we have a context for the property implementation declaration.
1115 if (!ClassImpDecl) {
1116 Diag(AtLoc, diag::error_missing_property_context);
1117 return 0;
1118 }
1119 ObjCPropertyDecl *property = 0;
1120 ObjCInterfaceDecl* IDecl = 0;
1121 // Find the class or category class where this property must have
1122 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001123 ObjCImplementationDecl *IC = 0;
1124 ObjCCategoryImplDecl* CatImplClass = 0;
1125 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001126 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001127 // We always synthesize an interface for an implementation
1128 // without an interface decl. So, IDecl is always non-zero.
1129 assert(IDecl &&
1130 "ActOnPropertyImplDecl - @implementation without @interface");
1131
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001132 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001133 property = IDecl->FindPropertyDeclaration(PropertyId);
1134 if (!property) {
1135 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001136 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001137 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001138 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001139 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001140 if (Synthesize) {
1141 Diag(AtLoc, diag::error_synthesize_category_decl);
1142 return 0;
1143 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001144 IDecl = CatImplClass->getClassInterface();
1145 if (!IDecl) {
1146 Diag(AtLoc, diag::error_missing_property_interface);
1147 return 0;
1148 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001149 ObjCCategoryDecl *Category =
1150 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1151
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001152 // If category for this implementation not found, it is an error which
1153 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001154 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001155 return 0;
1156 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001157 property = Category->FindPropertyDeclaration(PropertyId);
1158 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001159 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001160 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001161 return 0;
1162 }
1163 }
1164 else {
1165 Diag(AtLoc, diag::error_bad_property_context);
1166 return 0;
1167 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001168 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001169 // Check that we have a valid, previously declared ivar for @synthesize
1170 if (Synthesize) {
1171 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001172 if (!PropertyIvar)
1173 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001174 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001175 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001176 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001177 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1178 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001179 return 0;
1180 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001181 // Check that type of property and its ivar match.
1182 if (Ivar->getCanonicalType() != property->getCanonicalType()) {
1183 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1184 Ivar->getName());
1185 return 0;
1186 }
1187
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001188 } else if (PropertyIvar) {
1189 // @dynamic
1190 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1191 return 0;
1192 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001193 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001194 ObjCPropertyImplDecl *PIDecl =
1195 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1196 (Synthesize ?
1197 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1198 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1199 Ivar);
1200 if (IC)
1201 IC->addPropertyImplementation(PIDecl);
1202 else
1203 CatImplClass->addPropertyImplementation(PIDecl);
1204
1205 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001206}