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