blob: b2cad375f8a8dbf53ed888874aeebd8b66116944 [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!");
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 Lattner4d391482007-12-12 07:09:47 +0000416 unsigned j = 0;
Chris Lattner4c525092007-12-12 17:58:05 +0000417 ObjcInterfaceDecl::ivar_iterator
418 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
419 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Chris Lattner609e4c72007-12-12 18:11:49 +0000420 ObjcIvarDecl* ImplIvar = ivars[j++];
Chris Lattner4c525092007-12-12 17:58:05 +0000421 ObjcIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000422 assert (ImplIvar && "missing implementation ivar");
423 assert (ClsIvar && "missing class ivar");
424 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
425 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
426 ImplIvar->getIdentifier()->getName());
427 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
428 ClsIvar->getIdentifier()->getName());
429 }
430 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
431 // as error.
432 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
433 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
434 ImplIvar->getIdentifier()->getName());
435 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
436 ClsIvar->getIdentifier()->getName());
Chris Lattner609e4c72007-12-12 18:11:49 +0000437 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000438 }
439 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000440 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000441
442 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000443 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000444 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000445 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000446}
447
448/// CheckProtocolMethodDefs - This routine checks unimpletented methods
449/// Declared in protocol, and those referenced by it.
450void Sema::CheckProtocolMethodDefs(ObjcProtocolDecl *PDecl,
451 bool& IncompleteImpl,
452 const llvm::DenseSet<Selector> &InsMap,
453 const llvm::DenseSet<Selector> &ClsMap) {
454 // check unimplemented instance methods.
455 ObjcMethodDecl** methods = PDecl->getInstanceMethods();
456 for (int j = 0; j < PDecl->getNumInstanceMethods(); j++) {
457 if (!InsMap.count(methods[j]->getSelector()) &&
458 methods[j]->getImplementationControl() != ObjcMethodDecl::Optional) {
459 Diag(methods[j]->getLocation(), diag::warn_undef_method_impl,
460 methods[j]->getSelector().getName());
461 IncompleteImpl = true;
462 }
463 }
464 // check unimplemented class methods
465 methods = PDecl->getClassMethods();
466 for (int j = 0; j < PDecl->getNumClassMethods(); j++)
467 if (!ClsMap.count(methods[j]->getSelector()) &&
468 methods[j]->getImplementationControl() != ObjcMethodDecl::Optional) {
469 Diag(methods[j]->getLocation(), diag::warn_undef_method_impl,
470 methods[j]->getSelector().getName());
471 IncompleteImpl = true;
472 }
473
474 // Check on this protocols's referenced protocols, recursively
475 ObjcProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
476 for (int i = 0; i < PDecl->getNumReferencedProtocols(); i++)
477 CheckProtocolMethodDefs(RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
478}
479
480void Sema::ImplMethodsVsClassMethods(ObjcImplementationDecl* IMPDecl,
481 ObjcInterfaceDecl* IDecl) {
482 llvm::DenseSet<Selector> InsMap;
483 // Check and see if instance methods in class interface have been
484 // implemented in the implementation class.
Chris Lattner4c525092007-12-12 17:58:05 +0000485 for (ObjcImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
486 E = IMPDecl->instmeth_end(); I != E; ++I)
487 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000488
489 bool IncompleteImpl = false;
Chris Lattner4c525092007-12-12 17:58:05 +0000490 for (ObjcInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
491 E = IDecl->instmeth_end(); I != E; ++I)
492 if (!InsMap.count((*I)->getSelector())) {
493 Diag((*I)->getLocation(), diag::warn_undef_method_impl,
494 (*I)->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +0000495 IncompleteImpl = true;
496 }
Chris Lattner4c525092007-12-12 17:58:05 +0000497
Chris Lattner4d391482007-12-12 07:09:47 +0000498 llvm::DenseSet<Selector> ClsMap;
499 // Check and see if class methods in class interface have been
500 // implemented in the implementation class.
Chris Lattner4c525092007-12-12 17:58:05 +0000501 for (ObjcImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
502 E = IMPDecl->classmeth_end(); I != E; ++I)
503 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000504
Chris Lattner4c525092007-12-12 17:58:05 +0000505 for (ObjcInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
506 E = IDecl->classmeth_end(); I != E; ++I)
507 if (!ClsMap.count((*I)->getSelector())) {
508 Diag((*I)->getLocation(), diag::warn_undef_method_impl,
509 (*I)->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +0000510 IncompleteImpl = true;
511 }
512
513 // Check the protocol list for unimplemented methods in the @implementation
514 // class.
515 ObjcProtocolDecl** protocols = IDecl->getReferencedProtocols();
516 for (int i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
517 CheckProtocolMethodDefs(protocols[i], IncompleteImpl, InsMap, ClsMap);
518
519 if (IncompleteImpl)
520 Diag(IMPDecl->getLocation(), diag::warn_incomplete_impl_class,
521 IMPDecl->getName());
522}
523
524/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
525/// category interface is implemented in the category @implementation.
526void Sema::ImplCategoryMethodsVsIntfMethods(ObjcCategoryImplDecl *CatImplDecl,
527 ObjcCategoryDecl *CatClassDecl) {
528 llvm::DenseSet<Selector> InsMap;
529 // Check and see if instance methods in category interface have been
530 // implemented in its implementation class.
Chris Lattner4c525092007-12-12 17:58:05 +0000531 for (ObjcCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
532 E = CatImplDecl->instmeth_end(); I != E; ++I)
533 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000534
535 bool IncompleteImpl = false;
Chris Lattner4c525092007-12-12 17:58:05 +0000536 ObjcMethodDecl *const* methods = CatClassDecl->getInstanceMethods();
Chris Lattner4d391482007-12-12 07:09:47 +0000537 for (int j = 0; j < CatClassDecl->getNumInstanceMethods(); j++)
538 if (!InsMap.count(methods[j]->getSelector())) {
539 Diag(methods[j]->getLocation(), diag::warn_undef_method_impl,
540 methods[j]->getSelector().getName());
541 IncompleteImpl = true;
542 }
543 llvm::DenseSet<Selector> ClsMap;
544 // Check and see if class methods in category interface have been
545 // implemented in its implementation class.
Chris Lattner4c525092007-12-12 17:58:05 +0000546 for (ObjcCategoryImplDecl::classmeth_iterator
547 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
548 I != E; ++I)
549 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000550
551 methods = CatClassDecl->getClassMethods();
552 for (int j = 0; j < CatClassDecl->getNumClassMethods(); j++)
553 if (!ClsMap.count(methods[j]->getSelector())) {
554 Diag(methods[j]->getLocation(), diag::warn_undef_method_impl,
555 methods[j]->getSelector().getName());
556 IncompleteImpl = true;
557 }
558
559 // Check the protocol list for unimplemented methods in the @implementation
560 // class.
561 ObjcProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
562 for (int i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
563 ObjcProtocolDecl* PDecl = protocols[i];
564 CheckProtocolMethodDefs(PDecl, IncompleteImpl, InsMap, ClsMap);
565 }
566 if (IncompleteImpl)
567 Diag(CatImplDecl->getLocation(), diag::warn_incomplete_impl_category,
568 CatClassDecl->getName());
569}
570
571/// ActOnForwardClassDeclaration -
572Action::DeclTy *
573Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
574 IdentifierInfo **IdentList, unsigned NumElts)
575{
576 llvm::SmallVector<ObjcInterfaceDecl*, 32> Interfaces;
577
578 for (unsigned i = 0; i != NumElts; ++i) {
579 // Check for another declaration kind with the same name.
580 ScopedDecl *PrevDecl = LookupInterfaceDecl(IdentList[i]);
581 if (PrevDecl && !isa<ObjcInterfaceDecl>(PrevDecl)) {
582 Diag(AtClassLoc, diag::err_redefinition_different_kind,
583 IdentList[i]->getName());
584 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
585 }
586 ObjcInterfaceDecl *IDecl = dyn_cast_or_null<ObjcInterfaceDecl>(PrevDecl);
587 if (!IDecl) { // Not already seen? Make a forward decl.
588 IDecl = new ObjcInterfaceDecl(AtClassLoc, 0, IdentList[i], true);
589 // Chain & install the interface decl into the identifier.
590 IDecl->setNext(IdentList[i]->getFETokenInfo<ScopedDecl>());
591 IdentList[i]->setFETokenInfo(IDecl);
592
593 // Remember that this needs to be removed when the scope is popped.
594 TUScope->AddDecl(IDecl);
595 }
596
597 Interfaces.push_back(IDecl);
598 }
599
600 return new ObjcClassDecl(AtClassLoc, &Interfaces[0], Interfaces.size());
601}
602
603
604/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
605/// returns true, or false, accordingly.
606/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
607bool Sema::MatchTwoMethodDeclarations(const ObjcMethodDecl *Method,
608 const ObjcMethodDecl *PrevMethod) {
609 if (Method->getResultType().getCanonicalType() !=
610 PrevMethod->getResultType().getCanonicalType())
611 return false;
612 for (int i = 0; i < Method->getNumParams(); i++) {
613 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
614 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
615 if (ParamDecl->getCanonicalType() != PrevParamDecl->getCanonicalType())
616 return false;
617 }
618 return true;
619}
620
621void Sema::AddInstanceMethodToGlobalPool(ObjcMethodDecl *Method) {
622 ObjcMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
623 if (!FirstMethod.Method) {
624 // Haven't seen a method with this selector name yet - add it.
625 FirstMethod.Method = Method;
626 FirstMethod.Next = 0;
627 } else {
628 // We've seen a method with this name, now check the type signature(s).
629 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
630
631 for (ObjcMethodList *Next = FirstMethod.Next; !match && Next;
632 Next = Next->Next)
633 match = MatchTwoMethodDeclarations(Method, Next->Method);
634
635 if (!match) {
636 // We have a new signature for an existing method - add it.
637 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
638 struct ObjcMethodList *OMI = new ObjcMethodList(Method, FirstMethod.Next);
639 FirstMethod.Next = OMI;
640 }
641 }
642}
643
644void Sema::AddFactoryMethodToGlobalPool(ObjcMethodDecl *Method) {
645 ObjcMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
646 if (!FirstMethod.Method) {
647 // Haven't seen a method with this selector name yet - add it.
648 FirstMethod.Method = Method;
649 FirstMethod.Next = 0;
650 } else {
651 // We've seen a method with this name, now check the type signature(s).
652 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
653
654 for (ObjcMethodList *Next = FirstMethod.Next; !match && Next;
655 Next = Next->Next)
656 match = MatchTwoMethodDeclarations(Method, Next->Method);
657
658 if (!match) {
659 // We have a new signature for an existing method - add it.
660 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
661 struct ObjcMethodList *OMI = new ObjcMethodList(Method, FirstMethod.Next);
662 FirstMethod.Next = OMI;
663 }
664 }
665}
666
667void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
668 DeclTy **allMethods, unsigned allNum,
669 DeclTy **allProperties, unsigned pNum) {
670 Decl *ClassDecl = static_cast<Decl *>(classDecl);
671
672 // FIXME: If we don't have a ClassDecl, we have an error. I (snaroff) would
673 // prefer we always pass in a decl. If the decl has an error, isInvalidDecl()
674 // should be true.
675 if (!ClassDecl)
676 return;
677
678 llvm::SmallVector<ObjcMethodDecl*, 32> insMethods;
679 llvm::SmallVector<ObjcMethodDecl*, 16> clsMethods;
680
681 llvm::DenseMap<Selector, const ObjcMethodDecl*> InsMap;
682 llvm::DenseMap<Selector, const ObjcMethodDecl*> ClsMap;
683
684 bool isInterfaceDeclKind =
685 (isa<ObjcInterfaceDecl>(ClassDecl) || isa<ObjcCategoryDecl>(ClassDecl)
686 || isa<ObjcProtocolDecl>(ClassDecl));
687 bool checkIdenticalMethods = isa<ObjcImplementationDecl>(ClassDecl);
688
689 // TODO: property declaration in category and protocols.
690 if (pNum != 0 && isa<ObjcInterfaceDecl>(ClassDecl)) {
691 ObjcPropertyDecl **properties = new ObjcPropertyDecl*[pNum];
692 memcpy(properties, allProperties, pNum*sizeof(ObjcPropertyDecl*));
693 dyn_cast<ObjcInterfaceDecl>(ClassDecl)->setPropertyDecls(properties);
694 dyn_cast<ObjcInterfaceDecl>(ClassDecl)->setNumPropertyDecl(pNum);
695 }
696
697 for (unsigned i = 0; i < allNum; i++ ) {
698 ObjcMethodDecl *Method =
699 cast_or_null<ObjcMethodDecl>(static_cast<Decl*>(allMethods[i]));
700
701 if (!Method) continue; // Already issued a diagnostic.
702 if (Method->isInstance()) {
703 /// Check for instance method of the same name with incompatible types
704 const ObjcMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
705 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
706 : false;
707 if (isInterfaceDeclKind && PrevMethod && !match
708 || checkIdenticalMethods && match) {
709 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
710 Method->getSelector().getName());
711 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
712 } else {
713 insMethods.push_back(Method);
714 InsMap[Method->getSelector()] = Method;
715 /// The following allows us to typecheck messages to "id".
716 AddInstanceMethodToGlobalPool(Method);
717 }
718 }
719 else {
720 /// Check for class method of the same name with incompatible types
721 const ObjcMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
722 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
723 : false;
724 if (isInterfaceDeclKind && PrevMethod && !match
725 || checkIdenticalMethods && match) {
726 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
727 Method->getSelector().getName());
728 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
729 } else {
730 clsMethods.push_back(Method);
731 ClsMap[Method->getSelector()] = Method;
732 /// The following allows us to typecheck messages to "id".
733 AddInstanceMethodToGlobalPool(Method);
734 }
735 }
736 }
737
738 if (ObjcInterfaceDecl *I = dyn_cast<ObjcInterfaceDecl>(ClassDecl)) {
739 I->addMethods(&insMethods[0], insMethods.size(),
740 &clsMethods[0], clsMethods.size(), AtEndLoc);
741 } else if (ObjcProtocolDecl *P = dyn_cast<ObjcProtocolDecl>(ClassDecl)) {
742 P->addMethods(&insMethods[0], insMethods.size(),
743 &clsMethods[0], clsMethods.size(), AtEndLoc);
744 }
745 else if (ObjcCategoryDecl *C = dyn_cast<ObjcCategoryDecl>(ClassDecl)) {
746 C->addMethods(&insMethods[0], insMethods.size(),
747 &clsMethods[0], clsMethods.size(), AtEndLoc);
748 }
749 else if (ObjcImplementationDecl *IC =
750 dyn_cast<ObjcImplementationDecl>(ClassDecl)) {
751 IC->setLocEnd(AtEndLoc);
752 if (ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
753 ImplMethodsVsClassMethods(IC, IDecl);
754 } else {
755 ObjcCategoryImplDecl* CatImplClass = cast<ObjcCategoryImplDecl>(ClassDecl);
756 CatImplClass->setLocEnd(AtEndLoc);
757 ObjcInterfaceDecl* IDecl = CatImplClass->getClassInterface();
758 // Find category interface decl and then check that all methods declared
759 // in this interface is implemented in the category @implementation.
760 if (IDecl) {
761 for (ObjcCategoryDecl *Categories = IDecl->getCategoryList();
762 Categories; Categories = Categories->getNextClassCategory()) {
763 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
764 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
765 break;
766 }
767 }
768 }
769 }
770}
771
772
773/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
774/// objective-c's type qualifier from the parser version of the same info.
775static Decl::ObjcDeclQualifier
776CvtQTToAstBitMask(ObjcDeclSpec::ObjcDeclQualifier PQTVal) {
777 Decl::ObjcDeclQualifier ret = Decl::OBJC_TQ_None;
778 if (PQTVal & ObjcDeclSpec::DQ_In)
779 ret = (Decl::ObjcDeclQualifier)(ret | Decl::OBJC_TQ_In);
780 if (PQTVal & ObjcDeclSpec::DQ_Inout)
781 ret = (Decl::ObjcDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
782 if (PQTVal & ObjcDeclSpec::DQ_Out)
783 ret = (Decl::ObjcDeclQualifier)(ret | Decl::OBJC_TQ_Out);
784 if (PQTVal & ObjcDeclSpec::DQ_Bycopy)
785 ret = (Decl::ObjcDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
786 if (PQTVal & ObjcDeclSpec::DQ_Byref)
787 ret = (Decl::ObjcDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
788 if (PQTVal & ObjcDeclSpec::DQ_Oneway)
789 ret = (Decl::ObjcDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
790
791 return ret;
792}
793
794Sema::DeclTy *Sema::ActOnMethodDeclaration(
795 SourceLocation MethodLoc, SourceLocation EndLoc,
796 tok::TokenKind MethodType, DeclTy *ClassDecl,
797 ObjcDeclSpec &ReturnQT, TypeTy *ReturnType,
798 Selector Sel,
799 // optional arguments. The number of types/arguments is obtained
800 // from the Sel.getNumArgs().
801 ObjcDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
802 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
803 bool isVariadic) {
804 llvm::SmallVector<ParmVarDecl*, 16> Params;
805
806 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
807 // FIXME: arg->AttrList must be stored too!
808 QualType argType;
809
810 if (ArgTypes[i])
811 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
812 else
813 argType = Context.getObjcIdType();
814 ParmVarDecl* Param = new ParmVarDecl(SourceLocation(/*FIXME*/), ArgNames[i],
815 argType, VarDecl::None, 0);
816 Param->setObjcDeclQualifier(
817 CvtQTToAstBitMask(ArgQT[i].getObjcDeclQualifier()));
818 Params.push_back(Param);
819 }
820 QualType resultDeclType;
821
822 if (ReturnType)
823 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
824 else // get the type for "id".
825 resultDeclType = Context.getObjcIdType();
826
827 Decl *CDecl = static_cast<Decl*>(ClassDecl);
828 ObjcMethodDecl* ObjcMethod = new ObjcMethodDecl(MethodLoc, EndLoc, Sel,
829 resultDeclType,
830 CDecl,
831 0, -1, AttrList,
832 MethodType == tok::minus, isVariadic,
833 MethodDeclKind == tok::objc_optional ?
834 ObjcMethodDecl::Optional :
835 ObjcMethodDecl::Required);
836 ObjcMethod->setMethodParams(&Params[0], Sel.getNumArgs());
837 ObjcMethod->setObjcDeclQualifier(
838 CvtQTToAstBitMask(ReturnQT.getObjcDeclQualifier()));
839 const ObjcMethodDecl *PrevMethod = 0;
840
841 // For implementations (which can be very "coarse grain"), we add the
842 // method now. This allows the AST to implement lookup methods that work
843 // incrementally (without waiting until we parse the @end). It also allows
844 // us to flag multiple declaration errors as they occur.
845 if (ObjcImplementationDecl *ImpDecl =
846 dyn_cast<ObjcImplementationDecl>(CDecl)) {
847 if (MethodType == tok::minus) {
848 PrevMethod = ImpDecl->lookupInstanceMethod(Sel);
849 ImpDecl->addInstanceMethod(ObjcMethod);
850 } else {
851 PrevMethod = ImpDecl->lookupClassMethod(Sel);
852 ImpDecl->addClassMethod(ObjcMethod);
853 }
854 }
855 else if (ObjcCategoryImplDecl *CatImpDecl =
856 dyn_cast<ObjcCategoryImplDecl>(CDecl)) {
857 if (MethodType == tok::minus) {
858 PrevMethod = CatImpDecl->lookupInstanceMethod(Sel);
859 CatImpDecl->addInstanceMethod(ObjcMethod);
860 } else {
861 PrevMethod = CatImpDecl->lookupClassMethod(Sel);
862 CatImpDecl->addClassMethod(ObjcMethod);
863 }
864 }
865 if (PrevMethod) {
866 // You can never have two method definitions with the same name.
867 Diag(ObjcMethod->getLocation(), diag::error_duplicate_method_decl,
868 ObjcMethod->getSelector().getName());
869 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
870 }
871 return ObjcMethod;
872}
873
874Sema::DeclTy *Sema::ActOnAddObjcProperties(SourceLocation AtLoc,
875 DeclTy **allProperties, unsigned NumProperties, ObjcDeclSpec &DS) {
876 ObjcPropertyDecl *PDecl = new ObjcPropertyDecl(AtLoc);
877
878 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_readonly)
879 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_readonly);
880
881 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_getter) {
882 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_getter);
883 PDecl->setGetterName(DS.getGetterName());
884 }
885
886 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_setter) {
887 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_setter);
888 PDecl->setSetterName(DS.getSetterName());
889 }
890
891 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_assign)
892 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_assign);
893
894 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_readwrite)
895 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_readwrite);
896
897 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_retain)
898 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_retain);
899
900 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_copy)
901 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_copy);
902
903 if(DS.getPropertyAttributes() & ObjcDeclSpec::DQ_PR_nonatomic)
904 PDecl->setPropertyAttributes(ObjcPropertyDecl::OBJC_PR_nonatomic);
905
906 PDecl->setNumPropertyDecls(NumProperties);
907 if (NumProperties != 0) {
908 ObjcIvarDecl **properties = new ObjcIvarDecl*[NumProperties];
909 memcpy(properties, allProperties, NumProperties*sizeof(ObjcIvarDecl*));
910 PDecl->setPropertyDecls(properties);
911 }
912 return PDecl;
913}
914