blob: 9d2869be8a77ce6cf181b1d10a817aa9b258ab51 [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 Lattnerfc513a62008-07-26 03:40:25 +0000142 Diag(ProtocolNames[i].second, diag::err_undeclared_protocol,
143 ProtocolNames[i].first->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,
Chris Lattner17d50a92008-07-26 03:47:43 +0000147 ProtocolNames[i].first->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 Lattnerfc513a62008-07-26 03:40:25 +0000228 Diag(ProtoRefNames[i].second, diag::err_undeclared_protocol,
229 ProtoRefNames[i].first->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,
Chris Lattner17d50a92008-07-26 03:47:43 +0000233 ProtoRefNames[i].first->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
Chris Lattner17d50a92008-07-26 03:47:43 +0000248Sema::FindProtocolDeclaration(SourceLocation TypeLoc, bool WarnOnDeclarations,
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 Lattner17d50a92008-07-26 03:47:43 +0000253 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
254 if (!PDecl) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000255 Diag(ProtocolId[i].second, diag::err_undeclared_protocol,
256 ProtocolId[i].first->getName());
Chris Lattner17d50a92008-07-26 03:47:43 +0000257 continue;
258 }
259
260 // If this is a forward declaration and we are supposed to warn in this
261 // case, do it.
262 if (WarnOnDeclarations && PDecl->isForwardDecl())
263 Diag(ProtocolId[i].second, diag::warn_undef_protocolref,
264 ProtocolId[i].first->getName());
265 Protocols.push_back(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000266 }
267}
268
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000269/// DiagnosePropertyMismatch - Compares two properties for their
270/// attributes and types and warns on a variety of inconsistancies.
271///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000272void
273Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
274 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000275 const char *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000276 ObjCPropertyDecl::PropertyAttributeKind CAttr =
277 Property->getPropertyAttributes();
278 ObjCPropertyDecl::PropertyAttributeKind SAttr =
279 SuperProperty->getPropertyAttributes();
280 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
281 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000282 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000283 Property->getName(), inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000284 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
285 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
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(), "copy", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000288 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000289 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
290 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000291 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000292 Property->getName(), "retain", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000293 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000294
295 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
296 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
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(), "atomic", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000299 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000300 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000301 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000302 Property->getName(), "setter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000303 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000304 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000305 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000306 Property->getName(), "getter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000307 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000308
Fariborz Jahanian513e30a2008-05-01 18:05:01 +0000309 if (Property->getCanonicalType() != SuperProperty->getCanonicalType())
310 Diag(Property->getLocation(), diag::warn_property_type,
311 Property->getType().getAsString(),
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000312 inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000313
314}
315
316/// ComparePropertiesInBaseAndSuper - This routine compares property
317/// declarations in base and its super class, if any, and issues
318/// diagnostics in a variety of inconsistant situations.
319///
320void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000321Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000322 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
323 if (!SDecl)
324 return;
Fariborz Jahanianed986602008-05-01 00:03:38 +0000325 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
326 E = SDecl->classprop_end(); S != E; ++S) {
327 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000328 // Does property in super class has declaration in current class?
329 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
330 E = IDecl->classprop_end(); I != E; ++I) {
331 ObjCPropertyDecl *PDecl = (*I);
332 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000333 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000334 }
335 }
336}
337
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000338/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
339/// of properties declared in a protocol and adds them to the list
340/// of properties for current class if it is not there already.
341void
342Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
343 ObjCProtocolDecl *PDecl)
344{
345 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
346 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
347 E = PDecl->classprop_end(); P != E; ++P) {
348 ObjCPropertyDecl *Pr = (*P);
349 ObjCInterfaceDecl::classprop_iterator CP, CE;
350 // Is this property already in class's list of properties?
351 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
352 CP != CE; ++CP)
353 if ((*CP)->getIdentifier() == Pr->getIdentifier())
354 break;
355 if (CP == CE)
356 // Add this property to list of properties for thie class.
357 mergeProperties.push_back(Pr);
358 else
359 // Property protocol already exist in class. Diagnose any mismatch.
360 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
361 }
362 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
363}
364
365/// MergeProtocolPropertiesIntoClass - This routine merges properties
366/// declared in 'MergeItsProtocols' objects (which can be a class or an
367/// inherited protocol into the list of properties for class 'IDecl'
368///
369
370void
371Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
372 DeclTy *MergeItsProtocols) {
373 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000374 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000375 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
376 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000377 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000378 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
379
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000380 // Go thru the list of protocols for this class and recursively merge
381 // their properties into this class as well.
382 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
383 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000384 MergeProtocolPropertiesIntoClass(IDecl, *P);
385 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000386 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
387 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
388 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000389 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000390 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000391}
392
Chris Lattner855e51f2007-12-12 07:09:47 +0000393/// ActOnForwardProtocolDeclaration -
394Action::DeclTy *
395Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000396 const IdentifierLocPair *IdentList,
397 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000398 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000399
400 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000401 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000402 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattnere705e5e2008-07-21 22:17:28 +0000403 if (PDecl == 0) // Not already seen?
404 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000405
406 Protocols.push_back(PDecl);
407 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000408 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
409 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000410}
411
Chris Lattnere705e5e2008-07-21 22:17:28 +0000412Sema::DeclTy *Sema::
413ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
414 IdentifierInfo *ClassName, SourceLocation ClassLoc,
415 IdentifierInfo *CategoryName,
416 SourceLocation CategoryLoc,
417 const IdentifierLocPair *ProtoRefNames,
418 unsigned NumProtoRefs,
419 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000420 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000421
Chris Lattnere29dc832008-03-16 20:34:23 +0000422 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000423 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000424 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000425
426 /// Check that class of this category is already completely declared.
427 if (!IDecl || IDecl->isForwardDecl())
428 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffac0580b2008-06-05 15:03:27 +0000429 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000430 /// Check for duplicate interface declaration for this category
431 ObjCCategoryDecl *CDeclChain;
432 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
433 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000434 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff0a7149f2008-06-05 04:33:44 +0000435 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000436 CategoryName->getName());
437 break;
438 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000439 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000440 if (!CDeclChain)
441 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000442 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000443
444 if (NumProtoRefs) {
Chris Lattner321b5d12008-03-16 20:47:45 +0000445 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
446 /// Check and then save the referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000447 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000448 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i].first];
Chris Lattner8779c632008-07-21 18:34:02 +0000449 if (!RefPDecl)
Chris Lattnerfc513a62008-07-26 03:40:25 +0000450 Diag(ProtoRefNames[i].second, diag::err_undeclared_protocol,
451 ProtoRefNames[i].first->getName());
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000452 else {
453 if (RefPDecl->isForwardDecl())
Chris Lattnere705e5e2008-07-21 22:17:28 +0000454 Diag(ProtoRefNames[i].second, diag::warn_undef_protocolref,
Chris Lattner17d50a92008-07-26 03:47:43 +0000455 ProtoRefNames[i].first->getName());
Chris Lattner321b5d12008-03-16 20:47:45 +0000456 RefProtocols.push_back(RefPDecl);
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000457 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000458 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000459 if (!RefProtocols.empty())
Chris Lattnerc72eaaf2008-07-21 17:23:15 +0000460 CDecl->addReferencedProtocols(&RefProtocols[0], RefProtocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000461 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000462 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000463 return CDecl;
464}
465
466/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000467/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000468/// object.
469Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
470 SourceLocation AtCatImplLoc,
471 IdentifierInfo *ClassName, SourceLocation ClassLoc,
472 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000473 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000474 ObjCCategoryImplDecl *CDecl =
475 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000476 /// Check that class of this category is already completely declared.
477 if (!IDecl || IDecl->isForwardDecl())
478 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
479
480 /// TODO: Check that CatName, category name, is not used in another
481 // implementation.
482 return CDecl;
483}
484
485Sema::DeclTy *Sema::ActOnStartClassImplementation(
486 SourceLocation AtClassImplLoc,
487 IdentifierInfo *ClassName, SourceLocation ClassLoc,
488 IdentifierInfo *SuperClassname,
489 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000490 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000491 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000492 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000493 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000494 Diag(ClassLoc, diag::err_redefinition_different_kind,
495 ClassName->getName());
496 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
497 }
498 else {
499 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000500 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000501 if (!IDecl)
502 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
503 }
504
505 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000506 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000507 if (SuperClassname) {
508 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000509 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000510 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000511 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
512 SuperClassname->getName());
513 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
514 }
515 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000516 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000517 if (!SDecl)
518 Diag(SuperClassLoc, diag::err_undef_superclass,
519 SuperClassname->getName(), ClassName->getName());
520 else if (IDecl && IDecl->getSuperClass() != SDecl) {
521 // This implementation and its interface do not have the same
522 // super class.
523 Diag(SuperClassLoc, diag::err_conflicting_super_class,
524 SDecl->getName());
525 Diag(SDecl->getLocation(), diag::err_previous_definition);
526 }
527 }
528 }
529
530 if (!IDecl) {
531 // Legacy case of @implementation with no corresponding @interface.
532 // Build, chain & install the interface decl into the identifier.
Chris Lattner5cece462008-07-21 07:06:49 +0000533 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000534 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000535 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000536 IDecl->setSuperClass(SDecl);
537 IDecl->setLocEnd(ClassLoc);
538
539 // Remember that this needs to be removed when the scope is popped.
540 TUScope->AddDecl(IDecl);
541 }
542
Ted Kremenek42730c52008-01-07 19:49:32 +0000543 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000544 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
545 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000546
547 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000548 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000549 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000550 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
551 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000552 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000553 return IMPDecl;
554}
555
Ted Kremenek42730c52008-01-07 19:49:32 +0000556void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
557 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000558 SourceLocation RBrace) {
559 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000560 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000561 if (!IDecl)
562 return;
563 /// Check case of non-existing @interface decl.
564 /// (legacy objective-c @implementation decl without an @interface decl).
565 /// Add implementations's ivar to the synthesize class's ivar list.
566 if (IDecl->ImplicitInterfaceDecl()) {
567 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
568 return;
569 }
570 // If implementation has empty ivar list, just return.
571 if (numIvars == 0)
572 return;
573
574 assert(ivars && "missing @implementation ivars");
575
576 // Check interface's Ivar list against those in the implementation.
577 // names and types must match.
578 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000579 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000580 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000581 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
582 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000583 ObjCIvarDecl* ImplIvar = ivars[j++];
584 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000585 assert (ImplIvar && "missing implementation ivar");
586 assert (ClsIvar && "missing class ivar");
587 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
588 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
589 ImplIvar->getIdentifier()->getName());
590 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
591 ClsIvar->getIdentifier()->getName());
592 }
593 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
594 // as error.
595 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
596 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
597 ImplIvar->getIdentifier()->getName());
598 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
599 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000600 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000601 }
602 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000603 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000604
605 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000606 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000607 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000608 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000609}
610
Steve Naroffb4f48512008-02-10 21:38:56 +0000611void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
612 bool &IncompleteImpl) {
613 if (!IncompleteImpl) {
614 Diag(ImpLoc, diag::warn_incomplete_impl);
615 IncompleteImpl = true;
616 }
617 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
618}
619
Steve Naroffb268d2a2008-02-08 22:06:17 +0000620/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000621/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000622void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
623 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000624 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000625 const llvm::DenseSet<Selector> &InsMap,
626 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000627 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000628 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000629 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000630 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000631 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000632 method->getImplementationControl() != ObjCMethodDecl::Optional)
633 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000634 }
635 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000636 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000637 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000638 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000639 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000640 method->getImplementationControl() != ObjCMethodDecl::Optional)
641 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000642 }
Chris Lattner0be08822008-07-21 21:32:27 +0000643 // Check on this protocols's referenced protocols, recursively.
644 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
645 E = PDecl->protocol_end(); PI != E; ++PI)
646 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000647}
648
Ted Kremenek42730c52008-01-07 19:49:32 +0000649void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
650 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000651 llvm::DenseSet<Selector> InsMap;
652 // Check and see if instance methods in class interface have been
653 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000654 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000655 E = IMPDecl->instmeth_end(); I != E; ++I)
656 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000657
658 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000659 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000660 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000661 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000662 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000663
Chris Lattner855e51f2007-12-12 07:09:47 +0000664 llvm::DenseSet<Selector> ClsMap;
665 // Check and see if class methods in class interface have been
666 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000667 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000668 E = IMPDecl->classmeth_end(); I != E; ++I)
669 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000670
Ted Kremenek42730c52008-01-07 19:49:32 +0000671 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000672 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000673 if (!ClsMap.count((*I)->getSelector()))
674 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000675
676 // Check the protocol list for unimplemented methods in the @implementation
677 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000678 const ObjCList<ObjCProtocolDecl> &Protocols =
679 IDecl->getReferencedProtocols();
680 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
681 E = Protocols.end(); I != E; ++I)
682 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000683 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000684}
685
686/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
687/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000688void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
689 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000690 llvm::DenseSet<Selector> InsMap;
691 // Check and see if instance methods in category interface have been
692 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000693 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000694 E = CatImplDecl->instmeth_end(); I != E; ++I)
695 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000696
697 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000698 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000699 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000700 if (!InsMap.count((*I)->getSelector()))
701 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
702
Chris Lattner855e51f2007-12-12 07:09:47 +0000703 llvm::DenseSet<Selector> ClsMap;
704 // Check and see if class methods in category interface have been
705 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000706 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000707 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
708 I != E; ++I)
709 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000710
Ted Kremenek42730c52008-01-07 19:49:32 +0000711 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000712 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000713 if (!ClsMap.count((*I)->getSelector()))
714 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000715
716 // Check the protocol list for unimplemented methods in the @implementation
717 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000718 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
719 E = CatClassDecl->protocol_end(); PI != E; ++PI)
720 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000721 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000722}
723
724/// ActOnForwardClassDeclaration -
725Action::DeclTy *
726Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
727 IdentifierInfo **IdentList, unsigned NumElts)
728{
Ted Kremenek42730c52008-01-07 19:49:32 +0000729 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000730
731 for (unsigned i = 0; i != NumElts; ++i) {
732 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000733 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000734 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000735 // GCC apparently allows the following idiom:
736 //
737 // typedef NSObject < XCElementTogglerP > XCElementToggler;
738 // @class XCElementToggler;
739 //
740 // FIXME: Make an extension?
741 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
742 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
743 Diag(AtClassLoc, diag::err_redefinition_different_kind,
744 IdentList[i]->getName());
745 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
746 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000747 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000748 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000749 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000750 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000751 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000752 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000753
754 // Remember that this needs to be removed when the scope is popped.
755 TUScope->AddDecl(IDecl);
756 }
757
758 Interfaces.push_back(IDecl);
759 }
760
Chris Lattnere29dc832008-03-16 20:34:23 +0000761 return ObjCClassDecl::Create(Context, AtClassLoc,
762 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000763}
764
765
766/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
767/// returns true, or false, accordingly.
768/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000769bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
770 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000771 if (Method->getResultType().getCanonicalType() !=
772 PrevMethod->getResultType().getCanonicalType())
773 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000774 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000775 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
776 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000777 if (Context.getCanonicalType(ParamDecl->getType()) !=
778 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000779 return false;
780 }
781 return true;
782}
783
Ted Kremenek42730c52008-01-07 19:49:32 +0000784void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
785 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000786 if (!FirstMethod.Method) {
787 // Haven't seen a method with this selector name yet - add it.
788 FirstMethod.Method = Method;
789 FirstMethod.Next = 0;
790 } else {
791 // We've seen a method with this name, now check the type signature(s).
792 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
793
Ted Kremenek42730c52008-01-07 19:49:32 +0000794 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000795 Next = Next->Next)
796 match = MatchTwoMethodDeclarations(Method, Next->Method);
797
798 if (!match) {
799 // We have a new signature for an existing method - add it.
800 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000801 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000802 FirstMethod.Next = OMI;
803 }
804 }
805}
806
Ted Kremenek42730c52008-01-07 19:49:32 +0000807void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
808 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000809 if (!FirstMethod.Method) {
810 // Haven't seen a method with this selector name yet - add it.
811 FirstMethod.Method = Method;
812 FirstMethod.Next = 0;
813 } else {
814 // We've seen a method with this name, now check the type signature(s).
815 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
816
Ted Kremenek42730c52008-01-07 19:49:32 +0000817 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000818 Next = Next->Next)
819 match = MatchTwoMethodDeclarations(Method, Next->Method);
820
821 if (!match) {
822 // We have a new signature for an existing method - add it.
823 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000824 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000825 FirstMethod.Next = OMI;
826 }
827 }
828}
829
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000830// Note: For class/category implemenations, allMethods/allProperties is
831// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000832void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
833 DeclTy **allMethods, unsigned allNum,
834 DeclTy **allProperties, unsigned pNum) {
835 Decl *ClassDecl = static_cast<Decl *>(classDecl);
836
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000837 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
838 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000839 // should be true.
840 if (!ClassDecl)
841 return;
842
Ted Kremenek42730c52008-01-07 19:49:32 +0000843 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
844 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000845
Ted Kremenek42730c52008-01-07 19:49:32 +0000846 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
847 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000848
849 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000850 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
851 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000852 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000853
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000854 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000855 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
856 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000857 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
858 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
859 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
860 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000861 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000862 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000863 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000864
865 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000866 ObjCMethodDecl *Method =
867 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000868
869 if (!Method) continue; // Already issued a diagnostic.
870 if (Method->isInstance()) {
871 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000872 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000873 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
874 : false;
875 if (isInterfaceDeclKind && PrevMethod && !match
876 || checkIdenticalMethods && match) {
877 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
878 Method->getSelector().getName());
879 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
880 } else {
881 insMethods.push_back(Method);
882 InsMap[Method->getSelector()] = Method;
883 /// The following allows us to typecheck messages to "id".
884 AddInstanceMethodToGlobalPool(Method);
885 }
886 }
887 else {
888 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000889 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000890 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
891 : false;
892 if (isInterfaceDeclKind && PrevMethod && !match
893 || checkIdenticalMethods && match) {
894 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
895 Method->getSelector().getName());
896 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
897 } else {
898 clsMethods.push_back(Method);
899 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000900 /// The following allows us to typecheck messages to "Class".
901 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000902 }
903 }
904 }
905
Ted Kremenek42730c52008-01-07 19:49:32 +0000906 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000907 // Compares properties declaraed in this class to those of its
908 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000909 ComparePropertiesInBaseAndSuper(I);
910 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000911 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
912 E = I->classprop_end(); P != E; ++P) {
Steve Naroff638d6a42008-05-22 23:24:08 +0000913 // FIXME: It would be really nice if we could avoid this. Injecting
914 // methods into the interface makes it hard to distinguish "real" methods
915 // from synthesized "property" methods (that aren't in the source).
916 // This complicicates the rewriter's life.
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000917 I->addPropertyMethods(Context, *P, insMethods);
918 }
919 I->addMethods(&insMethods[0], insMethods.size(),
920 &clsMethods[0], clsMethods.size(), AtEndLoc);
921
Ted Kremenek42730c52008-01-07 19:49:32 +0000922 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000923 P->addMethods(&insMethods[0], insMethods.size(),
924 &clsMethods[0], clsMethods.size(), AtEndLoc);
925 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000926 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000927 C->addMethods(&insMethods[0], insMethods.size(),
928 &clsMethods[0], clsMethods.size(), AtEndLoc);
929 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000930 else if (ObjCImplementationDecl *IC =
931 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000932 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000933 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000934 ImplMethodsVsClassMethods(IC, IDecl);
935 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000936 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000937 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000938 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000939 // Find category interface decl and then check that all methods declared
940 // in this interface is implemented in the category @implementation.
941 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000942 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000943 Categories; Categories = Categories->getNextClassCategory()) {
944 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
945 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
946 break;
947 }
948 }
949 }
950 }
951}
952
953
954/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
955/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000956static Decl::ObjCDeclQualifier
957CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
958 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
959 if (PQTVal & ObjCDeclSpec::DQ_In)
960 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
961 if (PQTVal & ObjCDeclSpec::DQ_Inout)
962 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
963 if (PQTVal & ObjCDeclSpec::DQ_Out)
964 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
965 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
966 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
967 if (PQTVal & ObjCDeclSpec::DQ_Byref)
968 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
969 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
970 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000971
972 return ret;
973}
974
975Sema::DeclTy *Sema::ActOnMethodDeclaration(
976 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000977 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000978 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000979 Selector Sel,
980 // optional arguments. The number of types/arguments is obtained
981 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000982 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000983 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
984 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000985 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000986
987 // Make sure we can establish a context for the method.
988 if (!ClassDecl) {
989 Diag(MethodLoc, diag::error_missing_method_context);
990 return 0;
991 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000992 QualType resultDeclType;
993
994 if (ReturnType)
995 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
996 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000997 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000998
Chris Lattner114add62008-03-16 00:49:28 +0000999 ObjCMethodDecl* ObjCMethod =
1000 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +00001001 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +00001002 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001003 false,
Chris Lattner114add62008-03-16 00:49:28 +00001004 MethodDeclKind == tok::objc_optional ?
1005 ObjCMethodDecl::Optional :
1006 ObjCMethodDecl::Required);
1007
Chris Lattnereee57c02008-04-04 06:12:32 +00001008 llvm::SmallVector<ParmVarDecl*, 16> Params;
1009
1010 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1011 // FIXME: arg->AttrList must be stored too!
1012 QualType argType;
1013
1014 if (ArgTypes[i])
1015 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1016 else
1017 argType = Context.getObjCIdType();
1018 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1019 SourceLocation(/*FIXME*/),
1020 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001021 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +00001022 Param->setObjCDeclQualifier(
1023 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1024 Params.push_back(Param);
1025 }
1026
Ted Kremenek42730c52008-01-07 19:49:32 +00001027 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1028 ObjCMethod->setObjCDeclQualifier(
1029 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1030 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +00001031
1032 // For implementations (which can be very "coarse grain"), we add the
1033 // method now. This allows the AST to implement lookup methods that work
1034 // incrementally (without waiting until we parse the @end). It also allows
1035 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001036 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001037 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001038 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001039 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001040 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001041 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001042 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001043 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001044 }
1045 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001046 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001047 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001048 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001049 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001050 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001051 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001052 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001053 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001054 }
1055 }
1056 if (PrevMethod) {
1057 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001058 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1059 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001060 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1061 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001062 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001063}
1064
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001065Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1066 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001067 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001068 Selector GetterSel,
1069 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001070 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001071 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001072 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1073 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001074 // Regardless of setter/getter attribute, we save the default getter/setter
1075 // selector names in anticipation of declaration of setter/getter methods.
1076 PDecl->setGetterName(GetterSel);
1077 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001078
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001079 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001080 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001081
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001082 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001083 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001084
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001085 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001086 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001087
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001088 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001089 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001090
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001091 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001092 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001093
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001094 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001095 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001096
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001097 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001098 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001099
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001100 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001101 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001102
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001103 if (MethodImplKind == tok::objc_required)
1104 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1105 else if (MethodImplKind == tok::objc_optional)
1106 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1107
Chris Lattner855e51f2007-12-12 07:09:47 +00001108 return PDecl;
1109}
1110
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001111/// ActOnPropertyImplDecl - This routine performs semantic checks and
1112/// builds the AST node for a property implementation declaration; declared
1113/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001114///
1115Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1116 SourceLocation PropertyLoc,
1117 bool Synthesize,
1118 DeclTy *ClassCatImpDecl,
1119 IdentifierInfo *PropertyId,
1120 IdentifierInfo *PropertyIvar) {
1121 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1122 // Make sure we have a context for the property implementation declaration.
1123 if (!ClassImpDecl) {
1124 Diag(AtLoc, diag::error_missing_property_context);
1125 return 0;
1126 }
1127 ObjCPropertyDecl *property = 0;
1128 ObjCInterfaceDecl* IDecl = 0;
1129 // Find the class or category class where this property must have
1130 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001131 ObjCImplementationDecl *IC = 0;
1132 ObjCCategoryImplDecl* CatImplClass = 0;
1133 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001134 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001135 // We always synthesize an interface for an implementation
1136 // without an interface decl. So, IDecl is always non-zero.
1137 assert(IDecl &&
1138 "ActOnPropertyImplDecl - @implementation without @interface");
1139
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001140 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001141 property = IDecl->FindPropertyDeclaration(PropertyId);
1142 if (!property) {
1143 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001144 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001145 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001146 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001147 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001148 if (Synthesize) {
1149 Diag(AtLoc, diag::error_synthesize_category_decl);
1150 return 0;
1151 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001152 IDecl = CatImplClass->getClassInterface();
1153 if (!IDecl) {
1154 Diag(AtLoc, diag::error_missing_property_interface);
1155 return 0;
1156 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001157 ObjCCategoryDecl *Category =
1158 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1159
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001160 // If category for this implementation not found, it is an error which
1161 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001162 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001163 return 0;
1164 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001165 property = Category->FindPropertyDeclaration(PropertyId);
1166 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001167 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001168 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001169 return 0;
1170 }
1171 }
1172 else {
1173 Diag(AtLoc, diag::error_bad_property_context);
1174 return 0;
1175 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001176 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001177 // Check that we have a valid, previously declared ivar for @synthesize
1178 if (Synthesize) {
1179 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001180 if (!PropertyIvar)
1181 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001182 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001183 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001184 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001185 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1186 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001187 return 0;
1188 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001189 // Check that type of property and its ivar match.
1190 if (Ivar->getCanonicalType() != property->getCanonicalType()) {
1191 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1192 Ivar->getName());
1193 return 0;
1194 }
1195
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001196 } else if (PropertyIvar) {
1197 // @dynamic
1198 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1199 return 0;
1200 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001201 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001202 ObjCPropertyImplDecl *PIDecl =
1203 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1204 (Synthesize ?
1205 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1206 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1207 Ivar);
1208 if (IC)
1209 IC->addPropertyImplementation(PIDecl);
1210 else
1211 CatImplClass->addPropertyImplementation(PIDecl);
1212
1213 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001214}