blob: 6b9b9e2d4312c759ef6d38c9e58057d6a7411514 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +000021/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000023void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Chris Lattner4d391482007-12-12 07:09:47 +000024 assert(CurFunctionDecl == 0 && "Method parsing confused");
Ted Kremeneka526c5c2008-01-07 19:49:32 +000025 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Chris Lattner4d391482007-12-12 07:09:47 +000026 assert(MDecl != 0 && "Not a method declarator!");
Steve Naroffa56f6162007-12-18 01:30:32 +000027
28 // Allow the rest of sema to find private method decl implementations.
29 if (MDecl->isInstance())
30 AddInstanceMethodToGlobalPool(MDecl);
31 else
32 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000033
34 // Allow all of Sema to see that we are entering a method definition.
35 CurMethodDecl = MDecl;
Chris Lattner0ed844b2008-04-04 06:12:32 +000036 PushContextDecl(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000037
38 // Create Decl objects for each parameter, entrring them in the scope for
39 // binding to their use.
40 struct DeclaratorChunk::ParamInfo PI;
41
42 // Insert the invisible arguments, self and _cmd!
43 PI.Ident = &Context.Idents.get("self");
44 PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
45 PI.InvalidType = false;
Nate Begemanb7894b52008-02-17 21:20:31 +000046 PI.AttrList = 0;
Gabor Greif30037532008-02-29 20:35:55 +000047 PI.TypeInfo = Context.getObjCIdType().getAsOpaquePtr();
48
Chris Lattner4d391482007-12-12 07:09:47 +000049 if (MDecl->isInstance()) {
Gabor Greif30037532008-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
Fariborz Jahanian20552d22008-01-10 20:33:58 +000053 QualType selfTy = Context.getObjCInterfaceType(OID);
54 selfTy = Context.getPointerType(selfTy);
55 PI.TypeInfo = selfTy.getAsOpaquePtr();
56 }
Gabor Greif30037532008-02-29 20:35:55 +000057 }
58
Chris Lattner4d391482007-12-12 07:09:47 +000059 CurMethodDecl->setSelfDecl(ActOnParamDeclarator(PI, FnBodyScope));
60
61 PI.Ident = &Context.Idents.get("_cmd");
Ted Kremeneka526c5c2008-01-07 19:49:32 +000062 PI.TypeInfo = Context.getObjCSelType().getAsOpaquePtr();
Chris Lattner4d391482007-12-12 07:09:47 +000063 ActOnParamDeclarator(PI, FnBodyScope);
64
Chris Lattner58cce3b2008-03-16 01:07:14 +000065 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +000066 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
67 PI.Ident = PDecl->getIdentifier();
68 PI.IdentLoc = PDecl->getLocation(); // user vars have a real location.
69 PI.TypeInfo = PDecl->getType().getAsOpaquePtr();
Fariborz Jahanian2338d582008-01-21 22:59:53 +000070 MDecl->setParamDecl(i, ActOnParamDeclarator(PI, FnBodyScope));
Chris Lattner4d391482007-12-12 07:09:47 +000071 }
72}
73
74Sema::DeclTy *Sema::ActOnStartClassInterface(
75 SourceLocation AtInterfaceLoc,
76 IdentifierInfo *ClassName, SourceLocation ClassLoc,
77 IdentifierInfo *SuperName, SourceLocation SuperLoc,
78 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
79 SourceLocation EndProtoLoc, AttributeList *AttrList) {
80 assert(ClassName && "Missing class identifier");
81
82 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +000083 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000084 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +000085 Diag(ClassLoc, diag::err_redefinition_different_kind,
86 ClassName->getName());
87 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
88 }
89
Ted Kremeneka526c5c2008-01-07 19:49:32 +000090 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000091 if (IDecl) {
92 // Class already seen. Is it a forward declaration?
93 if (!IDecl->isForwardDecl())
94 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
95 else {
96 IDecl->setLocation(AtInterfaceLoc);
97 IDecl->setForwardDecl(false);
98 IDecl->AllocIntfRefProtocols(NumProtocols);
99 }
100 }
101 else {
Chris Lattner0e77ba02008-03-16 01:15:50 +0000102 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, NumProtocols,
103 ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000104
Steve Naroff31102512008-04-02 18:30:49 +0000105 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000106 // Remember that this needs to be removed when the scope is popped.
107 TUScope->AddDecl(IDecl);
108 }
109
110 if (SuperName) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000111 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000112 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000113 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000114 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000115 Diag(SuperLoc, diag::err_redefinition_different_kind,
116 SuperName->getName());
117 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
118 }
119 else {
120 // Check that super class is previously defined
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000121 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000122
123 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
124 Diag(AtInterfaceLoc, diag::err_undef_superclass,
125 SuperClassEntry ? SuperClassEntry->getName()
126 : SuperName->getName(),
127 ClassName->getName());
128 }
129 }
130 IDecl->setSuperClass(SuperClassEntry);
131 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) {
138 for (unsigned int i = 0; i != NumProtocols; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000139 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000140 if (!RefPDecl || RefPDecl->isForwardDecl())
141 Diag(ClassLoc, diag::warn_undef_protocolref,
142 ProtocolNames[i]->getName(),
143 ClassName->getName());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000144 IDecl->setIntfRefProtocols(i, RefPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000145 }
146 IDecl->setLocEnd(EndProtoLoc);
147 }
148 return IDecl;
149}
150
151/// ActOnCompatiblityAlias - this action is called after complete parsing of
152/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000153Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
154 IdentifierInfo *AliasName,
155 SourceLocation AliasLocation,
156 IdentifierInfo *ClassName,
157 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000158 // Look for previous declaration of alias name
Steve Naroffb327ce02008-04-02 14:35:35 +0000159 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000160 if (ADecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000161 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000162 Diag(AliasLocation, diag::warn_previous_alias_decl);
163 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
164 }
165 else {
166 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
167 AliasName->getName());
168 Diag(ADecl->getLocation(), diag::err_previous_declaration);
169 }
170 return 0;
171 }
172 // Check for class declaration
Steve Naroffb327ce02008-04-02 14:35:35 +0000173 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000174 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
175 if (CDecl == 0) {
176 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
177 if (CDeclU)
178 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000179 return 0;
180 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000181
182 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000183 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe8043c32008-04-01 23:04:06 +0000184 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
185
186 ObjCAliasDecls[AliasName] = AliasDecl;
187 TUScope->AddDecl(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000188 return AliasDecl;
189}
190
191Sema::DeclTy *Sema::ActOnStartProtocolInterface(
192 SourceLocation AtProtoInterfaceLoc,
193 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
194 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
195 SourceLocation EndProtoLoc) {
196 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000197 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000198 if (PDecl) {
199 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000200 if (!PDecl->isForwardDecl()) {
Chris Lattner4d391482007-12-12 07:09:47 +0000201 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
202 ProtocolName->getName());
Chris Lattner439e71f2008-03-16 01:25:17 +0000203 // Just return the protocol we already had.
204 // FIXME: don't leak the objects passed in!
205 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000206 }
Chris Lattner439e71f2008-03-16 01:25:17 +0000207
208 PDecl->setForwardDecl(false);
209 PDecl->AllocReferencedProtocols(NumProtoRefs);
210 } else {
Chris Lattnercca59d72008-03-16 01:23:04 +0000211 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattnerc8581052008-03-16 20:19:15 +0000212 ProtocolName);
213 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000214 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000215 }
Chris Lattner4d391482007-12-12 07:09:47 +0000216
217 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000218 /// Check then save referenced protocols.
Chris Lattner4d391482007-12-12 07:09:47 +0000219 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000220 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000221 if (!RefPDecl || RefPDecl->isForwardDecl())
222 Diag(ProtocolLoc, diag::warn_undef_protocolref,
223 ProtoRefNames[i]->getName(),
224 ProtocolName->getName());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000225 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000226 }
227 PDecl->setLocEnd(EndProtoLoc);
228 }
229 return PDecl;
230}
231
232/// FindProtocolDeclaration - This routine looks up protocols and
233/// issuer error if they are not declared. It returns list of protocol
234/// declarations in its 'Protocols' argument.
235void
236Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
237 IdentifierInfo **ProtocolId,
238 unsigned NumProtocols,
239 llvm::SmallVector<DeclTy *,8> &Protocols) {
240 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000241 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000242 if (!PDecl)
243 Diag(TypeLoc, diag::err_undeclared_protocol,
244 ProtocolId[i]->getName());
245 else
246 Protocols.push_back(PDecl);
247 }
248}
249
250/// ActOnForwardProtocolDeclaration -
251Action::DeclTy *
252Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
253 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000254 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000255
256 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000257 IdentifierInfo *Ident = IdentList[i];
258 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
259 if (PDecl == 0) { // Not already seen?
Chris Lattner4d391482007-12-12 07:09:47 +0000260 // FIXME: Pass in the location of the identifier!
Chris Lattnerc8581052008-03-16 20:19:15 +0000261 PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
Chris Lattner4d391482007-12-12 07:09:47 +0000262 }
263
264 Protocols.push_back(PDecl);
265 }
Chris Lattner61f9d412008-03-16 20:34:23 +0000266 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
267 &Protocols[0], Protocols.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000268}
269
270Sema::DeclTy *Sema::ActOnStartCategoryInterface(
271 SourceLocation AtInterfaceLoc,
272 IdentifierInfo *ClassName, SourceLocation ClassLoc,
273 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
274 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
275 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000276 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000277
Chris Lattner61f9d412008-03-16 20:34:23 +0000278 ObjCCategoryDecl *CDecl =
Chris Lattner68c82cf2008-03-16 20:47:45 +0000279 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000280 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000281
282 /// Check that class of this category is already completely declared.
283 if (!IDecl || IDecl->isForwardDecl())
284 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
285 else {
286 /// Check for duplicate interface declaration for this category
287 ObjCCategoryDecl *CDeclChain;
288 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
289 CDeclChain = CDeclChain->getNextClassCategory()) {
290 if (CDeclChain->getIdentifier() == CategoryName) {
291 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
292 CategoryName->getName());
293 break;
294 }
Chris Lattner4d391482007-12-12 07:09:47 +0000295 }
Chris Lattner4d391482007-12-12 07:09:47 +0000296 if (!CDeclChain)
297 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000298 }
Chris Lattner4d391482007-12-12 07:09:47 +0000299
300 if (NumProtoRefs) {
Chris Lattner68c82cf2008-03-16 20:47:45 +0000301 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
302 /// Check and then save the referenced protocols.
Chris Lattner4d391482007-12-12 07:09:47 +0000303 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000304 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000305 if (!RefPDecl || RefPDecl->isForwardDecl()) {
306 Diag(CategoryLoc, diag::warn_undef_protocolref,
307 ProtoRefNames[i]->getName(),
308 CategoryName->getName());
309 }
Chris Lattner68c82cf2008-03-16 20:47:45 +0000310 if (RefPDecl)
311 RefProtocols.push_back(RefPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000312 }
Chris Lattner68c82cf2008-03-16 20:47:45 +0000313 if (!RefProtocols.empty())
314 CDecl->setReferencedProtocolList(&RefProtocols[0], RefProtocols.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000315 }
Chris Lattner68c82cf2008-03-16 20:47:45 +0000316 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000317 return CDecl;
318}
319
320/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000321/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000322/// object.
323Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
324 SourceLocation AtCatImplLoc,
325 IdentifierInfo *ClassName, SourceLocation ClassLoc,
326 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000327 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000328 ObjCCategoryImplDecl *CDecl =
329 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000330 /// Check that class of this category is already completely declared.
331 if (!IDecl || IDecl->isForwardDecl())
332 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
333
334 /// TODO: Check that CatName, category name, is not used in another
335 // implementation.
336 return CDecl;
337}
338
339Sema::DeclTy *Sema::ActOnStartClassImplementation(
340 SourceLocation AtClassImplLoc,
341 IdentifierInfo *ClassName, SourceLocation ClassLoc,
342 IdentifierInfo *SuperClassname,
343 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000344 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000345 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000346 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000347 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000348 Diag(ClassLoc, diag::err_redefinition_different_kind,
349 ClassName->getName());
350 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
351 }
352 else {
353 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000354 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000355 if (!IDecl)
356 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
357 }
358
359 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000360 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000361 if (SuperClassname) {
362 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000363 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000364 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000365 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
366 SuperClassname->getName());
367 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
368 }
369 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000370 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000371 if (!SDecl)
372 Diag(SuperClassLoc, diag::err_undef_superclass,
373 SuperClassname->getName(), ClassName->getName());
374 else if (IDecl && IDecl->getSuperClass() != SDecl) {
375 // This implementation and its interface do not have the same
376 // super class.
377 Diag(SuperClassLoc, diag::err_conflicting_super_class,
378 SDecl->getName());
379 Diag(SDecl->getLocation(), diag::err_previous_definition);
380 }
381 }
382 }
383
384 if (!IDecl) {
385 // Legacy case of @implementation with no corresponding @interface.
386 // Build, chain & install the interface decl into the identifier.
Chris Lattner0e77ba02008-03-16 01:15:50 +0000387 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName,
388 false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000389 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000390 IDecl->setSuperClass(SDecl);
391 IDecl->setLocEnd(ClassLoc);
392
393 // Remember that this needs to be removed when the scope is popped.
394 TUScope->AddDecl(IDecl);
395 }
396
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000397 ObjCImplementationDecl* IMPDecl =
Chris Lattner75c9cae2008-03-16 20:53:07 +0000398 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
399 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000400
401 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000402 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000403 // FIXME: Don't leak everything!
Chris Lattner4d391482007-12-12 07:09:47 +0000404 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
405 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000406 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000407 return IMPDecl;
408}
409
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000410void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
411 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000412 SourceLocation RBrace) {
413 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000414 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000415 if (!IDecl)
416 return;
417 /// Check case of non-existing @interface decl.
418 /// (legacy objective-c @implementation decl without an @interface decl).
419 /// Add implementations's ivar to the synthesize class's ivar list.
420 if (IDecl->ImplicitInterfaceDecl()) {
421 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
422 return;
423 }
424 // If implementation has empty ivar list, just return.
425 if (numIvars == 0)
426 return;
427
428 assert(ivars && "missing @implementation ivars");
429
430 // Check interface's Ivar list against those in the implementation.
431 // names and types must match.
432 //
Chris Lattner4d391482007-12-12 07:09:47 +0000433 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000434 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000435 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
436 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000437 ObjCIvarDecl* ImplIvar = ivars[j++];
438 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000439 assert (ImplIvar && "missing implementation ivar");
440 assert (ClsIvar && "missing class ivar");
441 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
442 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
443 ImplIvar->getIdentifier()->getName());
444 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
445 ClsIvar->getIdentifier()->getName());
446 }
447 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
448 // as error.
449 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
450 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
451 ImplIvar->getIdentifier()->getName());
452 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
453 ClsIvar->getIdentifier()->getName());
Chris Lattner609e4c72007-12-12 18:11:49 +0000454 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000455 }
456 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000457 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000458
459 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000460 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000461 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000462 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000463}
464
Steve Naroff3c2eb662008-02-10 21:38:56 +0000465void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
466 bool &IncompleteImpl) {
467 if (!IncompleteImpl) {
468 Diag(ImpLoc, diag::warn_incomplete_impl);
469 IncompleteImpl = true;
470 }
471 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
472}
473
Steve Naroffefe7f362008-02-08 22:06:17 +0000474/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000475/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000476void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
477 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000478 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000479 const llvm::DenseSet<Selector> &InsMap,
480 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner4d391482007-12-12 07:09:47 +0000481 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000482 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000483 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000484 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000485 if (!InsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000486 method->getImplementationControl() != ObjCMethodDecl::Optional)
487 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000488 }
489 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000490 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000491 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000492 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000493 if (!ClsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000494 method->getImplementationControl() != ObjCMethodDecl::Optional)
495 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000496 }
Chris Lattner4d391482007-12-12 07:09:47 +0000497 // Check on this protocols's referenced protocols, recursively
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000498 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000499 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffefe7f362008-02-08 22:06:17 +0000500 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000501}
502
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000503void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
504 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000505 llvm::DenseSet<Selector> InsMap;
506 // Check and see if instance methods in class interface have been
507 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000508 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000509 E = IMPDecl->instmeth_end(); I != E; ++I)
510 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000511
512 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000513 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000514 E = IDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000515 if (!InsMap.count((*I)->getSelector()))
516 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4c525092007-12-12 17:58:05 +0000517
Chris Lattner4d391482007-12-12 07:09:47 +0000518 llvm::DenseSet<Selector> ClsMap;
519 // Check and see if class methods in class interface have been
520 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000521 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000522 E = IMPDecl->classmeth_end(); I != E; ++I)
523 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000524
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000525 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000526 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000527 if (!ClsMap.count((*I)->getSelector()))
528 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000529
530 // Check the protocol list for unimplemented methods in the @implementation
531 // class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000532 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000533 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
Steve Naroffefe7f362008-02-08 22:06:17 +0000534 CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i],
535 IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000536}
537
538/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
539/// category interface is implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000540void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
541 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000542 llvm::DenseSet<Selector> InsMap;
543 // Check and see if instance methods in category interface have been
544 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000545 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000546 E = CatImplDecl->instmeth_end(); I != E; ++I)
547 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000548
549 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000550 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000551 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000552 if (!InsMap.count((*I)->getSelector()))
553 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
554
Chris Lattner4d391482007-12-12 07:09:47 +0000555 llvm::DenseSet<Selector> ClsMap;
556 // Check and see if class methods in category interface have been
557 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000558 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000559 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
560 I != E; ++I)
561 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000562
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000563 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000564 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000565 if (!ClsMap.count((*I)->getSelector()))
566 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000567
568 // Check the protocol list for unimplemented methods in the @implementation
569 // class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000570 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000571 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000572 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffefe7f362008-02-08 22:06:17 +0000573 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
574 InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000575 }
Chris Lattner4d391482007-12-12 07:09:47 +0000576}
577
578/// ActOnForwardClassDeclaration -
579Action::DeclTy *
580Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
581 IdentifierInfo **IdentList, unsigned NumElts)
582{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000583 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000584
585 for (unsigned i = 0; i != NumElts; ++i) {
586 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000587 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000588 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000589 Diag(AtClassLoc, diag::err_redefinition_different_kind,
590 IdentList[i]->getName());
591 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
592 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000593 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000594 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner0e77ba02008-03-16 01:15:50 +0000595 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, 0, IdentList[i],
596 true);
Steve Naroff31102512008-04-02 18:30:49 +0000597 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000598
599 // Remember that this needs to be removed when the scope is popped.
600 TUScope->AddDecl(IDecl);
601 }
602
603 Interfaces.push_back(IDecl);
604 }
605
Chris Lattner61f9d412008-03-16 20:34:23 +0000606 return ObjCClassDecl::Create(Context, AtClassLoc,
607 &Interfaces[0], Interfaces.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000608}
609
610
611/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
612/// returns true, or false, accordingly.
613/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000614bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
615 const ObjCMethodDecl *PrevMethod) {
Chris Lattner4d391482007-12-12 07:09:47 +0000616 if (Method->getResultType().getCanonicalType() !=
617 PrevMethod->getResultType().getCanonicalType())
618 return false;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000619 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +0000620 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
621 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
622 if (ParamDecl->getCanonicalType() != PrevParamDecl->getCanonicalType())
623 return false;
624 }
625 return true;
626}
627
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000628void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
629 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000630 if (!FirstMethod.Method) {
631 // Haven't seen a method with this selector name yet - add it.
632 FirstMethod.Method = Method;
633 FirstMethod.Next = 0;
634 } else {
635 // We've seen a method with this name, now check the type signature(s).
636 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
637
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000638 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000639 Next = Next->Next)
640 match = MatchTwoMethodDeclarations(Method, Next->Method);
641
642 if (!match) {
643 // We have a new signature for an existing method - add it.
644 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000645 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000646 FirstMethod.Next = OMI;
647 }
648 }
649}
650
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000651void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
652 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000653 if (!FirstMethod.Method) {
654 // Haven't seen a method with this selector name yet - add it.
655 FirstMethod.Method = Method;
656 FirstMethod.Next = 0;
657 } else {
658 // We've seen a method with this name, now check the type signature(s).
659 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
660
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000661 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000662 Next = Next->Next)
663 match = MatchTwoMethodDeclarations(Method, Next->Method);
664
665 if (!match) {
666 // We have a new signature for an existing method - add it.
667 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000668 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000669 FirstMethod.Next = OMI;
670 }
671 }
672}
673
Steve Naroffa56f6162007-12-18 01:30:32 +0000674// Note: For class/category implemenations, allMethods/allProperties is
675// always null.
Chris Lattner4d391482007-12-12 07:09:47 +0000676void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
677 DeclTy **allMethods, unsigned allNum,
678 DeclTy **allProperties, unsigned pNum) {
679 Decl *ClassDecl = static_cast<Decl *>(classDecl);
680
Steve Naroffa56f6162007-12-18 01:30:32 +0000681 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
682 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +0000683 // should be true.
684 if (!ClassDecl)
685 return;
686
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000687 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
688 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +0000689
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000690 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
691 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +0000692
693 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000694 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
695 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000696 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000697
698 // TODO: property declaration in category and protocols.
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000699 if (pNum != 0)
Chris Lattner55d13b42008-03-16 21:23:50 +0000700 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
701 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Chris Lattner4d391482007-12-12 07:09:47 +0000702
703 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000704 ObjCMethodDecl *Method =
705 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +0000706
707 if (!Method) continue; // Already issued a diagnostic.
708 if (Method->isInstance()) {
709 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000710 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000711 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
712 : false;
713 if (isInterfaceDeclKind && PrevMethod && !match
714 || checkIdenticalMethods && match) {
715 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
716 Method->getSelector().getName());
717 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
718 } else {
719 insMethods.push_back(Method);
720 InsMap[Method->getSelector()] = Method;
721 /// The following allows us to typecheck messages to "id".
722 AddInstanceMethodToGlobalPool(Method);
723 }
724 }
725 else {
726 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000727 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000728 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
729 : false;
730 if (isInterfaceDeclKind && PrevMethod && !match
731 || checkIdenticalMethods && match) {
732 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
733 Method->getSelector().getName());
734 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
735 } else {
736 clsMethods.push_back(Method);
737 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +0000738 /// The following allows us to typecheck messages to "Class".
739 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +0000740 }
741 }
742 }
743
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000744 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000745 I->addMethods(&insMethods[0], insMethods.size(),
746 &clsMethods[0], clsMethods.size(), AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000747 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000748 P->addMethods(&insMethods[0], insMethods.size(),
749 &clsMethods[0], clsMethods.size(), AtEndLoc);
750 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000751 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000752 C->addMethods(&insMethods[0], insMethods.size(),
753 &clsMethods[0], clsMethods.size(), AtEndLoc);
754 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000755 else if (ObjCImplementationDecl *IC =
756 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000757 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000758 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +0000759 ImplMethodsVsClassMethods(IC, IDecl);
760 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000761 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000762 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000763 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000764 // Find category interface decl and then check that all methods declared
765 // in this interface is implemented in the category @implementation.
766 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000767 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +0000768 Categories; Categories = Categories->getNextClassCategory()) {
769 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
770 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
771 break;
772 }
773 }
774 }
775 }
776}
777
778
779/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
780/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000781static Decl::ObjCDeclQualifier
782CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
783 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
784 if (PQTVal & ObjCDeclSpec::DQ_In)
785 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
786 if (PQTVal & ObjCDeclSpec::DQ_Inout)
787 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
788 if (PQTVal & ObjCDeclSpec::DQ_Out)
789 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
790 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
791 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
792 if (PQTVal & ObjCDeclSpec::DQ_Byref)
793 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
794 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
795 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +0000796
797 return ret;
798}
799
800Sema::DeclTy *Sema::ActOnMethodDeclaration(
801 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000802 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000803 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +0000804 Selector Sel,
805 // optional arguments. The number of types/arguments is obtained
806 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000807 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +0000808 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
809 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000810 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +0000811
812 // Make sure we can establish a context for the method.
813 if (!ClassDecl) {
814 Diag(MethodLoc, diag::error_missing_method_context);
815 return 0;
816 }
Chris Lattner4d391482007-12-12 07:09:47 +0000817 QualType resultDeclType;
818
819 if (ReturnType)
820 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
821 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000822 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +0000823
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000824 ObjCMethodDecl* ObjCMethod =
825 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerb06fa3b2008-03-16 00:58:16 +0000826 ClassDecl, AttrList,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000827 MethodType == tok::minus, isVariadic,
828 MethodDeclKind == tok::objc_optional ?
829 ObjCMethodDecl::Optional :
830 ObjCMethodDecl::Required);
831
Chris Lattner0ed844b2008-04-04 06:12:32 +0000832 llvm::SmallVector<ParmVarDecl*, 16> Params;
833
834 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
835 // FIXME: arg->AttrList must be stored too!
836 QualType argType;
837
838 if (ArgTypes[i])
839 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
840 else
841 argType = Context.getObjCIdType();
842 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
843 SourceLocation(/*FIXME*/),
844 ArgNames[i], argType,
845 VarDecl::None, 0);
846 Param->setObjCDeclQualifier(
847 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
848 Params.push_back(Param);
849 }
850
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000851 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
852 ObjCMethod->setObjCDeclQualifier(
853 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
854 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000855
856 // For implementations (which can be very "coarse grain"), we add the
857 // method now. This allows the AST to implement lookup methods that work
858 // incrementally (without waiting until we parse the @end). It also allows
859 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000860 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000861 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000862 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000863 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000864 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000865 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +0000866 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000867 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000868 }
869 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000870 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000871 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000872 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000873 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000874 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000875 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +0000876 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000877 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000878 }
879 }
880 if (PrevMethod) {
881 // You can never have two method definitions with the same name.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000882 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
883 ObjCMethod->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +0000884 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
885 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000886 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +0000887}
888
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000889Sema::DeclTy *Sema::ActOnAddObjCProperties(SourceLocation AtLoc,
Chris Lattnerf4af5152008-03-17 01:19:02 +0000890 DeclTy **allProperties,
891 unsigned NumProperties,
892 ObjCDeclSpec &DS) {
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000893 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000894
Chris Lattnerf4af5152008-03-17 01:19:02 +0000895 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000896 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +0000897
Chris Lattnerf4af5152008-03-17 01:19:02 +0000898 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000899 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +0000900 PDecl->setGetterName(DS.getGetterName());
901 }
902
Chris Lattnerf4af5152008-03-17 01:19:02 +0000903 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000904 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +0000905 PDecl->setSetterName(DS.getSetterName());
906 }
907
Chris Lattnerf4af5152008-03-17 01:19:02 +0000908 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000909 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner4d391482007-12-12 07:09:47 +0000910
Chris Lattnerf4af5152008-03-17 01:19:02 +0000911 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000912 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +0000913
Chris Lattnerf4af5152008-03-17 01:19:02 +0000914 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000915 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +0000916
Chris Lattnerf4af5152008-03-17 01:19:02 +0000917 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +0000919
Chris Lattnerf4af5152008-03-17 01:19:02 +0000920 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000921 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +0000922
Chris Lattnerf4af5152008-03-17 01:19:02 +0000923 if (NumProperties != 0)
924 PDecl->setPropertyDeclLists((ObjCIvarDecl**)allProperties, NumProperties);
925
Chris Lattner4d391482007-12-12 07:09:47 +0000926 return PDecl;
927}
928