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