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