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