SemaDeclObjC.cpp revision b1224f69089815fd66b04ae33215b7fba10780aa
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 "clang/Sema/SemaInternal.h" 15#include "clang/Sema/Lookup.h" 16#include "clang/Sema/ExternalSemaSource.h" 17#include "clang/Sema/Scope.h" 18#include "clang/Sema/ScopeInfo.h" 19#include "clang/AST/Expr.h" 20#include "clang/AST/ASTContext.h" 21#include "clang/AST/DeclObjC.h" 22#include "clang/Sema/DeclSpec.h" 23#include "llvm/ADT/DenseSet.h" 24 25using namespace clang; 26 27/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible 28/// and user declared, in the method definition's AST. 29void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) { 30 assert(getCurMethodDecl() == 0 && "Method parsing confused"); 31 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); 32 33 // If we don't have a valid method decl, simply return. 34 if (!MDecl) 35 return; 36 37 // Allow the rest of sema to find private method decl implementations. 38 if (MDecl->isInstanceMethod()) 39 AddInstanceMethodToGlobalPool(MDecl, true); 40 else 41 AddFactoryMethodToGlobalPool(MDecl, true); 42 43 // Allow all of Sema to see that we are entering a method definition. 44 PushDeclContext(FnBodyScope, MDecl); 45 PushFunctionScope(); 46 47 // Create Decl objects for each parameter, entrring them in the scope for 48 // binding to their use. 49 50 // Insert the invisible arguments, self and _cmd! 51 MDecl->createImplicitParams(Context, MDecl->getClassInterface()); 52 53 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); 54 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); 55 56 // Introduce all of the other parameters into this scope. 57 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), 58 E = MDecl->param_end(); PI != E; ++PI) { 59 ParmVarDecl *Param = (*PI); 60 if (!Param->isInvalidDecl() && 61 RequireCompleteType(Param->getLocation(), Param->getType(), 62 diag::err_typecheck_decl_incomplete_type)) 63 Param->setInvalidDecl(); 64 if ((*PI)->getIdentifier()) 65 PushOnScopeChains(*PI, FnBodyScope); 66 } 67 // Warn on implementating deprecated methods under 68 // -Wdeprecated-implementations flag. 69 // FIXME. Refactor using common routine. 70 unsigned DIAG = diag::warn_depercated_def; 71 if (Diags.getDiagnosticLevel(DIAG, MDecl->getLocation()) 72 != Diagnostic::Ignored) 73 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) { 74 if (ObjCMethodDecl *IMD = 75 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod())) 76 if (NamedDecl *ND = dyn_cast<NamedDecl>(IMD)) 77 if (ND->getAttr<DeprecatedAttr>()) { 78 Diag(MDecl->getLocation(), DIAG) << 0; 79 Diag(IMD->getLocation(), diag::note_method_declared_at); 80 } 81 } 82} 83 84Decl *Sema:: 85ActOnStartClassInterface(SourceLocation AtInterfaceLoc, 86 IdentifierInfo *ClassName, SourceLocation ClassLoc, 87 IdentifierInfo *SuperName, SourceLocation SuperLoc, 88 Decl * const *ProtoRefs, unsigned NumProtoRefs, 89 const SourceLocation *ProtoLocs, 90 SourceLocation EndProtoLoc, AttributeList *AttrList) { 91 assert(ClassName && "Missing class identifier"); 92 93 // Check for another declaration kind with the same name. 94 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc, 95 LookupOrdinaryName, ForRedeclaration); 96 97 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 98 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; 99 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 100 } 101 102 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 103 if (IDecl) { 104 // Class already seen. Is it a forward declaration? 105 if (!IDecl->isForwardDecl()) { 106 IDecl->setInvalidDecl(); 107 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName(); 108 Diag(IDecl->getLocation(), diag::note_previous_definition); 109 110 // Return the previous class interface. 111 // FIXME: don't leak the objects passed in! 112 return IDecl; 113 } else { 114 IDecl->setLocation(AtInterfaceLoc); 115 IDecl->setForwardDecl(false); 116 IDecl->setClassLoc(ClassLoc); 117 // If the forward decl was in a PCH, we need to write it again in a 118 // dependent AST file. 119 IDecl->setChangedSinceDeserialization(true); 120 121 // Since this ObjCInterfaceDecl was created by a forward declaration, 122 // we now add it to the DeclContext since it wasn't added before 123 // (see ActOnForwardClassDeclaration). 124 IDecl->setLexicalDeclContext(CurContext); 125 CurContext->addDecl(IDecl); 126 127 if (AttrList) 128 ProcessDeclAttributeList(TUScope, IDecl, AttrList); 129 } 130 } else { 131 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, 132 ClassName, ClassLoc); 133 if (AttrList) 134 ProcessDeclAttributeList(TUScope, IDecl, AttrList); 135 136 PushOnScopeChains(IDecl, TUScope); 137 } 138 139 if (SuperName) { 140 // Check if a different kind of symbol declared in this scope. 141 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc, 142 LookupOrdinaryName); 143 144 if (!PrevDecl) { 145 // Try to correct for a typo in the superclass name. 146 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName); 147 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) && 148 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) { 149 Diag(SuperLoc, diag::err_undef_superclass_suggest) 150 << SuperName << ClassName << PrevDecl->getDeclName(); 151 Diag(PrevDecl->getLocation(), diag::note_previous_decl) 152 << PrevDecl->getDeclName(); 153 } 154 } 155 156 if (PrevDecl == IDecl) { 157 Diag(SuperLoc, diag::err_recursive_superclass) 158 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); 159 IDecl->setLocEnd(ClassLoc); 160 } else { 161 ObjCInterfaceDecl *SuperClassDecl = 162 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 163 164 // Diagnose classes that inherit from deprecated classes. 165 if (SuperClassDecl) 166 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); 167 168 if (PrevDecl && SuperClassDecl == 0) { 169 // The previous declaration was not a class decl. Check if we have a 170 // typedef. If we do, get the underlying class type. 171 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) { 172 QualType T = TDecl->getUnderlyingType(); 173 if (T->isObjCObjectType()) { 174 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) 175 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); 176 } 177 } 178 179 // This handles the following case: 180 // 181 // typedef int SuperClass; 182 // @interface MyClass : SuperClass {} @end 183 // 184 if (!SuperClassDecl) { 185 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; 186 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 187 } 188 } 189 190 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) { 191 if (!SuperClassDecl) 192 Diag(SuperLoc, diag::err_undef_superclass) 193 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); 194 else if (SuperClassDecl->isForwardDecl()) 195 Diag(SuperLoc, diag::err_undef_superclass) 196 << SuperClassDecl->getDeclName() << ClassName 197 << SourceRange(AtInterfaceLoc, ClassLoc); 198 } 199 IDecl->setSuperClass(SuperClassDecl); 200 IDecl->setSuperClassLoc(SuperLoc); 201 IDecl->setLocEnd(SuperLoc); 202 } 203 } else { // we have a root class. 204 IDecl->setLocEnd(ClassLoc); 205 } 206 207 // Check then save referenced protocols. 208 if (NumProtoRefs) { 209 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, 210 ProtoLocs, Context); 211 IDecl->setLocEnd(EndProtoLoc); 212 } 213 214 CheckObjCDeclScope(IDecl); 215 return IDecl; 216} 217 218/// ActOnCompatiblityAlias - this action is called after complete parsing of 219/// @compatibility_alias declaration. It sets up the alias relationships. 220Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, 221 IdentifierInfo *AliasName, 222 SourceLocation AliasLocation, 223 IdentifierInfo *ClassName, 224 SourceLocation ClassLocation) { 225 // Look for previous declaration of alias name 226 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation, 227 LookupOrdinaryName, ForRedeclaration); 228 if (ADecl) { 229 if (isa<ObjCCompatibleAliasDecl>(ADecl)) 230 Diag(AliasLocation, diag::warn_previous_alias_decl); 231 else 232 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; 233 Diag(ADecl->getLocation(), diag::note_previous_declaration); 234 return 0; 235 } 236 // Check for class declaration 237 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, 238 LookupOrdinaryName, ForRedeclaration); 239 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) { 240 QualType T = TDecl->getUnderlyingType(); 241 if (T->isObjCObjectType()) { 242 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { 243 ClassName = IDecl->getIdentifier(); 244 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, 245 LookupOrdinaryName, ForRedeclaration); 246 } 247 } 248 } 249 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); 250 if (CDecl == 0) { 251 Diag(ClassLocation, diag::warn_undef_interface) << ClassName; 252 if (CDeclU) 253 Diag(CDeclU->getLocation(), diag::note_previous_declaration); 254 return 0; 255 } 256 257 // Everything checked out, instantiate a new alias declaration AST. 258 ObjCCompatibleAliasDecl *AliasDecl = 259 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); 260 261 if (!CheckObjCDeclScope(AliasDecl)) 262 PushOnScopeChains(AliasDecl, TUScope); 263 264 return AliasDecl; 265} 266 267void Sema::CheckForwardProtocolDeclarationForCircularDependency( 268 IdentifierInfo *PName, 269 SourceLocation &Ploc, SourceLocation PrevLoc, 270 const ObjCList<ObjCProtocolDecl> &PList) { 271 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), 272 E = PList.end(); I != E; ++I) { 273 274 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(), 275 Ploc)) { 276 if (PDecl->getIdentifier() == PName) { 277 Diag(Ploc, diag::err_protocol_has_circular_dependency); 278 Diag(PrevLoc, diag::note_previous_definition); 279 } 280 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, 281 PDecl->getLocation(), PDecl->getReferencedProtocols()); 282 } 283 } 284} 285 286Decl * 287Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, 288 IdentifierInfo *ProtocolName, 289 SourceLocation ProtocolLoc, 290 Decl * const *ProtoRefs, 291 unsigned NumProtoRefs, 292 const SourceLocation *ProtoLocs, 293 SourceLocation EndProtoLoc, 294 AttributeList *AttrList) { 295 // FIXME: Deal with AttrList. 296 assert(ProtocolName && "Missing protocol identifier"); 297 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc); 298 if (PDecl) { 299 // Protocol already seen. Better be a forward protocol declaration 300 if (!PDecl->isForwardDecl()) { 301 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; 302 Diag(PDecl->getLocation(), diag::note_previous_definition); 303 // Just return the protocol we already had. 304 // FIXME: don't leak the objects passed in! 305 return PDecl; 306 } 307 ObjCList<ObjCProtocolDecl> PList; 308 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); 309 CheckForwardProtocolDeclarationForCircularDependency( 310 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList); 311 312 // Make sure the cached decl gets a valid start location. 313 PDecl->setLocation(AtProtoInterfaceLoc); 314 PDecl->setForwardDecl(false); 315 CurContext->addDecl(PDecl); 316 // Repeat in dependent AST files. 317 PDecl->setChangedSinceDeserialization(true); 318 } else { 319 PDecl = ObjCProtocolDecl::Create(Context, CurContext, 320 AtProtoInterfaceLoc,ProtocolName); 321 PushOnScopeChains(PDecl, TUScope); 322 PDecl->setForwardDecl(false); 323 } 324 if (AttrList) 325 ProcessDeclAttributeList(TUScope, PDecl, AttrList); 326 if (NumProtoRefs) { 327 /// Check then save referenced protocols. 328 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, 329 ProtoLocs, Context); 330 PDecl->setLocEnd(EndProtoLoc); 331 } 332 333 CheckObjCDeclScope(PDecl); 334 return PDecl; 335} 336 337/// FindProtocolDeclaration - This routine looks up protocols and 338/// issues an error if they are not declared. It returns list of 339/// protocol declarations in its 'Protocols' argument. 340void 341Sema::FindProtocolDeclaration(bool WarnOnDeclarations, 342 const IdentifierLocPair *ProtocolId, 343 unsigned NumProtocols, 344 llvm::SmallVectorImpl<Decl *> &Protocols) { 345 for (unsigned i = 0; i != NumProtocols; ++i) { 346 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first, 347 ProtocolId[i].second); 348 if (!PDecl) { 349 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second, 350 LookupObjCProtocolName); 351 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) && 352 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) { 353 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest) 354 << ProtocolId[i].first << R.getLookupName(); 355 Diag(PDecl->getLocation(), diag::note_previous_decl) 356 << PDecl->getDeclName(); 357 } 358 } 359 360 if (!PDecl) { 361 Diag(ProtocolId[i].second, diag::err_undeclared_protocol) 362 << ProtocolId[i].first; 363 continue; 364 } 365 366 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second); 367 368 // If this is a forward declaration and we are supposed to warn in this 369 // case, do it. 370 if (WarnOnDeclarations && PDecl->isForwardDecl()) 371 Diag(ProtocolId[i].second, diag::warn_undef_protocolref) 372 << ProtocolId[i].first; 373 Protocols.push_back(PDecl); 374 } 375} 376 377/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of 378/// a class method in its extension. 379/// 380void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, 381 ObjCInterfaceDecl *ID) { 382 if (!ID) 383 return; // Possibly due to previous error 384 385 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; 386 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(), 387 e = ID->meth_end(); i != e; ++i) { 388 ObjCMethodDecl *MD = *i; 389 MethodMap[MD->getSelector()] = MD; 390 } 391 392 if (MethodMap.empty()) 393 return; 394 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(), 395 e = CAT->meth_end(); i != e; ++i) { 396 ObjCMethodDecl *Method = *i; 397 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; 398 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) { 399 Diag(Method->getLocation(), diag::err_duplicate_method_decl) 400 << Method->getDeclName(); 401 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 402 } 403 } 404} 405 406/// ActOnForwardProtocolDeclaration - Handle @protocol foo; 407Decl * 408Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, 409 const IdentifierLocPair *IdentList, 410 unsigned NumElts, 411 AttributeList *attrList) { 412 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; 413 llvm::SmallVector<SourceLocation, 8> ProtoLocs; 414 415 for (unsigned i = 0; i != NumElts; ++i) { 416 IdentifierInfo *Ident = IdentList[i].first; 417 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second); 418 bool isNew = false; 419 if (PDecl == 0) { // Not already seen? 420 PDecl = ObjCProtocolDecl::Create(Context, CurContext, 421 IdentList[i].second, Ident); 422 PushOnScopeChains(PDecl, TUScope, false); 423 isNew = true; 424 } 425 if (attrList) { 426 ProcessDeclAttributeList(TUScope, PDecl, attrList); 427 if (!isNew) 428 PDecl->setChangedSinceDeserialization(true); 429 } 430 Protocols.push_back(PDecl); 431 ProtoLocs.push_back(IdentList[i].second); 432 } 433 434 ObjCForwardProtocolDecl *PDecl = 435 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc, 436 Protocols.data(), Protocols.size(), 437 ProtoLocs.data()); 438 CurContext->addDecl(PDecl); 439 CheckObjCDeclScope(PDecl); 440 return PDecl; 441} 442 443Decl *Sema:: 444ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, 445 IdentifierInfo *ClassName, SourceLocation ClassLoc, 446 IdentifierInfo *CategoryName, 447 SourceLocation CategoryLoc, 448 Decl * const *ProtoRefs, 449 unsigned NumProtoRefs, 450 const SourceLocation *ProtoLocs, 451 SourceLocation EndProtoLoc) { 452 ObjCCategoryDecl *CDecl; 453 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); 454 455 /// Check that class of this category is already completely declared. 456 if (!IDecl || IDecl->isForwardDecl()) { 457 // Create an invalid ObjCCategoryDecl to serve as context for 458 // the enclosing method declarations. We mark the decl invalid 459 // to make it clear that this isn't a valid AST. 460 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, 461 ClassLoc, CategoryLoc, CategoryName); 462 CDecl->setInvalidDecl(); 463 Diag(ClassLoc, diag::err_undef_interface) << ClassName; 464 return CDecl; 465 } 466 467 if (!CategoryName && IDecl->getImplementation()) { 468 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName; 469 Diag(IDecl->getImplementation()->getLocation(), 470 diag::note_implementation_declared); 471 } 472 473 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, 474 ClassLoc, CategoryLoc, CategoryName); 475 // FIXME: PushOnScopeChains? 476 CurContext->addDecl(CDecl); 477 478 CDecl->setClassInterface(IDecl); 479 // Insert class extension to the list of class's categories. 480 if (!CategoryName) 481 CDecl->insertNextClassCategory(); 482 483 // If the interface is deprecated, warn about it. 484 (void)DiagnoseUseOfDecl(IDecl, ClassLoc); 485 486 if (CategoryName) { 487 /// Check for duplicate interface declaration for this category 488 ObjCCategoryDecl *CDeclChain; 489 for (CDeclChain = IDecl->getCategoryList(); CDeclChain; 490 CDeclChain = CDeclChain->getNextClassCategory()) { 491 if (CDeclChain->getIdentifier() == CategoryName) { 492 // Class extensions can be declared multiple times. 493 Diag(CategoryLoc, diag::warn_dup_category_def) 494 << ClassName << CategoryName; 495 Diag(CDeclChain->getLocation(), diag::note_previous_definition); 496 break; 497 } 498 } 499 if (!CDeclChain) 500 CDecl->insertNextClassCategory(); 501 } 502 503 if (NumProtoRefs) { 504 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, 505 ProtoLocs, Context); 506 // Protocols in the class extension belong to the class. 507 if (CDecl->IsClassExtension()) 508 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs, 509 NumProtoRefs, Context); 510 } 511 512 CheckObjCDeclScope(CDecl); 513 return CDecl; 514} 515 516/// ActOnStartCategoryImplementation - Perform semantic checks on the 517/// category implementation declaration and build an ObjCCategoryImplDecl 518/// object. 519Decl *Sema::ActOnStartCategoryImplementation( 520 SourceLocation AtCatImplLoc, 521 IdentifierInfo *ClassName, SourceLocation ClassLoc, 522 IdentifierInfo *CatName, SourceLocation CatLoc) { 523 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); 524 ObjCCategoryDecl *CatIDecl = 0; 525 if (IDecl) { 526 CatIDecl = IDecl->FindCategoryDeclaration(CatName); 527 if (!CatIDecl) { 528 // Category @implementation with no corresponding @interface. 529 // Create and install one. 530 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(), 531 SourceLocation(), SourceLocation(), 532 CatName); 533 CatIDecl->setClassInterface(IDecl); 534 CatIDecl->insertNextClassCategory(); 535 } 536 } 537 538 ObjCCategoryImplDecl *CDecl = 539 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName, 540 IDecl); 541 /// Check that class of this category is already completely declared. 542 if (!IDecl || IDecl->isForwardDecl()) 543 Diag(ClassLoc, diag::err_undef_interface) << ClassName; 544 545 // FIXME: PushOnScopeChains? 546 CurContext->addDecl(CDecl); 547 548 /// Check that CatName, category name, is not used in another implementation. 549 if (CatIDecl) { 550 if (CatIDecl->getImplementation()) { 551 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName 552 << CatName; 553 Diag(CatIDecl->getImplementation()->getLocation(), 554 diag::note_previous_definition); 555 } else { 556 CatIDecl->setImplementation(CDecl); 557 // Warn on implementating category of deprecated class under 558 // -Wdeprecated-implementations flag. 559 // FIXME. Refactor using common routine. 560 unsigned DIAG = diag::warn_depercated_def; 561 if (Diags.getDiagnosticLevel(DIAG, CDecl->getLocation()) 562 != Diagnostic::Ignored) 563 if (NamedDecl *ND = dyn_cast<NamedDecl>(IDecl)) 564 if (ND->getAttr<DeprecatedAttr>()) { 565 Diag(CDecl->getLocation(), DIAG) << 2; 566 Diag(IDecl->getLocation(), diag::note_previous_decl) << "class"; 567 } 568 569 } 570 } 571 572 CheckObjCDeclScope(CDecl); 573 return CDecl; 574} 575 576Decl *Sema::ActOnStartClassImplementation( 577 SourceLocation AtClassImplLoc, 578 IdentifierInfo *ClassName, SourceLocation ClassLoc, 579 IdentifierInfo *SuperClassname, 580 SourceLocation SuperClassLoc) { 581 ObjCInterfaceDecl* IDecl = 0; 582 // Check for another declaration kind with the same name. 583 NamedDecl *PrevDecl 584 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, 585 ForRedeclaration); 586 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 587 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; 588 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 589 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) { 590 // If this is a forward declaration of an interface, warn. 591 if (IDecl->isForwardDecl()) { 592 Diag(ClassLoc, diag::warn_undef_interface) << ClassName; 593 IDecl = 0; 594 } 595 } else { 596 // We did not find anything with the name ClassName; try to correct for 597 // typos in the class name. 598 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName); 599 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) && 600 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) { 601 // Suggest the (potentially) correct interface name. However, put the 602 // fix-it hint itself in a separate note, since changing the name in 603 // the warning would make the fix-it change semantics.However, don't 604 // provide a code-modification hint or use the typo name for recovery, 605 // because this is just a warning. The program may actually be correct. 606 Diag(ClassLoc, diag::warn_undef_interface_suggest) 607 << ClassName << R.getLookupName(); 608 Diag(IDecl->getLocation(), diag::note_previous_decl) 609 << R.getLookupName() 610 << FixItHint::CreateReplacement(ClassLoc, 611 R.getLookupName().getAsString()); 612 IDecl = 0; 613 } else { 614 Diag(ClassLoc, diag::warn_undef_interface) << ClassName; 615 } 616 } 617 618 // Check that super class name is valid class name 619 ObjCInterfaceDecl* SDecl = 0; 620 if (SuperClassname) { 621 // Check if a different kind of symbol declared in this scope. 622 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc, 623 LookupOrdinaryName); 624 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 625 Diag(SuperClassLoc, diag::err_redefinition_different_kind) 626 << SuperClassname; 627 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 628 } else { 629 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 630 if (!SDecl) 631 Diag(SuperClassLoc, diag::err_undef_superclass) 632 << SuperClassname << ClassName; 633 else if (IDecl && IDecl->getSuperClass() != SDecl) { 634 // This implementation and its interface do not have the same 635 // super class. 636 Diag(SuperClassLoc, diag::err_conflicting_super_class) 637 << SDecl->getDeclName(); 638 Diag(SDecl->getLocation(), diag::note_previous_definition); 639 } 640 } 641 } 642 643 if (!IDecl) { 644 // Legacy case of @implementation with no corresponding @interface. 645 // Build, chain & install the interface decl into the identifier. 646 647 // FIXME: Do we support attributes on the @implementation? If so we should 648 // copy them over. 649 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, 650 ClassName, ClassLoc, false, true); 651 IDecl->setSuperClass(SDecl); 652 IDecl->setLocEnd(ClassLoc); 653 654 PushOnScopeChains(IDecl, TUScope); 655 } else { 656 // Mark the interface as being completed, even if it was just as 657 // @class ....; 658 // declaration; the user cannot reopen it. 659 IDecl->setForwardDecl(false); 660 } 661 662 ObjCImplementationDecl* IMPDecl = 663 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc, 664 IDecl, SDecl); 665 666 if (CheckObjCDeclScope(IMPDecl)) 667 return IMPDecl; 668 669 // Check that there is no duplicate implementation of this class. 670 if (IDecl->getImplementation()) { 671 // FIXME: Don't leak everything! 672 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; 673 Diag(IDecl->getImplementation()->getLocation(), 674 diag::note_previous_definition); 675 } else { // add it to the list. 676 IDecl->setImplementation(IMPDecl); 677 PushOnScopeChains(IMPDecl, TUScope); 678 // Warn on implementating deprecated class under 679 // -Wdeprecated-implementations flag. 680 // FIXME. Refactor using common routine. 681 unsigned DIAG = diag::warn_depercated_def; 682 if (Diags.getDiagnosticLevel(DIAG, IMPDecl->getLocation()) 683 != Diagnostic::Ignored) 684 if (NamedDecl *ND = dyn_cast<NamedDecl>(IDecl)) 685 if (ND->getAttr<DeprecatedAttr>()) { 686 Diag(IMPDecl->getLocation(), DIAG) << 1; 687 Diag(IDecl->getLocation(), diag::note_previous_decl) << "class"; 688 } 689 } 690 return IMPDecl; 691} 692 693void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, 694 ObjCIvarDecl **ivars, unsigned numIvars, 695 SourceLocation RBrace) { 696 assert(ImpDecl && "missing implementation decl"); 697 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); 698 if (!IDecl) 699 return; 700 /// Check case of non-existing @interface decl. 701 /// (legacy objective-c @implementation decl without an @interface decl). 702 /// Add implementations's ivar to the synthesize class's ivar list. 703 if (IDecl->isImplicitInterfaceDecl()) { 704 IDecl->setLocEnd(RBrace); 705 // Add ivar's to class's DeclContext. 706 for (unsigned i = 0, e = numIvars; i != e; ++i) { 707 ivars[i]->setLexicalDeclContext(ImpDecl); 708 IDecl->makeDeclVisibleInContext(ivars[i], false); 709 ImpDecl->addDecl(ivars[i]); 710 } 711 712 return; 713 } 714 // If implementation has empty ivar list, just return. 715 if (numIvars == 0) 716 return; 717 718 assert(ivars && "missing @implementation ivars"); 719 if (LangOpts.ObjCNonFragileABI2) { 720 if (ImpDecl->getSuperClass()) 721 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use); 722 for (unsigned i = 0; i < numIvars; i++) { 723 ObjCIvarDecl* ImplIvar = ivars[i]; 724 if (const ObjCIvarDecl *ClsIvar = 725 IDecl->getIvarDecl(ImplIvar->getIdentifier())) { 726 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); 727 Diag(ClsIvar->getLocation(), diag::note_previous_definition); 728 continue; 729 } 730 // Instance ivar to Implementation's DeclContext. 731 ImplIvar->setLexicalDeclContext(ImpDecl); 732 IDecl->makeDeclVisibleInContext(ImplIvar, false); 733 ImpDecl->addDecl(ImplIvar); 734 } 735 return; 736 } 737 // Check interface's Ivar list against those in the implementation. 738 // names and types must match. 739 // 740 unsigned j = 0; 741 ObjCInterfaceDecl::ivar_iterator 742 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); 743 for (; numIvars > 0 && IVI != IVE; ++IVI) { 744 ObjCIvarDecl* ImplIvar = ivars[j++]; 745 ObjCIvarDecl* ClsIvar = *IVI; 746 assert (ImplIvar && "missing implementation ivar"); 747 assert (ClsIvar && "missing class ivar"); 748 749 // First, make sure the types match. 750 if (Context.getCanonicalType(ImplIvar->getType()) != 751 Context.getCanonicalType(ClsIvar->getType())) { 752 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) 753 << ImplIvar->getIdentifier() 754 << ImplIvar->getType() << ClsIvar->getType(); 755 Diag(ClsIvar->getLocation(), diag::note_previous_definition); 756 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) { 757 Expr *ImplBitWidth = ImplIvar->getBitWidth(); 758 Expr *ClsBitWidth = ClsIvar->getBitWidth(); 759 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() != 760 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) { 761 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth) 762 << ImplIvar->getIdentifier(); 763 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition); 764 } 765 } 766 // Make sure the names are identical. 767 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { 768 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) 769 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); 770 Diag(ClsIvar->getLocation(), diag::note_previous_definition); 771 } 772 --numIvars; 773 } 774 775 if (numIvars > 0) 776 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); 777 else if (IVI != IVE) 778 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); 779} 780 781void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, 782 bool &IncompleteImpl, unsigned DiagID) { 783 if (!IncompleteImpl) { 784 Diag(ImpLoc, diag::warn_incomplete_impl); 785 IncompleteImpl = true; 786 } 787 if (DiagID == diag::warn_unimplemented_protocol_method) 788 Diag(ImpLoc, DiagID) << method->getDeclName(); 789 else 790 Diag(method->getLocation(), DiagID) << method->getDeclName(); 791} 792 793/// Determines if type B can be substituted for type A. Returns true if we can 794/// guarantee that anything that the user will do to an object of type A can 795/// also be done to an object of type B. This is trivially true if the two 796/// types are the same, or if B is a subclass of A. It becomes more complex 797/// in cases where protocols are involved. 798/// 799/// Object types in Objective-C describe the minimum requirements for an 800/// object, rather than providing a complete description of a type. For 801/// example, if A is a subclass of B, then B* may refer to an instance of A. 802/// The principle of substitutability means that we may use an instance of A 803/// anywhere that we may use an instance of B - it will implement all of the 804/// ivars of B and all of the methods of B. 805/// 806/// This substitutability is important when type checking methods, because 807/// the implementation may have stricter type definitions than the interface. 808/// The interface specifies minimum requirements, but the implementation may 809/// have more accurate ones. For example, a method may privately accept 810/// instances of B, but only publish that it accepts instances of A. Any 811/// object passed to it will be type checked against B, and so will implicitly 812/// by a valid A*. Similarly, a method may return a subclass of the class that 813/// it is declared as returning. 814/// 815/// This is most important when considering subclassing. A method in a 816/// subclass must accept any object as an argument that its superclass's 817/// implementation accepts. It may, however, accept a more general type 818/// without breaking substitutability (i.e. you can still use the subclass 819/// anywhere that you can use the superclass, but not vice versa). The 820/// converse requirement applies to return types: the return type for a 821/// subclass method must be a valid object of the kind that the superclass 822/// advertises, but it may be specified more accurately. This avoids the need 823/// for explicit down-casting by callers. 824/// 825/// Note: This is a stricter requirement than for assignment. 826static bool isObjCTypeSubstitutable(ASTContext &Context, 827 const ObjCObjectPointerType *A, 828 const ObjCObjectPointerType *B, 829 bool rejectId) { 830 // Reject a protocol-unqualified id. 831 if (rejectId && B->isObjCIdType()) return false; 832 833 // If B is a qualified id, then A must also be a qualified id and it must 834 // implement all of the protocols in B. It may not be a qualified class. 835 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a 836 // stricter definition so it is not substitutable for id<A>. 837 if (B->isObjCQualifiedIdType()) { 838 return A->isObjCQualifiedIdType() && 839 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0), 840 QualType(B,0), 841 false); 842 } 843 844 /* 845 // id is a special type that bypasses type checking completely. We want a 846 // warning when it is used in one place but not another. 847 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false; 848 849 850 // If B is a qualified id, then A must also be a qualified id (which it isn't 851 // if we've got this far) 852 if (B->isObjCQualifiedIdType()) return false; 853 */ 854 855 // Now we know that A and B are (potentially-qualified) class types. The 856 // normal rules for assignment apply. 857 return Context.canAssignObjCInterfaces(A, B); 858} 859 860static SourceRange getTypeRange(TypeSourceInfo *TSI) { 861 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange()); 862} 863 864static void CheckMethodOverrideReturn(Sema &S, 865 ObjCMethodDecl *MethodImpl, 866 ObjCMethodDecl *MethodIface) { 867 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(), 868 MethodIface->getResultType())) 869 return; 870 871 unsigned DiagID = diag::warn_conflicting_ret_types; 872 873 // Mismatches between ObjC pointers go into a different warning 874 // category, and sometimes they're even completely whitelisted. 875 if (const ObjCObjectPointerType *ImplPtrTy = 876 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) { 877 if (const ObjCObjectPointerType *IfacePtrTy = 878 MethodIface->getResultType()->getAs<ObjCObjectPointerType>()) { 879 // Allow non-matching return types as long as they don't violate 880 // the principle of substitutability. Specifically, we permit 881 // return types that are subclasses of the declared return type, 882 // or that are more-qualified versions of the declared type. 883 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false)) 884 return; 885 886 DiagID = diag::warn_non_covariant_ret_types; 887 } 888 } 889 890 S.Diag(MethodImpl->getLocation(), DiagID) 891 << MethodImpl->getDeclName() 892 << MethodIface->getResultType() 893 << MethodImpl->getResultType() 894 << getTypeRange(MethodImpl->getResultTypeSourceInfo()); 895 S.Diag(MethodIface->getLocation(), diag::note_previous_definition) 896 << getTypeRange(MethodIface->getResultTypeSourceInfo()); 897} 898 899static void CheckMethodOverrideParam(Sema &S, 900 ObjCMethodDecl *MethodImpl, 901 ObjCMethodDecl *MethodIface, 902 ParmVarDecl *ImplVar, 903 ParmVarDecl *IfaceVar) { 904 QualType ImplTy = ImplVar->getType(); 905 QualType IfaceTy = IfaceVar->getType(); 906 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy)) 907 return; 908 909 unsigned DiagID = diag::warn_conflicting_param_types; 910 911 // Mismatches between ObjC pointers go into a different warning 912 // category, and sometimes they're even completely whitelisted. 913 if (const ObjCObjectPointerType *ImplPtrTy = 914 ImplTy->getAs<ObjCObjectPointerType>()) { 915 if (const ObjCObjectPointerType *IfacePtrTy = 916 IfaceTy->getAs<ObjCObjectPointerType>()) { 917 // Allow non-matching argument types as long as they don't 918 // violate the principle of substitutability. Specifically, the 919 // implementation must accept any objects that the superclass 920 // accepts, however it may also accept others. 921 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true)) 922 return; 923 924 DiagID = diag::warn_non_contravariant_param_types; 925 } 926 } 927 928 S.Diag(ImplVar->getLocation(), DiagID) 929 << getTypeRange(ImplVar->getTypeSourceInfo()) 930 << MethodImpl->getDeclName() << IfaceTy << ImplTy; 931 S.Diag(IfaceVar->getLocation(), diag::note_previous_definition) 932 << getTypeRange(IfaceVar->getTypeSourceInfo()); 933} 934 935 936void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, 937 ObjCMethodDecl *IntfMethodDecl) { 938 CheckMethodOverrideReturn(*this, ImpMethodDecl, IntfMethodDecl); 939 940 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), 941 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end(); 942 IM != EM; ++IM, ++IF) 943 CheckMethodOverrideParam(*this, ImpMethodDecl, IntfMethodDecl, *IM, *IF); 944 945 if (ImpMethodDecl->isVariadic() != IntfMethodDecl->isVariadic()) { 946 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic); 947 Diag(IntfMethodDecl->getLocation(), diag::note_previous_declaration); 948 } 949} 950 951/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely 952/// improve the efficiency of selector lookups and type checking by associating 953/// with each protocol / interface / category the flattened instance tables. If 954/// we used an immutable set to keep the table then it wouldn't add significant 955/// memory cost and it would be handy for lookups. 956 957/// CheckProtocolMethodDefs - This routine checks unimplemented methods 958/// Declared in protocol, and those referenced by it. 959void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, 960 ObjCProtocolDecl *PDecl, 961 bool& IncompleteImpl, 962 const llvm::DenseSet<Selector> &InsMap, 963 const llvm::DenseSet<Selector> &ClsMap, 964 ObjCContainerDecl *CDecl) { 965 ObjCInterfaceDecl *IDecl; 966 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) 967 IDecl = C->getClassInterface(); 968 else 969 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); 970 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null"); 971 972 ObjCInterfaceDecl *Super = IDecl->getSuperClass(); 973 ObjCInterfaceDecl *NSIDecl = 0; 974 if (getLangOptions().NeXTRuntime) { 975 // check to see if class implements forwardInvocation method and objects 976 // of this class are derived from 'NSProxy' so that to forward requests 977 // from one object to another. 978 // Under such conditions, which means that every method possible is 979 // implemented in the class, we should not issue "Method definition not 980 // found" warnings. 981 // FIXME: Use a general GetUnarySelector method for this. 982 IdentifierInfo* II = &Context.Idents.get("forwardInvocation"); 983 Selector fISelector = Context.Selectors.getSelector(1, &II); 984 if (InsMap.count(fISelector)) 985 // Is IDecl derived from 'NSProxy'? If so, no instance methods 986 // need be implemented in the implementation. 987 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy")); 988 } 989 990 // If a method lookup fails locally we still need to look and see if 991 // the method was implemented by a base class or an inherited 992 // protocol. This lookup is slow, but occurs rarely in correct code 993 // and otherwise would terminate in a warning. 994 995 // check unimplemented instance methods. 996 if (!NSIDecl) 997 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), 998 E = PDecl->instmeth_end(); I != E; ++I) { 999 ObjCMethodDecl *method = *I; 1000 if (method->getImplementationControl() != ObjCMethodDecl::Optional && 1001 !method->isSynthesized() && !InsMap.count(method->getSelector()) && 1002 (!Super || 1003 !Super->lookupInstanceMethod(method->getSelector()))) { 1004 // Ugly, but necessary. Method declared in protcol might have 1005 // have been synthesized due to a property declared in the class which 1006 // uses the protocol. 1007 ObjCMethodDecl *MethodInClass = 1008 IDecl->lookupInstanceMethod(method->getSelector()); 1009 if (!MethodInClass || !MethodInClass->isSynthesized()) { 1010 unsigned DIAG = diag::warn_unimplemented_protocol_method; 1011 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) 1012 != Diagnostic::Ignored) { 1013 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG); 1014 Diag(method->getLocation(), diag::note_method_declared_at); 1015 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at) 1016 << PDecl->getDeclName(); 1017 } 1018 } 1019 } 1020 } 1021 // check unimplemented class methods 1022 for (ObjCProtocolDecl::classmeth_iterator 1023 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); 1024 I != E; ++I) { 1025 ObjCMethodDecl *method = *I; 1026 if (method->getImplementationControl() != ObjCMethodDecl::Optional && 1027 !ClsMap.count(method->getSelector()) && 1028 (!Super || !Super->lookupClassMethod(method->getSelector()))) { 1029 unsigned DIAG = diag::warn_unimplemented_protocol_method; 1030 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) { 1031 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG); 1032 Diag(method->getLocation(), diag::note_method_declared_at); 1033 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) << 1034 PDecl->getDeclName(); 1035 } 1036 } 1037 } 1038 // Check on this protocols's referenced protocols, recursively. 1039 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), 1040 E = PDecl->protocol_end(); PI != E; ++PI) 1041 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl); 1042} 1043 1044/// MatchAllMethodDeclarations - Check methods declaraed in interface or 1045/// or protocol against those declared in their implementations. 1046/// 1047void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap, 1048 const llvm::DenseSet<Selector> &ClsMap, 1049 llvm::DenseSet<Selector> &InsMapSeen, 1050 llvm::DenseSet<Selector> &ClsMapSeen, 1051 ObjCImplDecl* IMPDecl, 1052 ObjCContainerDecl* CDecl, 1053 bool &IncompleteImpl, 1054 bool ImmediateClass) { 1055 // Check and see if instance methods in class interface have been 1056 // implemented in the implementation class. If so, their types match. 1057 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(), 1058 E = CDecl->instmeth_end(); I != E; ++I) { 1059 if (InsMapSeen.count((*I)->getSelector())) 1060 continue; 1061 InsMapSeen.insert((*I)->getSelector()); 1062 if (!(*I)->isSynthesized() && 1063 !InsMap.count((*I)->getSelector())) { 1064 if (ImmediateClass) 1065 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl, 1066 diag::note_undef_method_impl); 1067 continue; 1068 } else { 1069 ObjCMethodDecl *ImpMethodDecl = 1070 IMPDecl->getInstanceMethod((*I)->getSelector()); 1071 ObjCMethodDecl *IntfMethodDecl = 1072 CDecl->getInstanceMethod((*I)->getSelector()); 1073 assert(IntfMethodDecl && 1074 "IntfMethodDecl is null in ImplMethodsVsClassMethods"); 1075 // ImpMethodDecl may be null as in a @dynamic property. 1076 if (ImpMethodDecl) 1077 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); 1078 } 1079 } 1080 1081 // Check and see if class methods in class interface have been 1082 // implemented in the implementation class. If so, their types match. 1083 for (ObjCInterfaceDecl::classmeth_iterator 1084 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) { 1085 if (ClsMapSeen.count((*I)->getSelector())) 1086 continue; 1087 ClsMapSeen.insert((*I)->getSelector()); 1088 if (!ClsMap.count((*I)->getSelector())) { 1089 if (ImmediateClass) 1090 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl, 1091 diag::note_undef_method_impl); 1092 } else { 1093 ObjCMethodDecl *ImpMethodDecl = 1094 IMPDecl->getClassMethod((*I)->getSelector()); 1095 ObjCMethodDecl *IntfMethodDecl = 1096 CDecl->getClassMethod((*I)->getSelector()); 1097 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); 1098 } 1099 } 1100 1101 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { 1102 // Also methods in class extensions need be looked at next. 1103 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension(); 1104 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) 1105 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, 1106 IMPDecl, 1107 const_cast<ObjCCategoryDecl *>(ClsExtDecl), 1108 IncompleteImpl, false); 1109 1110 // Check for any implementation of a methods declared in protocol. 1111 for (ObjCInterfaceDecl::all_protocol_iterator 1112 PI = I->all_referenced_protocol_begin(), 1113 E = I->all_referenced_protocol_end(); PI != E; ++PI) 1114 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, 1115 IMPDecl, 1116 (*PI), IncompleteImpl, false); 1117 if (I->getSuperClass()) 1118 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, 1119 IMPDecl, 1120 I->getSuperClass(), IncompleteImpl, false); 1121 } 1122} 1123 1124void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl, 1125 ObjCContainerDecl* CDecl, 1126 bool IncompleteImpl) { 1127 llvm::DenseSet<Selector> InsMap; 1128 // Check and see if instance methods in class interface have been 1129 // implemented in the implementation class. 1130 for (ObjCImplementationDecl::instmeth_iterator 1131 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I) 1132 InsMap.insert((*I)->getSelector()); 1133 1134 // Check and see if properties declared in the interface have either 1) 1135 // an implementation or 2) there is a @synthesize/@dynamic implementation 1136 // of the property in the @implementation. 1137 if (isa<ObjCInterfaceDecl>(CDecl) && 1138 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)) 1139 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap); 1140 1141 llvm::DenseSet<Selector> ClsMap; 1142 for (ObjCImplementationDecl::classmeth_iterator 1143 I = IMPDecl->classmeth_begin(), 1144 E = IMPDecl->classmeth_end(); I != E; ++I) 1145 ClsMap.insert((*I)->getSelector()); 1146 1147 // Check for type conflict of methods declared in a class/protocol and 1148 // its implementation; if any. 1149 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen; 1150 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, 1151 IMPDecl, CDecl, 1152 IncompleteImpl, true); 1153 1154 // Check the protocol list for unimplemented methods in the @implementation 1155 // class. 1156 // Check and see if class methods in class interface have been 1157 // implemented in the implementation class. 1158 1159 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { 1160 for (ObjCInterfaceDecl::all_protocol_iterator 1161 PI = I->all_referenced_protocol_begin(), 1162 E = I->all_referenced_protocol_end(); PI != E; ++PI) 1163 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, 1164 InsMap, ClsMap, I); 1165 // Check class extensions (unnamed categories) 1166 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension(); 1167 Categories; Categories = Categories->getNextClassExtension()) 1168 ImplMethodsVsClassMethods(S, IMPDecl, 1169 const_cast<ObjCCategoryDecl*>(Categories), 1170 IncompleteImpl); 1171 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { 1172 // For extended class, unimplemented methods in its protocols will 1173 // be reported in the primary class. 1174 if (!C->IsClassExtension()) { 1175 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(), 1176 E = C->protocol_end(); PI != E; ++PI) 1177 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, 1178 InsMap, ClsMap, CDecl); 1179 // Report unimplemented properties in the category as well. 1180 // When reporting on missing setter/getters, do not report when 1181 // setter/getter is implemented in category's primary class 1182 // implementation. 1183 if (ObjCInterfaceDecl *ID = C->getClassInterface()) 1184 if (ObjCImplDecl *IMP = ID->getImplementation()) { 1185 for (ObjCImplementationDecl::instmeth_iterator 1186 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I) 1187 InsMap.insert((*I)->getSelector()); 1188 } 1189 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap); 1190 } 1191 } else 1192 assert(false && "invalid ObjCContainerDecl type."); 1193} 1194 1195/// ActOnForwardClassDeclaration - 1196Decl * 1197Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, 1198 IdentifierInfo **IdentList, 1199 SourceLocation *IdentLocs, 1200 unsigned NumElts) { 1201 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; 1202 1203 for (unsigned i = 0; i != NumElts; ++i) { 1204 // Check for another declaration kind with the same name. 1205 NamedDecl *PrevDecl 1206 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i], 1207 LookupOrdinaryName, ForRedeclaration); 1208 if (PrevDecl && PrevDecl->isTemplateParameter()) { 1209 // Maybe we will complain about the shadowed template parameter. 1210 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl); 1211 // Just pretend that we didn't see the previous declaration. 1212 PrevDecl = 0; 1213 } 1214 1215 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 1216 // GCC apparently allows the following idiom: 1217 // 1218 // typedef NSObject < XCElementTogglerP > XCElementToggler; 1219 // @class XCElementToggler; 1220 // 1221 // FIXME: Make an extension? 1222 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl); 1223 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) { 1224 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; 1225 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 1226 } else { 1227 // a forward class declaration matching a typedef name of a class refers 1228 // to the underlying class. 1229 if (const ObjCObjectType *OI = 1230 TDD->getUnderlyingType()->getAs<ObjCObjectType>()) 1231 PrevDecl = OI->getInterface(); 1232 } 1233 } 1234 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 1235 if (!IDecl) { // Not already seen? Make a forward decl. 1236 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, 1237 IdentList[i], IdentLocs[i], true); 1238 1239 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to 1240 // the current DeclContext. This prevents clients that walk DeclContext 1241 // from seeing the imaginary ObjCInterfaceDecl until it is actually 1242 // declared later (if at all). We also take care to explicitly make 1243 // sure this declaration is visible for name lookup. 1244 PushOnScopeChains(IDecl, TUScope, false); 1245 CurContext->makeDeclVisibleInContext(IDecl, true); 1246 } 1247 1248 Interfaces.push_back(IDecl); 1249 } 1250 1251 assert(Interfaces.size() == NumElts); 1252 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc, 1253 Interfaces.data(), IdentLocs, 1254 Interfaces.size()); 1255 CurContext->addDecl(CDecl); 1256 CheckObjCDeclScope(CDecl); 1257 return CDecl; 1258} 1259 1260 1261/// MatchTwoMethodDeclarations - Checks that two methods have matching type and 1262/// returns true, or false, accordingly. 1263/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons 1264bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, 1265 const ObjCMethodDecl *PrevMethod, 1266 bool matchBasedOnSizeAndAlignment, 1267 bool matchBasedOnStrictEqulity) { 1268 QualType T1 = Context.getCanonicalType(Method->getResultType()); 1269 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType()); 1270 1271 if (T1 != T2) { 1272 // The result types are different. 1273 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity) 1274 return false; 1275 // Incomplete types don't have a size and alignment. 1276 if (T1->isIncompleteType() || T2->isIncompleteType()) 1277 return false; 1278 // Check is based on size and alignment. 1279 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) 1280 return false; 1281 } 1282 1283 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), 1284 E = Method->param_end(); 1285 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin(); 1286 1287 for (; ParamI != E; ++ParamI, ++PrevI) { 1288 assert(PrevI != PrevMethod->param_end() && "Param mismatch"); 1289 T1 = Context.getCanonicalType((*ParamI)->getType()); 1290 T2 = Context.getCanonicalType((*PrevI)->getType()); 1291 if (T1 != T2) { 1292 // The result types are different. 1293 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity) 1294 return false; 1295 // Incomplete types don't have a size and alignment. 1296 if (T1->isIncompleteType() || T2->isIncompleteType()) 1297 return false; 1298 // Check is based on size and alignment. 1299 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) 1300 return false; 1301 } 1302 } 1303 return true; 1304} 1305 1306/// \brief Read the contents of the method pool for a given selector from 1307/// external storage. 1308/// 1309/// This routine should only be called once, when the method pool has no entry 1310/// for this selector. 1311Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) { 1312 assert(ExternalSource && "We need an external AST source"); 1313 assert(MethodPool.find(Sel) == MethodPool.end() && 1314 "Selector data already loaded into the method pool"); 1315 1316 // Read the method list from the external source. 1317 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel); 1318 1319 return MethodPool.insert(std::make_pair(Sel, Methods)).first; 1320} 1321 1322void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl, 1323 bool instance) { 1324 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector()); 1325 if (Pos == MethodPool.end()) { 1326 if (ExternalSource) 1327 Pos = ReadMethodPool(Method->getSelector()); 1328 else 1329 Pos = MethodPool.insert(std::make_pair(Method->getSelector(), 1330 GlobalMethods())).first; 1331 } 1332 Method->setDefined(impl); 1333 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second; 1334 if (Entry.Method == 0) { 1335 // Haven't seen a method with this selector name yet - add it. 1336 Entry.Method = Method; 1337 Entry.Next = 0; 1338 return; 1339 } 1340 1341 // We've seen a method with this name, see if we have already seen this type 1342 // signature. 1343 for (ObjCMethodList *List = &Entry; List; List = List->Next) 1344 if (MatchTwoMethodDeclarations(Method, List->Method)) { 1345 ObjCMethodDecl *PrevObjCMethod = List->Method; 1346 PrevObjCMethod->setDefined(impl); 1347 // If a method is deprecated, push it in the global pool. 1348 // This is used for better diagnostics. 1349 if (Method->getAttr<DeprecatedAttr>()) { 1350 if (!PrevObjCMethod->getAttr<DeprecatedAttr>()) 1351 List->Method = Method; 1352 } 1353 // If new method is unavailable, push it into global pool 1354 // unless previous one is deprecated. 1355 if (Method->getAttr<UnavailableAttr>()) { 1356 if (!PrevObjCMethod->getAttr<UnavailableAttr>() && 1357 !PrevObjCMethod->getAttr<DeprecatedAttr>()) 1358 List->Method = Method; 1359 } 1360 return; 1361 } 1362 1363 // We have a new signature for an existing method - add it. 1364 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". 1365 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); 1366 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next); 1367} 1368 1369ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R, 1370 bool receiverIdOrClass, 1371 bool warn, bool instance) { 1372 GlobalMethodPool::iterator Pos = MethodPool.find(Sel); 1373 if (Pos == MethodPool.end()) { 1374 if (ExternalSource) 1375 Pos = ReadMethodPool(Sel); 1376 else 1377 return 0; 1378 } 1379 1380 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second; 1381 1382 bool strictSelectorMatch = receiverIdOrClass && warn && 1383 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl, 1384 R.getBegin()) != 1385 Diagnostic::Ignored); 1386 if (warn && MethList.Method && MethList.Next) { 1387 bool issueWarning = false; 1388 if (strictSelectorMatch) 1389 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) { 1390 // This checks if the methods differ in type mismatch. 1391 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true)) 1392 issueWarning = true; 1393 } 1394 1395 if (!issueWarning) 1396 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) { 1397 // This checks if the methods differ by size & alignment. 1398 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) 1399 issueWarning = true; 1400 } 1401 1402 if (issueWarning) { 1403 if (strictSelectorMatch) 1404 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R; 1405 else 1406 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; 1407 Diag(MethList.Method->getLocStart(), diag::note_using) 1408 << MethList.Method->getSourceRange(); 1409 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 1410 Diag(Next->Method->getLocStart(), diag::note_also_found) 1411 << Next->Method->getSourceRange(); 1412 } 1413 } 1414 return MethList.Method; 1415} 1416 1417ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) { 1418 GlobalMethodPool::iterator Pos = MethodPool.find(Sel); 1419 if (Pos == MethodPool.end()) 1420 return 0; 1421 1422 GlobalMethods &Methods = Pos->second; 1423 1424 if (Methods.first.Method && Methods.first.Method->isDefined()) 1425 return Methods.first.Method; 1426 if (Methods.second.Method && Methods.second.Method->isDefined()) 1427 return Methods.second.Method; 1428 return 0; 1429} 1430 1431/// CompareMethodParamsInBaseAndSuper - This routine compares methods with 1432/// identical selector names in current and its super classes and issues 1433/// a warning if any of their argument types are incompatible. 1434void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl, 1435 ObjCMethodDecl *Method, 1436 bool IsInstance) { 1437 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl); 1438 if (ID == 0) return; 1439 1440 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) { 1441 ObjCMethodDecl *SuperMethodDecl = 1442 SD->lookupMethod(Method->getSelector(), IsInstance); 1443 if (SuperMethodDecl == 0) { 1444 ID = SD; 1445 continue; 1446 } 1447 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), 1448 E = Method->param_end(); 1449 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin(); 1450 for (; ParamI != E; ++ParamI, ++PrevI) { 1451 // Number of parameters are the same and is guaranteed by selector match. 1452 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch"); 1453 QualType T1 = Context.getCanonicalType((*ParamI)->getType()); 1454 QualType T2 = Context.getCanonicalType((*PrevI)->getType()); 1455 // If type of arguement of method in this class does not match its 1456 // respective argument type in the super class method, issue warning; 1457 if (!Context.typesAreCompatible(T1, T2)) { 1458 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) 1459 << T1 << T2; 1460 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration); 1461 return; 1462 } 1463 } 1464 ID = SD; 1465 } 1466} 1467 1468/// DiagnoseDuplicateIvars - 1469/// Check for duplicate ivars in the entire class at the start of 1470/// @implementation. This becomes necesssary because class extension can 1471/// add ivars to a class in random order which will not be known until 1472/// class's @implementation is seen. 1473void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, 1474 ObjCInterfaceDecl *SID) { 1475 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(), 1476 IVE = ID->ivar_end(); IVI != IVE; ++IVI) { 1477 ObjCIvarDecl* Ivar = (*IVI); 1478 if (Ivar->isInvalidDecl()) 1479 continue; 1480 if (IdentifierInfo *II = Ivar->getIdentifier()) { 1481 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II); 1482 if (prevIvar) { 1483 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II; 1484 Diag(prevIvar->getLocation(), diag::note_previous_declaration); 1485 Ivar->setInvalidDecl(); 1486 } 1487 } 1488 } 1489} 1490 1491// Note: For class/category implemenations, allMethods/allProperties is 1492// always null. 1493void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, 1494 Decl *ClassDecl, 1495 Decl **allMethods, unsigned allNum, 1496 Decl **allProperties, unsigned pNum, 1497 DeclGroupPtrTy *allTUVars, unsigned tuvNum) { 1498 // FIXME: If we don't have a ClassDecl, we have an error. We should consider 1499 // always passing in a decl. If the decl has an error, isInvalidDecl() 1500 // should be true. 1501 if (!ClassDecl) 1502 return; 1503 1504 bool isInterfaceDeclKind = 1505 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) 1506 || isa<ObjCProtocolDecl>(ClassDecl); 1507 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); 1508 1509 if (!isInterfaceDeclKind && AtEnd.isInvalid()) { 1510 // FIXME: This is wrong. We shouldn't be pretending that there is 1511 // an '@end' in the declaration. 1512 SourceLocation L = ClassDecl->getLocation(); 1513 AtEnd.setBegin(L); 1514 AtEnd.setEnd(L); 1515 Diag(L, diag::warn_missing_atend); 1516 } 1517 1518 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. 1519 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; 1520 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; 1521 1522 for (unsigned i = 0; i < allNum; i++ ) { 1523 ObjCMethodDecl *Method = 1524 cast_or_null<ObjCMethodDecl>(allMethods[i]); 1525 1526 if (!Method) continue; // Already issued a diagnostic. 1527 if (Method->isInstanceMethod()) { 1528 /// Check for instance method of the same name with incompatible types 1529 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; 1530 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 1531 : false; 1532 if ((isInterfaceDeclKind && PrevMethod && !match) 1533 || (checkIdenticalMethods && match)) { 1534 Diag(Method->getLocation(), diag::err_duplicate_method_decl) 1535 << Method->getDeclName(); 1536 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1537 Method->setInvalidDecl(); 1538 } else { 1539 InsMap[Method->getSelector()] = Method; 1540 /// The following allows us to typecheck messages to "id". 1541 AddInstanceMethodToGlobalPool(Method); 1542 // verify that the instance method conforms to the same definition of 1543 // parent methods if it shadows one. 1544 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true); 1545 } 1546 } else { 1547 /// Check for class method of the same name with incompatible types 1548 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; 1549 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 1550 : false; 1551 if ((isInterfaceDeclKind && PrevMethod && !match) 1552 || (checkIdenticalMethods && match)) { 1553 Diag(Method->getLocation(), diag::err_duplicate_method_decl) 1554 << Method->getDeclName(); 1555 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1556 Method->setInvalidDecl(); 1557 } else { 1558 ClsMap[Method->getSelector()] = Method; 1559 /// The following allows us to typecheck messages to "Class". 1560 AddFactoryMethodToGlobalPool(Method); 1561 // verify that the class method conforms to the same definition of 1562 // parent methods if it shadows one. 1563 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false); 1564 } 1565 } 1566 } 1567 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 1568 // Compares properties declared in this class to those of its 1569 // super class. 1570 ComparePropertiesInBaseAndSuper(I); 1571 CompareProperties(I, I); 1572 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { 1573 // Categories are used to extend the class by declaring new methods. 1574 // By the same token, they are also used to add new properties. No 1575 // need to compare the added property to those in the class. 1576 1577 // Compare protocol properties with those in category 1578 CompareProperties(C, C); 1579 if (C->IsClassExtension()) { 1580 ObjCInterfaceDecl *CCPrimary = C->getClassInterface(); 1581 DiagnoseClassExtensionDupMethods(C, CCPrimary); 1582 } 1583 } 1584 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { 1585 if (CDecl->getIdentifier()) 1586 // ProcessPropertyDecl is responsible for diagnosing conflicts with any 1587 // user-defined setter/getter. It also synthesizes setter/getter methods 1588 // and adds them to the DeclContext and global method pools. 1589 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), 1590 E = CDecl->prop_end(); 1591 I != E; ++I) 1592 ProcessPropertyDecl(*I, CDecl); 1593 CDecl->setAtEndRange(AtEnd); 1594 } 1595 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 1596 IC->setAtEndRange(AtEnd); 1597 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { 1598 // Any property declared in a class extension might have user 1599 // declared setter or getter in current class extension or one 1600 // of the other class extensions. Mark them as synthesized as 1601 // property will be synthesized when property with same name is 1602 // seen in the @implementation. 1603 for (const ObjCCategoryDecl *ClsExtDecl = 1604 IDecl->getFirstClassExtension(); 1605 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) { 1606 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(), 1607 E = ClsExtDecl->prop_end(); I != E; ++I) { 1608 ObjCPropertyDecl *Property = (*I); 1609 // Skip over properties declared @dynamic 1610 if (const ObjCPropertyImplDecl *PIDecl 1611 = IC->FindPropertyImplDecl(Property->getIdentifier())) 1612 if (PIDecl->getPropertyImplementation() 1613 == ObjCPropertyImplDecl::Dynamic) 1614 continue; 1615 1616 for (const ObjCCategoryDecl *CExtDecl = 1617 IDecl->getFirstClassExtension(); 1618 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) { 1619 if (ObjCMethodDecl *GetterMethod = 1620 CExtDecl->getInstanceMethod(Property->getGetterName())) 1621 GetterMethod->setSynthesized(true); 1622 if (!Property->isReadOnly()) 1623 if (ObjCMethodDecl *SetterMethod = 1624 CExtDecl->getInstanceMethod(Property->getSetterName())) 1625 SetterMethod->setSynthesized(true); 1626 } 1627 } 1628 } 1629 1630 if (LangOpts.ObjCDefaultSynthProperties && 1631 LangOpts.ObjCNonFragileABI2) 1632 DefaultSynthesizeProperties(S, IC, IDecl); 1633 ImplMethodsVsClassMethods(S, IC, IDecl); 1634 AtomicPropertySetterGetterRules(IC, IDecl); 1635 1636 if (LangOpts.ObjCNonFragileABI2) 1637 while (IDecl->getSuperClass()) { 1638 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass()); 1639 IDecl = IDecl->getSuperClass(); 1640 } 1641 } 1642 SetIvarInitializers(IC); 1643 } else if (ObjCCategoryImplDecl* CatImplClass = 1644 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { 1645 CatImplClass->setAtEndRange(AtEnd); 1646 1647 // Find category interface decl and then check that all methods declared 1648 // in this interface are implemented in the category @implementation. 1649 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { 1650 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); 1651 Categories; Categories = Categories->getNextClassCategory()) { 1652 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { 1653 ImplMethodsVsClassMethods(S, CatImplClass, Categories); 1654 break; 1655 } 1656 } 1657 } 1658 } 1659 if (isInterfaceDeclKind) { 1660 // Reject invalid vardecls. 1661 for (unsigned i = 0; i != tuvNum; i++) { 1662 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>(); 1663 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) 1664 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { 1665 if (!VDecl->hasExternalStorage()) 1666 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); 1667 } 1668 } 1669 } 1670} 1671 1672 1673/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for 1674/// objective-c's type qualifier from the parser version of the same info. 1675static Decl::ObjCDeclQualifier 1676CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { 1677 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; 1678 if (PQTVal & ObjCDeclSpec::DQ_In) 1679 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); 1680 if (PQTVal & ObjCDeclSpec::DQ_Inout) 1681 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); 1682 if (PQTVal & ObjCDeclSpec::DQ_Out) 1683 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); 1684 if (PQTVal & ObjCDeclSpec::DQ_Bycopy) 1685 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); 1686 if (PQTVal & ObjCDeclSpec::DQ_Byref) 1687 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); 1688 if (PQTVal & ObjCDeclSpec::DQ_Oneway) 1689 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); 1690 1691 return ret; 1692} 1693 1694static inline 1695bool containsInvalidMethodImplAttribute(const AttrVec &A) { 1696 // The 'ibaction' attribute is allowed on method definitions because of 1697 // how the IBAction macro is used on both method declarations and definitions. 1698 // If the method definitions contains any other attributes, return true. 1699 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) 1700 if ((*i)->getKind() != attr::IBAction) 1701 return true; 1702 return false; 1703} 1704 1705Decl *Sema::ActOnMethodDeclaration( 1706 Scope *S, 1707 SourceLocation MethodLoc, SourceLocation EndLoc, 1708 tok::TokenKind MethodType, Decl *ClassDecl, 1709 ObjCDeclSpec &ReturnQT, ParsedType ReturnType, 1710 Selector Sel, 1711 // optional arguments. The number of types/arguments is obtained 1712 // from the Sel.getNumArgs(). 1713 ObjCArgInfo *ArgInfo, 1714 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args 1715 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, 1716 bool isVariadic) { 1717 // Make sure we can establish a context for the method. 1718 if (!ClassDecl) { 1719 Diag(MethodLoc, diag::error_missing_method_context); 1720 getCurFunction()->LabelMap.clear(); 1721 return 0; 1722 } 1723 QualType resultDeclType; 1724 1725 TypeSourceInfo *ResultTInfo = 0; 1726 if (ReturnType) { 1727 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo); 1728 1729 // Methods cannot return interface types. All ObjC objects are 1730 // passed by reference. 1731 if (resultDeclType->isObjCObjectType()) { 1732 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value) 1733 << 0 << resultDeclType; 1734 return 0; 1735 } 1736 } else // get the type for "id". 1737 resultDeclType = Context.getObjCIdType(); 1738 1739 ObjCMethodDecl* ObjCMethod = 1740 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, 1741 ResultTInfo, 1742 cast<DeclContext>(ClassDecl), 1743 MethodType == tok::minus, isVariadic, 1744 false, false, 1745 MethodDeclKind == tok::objc_optional ? 1746 ObjCMethodDecl::Optional : 1747 ObjCMethodDecl::Required); 1748 1749 llvm::SmallVector<ParmVarDecl*, 16> Params; 1750 1751 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { 1752 QualType ArgType; 1753 TypeSourceInfo *DI; 1754 1755 if (ArgInfo[i].Type == 0) { 1756 ArgType = Context.getObjCIdType(); 1757 DI = 0; 1758 } else { 1759 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); 1760 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). 1761 ArgType = adjustParameterType(ArgType); 1762 } 1763 1764 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc, 1765 LookupOrdinaryName, ForRedeclaration); 1766 LookupName(R, S); 1767 if (R.isSingleResult()) { 1768 NamedDecl *PrevDecl = R.getFoundDecl(); 1769 if (S->isDeclScope(PrevDecl)) { 1770 // FIXME. This should be an error; but will break projects. 1771 Diag(ArgInfo[i].NameLoc, diag::warn_method_param_redefinition) 1772 << ArgInfo[i].Name; 1773 Diag(PrevDecl->getLocation(), 1774 diag::note_previous_declaration); 1775 } 1776 } 1777 1778 ParmVarDecl* Param 1779 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc, 1780 ArgInfo[i].Name, ArgType, DI, 1781 SC_None, SC_None, 0); 1782 1783 if (ArgType->isObjCObjectType()) { 1784 Diag(ArgInfo[i].NameLoc, 1785 diag::err_object_cannot_be_passed_returned_by_value) 1786 << 1 << ArgType; 1787 Param->setInvalidDecl(); 1788 } 1789 1790 Param->setObjCDeclQualifier( 1791 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); 1792 1793 // Apply the attributes to the parameter. 1794 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); 1795 1796 S->AddDecl(Param); 1797 IdResolver.AddDecl(Param); 1798 1799 Params.push_back(Param); 1800 } 1801 1802 for (unsigned i = 0, e = CNumArgs; i != e; ++i) { 1803 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param); 1804 QualType ArgType = Param->getType(); 1805 if (ArgType.isNull()) 1806 ArgType = Context.getObjCIdType(); 1807 else 1808 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). 1809 ArgType = adjustParameterType(ArgType); 1810 if (ArgType->isObjCObjectType()) { 1811 Diag(Param->getLocation(), 1812 diag::err_object_cannot_be_passed_returned_by_value) 1813 << 1 << ArgType; 1814 Param->setInvalidDecl(); 1815 } 1816 Param->setDeclContext(ObjCMethod); 1817 1818 Params.push_back(Param); 1819 } 1820 1821 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(), 1822 Sel.getNumArgs()); 1823 ObjCMethod->setObjCDeclQualifier( 1824 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); 1825 const ObjCMethodDecl *PrevMethod = 0; 1826 1827 if (AttrList) 1828 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); 1829 1830 const ObjCMethodDecl *InterfaceMD = 0; 1831 1832 // Add the method now. 1833 if (ObjCImplementationDecl *ImpDecl = 1834 dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 1835 if (MethodType == tok::minus) { 1836 PrevMethod = ImpDecl->getInstanceMethod(Sel); 1837 ImpDecl->addInstanceMethod(ObjCMethod); 1838 } else { 1839 PrevMethod = ImpDecl->getClassMethod(Sel); 1840 ImpDecl->addClassMethod(ObjCMethod); 1841 } 1842 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel, 1843 MethodType == tok::minus); 1844 if (ObjCMethod->hasAttrs() && 1845 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs())) 1846 Diag(EndLoc, diag::warn_attribute_method_def); 1847 } else if (ObjCCategoryImplDecl *CatImpDecl = 1848 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { 1849 if (MethodType == tok::minus) { 1850 PrevMethod = CatImpDecl->getInstanceMethod(Sel); 1851 CatImpDecl->addInstanceMethod(ObjCMethod); 1852 } else { 1853 PrevMethod = CatImpDecl->getClassMethod(Sel); 1854 CatImpDecl->addClassMethod(ObjCMethod); 1855 } 1856 if (ObjCMethod->hasAttrs() && 1857 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs())) 1858 Diag(EndLoc, diag::warn_attribute_method_def); 1859 } else { 1860 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod); 1861 } 1862 if (PrevMethod) { 1863 // You can never have two method definitions with the same name. 1864 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) 1865 << ObjCMethod->getDeclName(); 1866 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1867 } 1868 1869 // If the interface declared this method, and it was deprecated there, 1870 // mark it deprecated here. 1871 if (InterfaceMD) 1872 if (Attr *DA = InterfaceMD->getAttr<DeprecatedAttr>()) { 1873 StringLiteral *SE = StringLiteral::CreateEmpty(Context, 1); 1874 ObjCMethod->addAttr(::new (Context) 1875 DeprecatedAttr(DA->getLocation(), 1876 Context, 1877 SE->getString())); 1878 } 1879 1880 return ObjCMethod; 1881} 1882 1883bool Sema::CheckObjCDeclScope(Decl *D) { 1884 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext())) 1885 return false; 1886 1887 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); 1888 D->setInvalidDecl(); 1889 1890 return true; 1891} 1892 1893/// Called whenever @defs(ClassName) is encountered in the source. Inserts the 1894/// instance variables of ClassName into Decls. 1895void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart, 1896 IdentifierInfo *ClassName, 1897 llvm::SmallVectorImpl<Decl*> &Decls) { 1898 // Check that ClassName is a valid class 1899 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart); 1900 if (!Class) { 1901 Diag(DeclStart, diag::err_undef_interface) << ClassName; 1902 return; 1903 } 1904 if (LangOpts.ObjCNonFragileABI) { 1905 Diag(DeclStart, diag::err_atdef_nonfragile_interface); 1906 return; 1907 } 1908 1909 // Collect the instance variables 1910 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars; 1911 Context.DeepCollectObjCIvars(Class, true, Ivars); 1912 // For each ivar, create a fresh ObjCAtDefsFieldDecl. 1913 for (unsigned i = 0; i < Ivars.size(); i++) { 1914 FieldDecl* ID = cast<FieldDecl>(Ivars[i]); 1915 RecordDecl *Record = dyn_cast<RecordDecl>(TagD); 1916 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(), 1917 ID->getIdentifier(), ID->getType(), 1918 ID->getBitWidth()); 1919 Decls.push_back(FD); 1920 } 1921 1922 // Introduce all of these fields into the appropriate scope. 1923 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin(); 1924 D != Decls.end(); ++D) { 1925 FieldDecl *FD = cast<FieldDecl>(*D); 1926 if (getLangOptions().CPlusPlus) 1927 PushOnScopeChains(cast<FieldDecl>(FD), S); 1928 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD)) 1929 Record->addDecl(FD); 1930 } 1931} 1932 1933/// \brief Build a type-check a new Objective-C exception variable declaration. 1934VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, 1935 QualType T, 1936 IdentifierInfo *Name, 1937 SourceLocation NameLoc, 1938 bool Invalid) { 1939 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage 1940 // duration shall not be qualified by an address-space qualifier." 1941 // Since all parameters have automatic store duration, they can not have 1942 // an address space. 1943 if (T.getAddressSpace() != 0) { 1944 Diag(NameLoc, diag::err_arg_with_address_space); 1945 Invalid = true; 1946 } 1947 1948 // An @catch parameter must be an unqualified object pointer type; 1949 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"? 1950 if (Invalid) { 1951 // Don't do any further checking. 1952 } else if (T->isDependentType()) { 1953 // Okay: we don't know what this type will instantiate to. 1954 } else if (!T->isObjCObjectPointerType()) { 1955 Invalid = true; 1956 Diag(NameLoc ,diag::err_catch_param_not_objc_type); 1957 } else if (T->isObjCQualifiedIdType()) { 1958 Invalid = true; 1959 Diag(NameLoc, diag::err_illegal_qualifiers_on_catch_parm); 1960 } 1961 1962 VarDecl *New = VarDecl::Create(Context, CurContext, NameLoc, Name, T, TInfo, 1963 SC_None, SC_None); 1964 New->setExceptionVariable(true); 1965 1966 if (Invalid) 1967 New->setInvalidDecl(); 1968 return New; 1969} 1970 1971Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) { 1972 const DeclSpec &DS = D.getDeclSpec(); 1973 1974 // We allow the "register" storage class on exception variables because 1975 // GCC did, but we drop it completely. Any other storage class is an error. 1976 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { 1977 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm) 1978 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc())); 1979 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) { 1980 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm) 1981 << DS.getStorageClassSpec(); 1982 } 1983 if (D.getDeclSpec().isThreadSpecified()) 1984 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread); 1985 D.getMutableDeclSpec().ClearStorageClassSpecs(); 1986 1987 DiagnoseFunctionSpecifiers(D); 1988 1989 // Check that there are no default arguments inside the type of this 1990 // exception object (C++ only). 1991 if (getLangOptions().CPlusPlus) 1992 CheckExtraCXXDefaultArguments(D); 1993 1994 TagDecl *OwnedDecl = 0; 1995 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl); 1996 QualType ExceptionType = TInfo->getType(); 1997 1998 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) { 1999 // Objective-C++: Types shall not be defined in exception types. 2000 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type) 2001 << Context.getTypeDeclType(OwnedDecl); 2002 } 2003 2004 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, D.getIdentifier(), 2005 D.getIdentifierLoc(), 2006 D.isInvalidType()); 2007 2008 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). 2009 if (D.getCXXScopeSpec().isSet()) { 2010 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm) 2011 << D.getCXXScopeSpec().getRange(); 2012 New->setInvalidDecl(); 2013 } 2014 2015 // Add the parameter declaration into this scope. 2016 S->AddDecl(New); 2017 if (D.getIdentifier()) 2018 IdResolver.AddDecl(New); 2019 2020 ProcessDeclAttributes(S, New, D); 2021 2022 if (New->hasAttr<BlocksAttr>()) 2023 Diag(New->getLocation(), diag::err_block_on_nonlocal); 2024 return New; 2025} 2026 2027/// CollectIvarsToConstructOrDestruct - Collect those ivars which require 2028/// initialization. 2029void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI, 2030 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { 2031 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv; 2032 Iv= Iv->getNextIvar()) { 2033 QualType QT = Context.getBaseElementType(Iv->getType()); 2034 if (QT->isRecordType()) 2035 Ivars.push_back(Iv); 2036 } 2037} 2038 2039void ObjCImplementationDecl::setIvarInitializers(ASTContext &C, 2040 CXXCtorInitializer ** initializers, 2041 unsigned numInitializers) { 2042 if (numInitializers > 0) { 2043 NumIvarInitializers = numInitializers; 2044 CXXCtorInitializer **ivarInitializers = 2045 new (C) CXXCtorInitializer*[NumIvarInitializers]; 2046 memcpy(ivarInitializers, initializers, 2047 numInitializers * sizeof(CXXCtorInitializer*)); 2048 IvarInitializers = ivarInitializers; 2049 } 2050} 2051 2052void Sema::DiagnoseUseOfUnimplementedSelectors() { 2053 // Warning will be issued only when selector table is 2054 // generated (which means there is at lease one implementation 2055 // in the TU). This is to match gcc's behavior. 2056 if (ReferencedSelectors.empty() || 2057 !Context.AnyObjCImplementation()) 2058 return; 2059 for (llvm::DenseMap<Selector, SourceLocation>::iterator S = 2060 ReferencedSelectors.begin(), 2061 E = ReferencedSelectors.end(); S != E; ++S) { 2062 Selector Sel = (*S).first; 2063 if (!LookupImplementedMethodInGlobalPool(Sel)) 2064 Diag((*S).second, diag::warn_unimplemented_selector) << Sel; 2065 } 2066 return; 2067} 2068