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