blob: d5d658d4ff1c3d97e77fdbae0ec63fbedd06f4c1 [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) {
Chris Lattner855e51f2007-12-12 07:09:47 +000024 assert(CurFunctionDecl == 0 && "Method parsing confused");
Ted Kremenek42730c52008-01-07 19:49:32 +000025 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Chris Lattner855e51f2007-12-12 07:09:47 +000026 assert(MDecl != 0 && "Not a method declarator!");
Steve Narofffe9eb6a2007-12-18 01:30:32 +000027
28 // Allow the rest of sema to find private method decl implementations.
29 if (MDecl->isInstance())
30 AddInstanceMethodToGlobalPool(MDecl);
31 else
32 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000033
34 // Allow all of Sema to see that we are entering a method definition.
35 CurMethodDecl = MDecl;
36
37 // Create Decl objects for each parameter, entrring them in the scope for
38 // binding to their use.
39 struct DeclaratorChunk::ParamInfo PI;
40
41 // Insert the invisible arguments, self and _cmd!
42 PI.Ident = &Context.Idents.get("self");
43 PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
44 PI.InvalidType = false;
Nate Begeman9f3c4bb2008-02-17 21:20:31 +000045 PI.AttrList = 0;
Gabor Greif9b2e0f92008-02-29 20:35:55 +000046 PI.TypeInfo = Context.getObjCIdType().getAsOpaquePtr();
47
Chris Lattner855e51f2007-12-12 07:09:47 +000048 if (MDecl->isInstance()) {
Gabor Greif9b2e0f92008-02-29 20:35:55 +000049 if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) {
50 // There may be no interface context due to error in declaration of the
51 // interface (which has been reported). Recover gracefully
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000052 QualType selfTy = Context.getObjCInterfaceType(OID);
53 selfTy = Context.getPointerType(selfTy);
54 PI.TypeInfo = selfTy.getAsOpaquePtr();
55 }
Gabor Greif9b2e0f92008-02-29 20:35:55 +000056 }
57
Chris Lattner855e51f2007-12-12 07:09:47 +000058 CurMethodDecl->setSelfDecl(ActOnParamDeclarator(PI, FnBodyScope));
59
60 PI.Ident = &Context.Idents.get("_cmd");
Ted Kremenek42730c52008-01-07 19:49:32 +000061 PI.TypeInfo = Context.getObjCSelType().getAsOpaquePtr();
Chris Lattner855e51f2007-12-12 07:09:47 +000062 ActOnParamDeclarator(PI, FnBodyScope);
63
Chris Lattner685d7922008-03-16 01:07:14 +000064 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000065 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
66 PI.Ident = PDecl->getIdentifier();
67 PI.IdentLoc = PDecl->getLocation(); // user vars have a real location.
68 PI.TypeInfo = PDecl->getType().getAsOpaquePtr();
Fariborz Jahanian00a313b2008-01-21 22:59:53 +000069 MDecl->setParamDecl(i, ActOnParamDeclarator(PI, FnBodyScope));
Chris Lattner855e51f2007-12-12 07:09:47 +000070 }
71}
72
73Sema::DeclTy *Sema::ActOnStartClassInterface(
74 SourceLocation AtInterfaceLoc,
75 IdentifierInfo *ClassName, SourceLocation ClassLoc,
76 IdentifierInfo *SuperName, SourceLocation SuperLoc,
77 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
78 SourceLocation EndProtoLoc, AttributeList *AttrList) {
79 assert(ClassName && "Missing class identifier");
80
81 // Check for another declaration kind with the same name.
82 ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName);
Ted Kremenek42730c52008-01-07 19:49:32 +000083 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +000084 Diag(ClassLoc, diag::err_redefinition_different_kind,
85 ClassName->getName());
86 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
87 }
88
Ted Kremenek42730c52008-01-07 19:49:32 +000089 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000090 if (IDecl) {
91 // Class already seen. Is it a forward declaration?
92 if (!IDecl->isForwardDecl())
93 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
94 else {
95 IDecl->setLocation(AtInterfaceLoc);
96 IDecl->setForwardDecl(false);
97 IDecl->AllocIntfRefProtocols(NumProtocols);
98 }
99 }
100 else {
Chris Lattner0db541b2008-03-16 01:15:50 +0000101 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, NumProtocols,
102 ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000103
104 // Chain & install the interface decl into the identifier.
105 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
106 ClassName->setFETokenInfo(IDecl);
107
108 // Remember that this needs to be removed when the scope is popped.
109 TUScope->AddDecl(IDecl);
110 }
111
112 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000113 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000114 // Check if a different kind of symbol declared in this scope.
115 PrevDecl = LookupInterfaceDecl(SuperName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000116 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000117 Diag(SuperLoc, diag::err_redefinition_different_kind,
118 SuperName->getName());
119 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
120 }
121 else {
122 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000123 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000124
125 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
126 Diag(AtInterfaceLoc, diag::err_undef_superclass,
127 SuperClassEntry ? SuperClassEntry->getName()
128 : SuperName->getName(),
129 ClassName->getName());
130 }
131 }
132 IDecl->setSuperClass(SuperClassEntry);
133 IDecl->setLocEnd(SuperLoc);
134 } else { // we have a root class.
135 IDecl->setLocEnd(ClassLoc);
136 }
137
138 /// Check then save referenced protocols
139 if (NumProtocols) {
140 for (unsigned int i = 0; i != NumProtocols; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000141 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000142 if (!RefPDecl || RefPDecl->isForwardDecl())
143 Diag(ClassLoc, diag::warn_undef_protocolref,
144 ProtocolNames[i]->getName(),
145 ClassName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000146 IDecl->setIntfRefProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000147 }
148 IDecl->setLocEnd(EndProtoLoc);
149 }
150 return IDecl;
151}
152
153/// ActOnCompatiblityAlias - this action is called after complete parsing of
154/// @compaatibility_alias declaration. It sets up the alias relationships.
155Sema::DeclTy *Sema::ActOnCompatiblityAlias(
156 SourceLocation AtCompatibilityAliasLoc,
157 IdentifierInfo *AliasName, SourceLocation AliasLocation,
158 IdentifierInfo *ClassName, SourceLocation ClassLocation) {
159 // Look for previous declaration of alias name
160 ScopedDecl *ADecl = LookupScopedDecl(AliasName, Decl::IDNS_Ordinary,
161 AliasLocation, TUScope);
162 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000163 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000164 Diag(AliasLocation, diag::warn_previous_alias_decl);
165 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
166 }
167 else {
168 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
169 AliasName->getName());
170 Diag(ADecl->getLocation(), diag::err_previous_declaration);
171 }
172 return 0;
173 }
174 // Check for class declaration
175 ScopedDecl *CDecl = LookupScopedDecl(ClassName, Decl::IDNS_Ordinary,
176 ClassLocation, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000177 if (!CDecl || !isa<ObjCInterfaceDecl>(CDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000178 Diag(ClassLocation, diag::warn_undef_interface,
179 ClassName->getName());
180 if (CDecl)
181 Diag(CDecl->getLocation(), diag::warn_previous_declaration);
182 return 0;
183 }
184 // Everything checked out, instantiate a new alias declaration ast
Ted Kremenek42730c52008-01-07 19:49:32 +0000185 ObjCCompatibleAliasDecl *AliasDecl =
186 new ObjCCompatibleAliasDecl(AtCompatibilityAliasLoc,
Chris Lattner855e51f2007-12-12 07:09:47 +0000187 AliasName,
Ted Kremenek42730c52008-01-07 19:49:32 +0000188 dyn_cast<ObjCInterfaceDecl>(CDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000189
190 // Chain & install the interface decl into the identifier.
191 AliasDecl->setNext(AliasName->getFETokenInfo<ScopedDecl>());
192 AliasName->setFETokenInfo(AliasDecl);
193 return AliasDecl;
194}
195
196Sema::DeclTy *Sema::ActOnStartProtocolInterface(
197 SourceLocation AtProtoInterfaceLoc,
198 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
199 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
200 SourceLocation EndProtoLoc) {
201 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000202 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000203 if (PDecl) {
204 // Protocol already seen. Better be a forward protocol declaration
205 if (!PDecl->isForwardDecl())
206 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
207 ProtocolName->getName());
208 else {
209 PDecl->setForwardDecl(false);
210 PDecl->AllocReferencedProtocols(NumProtoRefs);
211 }
212 }
213 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000214 PDecl = new ObjCProtocolDecl(AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattner855e51f2007-12-12 07:09:47 +0000215 ProtocolName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000216 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000217 }
218
219 if (NumProtoRefs) {
220 /// Check then save referenced protocols
221 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000222 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000223 if (!RefPDecl || RefPDecl->isForwardDecl())
224 Diag(ProtocolLoc, diag::warn_undef_protocolref,
225 ProtoRefNames[i]->getName(),
226 ProtocolName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000227 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000228 }
229 PDecl->setLocEnd(EndProtoLoc);
230 }
231 return PDecl;
232}
233
234/// FindProtocolDeclaration - This routine looks up protocols and
235/// issuer error if they are not declared. It returns list of protocol
236/// declarations in its 'Protocols' argument.
237void
238Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
239 IdentifierInfo **ProtocolId,
240 unsigned NumProtocols,
241 llvm::SmallVector<DeclTy *,8> &Protocols) {
242 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000243 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000244 if (!PDecl)
245 Diag(TypeLoc, diag::err_undeclared_protocol,
246 ProtocolId[i]->getName());
247 else
248 Protocols.push_back(PDecl);
249 }
250}
251
252/// ActOnForwardProtocolDeclaration -
253Action::DeclTy *
254Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
255 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000256 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000257
258 for (unsigned i = 0; i != NumElts; ++i) {
259 IdentifierInfo *P = IdentList[i];
Ted Kremenek42730c52008-01-07 19:49:32 +0000260 ObjCProtocolDecl *PDecl = ObjCProtocols[P];
Chris Lattner855e51f2007-12-12 07:09:47 +0000261 if (!PDecl) { // Not already seen?
262 // FIXME: Pass in the location of the identifier!
Ted Kremenek42730c52008-01-07 19:49:32 +0000263 PDecl = new ObjCProtocolDecl(AtProtocolLoc, 0, P, true);
264 ObjCProtocols[P] = PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000265 }
266
267 Protocols.push_back(PDecl);
268 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000269 return new ObjCForwardProtocolDecl(AtProtocolLoc,
Chris Lattner855e51f2007-12-12 07:09:47 +0000270 &Protocols[0], Protocols.size());
271}
272
273Sema::DeclTy *Sema::ActOnStartCategoryInterface(
274 SourceLocation AtInterfaceLoc,
275 IdentifierInfo *ClassName, SourceLocation ClassLoc,
276 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
277 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
278 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000279 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000280
Ted Kremenek42730c52008-01-07 19:49:32 +0000281 ObjCCategoryDecl *CDecl = new ObjCCategoryDecl(AtInterfaceLoc, NumProtoRefs,
Chris Lattner855e51f2007-12-12 07:09:47 +0000282 CategoryName);
283 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000284
285 /// Check that class of this category is already completely declared.
286 if (!IDecl || IDecl->isForwardDecl())
287 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
288 else {
289 /// Check for duplicate interface declaration for this category
290 ObjCCategoryDecl *CDeclChain;
291 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
292 CDeclChain = CDeclChain->getNextClassCategory()) {
293 if (CDeclChain->getIdentifier() == CategoryName) {
294 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
295 CategoryName->getName());
296 break;
297 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000298 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000299 if (!CDeclChain)
300 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000301 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000302
303 if (NumProtoRefs) {
304 /// Check then save referenced protocols
305 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000306 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000307 if (!RefPDecl || RefPDecl->isForwardDecl()) {
308 Diag(CategoryLoc, diag::warn_undef_protocolref,
309 ProtoRefNames[i]->getName(),
310 CategoryName->getName());
311 }
Fariborz Jahanian87829072007-12-20 19:24:10 +0000312 CDecl->setCatReferencedProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000313 }
314 CDecl->setLocEnd(EndProtoLoc);
315 }
316 return CDecl;
317}
318
319/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000320/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000321/// object.
322Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
323 SourceLocation AtCatImplLoc,
324 IdentifierInfo *ClassName, SourceLocation ClassLoc,
325 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000326 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
327 ObjCCategoryImplDecl *CDecl = new ObjCCategoryImplDecl(AtCatImplLoc,
Chris Lattner855e51f2007-12-12 07:09:47 +0000328 CatName, IDecl);
329 /// Check that class of this category is already completely declared.
330 if (!IDecl || IDecl->isForwardDecl())
331 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
332
333 /// TODO: Check that CatName, category name, is not used in another
334 // implementation.
335 return CDecl;
336}
337
338Sema::DeclTy *Sema::ActOnStartClassImplementation(
339 SourceLocation AtClassImplLoc,
340 IdentifierInfo *ClassName, SourceLocation ClassLoc,
341 IdentifierInfo *SuperClassname,
342 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000343 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000344 // Check for another declaration kind with the same name.
345 ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000346 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000347 Diag(ClassLoc, diag::err_redefinition_different_kind,
348 ClassName->getName());
349 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
350 }
351 else {
352 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000353 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000354 if (!IDecl)
355 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
356 }
357
358 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000359 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000360 if (SuperClassname) {
361 // Check if a different kind of symbol declared in this scope.
362 PrevDecl = LookupInterfaceDecl(SuperClassname);
Ted Kremenek42730c52008-01-07 19:49:32 +0000363 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000364 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
365 SuperClassname->getName());
366 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
367 }
368 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000369 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000370 if (!SDecl)
371 Diag(SuperClassLoc, diag::err_undef_superclass,
372 SuperClassname->getName(), ClassName->getName());
373 else if (IDecl && IDecl->getSuperClass() != SDecl) {
374 // This implementation and its interface do not have the same
375 // super class.
376 Diag(SuperClassLoc, diag::err_conflicting_super_class,
377 SDecl->getName());
378 Diag(SDecl->getLocation(), diag::err_previous_definition);
379 }
380 }
381 }
382
383 if (!IDecl) {
384 // Legacy case of @implementation with no corresponding @interface.
385 // Build, chain & install the interface decl into the identifier.
Chris Lattner0db541b2008-03-16 01:15:50 +0000386 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName,
387 false, true);
Chris Lattner855e51f2007-12-12 07:09:47 +0000388 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
389 ClassName->setFETokenInfo(IDecl);
390 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 Kremenek42730c52008-01-07 19:49:32 +0000397 ObjCImplementationDecl* IMPDecl =
398 new ObjCImplementationDecl(AtClassImplLoc, ClassName, IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000399
400 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000401 if (ObjCImplementations[ClassName])
Chris Lattner855e51f2007-12-12 07:09:47 +0000402 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
403 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000404 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000405 return IMPDecl;
406}
407
Ted Kremenek42730c52008-01-07 19:49:32 +0000408void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
409 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000410 SourceLocation RBrace) {
411 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000412 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000413 if (!IDecl)
414 return;
415 /// Check case of non-existing @interface decl.
416 /// (legacy objective-c @implementation decl without an @interface decl).
417 /// Add implementations's ivar to the synthesize class's ivar list.
418 if (IDecl->ImplicitInterfaceDecl()) {
419 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
420 return;
421 }
422 // If implementation has empty ivar list, just return.
423 if (numIvars == 0)
424 return;
425
426 assert(ivars && "missing @implementation ivars");
427
428 // Check interface's Ivar list against those in the implementation.
429 // names and types must match.
430 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000431 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000432 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000433 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
434 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000435 ObjCIvarDecl* ImplIvar = ivars[j++];
436 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000437 assert (ImplIvar && "missing implementation ivar");
438 assert (ClsIvar && "missing class ivar");
439 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
440 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
441 ImplIvar->getIdentifier()->getName());
442 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
443 ClsIvar->getIdentifier()->getName());
444 }
445 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
446 // as error.
447 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
448 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
449 ImplIvar->getIdentifier()->getName());
450 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
451 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000452 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000453 }
454 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000455 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000456
457 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000458 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000459 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000460 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000461}
462
Steve Naroffb4f48512008-02-10 21:38:56 +0000463void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
464 bool &IncompleteImpl) {
465 if (!IncompleteImpl) {
466 Diag(ImpLoc, diag::warn_incomplete_impl);
467 IncompleteImpl = true;
468 }
469 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
470}
471
Steve Naroffb268d2a2008-02-08 22:06:17 +0000472/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000473/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000474void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
475 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000476 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000477 const llvm::DenseSet<Selector> &InsMap,
478 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000479 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000480 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000481 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000482 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000483 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000484 method->getImplementationControl() != ObjCMethodDecl::Optional)
485 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000486 }
487 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000488 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000489 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000490 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000491 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000492 method->getImplementationControl() != ObjCMethodDecl::Optional)
493 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000494 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000495 // Check on this protocols's referenced protocols, recursively
Ted Kremenek42730c52008-01-07 19:49:32 +0000496 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000497 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000498 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000499}
500
Ted Kremenek42730c52008-01-07 19:49:32 +0000501void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
502 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000503 llvm::DenseSet<Selector> InsMap;
504 // Check and see if instance methods in class interface have been
505 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000506 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000507 E = IMPDecl->instmeth_end(); I != E; ++I)
508 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000509
510 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000511 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000512 E = IDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000513 if (!InsMap.count((*I)->getSelector()))
514 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000515
Chris Lattner855e51f2007-12-12 07:09:47 +0000516 llvm::DenseSet<Selector> ClsMap;
517 // Check and see if class methods in class interface have been
518 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000519 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000520 E = IMPDecl->classmeth_end(); I != E; ++I)
521 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000522
Ted Kremenek42730c52008-01-07 19:49:32 +0000523 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000524 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000525 if (!ClsMap.count((*I)->getSelector()))
526 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000527
528 // Check the protocol list for unimplemented methods in the @implementation
529 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000530 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000531 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000532 CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i],
533 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000534}
535
536/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
537/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000538void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
539 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000540 llvm::DenseSet<Selector> InsMap;
541 // Check and see if instance methods in category interface have been
542 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000543 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000544 E = CatImplDecl->instmeth_end(); I != E; ++I)
545 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000546
547 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000548 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000549 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000550 if (!InsMap.count((*I)->getSelector()))
551 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
552
Chris Lattner855e51f2007-12-12 07:09:47 +0000553 llvm::DenseSet<Selector> ClsMap;
554 // Check and see if class methods in category interface have been
555 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000556 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000557 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
558 I != E; ++I)
559 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000560
Ted Kremenek42730c52008-01-07 19:49:32 +0000561 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000562 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000563 if (!ClsMap.count((*I)->getSelector()))
564 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000565
566 // Check the protocol list for unimplemented methods in the @implementation
567 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000568 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000569 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000570 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffb268d2a2008-02-08 22:06:17 +0000571 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
572 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000573 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000574}
575
576/// ActOnForwardClassDeclaration -
577Action::DeclTy *
578Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
579 IdentifierInfo **IdentList, unsigned NumElts)
580{
Ted Kremenek42730c52008-01-07 19:49:32 +0000581 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000582
583 for (unsigned i = 0; i != NumElts; ++i) {
584 // Check for another declaration kind with the same name.
585 ScopedDecl *PrevDecl = LookupInterfaceDecl(IdentList[i]);
Ted Kremenek42730c52008-01-07 19:49:32 +0000586 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000587 Diag(AtClassLoc, diag::err_redefinition_different_kind,
588 IdentList[i]->getName());
589 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
590 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000591 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000592 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner0db541b2008-03-16 01:15:50 +0000593 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, 0, IdentList[i],
594 true);
Chris Lattner855e51f2007-12-12 07:09:47 +0000595 // Chain & install the interface decl into the identifier.
596 IDecl->setNext(IdentList[i]->getFETokenInfo<ScopedDecl>());
597 IdentList[i]->setFETokenInfo(IDecl);
598
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
Ted Kremenek42730c52008-01-07 19:49:32 +0000606 return new ObjCClassDecl(AtClassLoc, &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000607}
608
609
610/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
611/// returns true, or false, accordingly.
612/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000613bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
614 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000615 if (Method->getResultType().getCanonicalType() !=
616 PrevMethod->getResultType().getCanonicalType())
617 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000618 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000619 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
620 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
621 if (ParamDecl->getCanonicalType() != PrevParamDecl->getCanonicalType())
622 return false;
623 }
624 return true;
625}
626
Ted Kremenek42730c52008-01-07 19:49:32 +0000627void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
628 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000629 if (!FirstMethod.Method) {
630 // Haven't seen a method with this selector name yet - add it.
631 FirstMethod.Method = Method;
632 FirstMethod.Next = 0;
633 } else {
634 // We've seen a method with this name, now check the type signature(s).
635 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
636
Ted Kremenek42730c52008-01-07 19:49:32 +0000637 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000638 Next = Next->Next)
639 match = MatchTwoMethodDeclarations(Method, Next->Method);
640
641 if (!match) {
642 // We have a new signature for an existing method - add it.
643 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000644 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000645 FirstMethod.Next = OMI;
646 }
647 }
648}
649
Ted Kremenek42730c52008-01-07 19:49:32 +0000650void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
651 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000652 if (!FirstMethod.Method) {
653 // Haven't seen a method with this selector name yet - add it.
654 FirstMethod.Method = Method;
655 FirstMethod.Next = 0;
656 } else {
657 // We've seen a method with this name, now check the type signature(s).
658 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
659
Ted Kremenek42730c52008-01-07 19:49:32 +0000660 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000661 Next = Next->Next)
662 match = MatchTwoMethodDeclarations(Method, Next->Method);
663
664 if (!match) {
665 // We have a new signature for an existing method - add it.
666 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000667 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000668 FirstMethod.Next = OMI;
669 }
670 }
671}
672
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000673// Note: For class/category implemenations, allMethods/allProperties is
674// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000675void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
676 DeclTy **allMethods, unsigned allNum,
677 DeclTy **allProperties, unsigned pNum) {
678 Decl *ClassDecl = static_cast<Decl *>(classDecl);
679
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000680 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
681 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000682 // should be true.
683 if (!ClassDecl)
684 return;
685
Ted Kremenek42730c52008-01-07 19:49:32 +0000686 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
687 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000688
Ted Kremenek42730c52008-01-07 19:49:32 +0000689 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
690 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000691
692 bool isInterfaceDeclKind =
Ted Kremenek42730c52008-01-07 19:49:32 +0000693 (isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
694 || isa<ObjCProtocolDecl>(ClassDecl));
695 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000696
697 // TODO: property declaration in category and protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000698 if (pNum != 0 && isa<ObjCInterfaceDecl>(ClassDecl)) {
699 ObjCPropertyDecl **properties = new ObjCPropertyDecl*[pNum];
700 memcpy(properties, allProperties, pNum*sizeof(ObjCPropertyDecl*));
701 dyn_cast<ObjCInterfaceDecl>(ClassDecl)->setPropertyDecls(properties);
702 dyn_cast<ObjCInterfaceDecl>(ClassDecl)->setNumPropertyDecl(pNum);
Chris Lattner855e51f2007-12-12 07:09:47 +0000703 }
704
705 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000706 ObjCMethodDecl *Method =
707 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000708
709 if (!Method) continue; // Already issued a diagnostic.
710 if (Method->isInstance()) {
711 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000712 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000713 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
714 : false;
715 if (isInterfaceDeclKind && PrevMethod && !match
716 || checkIdenticalMethods && match) {
717 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
718 Method->getSelector().getName());
719 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
720 } else {
721 insMethods.push_back(Method);
722 InsMap[Method->getSelector()] = Method;
723 /// The following allows us to typecheck messages to "id".
724 AddInstanceMethodToGlobalPool(Method);
725 }
726 }
727 else {
728 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000729 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000730 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
731 : false;
732 if (isInterfaceDeclKind && PrevMethod && !match
733 || checkIdenticalMethods && match) {
734 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
735 Method->getSelector().getName());
736 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
737 } else {
738 clsMethods.push_back(Method);
739 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000740 /// The following allows us to typecheck messages to "Class".
741 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000742 }
743 }
744 }
745
Ted Kremenek42730c52008-01-07 19:49:32 +0000746 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000747 I->addMethods(&insMethods[0], insMethods.size(),
748 &clsMethods[0], clsMethods.size(), AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000749 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000750 P->addMethods(&insMethods[0], insMethods.size(),
751 &clsMethods[0], clsMethods.size(), AtEndLoc);
752 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000753 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000754 C->addMethods(&insMethods[0], insMethods.size(),
755 &clsMethods[0], clsMethods.size(), AtEndLoc);
756 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000757 else if (ObjCImplementationDecl *IC =
758 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000759 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000760 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000761 ImplMethodsVsClassMethods(IC, IDecl);
762 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000763 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000764 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000765 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000766 // Find category interface decl and then check that all methods declared
767 // in this interface is implemented in the category @implementation.
768 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000769 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000770 Categories; Categories = Categories->getNextClassCategory()) {
771 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
772 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
773 break;
774 }
775 }
776 }
777 }
778}
779
780
781/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
782/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000783static Decl::ObjCDeclQualifier
784CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
785 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
786 if (PQTVal & ObjCDeclSpec::DQ_In)
787 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
788 if (PQTVal & ObjCDeclSpec::DQ_Inout)
789 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
790 if (PQTVal & ObjCDeclSpec::DQ_Out)
791 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
792 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
793 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
794 if (PQTVal & ObjCDeclSpec::DQ_Byref)
795 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
796 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
797 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000798
799 return ret;
800}
801
802Sema::DeclTy *Sema::ActOnMethodDeclaration(
803 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000804 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000805 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000806 Selector Sel,
807 // optional arguments. The number of types/arguments is obtained
808 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000809 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000810 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
811 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000812 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000813
814 // Make sure we can establish a context for the method.
815 if (!ClassDecl) {
816 Diag(MethodLoc, diag::error_missing_method_context);
817 return 0;
818 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000819 llvm::SmallVector<ParmVarDecl*, 16> Params;
820
821 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
822 // FIXME: arg->AttrList must be stored too!
823 QualType argType;
824
825 if (ArgTypes[i])
826 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
827 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000828 argType = Context.getObjCIdType();
Chris Lattner58114f02008-03-15 21:32:50 +0000829 ParmVarDecl* Param = ParmVarDecl::Create(Context, SourceLocation(/*FIXME*/),
Chris Lattner48d225c2008-03-15 21:10:16 +0000830 ArgNames[i], argType,
Chris Lattner58114f02008-03-15 21:32:50 +0000831 VarDecl::None, 0);
Ted Kremenek42730c52008-01-07 19:49:32 +0000832 Param->setObjCDeclQualifier(
833 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
Chris Lattner855e51f2007-12-12 07:09:47 +0000834 Params.push_back(Param);
835 }
836 QualType resultDeclType;
837
838 if (ReturnType)
839 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
840 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000841 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000842
Chris Lattner114add62008-03-16 00:49:28 +0000843 ObjCMethodDecl* ObjCMethod =
844 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000845 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000846 MethodType == tok::minus, isVariadic,
847 MethodDeclKind == tok::objc_optional ?
848 ObjCMethodDecl::Optional :
849 ObjCMethodDecl::Required);
850
Ted Kremenek42730c52008-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 Lattner855e51f2007-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 Kremenek42730c52008-01-07 19:49:32 +0000860 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000861 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000862 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000863 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000864 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000865 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000866 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000867 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000868 }
869 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000870 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000871 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000872 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000873 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000874 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000875 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000876 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000877 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000878 }
879 }
880 if (PrevMethod) {
881 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +0000882 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
883 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000884 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
885 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000886 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +0000887}
888
Ted Kremenek42730c52008-01-07 19:49:32 +0000889Sema::DeclTy *Sema::ActOnAddObjCProperties(SourceLocation AtLoc,
890 DeclTy **allProperties, unsigned NumProperties, ObjCDeclSpec &DS) {
891 ObjCPropertyDecl *PDecl = new ObjCPropertyDecl(AtLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000892
Ted Kremenek42730c52008-01-07 19:49:32 +0000893 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
894 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +0000895
Ted Kremenek42730c52008-01-07 19:49:32 +0000896 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) {
897 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +0000898 PDecl->setGetterName(DS.getGetterName());
899 }
900
Ted Kremenek42730c52008-01-07 19:49:32 +0000901 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) {
902 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +0000903 PDecl->setSetterName(DS.getSetterName());
904 }
905
Ted Kremenek42730c52008-01-07 19:49:32 +0000906 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
907 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +0000908
Ted Kremenek42730c52008-01-07 19:49:32 +0000909 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
910 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +0000911
Ted Kremenek42730c52008-01-07 19:49:32 +0000912 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
913 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +0000914
Ted Kremenek42730c52008-01-07 19:49:32 +0000915 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
916 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +0000917
Ted Kremenek42730c52008-01-07 19:49:32 +0000918 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
919 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +0000920
921 PDecl->setNumPropertyDecls(NumProperties);
922 if (NumProperties != 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000923 ObjCIvarDecl **properties = new ObjCIvarDecl*[NumProperties];
924 memcpy(properties, allProperties, NumProperties*sizeof(ObjCIvarDecl*));
Chris Lattner855e51f2007-12-12 07:09:47 +0000925 PDecl->setPropertyDecls(properties);
926 }
927 return PDecl;
928}
929