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