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