blob: 3f959680001ee355cbe3a2348344ba876a407ea7 [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
21/// ObjcActOnStartOfMethodDef - This routine sets up parameters; invisible
22/// and user declared, in the method definition's AST.
23void Sema::ObjcActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
24 assert(CurFunctionDecl == 0 && "Method parsing confused");
25 ObjcMethodDecl *MDecl = dyn_cast<ObjcMethodDecl>(static_cast<Decl *>(D));
26 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;
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;
45 if (MDecl->isInstance()) {
46 QualType selfTy = Context.getObjcInterfaceType(MDecl->getClassInterface());
47 selfTy = Context.getPointerType(selfTy);
48 PI.TypeInfo = selfTy.getAsOpaquePtr();
49 } else
50 PI.TypeInfo = Context.getObjcIdType().getAsOpaquePtr();
51 CurMethodDecl->setSelfDecl(ActOnParamDeclarator(PI, FnBodyScope));
52
53 PI.Ident = &Context.Idents.get("_cmd");
54 PI.TypeInfo = Context.getObjcSelType().getAsOpaquePtr();
55 ActOnParamDeclarator(PI, FnBodyScope);
56
57 for (int i = 0; i < MDecl->getNumParams(); i++) {
58 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
59 PI.Ident = PDecl->getIdentifier();
60 PI.IdentLoc = PDecl->getLocation(); // user vars have a real location.
61 PI.TypeInfo = PDecl->getType().getAsOpaquePtr();
62 ActOnParamDeclarator(PI, FnBodyScope);
63 }
64}
65
66Sema::DeclTy *Sema::ActOnStartClassInterface(
67 SourceLocation AtInterfaceLoc,
68 IdentifierInfo *ClassName, SourceLocation ClassLoc,
69 IdentifierInfo *SuperName, SourceLocation SuperLoc,
70 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
71 SourceLocation EndProtoLoc, AttributeList *AttrList) {
72 assert(ClassName && "Missing class identifier");
73
74 // Check for another declaration kind with the same name.
75 ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName);
76 if (PrevDecl && !isa<ObjcInterfaceDecl>(PrevDecl)) {
77 Diag(ClassLoc, diag::err_redefinition_different_kind,
78 ClassName->getName());
79 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
80 }
81
82 ObjcInterfaceDecl* IDecl = dyn_cast_or_null<ObjcInterfaceDecl>(PrevDecl);
83 if (IDecl) {
84 // Class already seen. Is it a forward declaration?
85 if (!IDecl->isForwardDecl())
86 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
87 else {
88 IDecl->setLocation(AtInterfaceLoc);
89 IDecl->setForwardDecl(false);
90 IDecl->AllocIntfRefProtocols(NumProtocols);
91 }
92 }
93 else {
94 IDecl = new ObjcInterfaceDecl(AtInterfaceLoc, NumProtocols, ClassName);
95
96 // Chain & install the interface decl into the identifier.
97 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
98 ClassName->setFETokenInfo(IDecl);
99
100 // Remember that this needs to be removed when the scope is popped.
101 TUScope->AddDecl(IDecl);
102 }
103
104 if (SuperName) {
105 ObjcInterfaceDecl* SuperClassEntry = 0;
106 // Check if a different kind of symbol declared in this scope.
107 PrevDecl = LookupInterfaceDecl(SuperName);
108 if (PrevDecl && !isa<ObjcInterfaceDecl>(PrevDecl)) {
109 Diag(SuperLoc, diag::err_redefinition_different_kind,
110 SuperName->getName());
111 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
112 }
113 else {
114 // Check that super class is previously defined
115 SuperClassEntry = dyn_cast_or_null<ObjcInterfaceDecl>(PrevDecl);
116
117 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
118 Diag(AtInterfaceLoc, diag::err_undef_superclass,
119 SuperClassEntry ? SuperClassEntry->getName()
120 : SuperName->getName(),
121 ClassName->getName());
122 }
123 }
124 IDecl->setSuperClass(SuperClassEntry);
125 IDecl->setLocEnd(SuperLoc);
126 } else { // we have a root class.
127 IDecl->setLocEnd(ClassLoc);
128 }
129
130 /// Check then save referenced protocols
131 if (NumProtocols) {
132 for (unsigned int i = 0; i != NumProtocols; i++) {
133 ObjcProtocolDecl* RefPDecl = ObjcProtocols[ProtocolNames[i]];
134 if (!RefPDecl || RefPDecl->isForwardDecl())
135 Diag(ClassLoc, diag::warn_undef_protocolref,
136 ProtocolNames[i]->getName(),
137 ClassName->getName());
138 IDecl->setIntfRefProtocols((int)i, RefPDecl);
139 }
140 IDecl->setLocEnd(EndProtoLoc);
141 }
142 return IDecl;
143}
144
145/// ActOnCompatiblityAlias - this action is called after complete parsing of
146/// @compaatibility_alias declaration. It sets up the alias relationships.
147Sema::DeclTy *Sema::ActOnCompatiblityAlias(
148 SourceLocation AtCompatibilityAliasLoc,
149 IdentifierInfo *AliasName, SourceLocation AliasLocation,
150 IdentifierInfo *ClassName, SourceLocation ClassLocation) {
151 // Look for previous declaration of alias name
152 ScopedDecl *ADecl = LookupScopedDecl(AliasName, Decl::IDNS_Ordinary,
153 AliasLocation, TUScope);
154 if (ADecl) {
155 if (isa<ObjcCompatibleAliasDecl>(ADecl)) {
156 Diag(AliasLocation, diag::warn_previous_alias_decl);
157 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
158 }
159 else {
160 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
161 AliasName->getName());
162 Diag(ADecl->getLocation(), diag::err_previous_declaration);
163 }
164 return 0;
165 }
166 // Check for class declaration
167 ScopedDecl *CDecl = LookupScopedDecl(ClassName, Decl::IDNS_Ordinary,
168 ClassLocation, TUScope);
169 if (!CDecl || !isa<ObjcInterfaceDecl>(CDecl)) {
170 Diag(ClassLocation, diag::warn_undef_interface,
171 ClassName->getName());
172 if (CDecl)
173 Diag(CDecl->getLocation(), diag::warn_previous_declaration);
174 return 0;
175 }
176 // Everything checked out, instantiate a new alias declaration ast
177 ObjcCompatibleAliasDecl *AliasDecl =
178 new ObjcCompatibleAliasDecl(AtCompatibilityAliasLoc,
179 AliasName,
180 dyn_cast<ObjcInterfaceDecl>(CDecl));
181
182 // Chain & install the interface decl into the identifier.
183 AliasDecl->setNext(AliasName->getFETokenInfo<ScopedDecl>());
184 AliasName->setFETokenInfo(AliasDecl);
185 return AliasDecl;
186}
187
188Sema::DeclTy *Sema::ActOnStartProtocolInterface(
189 SourceLocation AtProtoInterfaceLoc,
190 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
191 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
192 SourceLocation EndProtoLoc) {
193 assert(ProtocolName && "Missing protocol identifier");
194 ObjcProtocolDecl *PDecl = ObjcProtocols[ProtocolName];
195 if (PDecl) {
196 // Protocol already seen. Better be a forward protocol declaration
197 if (!PDecl->isForwardDecl())
198 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
199 ProtocolName->getName());
200 else {
201 PDecl->setForwardDecl(false);
202 PDecl->AllocReferencedProtocols(NumProtoRefs);
203 }
204 }
205 else {
206 PDecl = new ObjcProtocolDecl(AtProtoInterfaceLoc, NumProtoRefs,
207 ProtocolName);
208 ObjcProtocols[ProtocolName] = PDecl;
209 }
210
211 if (NumProtoRefs) {
212 /// Check then save referenced protocols
213 for (unsigned int i = 0; i != NumProtoRefs; i++) {
214 ObjcProtocolDecl* RefPDecl = ObjcProtocols[ProtoRefNames[i]];
215 if (!RefPDecl || RefPDecl->isForwardDecl())
216 Diag(ProtocolLoc, diag::warn_undef_protocolref,
217 ProtoRefNames[i]->getName(),
218 ProtocolName->getName());
219 PDecl->setReferencedProtocols((int)i, RefPDecl);
220 }
221 PDecl->setLocEnd(EndProtoLoc);
222 }
223 return PDecl;
224}
225
226/// FindProtocolDeclaration - This routine looks up protocols and
227/// issuer error if they are not declared. It returns list of protocol
228/// declarations in its 'Protocols' argument.
229void
230Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
231 IdentifierInfo **ProtocolId,
232 unsigned NumProtocols,
233 llvm::SmallVector<DeclTy *,8> &Protocols) {
234 for (unsigned i = 0; i != NumProtocols; ++i) {
235 ObjcProtocolDecl *PDecl = ObjcProtocols[ProtocolId[i]];
236 if (!PDecl)
237 Diag(TypeLoc, diag::err_undeclared_protocol,
238 ProtocolId[i]->getName());
239 else
240 Protocols.push_back(PDecl);
241 }
242}
243
244/// ActOnForwardProtocolDeclaration -
245Action::DeclTy *
246Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
247 IdentifierInfo **IdentList, unsigned NumElts) {
248 llvm::SmallVector<ObjcProtocolDecl*, 32> Protocols;
249
250 for (unsigned i = 0; i != NumElts; ++i) {
251 IdentifierInfo *P = IdentList[i];
252 ObjcProtocolDecl *PDecl = ObjcProtocols[P];
253 if (!PDecl) { // Not already seen?
254 // FIXME: Pass in the location of the identifier!
255 PDecl = new ObjcProtocolDecl(AtProtocolLoc, 0, P, true);
256 ObjcProtocols[P] = PDecl;
257 }
258
259 Protocols.push_back(PDecl);
260 }
261 return new ObjcForwardProtocolDecl(AtProtocolLoc,
262 &Protocols[0], Protocols.size());
263}
264
265Sema::DeclTy *Sema::ActOnStartCategoryInterface(
266 SourceLocation AtInterfaceLoc,
267 IdentifierInfo *ClassName, SourceLocation ClassLoc,
268 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
269 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
270 SourceLocation EndProtoLoc) {
271 ObjcInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
272
273 /// Check that class of this category is already completely declared.
274 if (!IDecl || IDecl->isForwardDecl()) {
275 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
276 return 0;
277 }
278 ObjcCategoryDecl *CDecl = new ObjcCategoryDecl(AtInterfaceLoc, NumProtoRefs,
279 CategoryName);
280 CDecl->setClassInterface(IDecl);
281 /// Check for duplicate interface declaration for this category
282 ObjcCategoryDecl *CDeclChain;
283 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
284 CDeclChain = CDeclChain->getNextClassCategory()) {
285 if (CDeclChain->getIdentifier() == CategoryName) {
286 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
287 CategoryName->getName());
288 break;
289 }
290 }
291 if (!CDeclChain)
292 CDecl->insertNextClassCategory();
293
294 if (NumProtoRefs) {
295 /// Check then save referenced protocols
296 for (unsigned int i = 0; i != NumProtoRefs; i++) {
297 ObjcProtocolDecl* RefPDecl = ObjcProtocols[ProtoRefNames[i]];
298 if (!RefPDecl || RefPDecl->isForwardDecl()) {
299 Diag(CategoryLoc, diag::warn_undef_protocolref,
300 ProtoRefNames[i]->getName(),
301 CategoryName->getName());
302 }
303 CDecl->setCatReferencedProtocols((int)i, RefPDecl);
304 }
305 CDecl->setLocEnd(EndProtoLoc);
306 }
307 return CDecl;
308}
309
310/// ActOnStartCategoryImplementation - Perform semantic checks on the
311/// category implementation declaration and build an ObjcCategoryImplDecl
312/// object.
313Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
314 SourceLocation AtCatImplLoc,
315 IdentifierInfo *ClassName, SourceLocation ClassLoc,
316 IdentifierInfo *CatName, SourceLocation CatLoc) {
317 ObjcInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
318 ObjcCategoryImplDecl *CDecl = new ObjcCategoryImplDecl(AtCatImplLoc,
319 CatName, IDecl);
320 /// Check that class of this category is already completely declared.
321 if (!IDecl || IDecl->isForwardDecl())
322 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
323
324 /// TODO: Check that CatName, category name, is not used in another
325 // implementation.
326 return CDecl;
327}
328
329Sema::DeclTy *Sema::ActOnStartClassImplementation(
330 SourceLocation AtClassImplLoc,
331 IdentifierInfo *ClassName, SourceLocation ClassLoc,
332 IdentifierInfo *SuperClassname,
333 SourceLocation SuperClassLoc) {
334 ObjcInterfaceDecl* IDecl = 0;
335 // Check for another declaration kind with the same name.
336 ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName);
337 if (PrevDecl && !isa<ObjcInterfaceDecl>(PrevDecl)) {
338 Diag(ClassLoc, diag::err_redefinition_different_kind,
339 ClassName->getName());
340 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
341 }
342 else {
343 // Is there an interface declaration of this class; if not, warn!
344 IDecl = dyn_cast_or_null<ObjcInterfaceDecl>(PrevDecl);
345 if (!IDecl)
346 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
347 }
348
349 // Check that super class name is valid class name
350 ObjcInterfaceDecl* SDecl = 0;
351 if (SuperClassname) {
352 // Check if a different kind of symbol declared in this scope.
353 PrevDecl = LookupInterfaceDecl(SuperClassname);
354 if (PrevDecl && !isa<ObjcInterfaceDecl>(PrevDecl)) {
355 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
356 SuperClassname->getName());
357 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
358 }
359 else {
360 SDecl = dyn_cast_or_null<ObjcInterfaceDecl>(PrevDecl);
361 if (!SDecl)
362 Diag(SuperClassLoc, diag::err_undef_superclass,
363 SuperClassname->getName(), ClassName->getName());
364 else if (IDecl && IDecl->getSuperClass() != SDecl) {
365 // This implementation and its interface do not have the same
366 // super class.
367 Diag(SuperClassLoc, diag::err_conflicting_super_class,
368 SDecl->getName());
369 Diag(SDecl->getLocation(), diag::err_previous_definition);
370 }
371 }
372 }
373
374 if (!IDecl) {
375 // Legacy case of @implementation with no corresponding @interface.
376 // Build, chain & install the interface decl into the identifier.
377 IDecl = new ObjcInterfaceDecl(AtClassImplLoc, 0, ClassName,
378 false, true);
379 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
380 ClassName->setFETokenInfo(IDecl);
381 IDecl->setSuperClass(SDecl);
382 IDecl->setLocEnd(ClassLoc);
383
384 // Remember that this needs to be removed when the scope is popped.
385 TUScope->AddDecl(IDecl);
386 }
387
388 ObjcImplementationDecl* IMPDecl =
389 new ObjcImplementationDecl(AtClassImplLoc, ClassName, IDecl, SDecl);
390
391 // Check that there is no duplicate implementation of this class.
392 if (ObjcImplementations[ClassName])
393 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
394 else // add it to the list.
395 ObjcImplementations[ClassName] = IMPDecl;
396 return IMPDecl;
397}
398
399void Sema::CheckImplementationIvars(ObjcImplementationDecl *ImpDecl,
400 ObjcIvarDecl **ivars, unsigned numIvars,
401 SourceLocation RBrace) {
402 assert(ImpDecl && "missing implementation decl");
403 ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
404 if (!IDecl)
405 return;
406 /// Check case of non-existing @interface decl.
407 /// (legacy objective-c @implementation decl without an @interface decl).
408 /// Add implementations's ivar to the synthesize class's ivar list.
409 if (IDecl->ImplicitInterfaceDecl()) {
410 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
411 return;
412 }
413 // If implementation has empty ivar list, just return.
414 if (numIvars == 0)
415 return;
416
417 assert(ivars && "missing @implementation ivars");
418
419 // Check interface's Ivar list against those in the implementation.
420 // names and types must match.
421 //
Chris Lattner4d391482007-12-12 07:09:47 +0000422 unsigned j = 0;
Chris Lattner4c525092007-12-12 17:58:05 +0000423 ObjcInterfaceDecl::ivar_iterator
424 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
425 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Chris Lattner609e4c72007-12-12 18:11:49 +0000426 ObjcIvarDecl* ImplIvar = ivars[j++];
Chris Lattner4c525092007-12-12 17:58:05 +0000427 ObjcIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000428 assert (ImplIvar && "missing implementation ivar");
429 assert (ClsIvar && "missing class ivar");
430 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
431 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
432 ImplIvar->getIdentifier()->getName());
433 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
434 ClsIvar->getIdentifier()->getName());
435 }
436 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
437 // as error.
438 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
439 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
440 ImplIvar->getIdentifier()->getName());
441 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
442 ClsIvar->getIdentifier()->getName());
Chris Lattner609e4c72007-12-12 18:11:49 +0000443 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000444 }
445 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000446 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000447
448 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000449 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000450 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000451 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000452}
453
454/// CheckProtocolMethodDefs - This routine checks unimpletented methods
455/// Declared in protocol, and those referenced by it.
456void Sema::CheckProtocolMethodDefs(ObjcProtocolDecl *PDecl,
457 bool& IncompleteImpl,
458 const llvm::DenseSet<Selector> &InsMap,
459 const llvm::DenseSet<Selector> &ClsMap) {
460 // check unimplemented instance methods.
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000461 for (ObjcProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
462 E = PDecl->instmeth_end(); I != E; ++I) {
463 ObjcMethodDecl *method = *I;
464 if (!InsMap.count(method->getSelector()) &&
465 method->getImplementationControl() != ObjcMethodDecl::Optional) {
466 Diag(method->getLocation(), diag::warn_undef_method_impl,
467 method->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +0000468 IncompleteImpl = true;
469 }
470 }
471 // check unimplemented class methods
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000472 for (ObjcProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
473 E = PDecl->classmeth_end(); I != E; ++I) {
474 ObjcMethodDecl *method = *I;
475 if (!ClsMap.count(method->getSelector()) &&
476 method->getImplementationControl() != ObjcMethodDecl::Optional) {
477 Diag(method->getLocation(), diag::warn_undef_method_impl,
478 method->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +0000479 IncompleteImpl = true;
480 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000481 }
Chris Lattner4d391482007-12-12 07:09:47 +0000482 // Check on this protocols's referenced protocols, recursively
483 ObjcProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
484 for (int i = 0; i < PDecl->getNumReferencedProtocols(); i++)
485 CheckProtocolMethodDefs(RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
486}
487
488void Sema::ImplMethodsVsClassMethods(ObjcImplementationDecl* IMPDecl,
489 ObjcInterfaceDecl* IDecl) {
490 llvm::DenseSet<Selector> InsMap;
491 // Check and see if instance methods in class interface have been
492 // implemented in the implementation class.
Chris Lattner4c525092007-12-12 17:58:05 +0000493 for (ObjcImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
494 E = IMPDecl->instmeth_end(); I != E; ++I)
495 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000496
497 bool IncompleteImpl = false;
Chris Lattner4c525092007-12-12 17:58:05 +0000498 for (ObjcInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
499 E = IDecl->instmeth_end(); I != E; ++I)
500 if (!InsMap.count((*I)->getSelector())) {
501 Diag((*I)->getLocation(), diag::warn_undef_method_impl,
502 (*I)->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +0000503 IncompleteImpl = true;
504 }
Chris Lattner4c525092007-12-12 17:58:05 +0000505
Chris Lattner4d391482007-12-12 07:09:47 +0000506 llvm::DenseSet<Selector> ClsMap;
507 // Check and see if class methods in class interface have been
508 // implemented in the implementation class.
Chris Lattner4c525092007-12-12 17:58:05 +0000509 for (ObjcImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
510 E = IMPDecl->classmeth_end(); I != E; ++I)
511 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000512
Chris Lattner4c525092007-12-12 17:58:05 +0000513 for (ObjcInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
514 E = IDecl->classmeth_end(); I != E; ++I)
515 if (!ClsMap.count((*I)->getSelector())) {
516 Diag((*I)->getLocation(), diag::warn_undef_method_impl,
517 (*I)->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +0000518 IncompleteImpl = true;
519 }
520
521 // Check the protocol list for unimplemented methods in the @implementation
522 // class.
523 ObjcProtocolDecl** protocols = IDecl->getReferencedProtocols();
524 for (int i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
525 CheckProtocolMethodDefs(protocols[i], IncompleteImpl, InsMap, ClsMap);
526
527 if (IncompleteImpl)
528 Diag(IMPDecl->getLocation(), diag::warn_incomplete_impl_class,
529 IMPDecl->getName());
530}
531
532/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
533/// category interface is implemented in the category @implementation.
534void Sema::ImplCategoryMethodsVsIntfMethods(ObjcCategoryImplDecl *CatImplDecl,
535 ObjcCategoryDecl *CatClassDecl) {
536 llvm::DenseSet<Selector> InsMap;
537 // Check and see if instance methods in category interface have been
538 // implemented in its implementation class.
Chris Lattner4c525092007-12-12 17:58:05 +0000539 for (ObjcCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
540 E = CatImplDecl->instmeth_end(); I != E; ++I)
541 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000542
543 bool IncompleteImpl = false;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000544 for (ObjcCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
545 E = CatClassDecl->instmeth_end(); I != E; ++I)
546 if (!InsMap.count((*I)->getSelector())) {
547 Diag((*I)->getLocation(), diag::warn_undef_method_impl,
548 (*I)->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +0000549 IncompleteImpl = true;
550 }
551 llvm::DenseSet<Selector> ClsMap;
552 // Check and see if class methods in category interface have been
553 // implemented in its implementation class.
Chris Lattner4c525092007-12-12 17:58:05 +0000554 for (ObjcCategoryImplDecl::classmeth_iterator
555 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
556 I != E; ++I)
557 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000558
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000559 for (ObjcCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
560 E = CatClassDecl->classmeth_end(); I != E; ++I)
561 if (!ClsMap.count((*I)->getSelector())) {
562 Diag((*I)->getLocation(), diag::warn_undef_method_impl,
563 (*I)->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +0000564 IncompleteImpl = true;
565 }
566
567 // Check the protocol list for unimplemented methods in the @implementation
568 // class.
569 ObjcProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
570 for (int i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
571 ObjcProtocolDecl* PDecl = protocols[i];
572 CheckProtocolMethodDefs(PDecl, IncompleteImpl, InsMap, ClsMap);
573 }
574 if (IncompleteImpl)
575 Diag(CatImplDecl->getLocation(), diag::warn_incomplete_impl_category,
576 CatClassDecl->getName());
577}
578
579/// ActOnForwardClassDeclaration -
580Action::DeclTy *
581Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
582 IdentifierInfo **IdentList, unsigned NumElts)
583{
584 llvm::SmallVector<ObjcInterfaceDecl*, 32> Interfaces;
585
586 for (unsigned i = 0; i != NumElts; ++i) {
587 // Check for another declaration kind with the same name.
588 ScopedDecl *PrevDecl = LookupInterfaceDecl(IdentList[i]);
589 if (PrevDecl && !isa<ObjcInterfaceDecl>(PrevDecl)) {
590 Diag(AtClassLoc, diag::err_redefinition_different_kind,
591 IdentList[i]->getName());
592 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
593 }
594 ObjcInterfaceDecl *IDecl = dyn_cast_or_null<ObjcInterfaceDecl>(PrevDecl);
595 if (!IDecl) { // Not already seen? Make a forward decl.
596 IDecl = new ObjcInterfaceDecl(AtClassLoc, 0, IdentList[i], true);
597 // Chain & install the interface decl into the identifier.
598 IDecl->setNext(IdentList[i]->getFETokenInfo<ScopedDecl>());
599 IdentList[i]->setFETokenInfo(IDecl);
600
601 // Remember that this needs to be removed when the scope is popped.
602 TUScope->AddDecl(IDecl);
603 }
604
605 Interfaces.push_back(IDecl);
606 }
607
608 return new ObjcClassDecl(AtClassLoc, &Interfaces[0], Interfaces.size());
609}
610
611
612/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
613/// returns true, or false, accordingly.
614/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
615bool Sema::MatchTwoMethodDeclarations(const ObjcMethodDecl *Method,
616 const ObjcMethodDecl *PrevMethod) {
617 if (Method->getResultType().getCanonicalType() !=
618 PrevMethod->getResultType().getCanonicalType())
619 return false;
620 for (int i = 0; i < Method->getNumParams(); i++) {
621 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
622 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
623 if (ParamDecl->getCanonicalType() != PrevParamDecl->getCanonicalType())
624 return false;
625 }
626 return true;
627}
628
629void Sema::AddInstanceMethodToGlobalPool(ObjcMethodDecl *Method) {
630 ObjcMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
631 if (!FirstMethod.Method) {
632 // Haven't seen a method with this selector name yet - add it.
633 FirstMethod.Method = Method;
634 FirstMethod.Next = 0;
635 } else {
636 // We've seen a method with this name, now check the type signature(s).
637 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
638
639 for (ObjcMethodList *Next = FirstMethod.Next; !match && Next;
640 Next = Next->Next)
641 match = MatchTwoMethodDeclarations(Method, Next->Method);
642
643 if (!match) {
644 // We have a new signature for an existing method - add it.
645 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
646 struct ObjcMethodList *OMI = new ObjcMethodList(Method, FirstMethod.Next);
647 FirstMethod.Next = OMI;
648 }
649 }
650}
651
652void Sema::AddFactoryMethodToGlobalPool(ObjcMethodDecl *Method) {
653 ObjcMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
654 if (!FirstMethod.Method) {
655 // Haven't seen a method with this selector name yet - add it.
656 FirstMethod.Method = Method;
657 FirstMethod.Next = 0;
658 } else {
659 // We've seen a method with this name, now check the type signature(s).
660 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
661
662 for (ObjcMethodList *Next = FirstMethod.Next; !match && Next;
663 Next = Next->Next)
664 match = MatchTwoMethodDeclarations(Method, Next->Method);
665
666 if (!match) {
667 // We have a new signature for an existing method - add it.
668 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
669 struct ObjcMethodList *OMI = new ObjcMethodList(Method, FirstMethod.Next);
670 FirstMethod.Next = OMI;
671 }
672 }
673}
674
Steve Naroffa56f6162007-12-18 01:30:32 +0000675// Note: For class/category implemenations, allMethods/allProperties is
676// always null.
Chris Lattner4d391482007-12-12 07:09:47 +0000677void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
678 DeclTy **allMethods, unsigned allNum,
679 DeclTy **allProperties, unsigned pNum) {
680 Decl *ClassDecl = static_cast<Decl *>(classDecl);
681
Steve Naroffa56f6162007-12-18 01:30:32 +0000682 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
683 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +0000684 // should be true.
685 if (!ClassDecl)
686 return;
687
688 llvm::SmallVector<ObjcMethodDecl*, 32> insMethods;
689 llvm::SmallVector<ObjcMethodDecl*, 16> clsMethods;
690
691 llvm::DenseMap<Selector, const ObjcMethodDecl*> InsMap;
692 llvm::DenseMap<Selector, const ObjcMethodDecl*> ClsMap;
693
694 bool isInterfaceDeclKind =
695 (isa<ObjcInterfaceDecl>(ClassDecl) || isa<ObjcCategoryDecl>(ClassDecl)
696 || isa<ObjcProtocolDecl>(ClassDecl));
697 bool checkIdenticalMethods = isa<ObjcImplementationDecl>(ClassDecl);
698
699 // TODO: property declaration in category and protocols.
700 if (pNum != 0 && isa<ObjcInterfaceDecl>(ClassDecl)) {
701 ObjcPropertyDecl **properties = new ObjcPropertyDecl*[pNum];
702 memcpy(properties, allProperties, pNum*sizeof(ObjcPropertyDecl*));
703 dyn_cast<ObjcInterfaceDecl>(ClassDecl)->setPropertyDecls(properties);
704 dyn_cast<ObjcInterfaceDecl>(ClassDecl)->setNumPropertyDecl(pNum);
705 }
706
707 for (unsigned i = 0; i < allNum; i++ ) {
708 ObjcMethodDecl *Method =
709 cast_or_null<ObjcMethodDecl>(static_cast<Decl*>(allMethods[i]));
710
711 if (!Method) continue; // Already issued a diagnostic.
712 if (Method->isInstance()) {
713 /// Check for instance method of the same name with incompatible types
714 const ObjcMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
715 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
716 : false;
717 if (isInterfaceDeclKind && PrevMethod && !match
718 || checkIdenticalMethods && match) {
719 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
720 Method->getSelector().getName());
721 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
722 } else {
723 insMethods.push_back(Method);
724 InsMap[Method->getSelector()] = Method;
725 /// The following allows us to typecheck messages to "id".
726 AddInstanceMethodToGlobalPool(Method);
727 }
728 }
729 else {
730 /// Check for class method of the same name with incompatible types
731 const ObjcMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
732 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
733 : false;
734 if (isInterfaceDeclKind && PrevMethod && !match
735 || checkIdenticalMethods && match) {
736 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
737 Method->getSelector().getName());
738 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
739 } else {
740 clsMethods.push_back(Method);
741 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +0000742 /// The following allows us to typecheck messages to "Class".
743 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +0000744 }
745 }
746 }
747
748 if (ObjcInterfaceDecl *I = dyn_cast<ObjcInterfaceDecl>(ClassDecl)) {
749 I->addMethods(&insMethods[0], insMethods.size(),
750 &clsMethods[0], clsMethods.size(), AtEndLoc);
751 } else if (ObjcProtocolDecl *P = dyn_cast<ObjcProtocolDecl>(ClassDecl)) {
752 P->addMethods(&insMethods[0], insMethods.size(),
753 &clsMethods[0], clsMethods.size(), AtEndLoc);
754 }
755 else if (ObjcCategoryDecl *C = dyn_cast<ObjcCategoryDecl>(ClassDecl)) {
756 C->addMethods(&insMethods[0], insMethods.size(),
757 &clsMethods[0], clsMethods.size(), AtEndLoc);
758 }
759 else if (ObjcImplementationDecl *IC =
760 dyn_cast<ObjcImplementationDecl>(ClassDecl)) {
761 IC->setLocEnd(AtEndLoc);
762 if (ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
763 ImplMethodsVsClassMethods(IC, IDecl);
764 } else {
765 ObjcCategoryImplDecl* CatImplClass = cast<ObjcCategoryImplDecl>(ClassDecl);
766 CatImplClass->setLocEnd(AtEndLoc);
767 ObjcInterfaceDecl* IDecl = CatImplClass->getClassInterface();
768 // Find category interface decl and then check that all methods declared
769 // in this interface is implemented in the category @implementation.
770 if (IDecl) {
771 for (ObjcCategoryDecl *Categories = IDecl->getCategoryList();
772 Categories; Categories = Categories->getNextClassCategory()) {
773 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
774 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
775 break;
776 }
777 }
778 }
779 }
780}
781
782
783/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
784/// objective-c's type qualifier from the parser version of the same info.
785static Decl::ObjcDeclQualifier
786CvtQTToAstBitMask(ObjcDeclSpec::ObjcDeclQualifier PQTVal) {
787 Decl::ObjcDeclQualifier ret = Decl::OBJC_TQ_None;
788 if (PQTVal & ObjcDeclSpec::DQ_In)
789 ret = (Decl::ObjcDeclQualifier)(ret | Decl::OBJC_TQ_In);
790 if (PQTVal & ObjcDeclSpec::DQ_Inout)
791 ret = (Decl::ObjcDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
792 if (PQTVal & ObjcDeclSpec::DQ_Out)
793 ret = (Decl::ObjcDeclQualifier)(ret | Decl::OBJC_TQ_Out);
794 if (PQTVal & ObjcDeclSpec::DQ_Bycopy)
795 ret = (Decl::ObjcDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
796 if (PQTVal & ObjcDeclSpec::DQ_Byref)
797 ret = (Decl::ObjcDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
798 if (PQTVal & ObjcDeclSpec::DQ_Oneway)
799 ret = (Decl::ObjcDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
800
801 return ret;
802}
803
804Sema::DeclTy *Sema::ActOnMethodDeclaration(
805 SourceLocation MethodLoc, SourceLocation EndLoc,
806 tok::TokenKind MethodType, DeclTy *ClassDecl,
807 ObjcDeclSpec &ReturnQT, TypeTy *ReturnType,
808 Selector Sel,
809 // optional arguments. The number of types/arguments is obtained
810 // from the Sel.getNumArgs().
811 ObjcDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
812 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
813 bool isVariadic) {
814 llvm::SmallVector<ParmVarDecl*, 16> Params;
815
816 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
817 // FIXME: arg->AttrList must be stored too!
818 QualType argType;
819
820 if (ArgTypes[i])
821 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
822 else
823 argType = Context.getObjcIdType();
824 ParmVarDecl* Param = new ParmVarDecl(SourceLocation(/*FIXME*/), ArgNames[i],
825 argType, VarDecl::None, 0);
826 Param->setObjcDeclQualifier(
827 CvtQTToAstBitMask(ArgQT[i].getObjcDeclQualifier()));
828 Params.push_back(Param);
829 }
830 QualType resultDeclType;
831
832 if (ReturnType)
833 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
834 else // get the type for "id".
835 resultDeclType = Context.getObjcIdType();
836
837 Decl *CDecl = static_cast<Decl*>(ClassDecl);
838 ObjcMethodDecl* ObjcMethod = new ObjcMethodDecl(MethodLoc, EndLoc, Sel,
839 resultDeclType,
840 CDecl,
841 0, -1, AttrList,
842 MethodType == tok::minus, isVariadic,
843 MethodDeclKind == tok::objc_optional ?
844 ObjcMethodDecl::Optional :
845 ObjcMethodDecl::Required);
846 ObjcMethod->setMethodParams(&Params[0], Sel.getNumArgs());
847 ObjcMethod->setObjcDeclQualifier(
848 CvtQTToAstBitMask(ReturnQT.getObjcDeclQualifier()));
849 const ObjcMethodDecl *PrevMethod = 0;
850
851 // For implementations (which can be very "coarse grain"), we add the
852 // method now. This allows the AST to implement lookup methods that work
853 // incrementally (without waiting until we parse the @end). It also allows
854 // us to flag multiple declaration errors as they occur.
855 if (ObjcImplementationDecl *ImpDecl =
856 dyn_cast<ObjcImplementationDecl>(CDecl)) {
857 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000858 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Chris Lattner4d391482007-12-12 07:09:47 +0000859 ImpDecl->addInstanceMethod(ObjcMethod);
860 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +0000861 PrevMethod = ImpDecl->getClassMethod(Sel);
Chris Lattner4d391482007-12-12 07:09:47 +0000862 ImpDecl->addClassMethod(ObjcMethod);
863 }
864 }
865 else if (ObjcCategoryImplDecl *CatImpDecl =
866 dyn_cast<ObjcCategoryImplDecl>(CDecl)) {
867 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000868 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Chris Lattner4d391482007-12-12 07:09:47 +0000869 CatImpDecl->addInstanceMethod(ObjcMethod);
870 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +0000871 PrevMethod = CatImpDecl->getClassMethod(Sel);
Chris Lattner4d391482007-12-12 07:09:47 +0000872 CatImpDecl->addClassMethod(ObjcMethod);
873 }
874 }
875 if (PrevMethod) {
876 // You can never have two method definitions with the same name.
877 Diag(ObjcMethod->getLocation(), diag::error_duplicate_method_decl,
878 ObjcMethod->getSelector().getName());
879 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
880 }
881 return ObjcMethod;
882}
883
884Sema::DeclTy *Sema::ActOnAddObjcProperties(SourceLocation AtLoc,
885 DeclTy **allProperties, unsigned NumProperties, ObjcDeclSpec &DS) {
886 ObjcPropertyDecl *PDecl = new ObjcPropertyDecl(AtLoc);
887
888 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_readonly)
889 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_readonly);
890
891 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_getter) {
892 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_getter);
893 PDecl->setGetterName(DS.getGetterName());
894 }
895
896 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_setter) {
897 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_setter);
898 PDecl->setSetterName(DS.getSetterName());
899 }
900
901 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_assign)
902 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_assign);
903
904 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_readwrite)
905 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_readwrite);
906
907 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_retain)
908 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_retain);
909
910 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_copy)
911 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_copy);
912
913 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_nonatomic)
914 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_nonatomic);
915
916 PDecl->setNumPropertyDecls(NumProperties);
917 if (NumProperties != 0) {
918 ObjcIvarDecl **properties = new ObjcIvarDecl*[NumProperties];
919 memcpy(properties, allProperties, NumProperties*sizeof(ObjcIvarDecl*));
920 PDecl->setPropertyDecls(properties);
921 }
922 return PDecl;
923}
924