SemaDeclObjC.cpp revision 87eabddeb17746908be6f8b202b6ca5e95b09a4a
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 "clang/Sema/ExternalSemaSource.h" 16#include "clang/AST/Expr.h" 17#include "clang/AST/ASTContext.h" 18#include "clang/AST/DeclObjC.h" 19#include "clang/Parse/DeclSpec.h" 20using namespace clang; 21 22bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, 23 ObjCMethodDecl *GetterMethod, 24 SourceLocation Loc) { 25 if (GetterMethod && 26 GetterMethod->getResultType() != property->getType()) { 27 AssignConvertType result = Incompatible; 28 if (property->getType()->isObjCObjectPointerType()) 29 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType()); 30 if (result != Compatible) { 31 Diag(Loc, diag::warn_accessor_property_type_mismatch) 32 << property->getDeclName() 33 << GetterMethod->getSelector(); 34 Diag(GetterMethod->getLocation(), diag::note_declared_at); 35 return true; 36 } 37 } 38 return false; 39} 40 41/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible 42/// and user declared, in the method definition's AST. 43void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) { 44 assert(getCurMethodDecl() == 0 && "Method parsing confused"); 45 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>()); 46 47 // If we don't have a valid method decl, simply return. 48 if (!MDecl) 49 return; 50 51 CurFunctionNeedsScopeChecking = false; 52 53 // Allow the rest of sema to find private method decl implementations. 54 if (MDecl->isInstanceMethod()) 55 AddInstanceMethodToGlobalPool(MDecl); 56 else 57 AddFactoryMethodToGlobalPool(MDecl); 58 59 // Allow all of Sema to see that we are entering a method definition. 60 PushDeclContext(FnBodyScope, MDecl); 61 62 // Create Decl objects for each parameter, entrring them in the scope for 63 // binding to their use. 64 65 // Insert the invisible arguments, self and _cmd! 66 MDecl->createImplicitParams(Context, MDecl->getClassInterface()); 67 68 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); 69 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); 70 71 // Introduce all of the other parameters into this scope. 72 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), 73 E = MDecl->param_end(); PI != E; ++PI) 74 if ((*PI)->getIdentifier()) 75 PushOnScopeChains(*PI, FnBodyScope); 76} 77 78Sema::DeclPtrTy Sema:: 79ActOnStartClassInterface(SourceLocation AtInterfaceLoc, 80 IdentifierInfo *ClassName, SourceLocation ClassLoc, 81 IdentifierInfo *SuperName, SourceLocation SuperLoc, 82 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs, 83 SourceLocation EndProtoLoc, AttributeList *AttrList) { 84 assert(ClassName && "Missing class identifier"); 85 86 // Check for another declaration kind with the same name. 87 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); 88 if (PrevDecl && PrevDecl->isTemplateParameter()) { 89 // Maybe we will complain about the shadowed template parameter. 90 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl); 91 // Just pretend that we didn't see the previous declaration. 92 PrevDecl = 0; 93 } 94 95 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 96 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; 97 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 98 } 99 100 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 101 if (IDecl) { 102 // Class already seen. Is it a forward declaration? 103 if (!IDecl->isForwardDecl()) { 104 IDecl->setInvalidDecl(); 105 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName(); 106 Diag(IDecl->getLocation(), diag::note_previous_definition); 107 108 // Return the previous class interface. 109 // FIXME: don't leak the objects passed in! 110 return DeclPtrTy::make(IDecl); 111 } else { 112 IDecl->setLocation(AtInterfaceLoc); 113 IDecl->setForwardDecl(false); 114 IDecl->setClassLoc(ClassLoc); 115 116 // Since this ObjCInterfaceDecl was created by a forward declaration, 117 // we now add it to the DeclContext since it wasn't added before 118 // (see ActOnForwardClassDeclaration). 119 CurContext->addDecl(IDecl); 120 121 if (AttrList) 122 ProcessDeclAttributeList(TUScope, IDecl, AttrList); 123 } 124 } else { 125 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, 126 ClassName, ClassLoc); 127 if (AttrList) 128 ProcessDeclAttributeList(TUScope, IDecl, AttrList); 129 130 PushOnScopeChains(IDecl, TUScope); 131 } 132 133 if (SuperName) { 134 // Check if a different kind of symbol declared in this scope. 135 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName); 136 if (PrevDecl == IDecl) { 137 Diag(SuperLoc, diag::err_recursive_superclass) 138 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); 139 IDecl->setLocEnd(ClassLoc); 140 } else { 141 ObjCInterfaceDecl *SuperClassDecl = 142 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 143 144 // Diagnose classes that inherit from deprecated classes. 145 if (SuperClassDecl) 146 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); 147 148 if (PrevDecl && SuperClassDecl == 0) { 149 // The previous declaration was not a class decl. Check if we have a 150 // typedef. If we do, get the underlying class type. 151 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) { 152 QualType T = TDecl->getUnderlyingType(); 153 if (T->isObjCInterfaceType()) { 154 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) 155 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); 156 } 157 } 158 159 // This handles the following case: 160 // 161 // typedef int SuperClass; 162 // @interface MyClass : SuperClass {} @end 163 // 164 if (!SuperClassDecl) { 165 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; 166 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 167 } 168 } 169 170 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) { 171 if (!SuperClassDecl) 172 Diag(SuperLoc, diag::err_undef_superclass) 173 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); 174 else if (SuperClassDecl->isForwardDecl()) 175 Diag(SuperLoc, diag::err_undef_superclass) 176 << SuperClassDecl->getDeclName() << ClassName 177 << SourceRange(AtInterfaceLoc, ClassLoc); 178 } 179 IDecl->setSuperClass(SuperClassDecl); 180 IDecl->setSuperClassLoc(SuperLoc); 181 IDecl->setLocEnd(SuperLoc); 182 } 183 } else { // we have a root class. 184 IDecl->setLocEnd(ClassLoc); 185 } 186 187 /// Check then save referenced protocols. 188 if (NumProtoRefs) { 189 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, 190 Context); 191 IDecl->setLocEnd(EndProtoLoc); 192 } 193 194 CheckObjCDeclScope(IDecl); 195 return DeclPtrTy::make(IDecl); 196} 197 198/// ActOnCompatiblityAlias - this action is called after complete parsing of 199/// @compatibility_alias declaration. It sets up the alias relationships. 200Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, 201 IdentifierInfo *AliasName, 202 SourceLocation AliasLocation, 203 IdentifierInfo *ClassName, 204 SourceLocation ClassLocation) { 205 // Look for previous declaration of alias name 206 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName); 207 if (ADecl) { 208 if (isa<ObjCCompatibleAliasDecl>(ADecl)) 209 Diag(AliasLocation, diag::warn_previous_alias_decl); 210 else 211 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; 212 Diag(ADecl->getLocation(), diag::note_previous_declaration); 213 return DeclPtrTy(); 214 } 215 // Check for class declaration 216 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); 217 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) { 218 QualType T = TDecl->getUnderlyingType(); 219 if (T->isObjCInterfaceType()) { 220 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) { 221 ClassName = IDecl->getIdentifier(); 222 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); 223 } 224 } 225 } 226 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); 227 if (CDecl == 0) { 228 Diag(ClassLocation, diag::warn_undef_interface) << ClassName; 229 if (CDeclU) 230 Diag(CDeclU->getLocation(), diag::note_previous_declaration); 231 return DeclPtrTy(); 232 } 233 234 // Everything checked out, instantiate a new alias declaration AST. 235 ObjCCompatibleAliasDecl *AliasDecl = 236 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); 237 238 if (!CheckObjCDeclScope(AliasDecl)) 239 PushOnScopeChains(AliasDecl, TUScope); 240 241 return DeclPtrTy::make(AliasDecl); 242} 243 244void Sema::CheckForwardProtocolDeclarationForCircularDependency( 245 IdentifierInfo *PName, 246 SourceLocation &Ploc, SourceLocation PrevLoc, 247 const ObjCList<ObjCProtocolDecl> &PList) { 248 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), 249 E = PList.end(); I != E; ++I) { 250 251 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) { 252 if (PDecl->getIdentifier() == PName) { 253 Diag(Ploc, diag::err_protocol_has_circular_dependency); 254 Diag(PrevLoc, diag::note_previous_definition); 255 } 256 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, 257 PDecl->getLocation(), PDecl->getReferencedProtocols()); 258 } 259 } 260} 261 262Sema::DeclPtrTy 263Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, 264 IdentifierInfo *ProtocolName, 265 SourceLocation ProtocolLoc, 266 const DeclPtrTy *ProtoRefs, 267 unsigned NumProtoRefs, 268 SourceLocation EndProtoLoc, 269 AttributeList *AttrList) { 270 // FIXME: Deal with AttrList. 271 assert(ProtocolName && "Missing protocol identifier"); 272 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName); 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 DeclPtrTy::make(PDecl); 281 } 282 ObjCList<ObjCProtocolDecl> PList; 283 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); 284 CheckForwardProtocolDeclarationForCircularDependency( 285 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList); 286 PList.Destroy(Context); 287 288 // Make sure the cached decl gets a valid start location. 289 PDecl->setLocation(AtProtoInterfaceLoc); 290 PDecl->setForwardDecl(false); 291 } else { 292 PDecl = ObjCProtocolDecl::Create(Context, CurContext, 293 AtProtoInterfaceLoc,ProtocolName); 294 PushOnScopeChains(PDecl, TUScope); 295 PDecl->setForwardDecl(false); 296 } 297 if (AttrList) 298 ProcessDeclAttributeList(TUScope, PDecl, AttrList); 299 if (NumProtoRefs) { 300 /// Check then save referenced protocols. 301 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context); 302 PDecl->setLocEnd(EndProtoLoc); 303 } 304 305 CheckObjCDeclScope(PDecl); 306 return DeclPtrTy::make(PDecl); 307} 308 309/// FindProtocolDeclaration - This routine looks up protocols and 310/// issues an error if they are not declared. It returns list of 311/// protocol declarations in its 'Protocols' argument. 312void 313Sema::FindProtocolDeclaration(bool WarnOnDeclarations, 314 const IdentifierLocPair *ProtocolId, 315 unsigned NumProtocols, 316 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) { 317 for (unsigned i = 0; i != NumProtocols; ++i) { 318 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first); 319 if (!PDecl) { 320 Diag(ProtocolId[i].second, diag::err_undeclared_protocol) 321 << ProtocolId[i].first; 322 continue; 323 } 324 325 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second); 326 327 // If this is a forward declaration and we are supposed to warn in this 328 // case, do it. 329 if (WarnOnDeclarations && PDecl->isForwardDecl()) 330 Diag(ProtocolId[i].second, diag::warn_undef_protocolref) 331 << ProtocolId[i].first; 332 Protocols.push_back(DeclPtrTy::make(PDecl)); 333 } 334} 335 336/// DiagnosePropertyMismatch - Compares two properties for their 337/// attributes and types and warns on a variety of inconsistencies. 338/// 339void 340Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, 341 ObjCPropertyDecl *SuperProperty, 342 const IdentifierInfo *inheritedName) { 343 ObjCPropertyDecl::PropertyAttributeKind CAttr = 344 Property->getPropertyAttributes(); 345 ObjCPropertyDecl::PropertyAttributeKind SAttr = 346 SuperProperty->getPropertyAttributes(); 347 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) 348 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) 349 Diag(Property->getLocation(), diag::warn_readonly_property) 350 << Property->getDeclName() << inheritedName; 351 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) 352 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) 353 Diag(Property->getLocation(), diag::warn_property_attribute) 354 << Property->getDeclName() << "copy" << inheritedName; 355 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain) 356 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain)) 357 Diag(Property->getLocation(), diag::warn_property_attribute) 358 << Property->getDeclName() << "retain" << inheritedName; 359 360 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) 361 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) 362 Diag(Property->getLocation(), diag::warn_property_attribute) 363 << Property->getDeclName() << "atomic" << inheritedName; 364 if (Property->getSetterName() != SuperProperty->getSetterName()) 365 Diag(Property->getLocation(), diag::warn_property_attribute) 366 << Property->getDeclName() << "setter" << inheritedName; 367 if (Property->getGetterName() != SuperProperty->getGetterName()) 368 Diag(Property->getLocation(), diag::warn_property_attribute) 369 << Property->getDeclName() << "getter" << inheritedName; 370 371 QualType LHSType = 372 Context.getCanonicalType(SuperProperty->getType()); 373 QualType RHSType = 374 Context.getCanonicalType(Property->getType()); 375 376 if (!Context.typesAreCompatible(LHSType, RHSType)) { 377 // FIXME: Incorporate this test with typesAreCompatible. 378 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType()) 379 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false)) 380 return; 381 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) 382 << Property->getType() << SuperProperty->getType() << inheritedName; 383 } 384} 385 386/// ComparePropertiesInBaseAndSuper - This routine compares property 387/// declarations in base and its super class, if any, and issues 388/// diagnostics in a variety of inconsistant situations. 389/// 390void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { 391 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); 392 if (!SDecl) 393 return; 394 // FIXME: O(N^2) 395 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(), 396 E = SDecl->prop_end(); S != E; ++S) { 397 ObjCPropertyDecl *SuperPDecl = (*S); 398 // Does property in super class has declaration in current class? 399 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(), 400 E = IDecl->prop_end(); I != E; ++I) { 401 ObjCPropertyDecl *PDecl = (*I); 402 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) 403 DiagnosePropertyMismatch(PDecl, SuperPDecl, 404 SDecl->getIdentifier()); 405 } 406 } 407} 408 409/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list 410/// of properties declared in a protocol and adds them to the list 411/// of properties for current class/category if it is not there already. 412void 413Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl, 414 ObjCProtocolDecl *PDecl) { 415 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); 416 if (!IDecl) { 417 // Category 418 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); 419 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass"); 420 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), 421 E = PDecl->prop_end(); P != E; ++P) { 422 ObjCPropertyDecl *Pr = (*P); 423 ObjCCategoryDecl::prop_iterator CP, CE; 424 // Is this property already in category's list of properties? 425 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP) 426 if ((*CP)->getIdentifier() == Pr->getIdentifier()) 427 break; 428 if (CP != CE) 429 // Property protocol already exist in class. Diagnose any mismatch. 430 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); 431 } 432 return; 433 } 434 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), 435 E = PDecl->prop_end(); P != E; ++P) { 436 ObjCPropertyDecl *Pr = (*P); 437 ObjCInterfaceDecl::prop_iterator CP, CE; 438 // Is this property already in class's list of properties? 439 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP) 440 if ((*CP)->getIdentifier() == Pr->getIdentifier()) 441 break; 442 if (CP != CE) 443 // Property protocol already exist in class. Diagnose any mismatch. 444 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); 445 } 446} 447 448/// MergeProtocolPropertiesIntoClass - This routine merges properties 449/// declared in 'MergeItsProtocols' objects (which can be a class or an 450/// inherited protocol into the list of properties for class/category 'CDecl' 451/// 452void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl, 453 DeclPtrTy MergeItsProtocols) { 454 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>(); 455 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); 456 457 if (!IDecl) { 458 // Category 459 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); 460 assert (CatDecl && "MergeProtocolPropertiesIntoClass"); 461 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { 462 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(), 463 E = MDecl->protocol_end(); P != E; ++P) 464 // Merge properties of category (*P) into IDECL's 465 MergeOneProtocolPropertiesIntoClass(CatDecl, *P); 466 467 // Go thru the list of protocols for this category and recursively merge 468 // their properties into this class as well. 469 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(), 470 E = CatDecl->protocol_end(); P != E; ++P) 471 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P)); 472 } else { 473 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); 474 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), 475 E = MD->protocol_end(); P != E; ++P) 476 MergeOneProtocolPropertiesIntoClass(CatDecl, *P); 477 } 478 return; 479 } 480 481 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 482 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(), 483 E = MDecl->protocol_end(); P != E; ++P) 484 // Merge properties of class (*P) into IDECL's 485 MergeOneProtocolPropertiesIntoClass(IDecl, *P); 486 487 // Go thru the list of protocols for this class and recursively merge 488 // their properties into this class as well. 489 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(), 490 E = IDecl->protocol_end(); P != E; ++P) 491 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P)); 492 } else { 493 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); 494 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), 495 E = MD->protocol_end(); P != E; ++P) 496 MergeOneProtocolPropertiesIntoClass(IDecl, *P); 497 } 498} 499 500/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of 501/// a class method in its extension. 502/// 503void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, 504 ObjCInterfaceDecl *ID) { 505 if (!ID) 506 return; // Possibly due to previous error 507 508 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; 509 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(), 510 e = ID->meth_end(); i != e; ++i) { 511 ObjCMethodDecl *MD = *i; 512 MethodMap[MD->getSelector()] = MD; 513 } 514 515 if (MethodMap.empty()) 516 return; 517 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(), 518 e = CAT->meth_end(); i != e; ++i) { 519 ObjCMethodDecl *Method = *i; 520 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; 521 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) { 522 Diag(Method->getLocation(), diag::err_duplicate_method_decl) 523 << Method->getDeclName(); 524 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 525 } 526 } 527} 528 529/// ActOnForwardProtocolDeclaration - Handle @protocol foo; 530Action::DeclPtrTy 531Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, 532 const IdentifierLocPair *IdentList, 533 unsigned NumElts, 534 AttributeList *attrList) { 535 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; 536 537 for (unsigned i = 0; i != NumElts; ++i) { 538 IdentifierInfo *Ident = IdentList[i].first; 539 ObjCProtocolDecl *PDecl = LookupProtocol(Ident); 540 if (PDecl == 0) { // Not already seen? 541 PDecl = ObjCProtocolDecl::Create(Context, CurContext, 542 IdentList[i].second, Ident); 543 PushOnScopeChains(PDecl, TUScope); 544 } 545 if (attrList) 546 ProcessDeclAttributeList(TUScope, PDecl, attrList); 547 Protocols.push_back(PDecl); 548 } 549 550 ObjCForwardProtocolDecl *PDecl = 551 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc, 552 &Protocols[0], Protocols.size()); 553 CurContext->addDecl(PDecl); 554 CheckObjCDeclScope(PDecl); 555 return DeclPtrTy::make(PDecl); 556} 557 558Sema::DeclPtrTy Sema:: 559ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, 560 IdentifierInfo *ClassName, SourceLocation ClassLoc, 561 IdentifierInfo *CategoryName, 562 SourceLocation CategoryLoc, 563 const DeclPtrTy *ProtoRefs, 564 unsigned NumProtoRefs, 565 SourceLocation EndProtoLoc) { 566 ObjCCategoryDecl *CDecl = 567 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName); 568 // FIXME: PushOnScopeChains? 569 CurContext->addDecl(CDecl); 570 571 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); 572 /// Check that class of this category is already completely declared. 573 if (!IDecl || IDecl->isForwardDecl()) { 574 CDecl->setInvalidDecl(); 575 Diag(ClassLoc, diag::err_undef_interface) << ClassName; 576 return DeclPtrTy::make(CDecl); 577 } 578 579 CDecl->setClassInterface(IDecl); 580 581 // If the interface is deprecated, warn about it. 582 (void)DiagnoseUseOfDecl(IDecl, ClassLoc); 583 584 /// Check for duplicate interface declaration for this category 585 ObjCCategoryDecl *CDeclChain; 586 for (CDeclChain = IDecl->getCategoryList(); CDeclChain; 587 CDeclChain = CDeclChain->getNextClassCategory()) { 588 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) { 589 Diag(CategoryLoc, diag::warn_dup_category_def) 590 << ClassName << CategoryName; 591 Diag(CDeclChain->getLocation(), diag::note_previous_definition); 592 break; 593 } 594 } 595 if (!CDeclChain) 596 CDecl->insertNextClassCategory(); 597 598 if (NumProtoRefs) { 599 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, 600 Context); 601 CDecl->setLocEnd(EndProtoLoc); 602 // Protocols in the class extension belong to the class. 603 if (!CDecl->getIdentifier()) 604 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs, 605 NumProtoRefs,Context); 606 } 607 608 CheckObjCDeclScope(CDecl); 609 return DeclPtrTy::make(CDecl); 610} 611 612/// ActOnStartCategoryImplementation - Perform semantic checks on the 613/// category implementation declaration and build an ObjCCategoryImplDecl 614/// object. 615Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation( 616 SourceLocation AtCatImplLoc, 617 IdentifierInfo *ClassName, SourceLocation ClassLoc, 618 IdentifierInfo *CatName, SourceLocation CatLoc) { 619 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); 620 ObjCCategoryDecl *CatIDecl = 0; 621 if (IDecl) { 622 CatIDecl = IDecl->FindCategoryDeclaration(CatName); 623 if (!CatIDecl) { 624 // Category @implementation with no corresponding @interface. 625 // Create and install one. 626 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(), 627 CatName); 628 CatIDecl->setClassInterface(IDecl); 629 CatIDecl->insertNextClassCategory(); 630 } 631 } 632 633 ObjCCategoryImplDecl *CDecl = 634 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName, 635 IDecl); 636 /// Check that class of this category is already completely declared. 637 if (!IDecl || IDecl->isForwardDecl()) 638 Diag(ClassLoc, diag::err_undef_interface) << ClassName; 639 640 // FIXME: PushOnScopeChains? 641 CurContext->addDecl(CDecl); 642 643 /// Check that CatName, category name, is not used in another implementation. 644 if (CatIDecl) { 645 if (CatIDecl->getImplementation()) { 646 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName 647 << CatName; 648 Diag(CatIDecl->getImplementation()->getLocation(), 649 diag::note_previous_definition); 650 } else 651 CatIDecl->setImplementation(CDecl); 652 } 653 654 CheckObjCDeclScope(CDecl); 655 return DeclPtrTy::make(CDecl); 656} 657 658Sema::DeclPtrTy Sema::ActOnStartClassImplementation( 659 SourceLocation AtClassImplLoc, 660 IdentifierInfo *ClassName, SourceLocation ClassLoc, 661 IdentifierInfo *SuperClassname, 662 SourceLocation SuperClassLoc) { 663 ObjCInterfaceDecl* IDecl = 0; 664 // Check for another declaration kind with the same name. 665 NamedDecl *PrevDecl 666 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); 667 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 668 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; 669 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 670 } else { 671 // Is there an interface declaration of this class; if not, warn! 672 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 673 if (!IDecl || IDecl->isForwardDecl()) { 674 Diag(ClassLoc, diag::warn_undef_interface) << ClassName; 675 IDecl = 0; 676 } 677 } 678 679 // Check that super class name is valid class name 680 ObjCInterfaceDecl* SDecl = 0; 681 if (SuperClassname) { 682 // Check if a different kind of symbol declared in this scope. 683 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName); 684 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 685 Diag(SuperClassLoc, diag::err_redefinition_different_kind) 686 << SuperClassname; 687 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 688 } else { 689 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 690 if (!SDecl) 691 Diag(SuperClassLoc, diag::err_undef_superclass) 692 << SuperClassname << ClassName; 693 else if (IDecl && IDecl->getSuperClass() != SDecl) { 694 // This implementation and its interface do not have the same 695 // super class. 696 Diag(SuperClassLoc, diag::err_conflicting_super_class) 697 << SDecl->getDeclName(); 698 Diag(SDecl->getLocation(), diag::note_previous_definition); 699 } 700 } 701 } 702 703 if (!IDecl) { 704 // Legacy case of @implementation with no corresponding @interface. 705 // Build, chain & install the interface decl into the identifier. 706 707 // FIXME: Do we support attributes on the @implementation? If so we should 708 // copy them over. 709 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, 710 ClassName, ClassLoc, false, true); 711 IDecl->setSuperClass(SDecl); 712 IDecl->setLocEnd(ClassLoc); 713 714 PushOnScopeChains(IDecl, TUScope); 715 } else { 716 // Mark the interface as being completed, even if it was just as 717 // @class ....; 718 // declaration; the user cannot reopen it. 719 IDecl->setForwardDecl(false); 720 } 721 722 ObjCImplementationDecl* IMPDecl = 723 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc, 724 IDecl, SDecl); 725 726 if (CheckObjCDeclScope(IMPDecl)) 727 return DeclPtrTy::make(IMPDecl); 728 729 // Check that there is no duplicate implementation of this class. 730 if (IDecl->getImplementation()) { 731 // FIXME: Don't leak everything! 732 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; 733 Diag(IDecl->getImplementation()->getLocation(), 734 diag::note_previous_definition); 735 } else { // add it to the list. 736 IDecl->setImplementation(IMPDecl); 737 PushOnScopeChains(IMPDecl, TUScope); 738 } 739 return DeclPtrTy::make(IMPDecl); 740} 741 742void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, 743 ObjCIvarDecl **ivars, unsigned numIvars, 744 SourceLocation RBrace) { 745 assert(ImpDecl && "missing implementation decl"); 746 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); 747 if (!IDecl) 748 return; 749 /// Check case of non-existing @interface decl. 750 /// (legacy objective-c @implementation decl without an @interface decl). 751 /// Add implementations's ivar to the synthesize class's ivar list. 752 if (IDecl->isImplicitInterfaceDecl()) { 753 IDecl->setIVarList(ivars, numIvars, Context); 754 IDecl->setLocEnd(RBrace); 755 return; 756 } 757 // If implementation has empty ivar list, just return. 758 if (numIvars == 0) 759 return; 760 761 assert(ivars && "missing @implementation ivars"); 762 763 // Check interface's Ivar list against those in the implementation. 764 // names and types must match. 765 // 766 unsigned j = 0; 767 ObjCInterfaceDecl::ivar_iterator 768 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); 769 for (; numIvars > 0 && IVI != IVE; ++IVI) { 770 ObjCIvarDecl* ImplIvar = ivars[j++]; 771 ObjCIvarDecl* ClsIvar = *IVI; 772 assert (ImplIvar && "missing implementation ivar"); 773 assert (ClsIvar && "missing class ivar"); 774 775 // First, make sure the types match. 776 if (Context.getCanonicalType(ImplIvar->getType()) != 777 Context.getCanonicalType(ClsIvar->getType())) { 778 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) 779 << ImplIvar->getIdentifier() 780 << ImplIvar->getType() << ClsIvar->getType(); 781 Diag(ClsIvar->getLocation(), diag::note_previous_definition); 782 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) { 783 Expr *ImplBitWidth = ImplIvar->getBitWidth(); 784 Expr *ClsBitWidth = ClsIvar->getBitWidth(); 785 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() != 786 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) { 787 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth) 788 << ImplIvar->getIdentifier(); 789 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition); 790 } 791 } 792 // Make sure the names are identical. 793 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { 794 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) 795 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); 796 Diag(ClsIvar->getLocation(), diag::note_previous_definition); 797 } 798 --numIvars; 799 } 800 801 if (numIvars > 0) 802 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); 803 else if (IVI != IVE) 804 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); 805} 806 807void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, 808 bool &IncompleteImpl) { 809 if (!IncompleteImpl) { 810 Diag(ImpLoc, diag::warn_incomplete_impl); 811 IncompleteImpl = true; 812 } 813 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName(); 814} 815 816void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, 817 ObjCMethodDecl *IntfMethodDecl) { 818 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(), 819 ImpMethodDecl->getResultType()) && 820 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(), 821 ImpMethodDecl->getResultType())) { 822 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types) 823 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType() 824 << ImpMethodDecl->getResultType(); 825 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition); 826 } 827 828 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), 829 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end(); 830 IM != EM; ++IM, ++IF) { 831 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType(); 832 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType(); 833 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) || 834 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy)) 835 continue; 836 837 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types) 838 << ImpMethodDecl->getDeclName() << (*IF)->getType() 839 << (*IM)->getType(); 840 Diag((*IF)->getLocation(), diag::note_previous_definition); 841 } 842} 843 844/// isPropertyReadonly - Return true if property is readonly, by searching 845/// for the property in the class and in its categories and implementations 846/// 847bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, 848 ObjCInterfaceDecl *IDecl) { 849 // by far the most common case. 850 if (!PDecl->isReadOnly()) 851 return false; 852 // Even if property is ready only, if interface has a user defined setter, 853 // it is not considered read only. 854 if (IDecl->getInstanceMethod(PDecl->getSetterName())) 855 return false; 856 857 // Main class has the property as 'readonly'. Must search 858 // through the category list to see if the property's 859 // attribute has been over-ridden to 'readwrite'. 860 for (ObjCCategoryDecl *Category = IDecl->getCategoryList(); 861 Category; Category = Category->getNextClassCategory()) { 862 // Even if property is ready only, if a category has a user defined setter, 863 // it is not considered read only. 864 if (Category->getInstanceMethod(PDecl->getSetterName())) 865 return false; 866 ObjCPropertyDecl *P = 867 Category->FindPropertyDeclaration(PDecl->getIdentifier()); 868 if (P && !P->isReadOnly()) 869 return false; 870 } 871 872 // Also, check for definition of a setter method in the implementation if 873 // all else failed. 874 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { 875 if (ObjCImplementationDecl *IMD = 876 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { 877 if (IMD->getInstanceMethod(PDecl->getSetterName())) 878 return false; 879 } else if (ObjCCategoryImplDecl *CIMD = 880 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { 881 if (CIMD->getInstanceMethod(PDecl->getSetterName())) 882 return false; 883 } 884 } 885 // Lastly, look through the implementation (if one is in scope). 886 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation()) 887 if (ImpDecl->getInstanceMethod(PDecl->getSetterName())) 888 return false; 889 // If all fails, look at the super class. 890 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass()) 891 return isPropertyReadonly(PDecl, SIDecl); 892 return true; 893} 894 895/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely 896/// improve the efficiency of selector lookups and type checking by associating 897/// with each protocol / interface / category the flattened instance tables. If 898/// we used an immutable set to keep the table then it wouldn't add significant 899/// memory cost and it would be handy for lookups. 900 901/// CheckProtocolMethodDefs - This routine checks unimplemented methods 902/// Declared in protocol, and those referenced by it. 903void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, 904 ObjCProtocolDecl *PDecl, 905 bool& IncompleteImpl, 906 const llvm::DenseSet<Selector> &InsMap, 907 const llvm::DenseSet<Selector> &ClsMap, 908 ObjCInterfaceDecl *IDecl) { 909 ObjCInterfaceDecl *Super = IDecl->getSuperClass(); 910 ObjCInterfaceDecl *NSIDecl = 0; 911 if (getLangOptions().NeXTRuntime) { 912 // check to see if class implements forwardInvocation method and objects 913 // of this class are derived from 'NSProxy' so that to forward requests 914 // from one object to another. 915 // Under such conditions, which means that every method possible is 916 // implemented in the class, we should not issue "Method definition not 917 // found" warnings. 918 // FIXME: Use a general GetUnarySelector method for this. 919 IdentifierInfo* II = &Context.Idents.get("forwardInvocation"); 920 Selector fISelector = Context.Selectors.getSelector(1, &II); 921 if (InsMap.count(fISelector)) 922 // Is IDecl derived from 'NSProxy'? If so, no instance methods 923 // need be implemented in the implementation. 924 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy")); 925 } 926 927 // If a method lookup fails locally we still need to look and see if 928 // the method was implemented by a base class or an inherited 929 // protocol. This lookup is slow, but occurs rarely in correct code 930 // and otherwise would terminate in a warning. 931 932 // check unimplemented instance methods. 933 if (!NSIDecl) 934 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), 935 E = PDecl->instmeth_end(); I != E; ++I) { 936 ObjCMethodDecl *method = *I; 937 if (method->getImplementationControl() != ObjCMethodDecl::Optional && 938 !method->isSynthesized() && !InsMap.count(method->getSelector()) && 939 (!Super || 940 !Super->lookupInstanceMethod(method->getSelector()))) { 941 // Ugly, but necessary. Method declared in protcol might have 942 // have been synthesized due to a property declared in the class which 943 // uses the protocol. 944 ObjCMethodDecl *MethodInClass = 945 IDecl->lookupInstanceMethod(method->getSelector()); 946 if (!MethodInClass || !MethodInClass->isSynthesized()) 947 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); 948 } 949 } 950 // check unimplemented class methods 951 for (ObjCProtocolDecl::classmeth_iterator 952 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); 953 I != E; ++I) { 954 ObjCMethodDecl *method = *I; 955 if (method->getImplementationControl() != ObjCMethodDecl::Optional && 956 !ClsMap.count(method->getSelector()) && 957 (!Super || !Super->lookupClassMethod(method->getSelector()))) 958 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); 959 } 960 // Check on this protocols's referenced protocols, recursively. 961 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), 962 E = PDecl->protocol_end(); PI != E; ++PI) 963 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl); 964} 965 966/// MatchAllMethodDeclarations - Check methods declaraed in interface or 967/// or protocol against those declared in their implementations. 968/// 969void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap, 970 const llvm::DenseSet<Selector> &ClsMap, 971 llvm::DenseSet<Selector> &InsMapSeen, 972 llvm::DenseSet<Selector> &ClsMapSeen, 973 ObjCImplDecl* IMPDecl, 974 ObjCContainerDecl* CDecl, 975 bool &IncompleteImpl, 976 bool ImmediateClass) { 977 // Check and see if instance methods in class interface have been 978 // implemented in the implementation class. If so, their types match. 979 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(), 980 E = CDecl->instmeth_end(); I != E; ++I) { 981 if (InsMapSeen.count((*I)->getSelector())) 982 continue; 983 InsMapSeen.insert((*I)->getSelector()); 984 if (!(*I)->isSynthesized() && 985 !InsMap.count((*I)->getSelector())) { 986 if (ImmediateClass) 987 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); 988 continue; 989 } else { 990 ObjCMethodDecl *ImpMethodDecl = 991 IMPDecl->getInstanceMethod((*I)->getSelector()); 992 ObjCMethodDecl *IntfMethodDecl = 993 CDecl->getInstanceMethod((*I)->getSelector()); 994 assert(IntfMethodDecl && 995 "IntfMethodDecl is null in ImplMethodsVsClassMethods"); 996 // ImpMethodDecl may be null as in a @dynamic property. 997 if (ImpMethodDecl) 998 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); 999 } 1000 } 1001 1002 // Check and see if class methods in class interface have been 1003 // implemented in the implementation class. If so, their types match. 1004 for (ObjCInterfaceDecl::classmeth_iterator 1005 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) { 1006 if (ClsMapSeen.count((*I)->getSelector())) 1007 continue; 1008 ClsMapSeen.insert((*I)->getSelector()); 1009 if (!ClsMap.count((*I)->getSelector())) { 1010 if (ImmediateClass) 1011 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); 1012 } else { 1013 ObjCMethodDecl *ImpMethodDecl = 1014 IMPDecl->getClassMethod((*I)->getSelector()); 1015 ObjCMethodDecl *IntfMethodDecl = 1016 CDecl->getClassMethod((*I)->getSelector()); 1017 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); 1018 } 1019 } 1020 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { 1021 // Check for any implementation of a methods declared in protocol. 1022 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(), 1023 E = I->protocol_end(); PI != E; ++PI) 1024 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, 1025 IMPDecl, 1026 (*PI), IncompleteImpl, false); 1027 if (I->getSuperClass()) 1028 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, 1029 IMPDecl, 1030 I->getSuperClass(), IncompleteImpl, false); 1031 } 1032} 1033 1034void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl, 1035 ObjCContainerDecl* CDecl, 1036 bool IncompleteImpl) { 1037 llvm::DenseSet<Selector> InsMap; 1038 // Check and see if instance methods in class interface have been 1039 // implemented in the implementation class. 1040 for (ObjCImplementationDecl::instmeth_iterator 1041 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I) 1042 InsMap.insert((*I)->getSelector()); 1043 1044 // Check and see if properties declared in the interface have either 1) 1045 // an implementation or 2) there is a @synthesize/@dynamic implementation 1046 // of the property in the @implementation. 1047 if (isa<ObjCInterfaceDecl>(CDecl)) 1048 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(), 1049 E = CDecl->prop_end(); P != E; ++P) { 1050 ObjCPropertyDecl *Prop = (*P); 1051 if (Prop->isInvalidDecl()) 1052 continue; 1053 ObjCPropertyImplDecl *PI = 0; 1054 // Is there a matching propery synthesize/dynamic? 1055 for (ObjCImplDecl::propimpl_iterator 1056 I = IMPDecl->propimpl_begin(), 1057 EI = IMPDecl->propimpl_end(); I != EI; ++I) 1058 if ((*I)->getPropertyDecl() == Prop) { 1059 PI = (*I); 1060 break; 1061 } 1062 if (PI) 1063 continue; 1064 if (!InsMap.count(Prop->getGetterName())) { 1065 Diag(Prop->getLocation(), 1066 diag::warn_setter_getter_impl_required) 1067 << Prop->getDeclName() << Prop->getGetterName(); 1068 Diag(IMPDecl->getLocation(), 1069 diag::note_property_impl_required); 1070 } 1071 1072 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) { 1073 Diag(Prop->getLocation(), 1074 diag::warn_setter_getter_impl_required) 1075 << Prop->getDeclName() << Prop->getSetterName(); 1076 Diag(IMPDecl->getLocation(), 1077 diag::note_property_impl_required); 1078 } 1079 } 1080 1081 llvm::DenseSet<Selector> ClsMap; 1082 for (ObjCImplementationDecl::classmeth_iterator 1083 I = IMPDecl->classmeth_begin(), 1084 E = IMPDecl->classmeth_end(); I != E; ++I) 1085 ClsMap.insert((*I)->getSelector()); 1086 1087 // Check for type conflict of methods declared in a class/protocol and 1088 // its implementation; if any. 1089 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen; 1090 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, 1091 IMPDecl, CDecl, 1092 IncompleteImpl, true); 1093 1094 // Check the protocol list for unimplemented methods in the @implementation 1095 // class. 1096 // Check and see if class methods in class interface have been 1097 // implemented in the implementation class. 1098 1099 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { 1100 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(), 1101 E = I->protocol_end(); PI != E; ++PI) 1102 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, 1103 InsMap, ClsMap, I); 1104 // Check class extensions (unnamed categories) 1105 for (ObjCCategoryDecl *Categories = I->getCategoryList(); 1106 Categories; Categories = Categories->getNextClassCategory()) { 1107 if (!Categories->getIdentifier()) { 1108 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl); 1109 break; 1110 } 1111 } 1112 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { 1113 // For extended class, unimplemented methods in its protocols will 1114 // be reported in the primary class. 1115 if (C->getIdentifier()) { 1116 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(), 1117 E = C->protocol_end(); PI != E; ++PI) 1118 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, 1119 InsMap, ClsMap, C->getClassInterface()); 1120 } 1121 } else 1122 assert(false && "invalid ObjCContainerDecl type."); 1123} 1124 1125void 1126Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, 1127 ObjCContainerDecl* IDecl) { 1128 // Rules apply in non-GC mode only 1129 if (getLangOptions().getGCMode() != LangOptions::NonGC) 1130 return; 1131 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(), 1132 E = IDecl->prop_end(); 1133 I != E; ++I) { 1134 ObjCPropertyDecl *Property = (*I); 1135 unsigned Attributes = Property->getPropertyAttributes(); 1136 // We only care about readwrite atomic property. 1137 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || 1138 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) 1139 continue; 1140 if (const ObjCPropertyImplDecl *PIDecl 1141 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) { 1142 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) 1143 continue; 1144 ObjCMethodDecl *GetterMethod = 1145 IMPDecl->getInstanceMethod(Property->getGetterName()); 1146 ObjCMethodDecl *SetterMethod = 1147 IMPDecl->getInstanceMethod(Property->getSetterName()); 1148 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { 1149 SourceLocation MethodLoc = 1150 (GetterMethod ? GetterMethod->getLocation() 1151 : SetterMethod->getLocation()); 1152 Diag(MethodLoc, diag::warn_atomic_property_rule) 1153 << Property->getIdentifier(); 1154 Diag(Property->getLocation(), diag::note_property_declare); 1155 } 1156 } 1157 } 1158} 1159 1160/// ActOnForwardClassDeclaration - 1161Action::DeclPtrTy 1162Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, 1163 IdentifierInfo **IdentList, 1164 SourceLocation *IdentLocs, 1165 unsigned NumElts) { 1166 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; 1167 1168 for (unsigned i = 0; i != NumElts; ++i) { 1169 // Check for another declaration kind with the same name. 1170 NamedDecl *PrevDecl 1171 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName); 1172 if (PrevDecl && PrevDecl->isTemplateParameter()) { 1173 // Maybe we will complain about the shadowed template parameter. 1174 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl); 1175 // Just pretend that we didn't see the previous declaration. 1176 PrevDecl = 0; 1177 } 1178 1179 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 1180 // GCC apparently allows the following idiom: 1181 // 1182 // typedef NSObject < XCElementTogglerP > XCElementToggler; 1183 // @class XCElementToggler; 1184 // 1185 // FIXME: Make an extension? 1186 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl); 1187 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) { 1188 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; 1189 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 1190 } else if (TDD) { 1191 // a forward class declaration matching a typedef name of a class refers 1192 // to the underlying class. 1193 if (ObjCInterfaceType * OI = 1194 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType())) 1195 PrevDecl = OI->getDecl(); 1196 } 1197 } 1198 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 1199 if (!IDecl) { // Not already seen? Make a forward decl. 1200 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, 1201 IdentList[i], IdentLocs[i], true); 1202 1203 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to 1204 // the current DeclContext. This prevents clients that walk DeclContext 1205 // from seeing the imaginary ObjCInterfaceDecl until it is actually 1206 // declared later (if at all). We also take care to explicitly make 1207 // sure this declaration is visible for name lookup. 1208 PushOnScopeChains(IDecl, TUScope, false); 1209 CurContext->makeDeclVisibleInContext(IDecl, true); 1210 } 1211 1212 Interfaces.push_back(IDecl); 1213 } 1214 1215 assert(Interfaces.size() == NumElts); 1216 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc, 1217 Interfaces.data(), IdentLocs, 1218 Interfaces.size()); 1219 CurContext->addDecl(CDecl); 1220 CheckObjCDeclScope(CDecl); 1221 return DeclPtrTy::make(CDecl); 1222} 1223 1224 1225/// MatchTwoMethodDeclarations - Checks that two methods have matching type and 1226/// returns true, or false, accordingly. 1227/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons 1228bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, 1229 const ObjCMethodDecl *PrevMethod, 1230 bool matchBasedOnSizeAndAlignment) { 1231 QualType T1 = Context.getCanonicalType(Method->getResultType()); 1232 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType()); 1233 1234 if (T1 != T2) { 1235 // The result types are different. 1236 if (!matchBasedOnSizeAndAlignment) 1237 return false; 1238 // Incomplete types don't have a size and alignment. 1239 if (T1->isIncompleteType() || T2->isIncompleteType()) 1240 return false; 1241 // Check is based on size and alignment. 1242 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) 1243 return false; 1244 } 1245 1246 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), 1247 E = Method->param_end(); 1248 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin(); 1249 1250 for (; ParamI != E; ++ParamI, ++PrevI) { 1251 assert(PrevI != PrevMethod->param_end() && "Param mismatch"); 1252 T1 = Context.getCanonicalType((*ParamI)->getType()); 1253 T2 = Context.getCanonicalType((*PrevI)->getType()); 1254 if (T1 != T2) { 1255 // The result types are different. 1256 if (!matchBasedOnSizeAndAlignment) 1257 return false; 1258 // Incomplete types don't have a size and alignment. 1259 if (T1->isIncompleteType() || T2->isIncompleteType()) 1260 return false; 1261 // Check is based on size and alignment. 1262 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) 1263 return false; 1264 } 1265 } 1266 return true; 1267} 1268 1269/// \brief Read the contents of the instance and factory method pools 1270/// for a given selector from external storage. 1271/// 1272/// This routine should only be called once, when neither the instance 1273/// nor the factory method pool has an entry for this selector. 1274Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel, 1275 bool isInstance) { 1276 assert(ExternalSource && "We need an external AST source"); 1277 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() && 1278 "Selector data already loaded into the instance method pool"); 1279 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() && 1280 "Selector data already loaded into the factory method pool"); 1281 1282 // Read the method list from the external source. 1283 std::pair<ObjCMethodList, ObjCMethodList> Methods 1284 = ExternalSource->ReadMethodPool(Sel); 1285 1286 if (isInstance) { 1287 if (Methods.second.Method) 1288 FactoryMethodPool[Sel] = Methods.second; 1289 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first; 1290 } 1291 1292 if (Methods.first.Method) 1293 InstanceMethodPool[Sel] = Methods.first; 1294 1295 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first; 1296} 1297 1298void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) { 1299 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos 1300 = InstanceMethodPool.find(Method->getSelector()); 1301 if (Pos == InstanceMethodPool.end()) { 1302 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector())) 1303 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true); 1304 else 1305 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(), 1306 ObjCMethodList())).first; 1307 } 1308 1309 ObjCMethodList &Entry = Pos->second; 1310 if (Entry.Method == 0) { 1311 // Haven't seen a method with this selector name yet - add it. 1312 Entry.Method = Method; 1313 Entry.Next = 0; 1314 return; 1315 } 1316 1317 // We've seen a method with this name, see if we have already seen this type 1318 // signature. 1319 for (ObjCMethodList *List = &Entry; List; List = List->Next) 1320 if (MatchTwoMethodDeclarations(Method, List->Method)) 1321 return; 1322 1323 // We have a new signature for an existing method - add it. 1324 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". 1325 Entry.Next = new ObjCMethodList(Method, Entry.Next); 1326} 1327 1328// FIXME: Finish implementing -Wno-strict-selector-match. 1329ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel, 1330 SourceRange R, 1331 bool warn) { 1332 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos 1333 = InstanceMethodPool.find(Sel); 1334 if (Pos == InstanceMethodPool.end()) { 1335 if (ExternalSource && !FactoryMethodPool.count(Sel)) 1336 Pos = ReadMethodPool(Sel, /*isInstance=*/true); 1337 else 1338 return 0; 1339 } 1340 1341 ObjCMethodList &MethList = Pos->second; 1342 bool issueWarning = false; 1343 1344 if (MethList.Method && MethList.Next) { 1345 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 1346 // This checks if the methods differ by size & alignment. 1347 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) 1348 issueWarning = warn; 1349 } 1350 if (issueWarning && (MethList.Method && MethList.Next)) { 1351 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; 1352 Diag(MethList.Method->getLocStart(), diag::note_using) 1353 << MethList.Method->getSourceRange(); 1354 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 1355 Diag(Next->Method->getLocStart(), diag::note_also_found) 1356 << Next->Method->getSourceRange(); 1357 } 1358 return MethList.Method; 1359} 1360 1361void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { 1362 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos 1363 = FactoryMethodPool.find(Method->getSelector()); 1364 if (Pos == FactoryMethodPool.end()) { 1365 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector())) 1366 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false); 1367 else 1368 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(), 1369 ObjCMethodList())).first; 1370 } 1371 1372 ObjCMethodList &FirstMethod = Pos->second; 1373 if (!FirstMethod.Method) { 1374 // Haven't seen a method with this selector name yet - add it. 1375 FirstMethod.Method = Method; 1376 FirstMethod.Next = 0; 1377 } else { 1378 // We've seen a method with this name, now check the type signature(s). 1379 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); 1380 1381 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; 1382 Next = Next->Next) 1383 match = MatchTwoMethodDeclarations(Method, Next->Method); 1384 1385 if (!match) { 1386 // We have a new signature for an existing method - add it. 1387 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". 1388 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); 1389 FirstMethod.Next = OMI; 1390 } 1391 } 1392} 1393 1394ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel, 1395 SourceRange R) { 1396 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos 1397 = FactoryMethodPool.find(Sel); 1398 if (Pos == FactoryMethodPool.end()) { 1399 if (ExternalSource && !InstanceMethodPool.count(Sel)) 1400 Pos = ReadMethodPool(Sel, /*isInstance=*/false); 1401 else 1402 return 0; 1403 } 1404 1405 ObjCMethodList &MethList = Pos->second; 1406 bool issueWarning = false; 1407 1408 if (MethList.Method && MethList.Next) { 1409 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 1410 // This checks if the methods differ by size & alignment. 1411 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) 1412 issueWarning = true; 1413 } 1414 if (issueWarning && (MethList.Method && MethList.Next)) { 1415 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; 1416 Diag(MethList.Method->getLocStart(), diag::note_using) 1417 << MethList.Method->getSourceRange(); 1418 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 1419 Diag(Next->Method->getLocStart(), diag::note_also_found) 1420 << Next->Method->getSourceRange(); 1421 } 1422 return MethList.Method; 1423} 1424 1425/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods 1426/// have the property type and issue diagnostics if they don't. 1427/// Also synthesize a getter/setter method if none exist (and update the 1428/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized 1429/// methods is the "right" thing to do. 1430void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, 1431 ObjCContainerDecl *CD) { 1432 ObjCMethodDecl *GetterMethod, *SetterMethod; 1433 1434 GetterMethod = CD->getInstanceMethod(property->getGetterName()); 1435 SetterMethod = CD->getInstanceMethod(property->getSetterName()); 1436 DiagnosePropertyAccessorMismatch(property, GetterMethod, 1437 property->getLocation()); 1438 1439 if (SetterMethod) { 1440 if (Context.getCanonicalType(SetterMethod->getResultType()) 1441 != Context.VoidTy) 1442 Diag(SetterMethod->getLocation(), diag::err_setter_type_void); 1443 if (SetterMethod->param_size() != 1 || 1444 ((*SetterMethod->param_begin())->getType() != property->getType())) { 1445 Diag(property->getLocation(), 1446 diag::warn_accessor_property_type_mismatch) 1447 << property->getDeclName() 1448 << SetterMethod->getSelector(); 1449 Diag(SetterMethod->getLocation(), diag::note_declared_at); 1450 } 1451 } 1452 1453 // Synthesize getter/setter methods if none exist. 1454 // Find the default getter and if one not found, add one. 1455 // FIXME: The synthesized property we set here is misleading. We almost always 1456 // synthesize these methods unless the user explicitly provided prototypes 1457 // (which is odd, but allowed). Sema should be typechecking that the 1458 // declarations jive in that situation (which it is not currently). 1459 if (!GetterMethod) { 1460 // No instance method of same name as property getter name was found. 1461 // Declare a getter method and add it to the list of methods 1462 // for this class. 1463 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), 1464 property->getLocation(), property->getGetterName(), 1465 property->getType(), CD, true, false, true, 1466 (property->getPropertyImplementation() == 1467 ObjCPropertyDecl::Optional) ? 1468 ObjCMethodDecl::Optional : 1469 ObjCMethodDecl::Required); 1470 CD->addDecl(GetterMethod); 1471 } else 1472 // A user declared getter will be synthesize when @synthesize of 1473 // the property with the same name is seen in the @implementation 1474 GetterMethod->setSynthesized(true); 1475 property->setGetterMethodDecl(GetterMethod); 1476 1477 // Skip setter if property is read-only. 1478 if (!property->isReadOnly()) { 1479 // Find the default setter and if one not found, add one. 1480 if (!SetterMethod) { 1481 // No instance method of same name as property setter name was found. 1482 // Declare a setter method and add it to the list of methods 1483 // for this class. 1484 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), 1485 property->getLocation(), 1486 property->getSetterName(), 1487 Context.VoidTy, CD, true, false, true, 1488 (property->getPropertyImplementation() == 1489 ObjCPropertyDecl::Optional) ? 1490 ObjCMethodDecl::Optional : 1491 ObjCMethodDecl::Required); 1492 // Invent the arguments for the setter. We don't bother making a 1493 // nice name for the argument. 1494 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, 1495 property->getLocation(), 1496 property->getIdentifier(), 1497 property->getType(), 1498 /*TInfo=*/0, 1499 VarDecl::None, 1500 0); 1501 SetterMethod->setMethodParams(Context, &Argument, 1); 1502 CD->addDecl(SetterMethod); 1503 } else 1504 // A user declared setter will be synthesize when @synthesize of 1505 // the property with the same name is seen in the @implementation 1506 SetterMethod->setSynthesized(true); 1507 property->setSetterMethodDecl(SetterMethod); 1508 } 1509 // Add any synthesized methods to the global pool. This allows us to 1510 // handle the following, which is supported by GCC (and part of the design). 1511 // 1512 // @interface Foo 1513 // @property double bar; 1514 // @end 1515 // 1516 // void thisIsUnfortunate() { 1517 // id foo; 1518 // double bar = [foo bar]; 1519 // } 1520 // 1521 if (GetterMethod) 1522 AddInstanceMethodToGlobalPool(GetterMethod); 1523 if (SetterMethod) 1524 AddInstanceMethodToGlobalPool(SetterMethod); 1525} 1526 1527/// CompareMethodParamsInBaseAndSuper - This routine compares methods with 1528/// identical selector names in current and its super classes and issues 1529/// a warning if any of their argument types are incompatible. 1530void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl, 1531 ObjCMethodDecl *Method, 1532 bool IsInstance) { 1533 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl); 1534 if (ID == 0) return; 1535 1536 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) { 1537 ObjCMethodDecl *SuperMethodDecl = 1538 SD->lookupMethod(Method->getSelector(), IsInstance); 1539 if (SuperMethodDecl == 0) { 1540 ID = SD; 1541 continue; 1542 } 1543 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), 1544 E = Method->param_end(); 1545 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin(); 1546 for (; ParamI != E; ++ParamI, ++PrevI) { 1547 // Number of parameters are the same and is guaranteed by selector match. 1548 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch"); 1549 QualType T1 = Context.getCanonicalType((*ParamI)->getType()); 1550 QualType T2 = Context.getCanonicalType((*PrevI)->getType()); 1551 // If type of arguement of method in this class does not match its 1552 // respective argument type in the super class method, issue warning; 1553 if (!Context.typesAreCompatible(T1, T2)) { 1554 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) 1555 << T1 << T2; 1556 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration); 1557 return; 1558 } 1559 } 1560 ID = SD; 1561 } 1562} 1563 1564// Note: For class/category implemenations, allMethods/allProperties is 1565// always null. 1566void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl, 1567 DeclPtrTy *allMethods, unsigned allNum, 1568 DeclPtrTy *allProperties, unsigned pNum, 1569 DeclGroupPtrTy *allTUVars, unsigned tuvNum) { 1570 Decl *ClassDecl = classDecl.getAs<Decl>(); 1571 1572 // FIXME: If we don't have a ClassDecl, we have an error. We should consider 1573 // always passing in a decl. If the decl has an error, isInvalidDecl() 1574 // should be true. 1575 if (!ClassDecl) 1576 return; 1577 1578 bool isInterfaceDeclKind = 1579 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) 1580 || isa<ObjCProtocolDecl>(ClassDecl); 1581 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); 1582 1583 if (!isInterfaceDeclKind && AtEndLoc.isInvalid()) { 1584 AtEndLoc = ClassDecl->getLocation(); 1585 Diag(AtEndLoc, diag::warn_missing_atend); 1586 } 1587 1588 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); 1589 1590 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. 1591 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; 1592 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; 1593 1594 for (unsigned i = 0; i < allNum; i++ ) { 1595 ObjCMethodDecl *Method = 1596 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>()); 1597 1598 if (!Method) continue; // Already issued a diagnostic. 1599 if (Method->isInstanceMethod()) { 1600 /// Check for instance method of the same name with incompatible types 1601 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; 1602 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 1603 : false; 1604 if ((isInterfaceDeclKind && PrevMethod && !match) 1605 || (checkIdenticalMethods && match)) { 1606 Diag(Method->getLocation(), diag::err_duplicate_method_decl) 1607 << Method->getDeclName(); 1608 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1609 } else { 1610 DC->addDecl(Method); 1611 InsMap[Method->getSelector()] = Method; 1612 /// The following allows us to typecheck messages to "id". 1613 AddInstanceMethodToGlobalPool(Method); 1614 // verify that the instance method conforms to the same definition of 1615 // parent methods if it shadows one. 1616 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true); 1617 } 1618 } else { 1619 /// Check for class method of the same name with incompatible types 1620 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; 1621 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 1622 : false; 1623 if ((isInterfaceDeclKind && PrevMethod && !match) 1624 || (checkIdenticalMethods && match)) { 1625 Diag(Method->getLocation(), diag::err_duplicate_method_decl) 1626 << Method->getDeclName(); 1627 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1628 } else { 1629 DC->addDecl(Method); 1630 ClsMap[Method->getSelector()] = Method; 1631 /// The following allows us to typecheck messages to "Class". 1632 AddFactoryMethodToGlobalPool(Method); 1633 // verify that the class method conforms to the same definition of 1634 // parent methods if it shadows one. 1635 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false); 1636 } 1637 } 1638 } 1639 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 1640 // Compares properties declared in this class to those of its 1641 // super class. 1642 ComparePropertiesInBaseAndSuper(I); 1643 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I)); 1644 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { 1645 // Categories are used to extend the class by declaring new methods. 1646 // By the same token, they are also used to add new properties. No 1647 // need to compare the added property to those in the class. 1648 1649 // Merge protocol properties into category 1650 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C)); 1651 if (C->getIdentifier() == 0) 1652 DiagnoseClassExtensionDupMethods(C, C->getClassInterface()); 1653 } 1654 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { 1655 // ProcessPropertyDecl is responsible for diagnosing conflicts with any 1656 // user-defined setter/getter. It also synthesizes setter/getter methods 1657 // and adds them to the DeclContext and global method pools. 1658 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), 1659 E = CDecl->prop_end(); 1660 I != E; ++I) 1661 ProcessPropertyDecl(*I, CDecl); 1662 CDecl->setAtEndLoc(AtEndLoc); 1663 } 1664 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 1665 IC->setAtEndLoc(AtEndLoc); 1666 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { 1667 ImplMethodsVsClassMethods(IC, IDecl); 1668 AtomicPropertySetterGetterRules(IC, IDecl); 1669 } 1670 } else if (ObjCCategoryImplDecl* CatImplClass = 1671 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { 1672 CatImplClass->setAtEndLoc(AtEndLoc); 1673 1674 // Find category interface decl and then check that all methods declared 1675 // in this interface are implemented in the category @implementation. 1676 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { 1677 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); 1678 Categories; Categories = Categories->getNextClassCategory()) { 1679 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { 1680 ImplMethodsVsClassMethods(CatImplClass, Categories); 1681 break; 1682 } 1683 } 1684 } 1685 } 1686 if (isInterfaceDeclKind) { 1687 // Reject invalid vardecls. 1688 for (unsigned i = 0; i != tuvNum; i++) { 1689 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>(); 1690 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) 1691 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { 1692 if (!VDecl->hasExternalStorage()) 1693 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); 1694 } 1695 } 1696 } 1697} 1698 1699 1700/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for 1701/// objective-c's type qualifier from the parser version of the same info. 1702static Decl::ObjCDeclQualifier 1703CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { 1704 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; 1705 if (PQTVal & ObjCDeclSpec::DQ_In) 1706 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); 1707 if (PQTVal & ObjCDeclSpec::DQ_Inout) 1708 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); 1709 if (PQTVal & ObjCDeclSpec::DQ_Out) 1710 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); 1711 if (PQTVal & ObjCDeclSpec::DQ_Bycopy) 1712 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); 1713 if (PQTVal & ObjCDeclSpec::DQ_Byref) 1714 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); 1715 if (PQTVal & ObjCDeclSpec::DQ_Oneway) 1716 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); 1717 1718 return ret; 1719} 1720 1721Sema::DeclPtrTy Sema::ActOnMethodDeclaration( 1722 SourceLocation MethodLoc, SourceLocation EndLoc, 1723 tok::TokenKind MethodType, DeclPtrTy classDecl, 1724 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, 1725 Selector Sel, 1726 // optional arguments. The number of types/arguments is obtained 1727 // from the Sel.getNumArgs(). 1728 ObjCArgInfo *ArgInfo, 1729 llvm::SmallVectorImpl<Declarator> &Cdecls, 1730 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, 1731 bool isVariadic) { 1732 Decl *ClassDecl = classDecl.getAs<Decl>(); 1733 1734 // Make sure we can establish a context for the method. 1735 if (!ClassDecl) { 1736 Diag(MethodLoc, diag::error_missing_method_context); 1737 FunctionLabelMap.clear(); 1738 return DeclPtrTy(); 1739 } 1740 QualType resultDeclType; 1741 1742 if (ReturnType) { 1743 resultDeclType = GetTypeFromParser(ReturnType); 1744 1745 // Methods cannot return interface types. All ObjC objects are 1746 // passed by reference. 1747 if (resultDeclType->isObjCInterfaceType()) { 1748 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value) 1749 << 0 << resultDeclType; 1750 return DeclPtrTy(); 1751 } 1752 } else // get the type for "id". 1753 resultDeclType = Context.getObjCIdType(); 1754 1755 ObjCMethodDecl* ObjCMethod = 1756 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, 1757 cast<DeclContext>(ClassDecl), 1758 MethodType == tok::minus, isVariadic, 1759 false, 1760 MethodDeclKind == tok::objc_optional ? 1761 ObjCMethodDecl::Optional : 1762 ObjCMethodDecl::Required); 1763 1764 llvm::SmallVector<ParmVarDecl*, 16> Params; 1765 1766 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { 1767 QualType ArgType; 1768 TypeSourceInfo *DI; 1769 1770 if (ArgInfo[i].Type == 0) { 1771 ArgType = Context.getObjCIdType(); 1772 DI = 0; 1773 } else { 1774 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); 1775 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). 1776 ArgType = adjustParameterType(ArgType); 1777 } 1778 1779 ParmVarDecl* Param 1780 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc, 1781 ArgInfo[i].Name, ArgType, DI, 1782 VarDecl::None, 0); 1783 1784 if (ArgType->isObjCInterfaceType()) { 1785 Diag(ArgInfo[i].NameLoc, 1786 diag::err_object_cannot_be_passed_returned_by_value) 1787 << 1 << ArgType; 1788 Param->setInvalidDecl(); 1789 } 1790 1791 Param->setObjCDeclQualifier( 1792 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); 1793 1794 // Apply the attributes to the parameter. 1795 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); 1796 1797 Params.push_back(Param); 1798 } 1799 1800 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs()); 1801 ObjCMethod->setObjCDeclQualifier( 1802 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); 1803 const ObjCMethodDecl *PrevMethod = 0; 1804 1805 if (AttrList) 1806 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); 1807 1808 const ObjCMethodDecl *InterfaceMD = 0; 1809 1810 // For implementations (which can be very "coarse grain"), we add the 1811 // method now. This allows the AST to implement lookup methods that work 1812 // incrementally (without waiting until we parse the @end). It also allows 1813 // us to flag multiple declaration errors as they occur. 1814 if (ObjCImplementationDecl *ImpDecl = 1815 dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 1816 if (MethodType == tok::minus) { 1817 PrevMethod = ImpDecl->getInstanceMethod(Sel); 1818 ImpDecl->addInstanceMethod(ObjCMethod); 1819 } else { 1820 PrevMethod = ImpDecl->getClassMethod(Sel); 1821 ImpDecl->addClassMethod(ObjCMethod); 1822 } 1823 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel, 1824 MethodType == tok::minus); 1825 if (AttrList) 1826 Diag(EndLoc, diag::warn_attribute_method_def); 1827 } else if (ObjCCategoryImplDecl *CatImpDecl = 1828 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { 1829 if (MethodType == tok::minus) { 1830 PrevMethod = CatImpDecl->getInstanceMethod(Sel); 1831 CatImpDecl->addInstanceMethod(ObjCMethod); 1832 } else { 1833 PrevMethod = CatImpDecl->getClassMethod(Sel); 1834 CatImpDecl->addClassMethod(ObjCMethod); 1835 } 1836 if (AttrList) 1837 Diag(EndLoc, diag::warn_attribute_method_def); 1838 } 1839 if (PrevMethod) { 1840 // You can never have two method definitions with the same name. 1841 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) 1842 << ObjCMethod->getDeclName(); 1843 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1844 } 1845 1846 // If the interface declared this method, and it was deprecated there, 1847 // mark it deprecated here. 1848 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>()) 1849 ObjCMethod->addAttr(::new (Context) DeprecatedAttr()); 1850 1851 return DeclPtrTy::make(ObjCMethod); 1852} 1853 1854void Sema::CheckObjCPropertyAttributes(QualType PropertyTy, 1855 SourceLocation Loc, 1856 unsigned &Attributes) { 1857 // FIXME: Improve the reported location. 1858 1859 // readonly and readwrite/assign/retain/copy conflict. 1860 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1861 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | 1862 ObjCDeclSpec::DQ_PR_assign | 1863 ObjCDeclSpec::DQ_PR_copy | 1864 ObjCDeclSpec::DQ_PR_retain))) { 1865 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? 1866 "readwrite" : 1867 (Attributes & ObjCDeclSpec::DQ_PR_assign) ? 1868 "assign" : 1869 (Attributes & ObjCDeclSpec::DQ_PR_copy) ? 1870 "copy" : "retain"; 1871 1872 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? 1873 diag::err_objc_property_attr_mutually_exclusive : 1874 diag::warn_objc_property_attr_mutually_exclusive) 1875 << "readonly" << which; 1876 } 1877 1878 // Check for copy or retain on non-object types. 1879 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) && 1880 !PropertyTy->isObjCObjectPointerType() && 1881 !PropertyTy->isBlockPointerType() && 1882 !Context.isObjCNSObjectType(PropertyTy)) { 1883 Diag(Loc, diag::err_objc_property_requires_object) 1884 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain"); 1885 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain); 1886 } 1887 1888 // Check for more than one of { assign, copy, retain }. 1889 if (Attributes & ObjCDeclSpec::DQ_PR_assign) { 1890 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1891 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1892 << "assign" << "copy"; 1893 Attributes &= ~ObjCDeclSpec::DQ_PR_copy; 1894 } 1895 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1896 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1897 << "assign" << "retain"; 1898 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1899 } 1900 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1901 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1902 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1903 << "copy" << "retain"; 1904 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1905 } 1906 } 1907 1908 // Warn if user supplied no assignment attribute, property is 1909 // readwrite, and this is an object type. 1910 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | 1911 ObjCDeclSpec::DQ_PR_retain)) && 1912 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1913 PropertyTy->isObjCObjectPointerType()) { 1914 // Skip this warning in gc-only mode. 1915 if (getLangOptions().getGCMode() != LangOptions::GCOnly) 1916 Diag(Loc, diag::warn_objc_property_no_assignment_attribute); 1917 1918 // If non-gc code warn that this is likely inappropriate. 1919 if (getLangOptions().getGCMode() == LangOptions::NonGC) 1920 Diag(Loc, diag::warn_objc_property_default_assign_on_object); 1921 1922 // FIXME: Implement warning dependent on NSCopying being 1923 // implemented. See also: 1924 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> 1925 // (please trim this list while you are at it). 1926 } 1927 1928 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) 1929 && getLangOptions().getGCMode() == LangOptions::GCOnly 1930 && PropertyTy->isBlockPointerType()) 1931 Diag(Loc, diag::warn_objc_property_copy_missing_on_block); 1932} 1933 1934Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, 1935 FieldDeclarator &FD, 1936 ObjCDeclSpec &ODS, 1937 Selector GetterSel, 1938 Selector SetterSel, 1939 DeclPtrTy ClassCategory, 1940 bool *isOverridingProperty, 1941 tok::ObjCKeywordKind MethodImplKind) { 1942 unsigned Attributes = ODS.getPropertyAttributes(); 1943 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || 1944 // default is readwrite! 1945 !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); 1946 // property is defaulted to 'assign' if it is readwrite and is 1947 // not retain or copy 1948 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || 1949 (isReadWrite && 1950 !(Attributes & ObjCDeclSpec::DQ_PR_retain) && 1951 !(Attributes & ObjCDeclSpec::DQ_PR_copy))); 1952 QualType T = GetTypeForDeclarator(FD.D, S); 1953 if (T->isReferenceType()) { 1954 Diag(AtLoc, diag::error_reference_property); 1955 return DeclPtrTy(); 1956 } 1957 Decl *ClassDecl = ClassCategory.getAs<Decl>(); 1958 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class 1959 // May modify Attributes. 1960 CheckObjCPropertyAttributes(T, AtLoc, Attributes); 1961 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) 1962 if (!CDecl->getIdentifier()) { 1963 // This is a continuation class. property requires special 1964 // handling. 1965 if ((CCPrimary = CDecl->getClassInterface())) { 1966 // Find the property in continuation class's primary class only. 1967 IdentifierInfo *PropertyId = FD.D.getIdentifier(); 1968 if (ObjCPropertyDecl *PIDecl = 1969 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) { 1970 // property 'PIDecl's readonly attribute will be over-ridden 1971 // with continuation class's readwrite property attribute! 1972 unsigned PIkind = PIDecl->getPropertyAttributes(); 1973 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { 1974 unsigned retainCopyNonatomic = 1975 (ObjCPropertyDecl::OBJC_PR_retain | 1976 ObjCPropertyDecl::OBJC_PR_copy | 1977 ObjCPropertyDecl::OBJC_PR_nonatomic); 1978 if ((Attributes & retainCopyNonatomic) != 1979 (PIkind & retainCopyNonatomic)) { 1980 Diag(AtLoc, diag::warn_property_attr_mismatch); 1981 Diag(PIDecl->getLocation(), diag::note_property_declare); 1982 } 1983 PIDecl->makeitReadWriteAttribute(); 1984 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 1985 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 1986 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 1987 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 1988 PIDecl->setSetterName(SetterSel); 1989 } else { 1990 Diag(AtLoc, diag::err_use_continuation_class) 1991 << CCPrimary->getDeclName(); 1992 Diag(PIDecl->getLocation(), diag::note_property_declare); 1993 } 1994 *isOverridingProperty = true; 1995 // Make sure setter decl is synthesized, and added to primary 1996 // class's list. 1997 ProcessPropertyDecl(PIDecl, CCPrimary); 1998 return DeclPtrTy(); 1999 } 2000 // No matching property found in the primary class. Just fall thru 2001 // and add property to continuation class's primary class. 2002 ClassDecl = CCPrimary; 2003 } else { 2004 Diag(CDecl->getLocation(), diag::err_continuation_class); 2005 *isOverridingProperty = true; 2006 return DeclPtrTy(); 2007 } 2008 } 2009 2010 // Issue a warning if property is 'assign' as default and its object, which is 2011 // gc'able conforms to NSCopying protocol 2012 if (getLangOptions().getGCMode() != LangOptions::NonGC && 2013 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) 2014 if (T->isObjCObjectPointerType()) { 2015 QualType InterfaceTy = T->getPointeeType(); 2016 if (const ObjCInterfaceType *OIT = 2017 InterfaceTy->getAs<ObjCInterfaceType>()) { 2018 ObjCInterfaceDecl *IDecl = OIT->getDecl(); 2019 if (IDecl) 2020 if (ObjCProtocolDecl* PNSCopying = 2021 LookupProtocol(&Context.Idents.get("NSCopying"))) 2022 if (IDecl->ClassImplementsProtocol(PNSCopying, true)) 2023 Diag(AtLoc, diag::warn_implements_nscopying) 2024 << FD.D.getIdentifier(); 2025 } 2026 } 2027 if (T->isObjCInterfaceType()) 2028 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object); 2029 2030 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); 2031 assert(DC && "ClassDecl is not a DeclContext"); 2032 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, 2033 FD.D.getIdentifierLoc(), 2034 FD.D.getIdentifier(), T); 2035 DC->addDecl(PDecl); 2036 2037 if (T->isArrayType() || T->isFunctionType()) { 2038 Diag(AtLoc, diag::err_property_type) << T; 2039 PDecl->setInvalidDecl(); 2040 } 2041 2042 ProcessDeclAttributes(S, PDecl, FD.D); 2043 2044 // Regardless of setter/getter attribute, we save the default getter/setter 2045 // selector names in anticipation of declaration of setter/getter methods. 2046 PDecl->setGetterName(GetterSel); 2047 PDecl->setSetterName(SetterSel); 2048 2049 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) 2050 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); 2051 2052 if (Attributes & ObjCDeclSpec::DQ_PR_getter) 2053 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); 2054 2055 if (Attributes & ObjCDeclSpec::DQ_PR_setter) 2056 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); 2057 2058 if (isReadWrite) 2059 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); 2060 2061 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 2062 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 2063 2064 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 2065 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 2066 2067 if (isAssign) 2068 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); 2069 2070 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) 2071 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); 2072 2073 if (MethodImplKind == tok::objc_required) 2074 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); 2075 else if (MethodImplKind == tok::objc_optional) 2076 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); 2077 // A case of continuation class adding a new property in the class. This 2078 // is not what it was meant for. However, gcc supports it and so should we. 2079 // Make sure setter/getters are declared here. 2080 if (CCPrimary) 2081 ProcessPropertyDecl(PDecl, CCPrimary); 2082 2083 return DeclPtrTy::make(PDecl); 2084} 2085 2086/// ActOnPropertyImplDecl - This routine performs semantic checks and 2087/// builds the AST node for a property implementation declaration; declared 2088/// as @synthesize or @dynamic. 2089/// 2090Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc, 2091 SourceLocation PropertyLoc, 2092 bool Synthesize, 2093 DeclPtrTy ClassCatImpDecl, 2094 IdentifierInfo *PropertyId, 2095 IdentifierInfo *PropertyIvar) { 2096 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>(); 2097 // Make sure we have a context for the property implementation declaration. 2098 if (!ClassImpDecl) { 2099 Diag(AtLoc, diag::error_missing_property_context); 2100 return DeclPtrTy(); 2101 } 2102 ObjCPropertyDecl *property = 0; 2103 ObjCInterfaceDecl* IDecl = 0; 2104 // Find the class or category class where this property must have 2105 // a declaration. 2106 ObjCImplementationDecl *IC = 0; 2107 ObjCCategoryImplDecl* CatImplClass = 0; 2108 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { 2109 IDecl = IC->getClassInterface(); 2110 // We always synthesize an interface for an implementation 2111 // without an interface decl. So, IDecl is always non-zero. 2112 assert(IDecl && 2113 "ActOnPropertyImplDecl - @implementation without @interface"); 2114 2115 // Look for this property declaration in the @implementation's @interface 2116 property = IDecl->FindPropertyDeclaration(PropertyId); 2117 if (!property) { 2118 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); 2119 return DeclPtrTy(); 2120 } 2121 if (const ObjCCategoryDecl *CD = 2122 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) { 2123 if (CD->getIdentifier()) { 2124 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName(); 2125 Diag(property->getLocation(), diag::note_property_declare); 2126 return DeclPtrTy(); 2127 } 2128 } 2129 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { 2130 if (Synthesize) { 2131 Diag(AtLoc, diag::error_synthesize_category_decl); 2132 return DeclPtrTy(); 2133 } 2134 IDecl = CatImplClass->getClassInterface(); 2135 if (!IDecl) { 2136 Diag(AtLoc, diag::error_missing_property_interface); 2137 return DeclPtrTy(); 2138 } 2139 ObjCCategoryDecl *Category = 2140 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); 2141 2142 // If category for this implementation not found, it is an error which 2143 // has already been reported eralier. 2144 if (!Category) 2145 return DeclPtrTy(); 2146 // Look for this property declaration in @implementation's category 2147 property = Category->FindPropertyDeclaration(PropertyId); 2148 if (!property) { 2149 Diag(PropertyLoc, diag::error_bad_category_property_decl) 2150 << Category->getDeclName(); 2151 return DeclPtrTy(); 2152 } 2153 } else { 2154 Diag(AtLoc, diag::error_bad_property_context); 2155 return DeclPtrTy(); 2156 } 2157 ObjCIvarDecl *Ivar = 0; 2158 // Check that we have a valid, previously declared ivar for @synthesize 2159 if (Synthesize) { 2160 // @synthesize 2161 if (!PropertyIvar) 2162 PropertyIvar = PropertyId; 2163 QualType PropType = Context.getCanonicalType(property->getType()); 2164 // Check that this is a previously declared 'ivar' in 'IDecl' interface 2165 ObjCInterfaceDecl *ClassDeclared; 2166 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); 2167 if (!Ivar) { 2168 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl); 2169 assert(EnclosingContext && 2170 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl"); 2171 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc, 2172 PropertyIvar, PropType, /*Dinfo=*/0, 2173 ObjCIvarDecl::Public, 2174 (Expr *)0); 2175 Ivar->setLexicalDeclContext(IDecl); 2176 IDecl->addDecl(Ivar); 2177 property->setPropertyIvarDecl(Ivar); 2178 if (!getLangOptions().ObjCNonFragileABI) 2179 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId; 2180 // Note! I deliberately want it to fall thru so, we have a 2181 // a property implementation and to avoid future warnings. 2182 } else if (getLangOptions().ObjCNonFragileABI && 2183 ClassDeclared != IDecl) { 2184 Diag(PropertyLoc, diag::error_ivar_in_superclass_use) 2185 << property->getDeclName() << Ivar->getDeclName() 2186 << ClassDeclared->getDeclName(); 2187 Diag(Ivar->getLocation(), diag::note_previous_access_declaration) 2188 << Ivar << Ivar->getNameAsCString(); 2189 // Note! I deliberately want it to fall thru so more errors are caught. 2190 } 2191 QualType IvarType = Context.getCanonicalType(Ivar->getType()); 2192 2193 // Check that type of property and its ivar are type compatible. 2194 if (PropType != IvarType) { 2195 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) { 2196 Diag(PropertyLoc, diag::error_property_ivar_type) 2197 << property->getDeclName() << Ivar->getDeclName(); 2198 // Note! I deliberately want it to fall thru so, we have a 2199 // a property implementation and to avoid future warnings. 2200 } 2201 2202 // FIXME! Rules for properties are somewhat different that those 2203 // for assignments. Use a new routine to consolidate all cases; 2204 // specifically for property redeclarations as well as for ivars. 2205 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType(); 2206 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); 2207 if (lhsType != rhsType && 2208 lhsType->isArithmeticType()) { 2209 Diag(PropertyLoc, diag::error_property_ivar_type) 2210 << property->getDeclName() << Ivar->getDeclName(); 2211 // Fall thru - see previous comment 2212 } 2213 // __weak is explicit. So it works on Canonical type. 2214 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && 2215 getLangOptions().getGCMode() != LangOptions::NonGC) { 2216 Diag(PropertyLoc, diag::error_weak_property) 2217 << property->getDeclName() << Ivar->getDeclName(); 2218 // Fall thru - see previous comment 2219 } 2220 if ((property->getType()->isObjCObjectPointerType() || 2221 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && 2222 getLangOptions().getGCMode() != LangOptions::NonGC) { 2223 Diag(PropertyLoc, diag::error_strong_property) 2224 << property->getDeclName() << Ivar->getDeclName(); 2225 // Fall thru - see previous comment 2226 } 2227 } 2228 } else if (PropertyIvar) 2229 // @dynamic 2230 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); 2231 assert (property && "ActOnPropertyImplDecl - property declaration missing"); 2232 ObjCPropertyImplDecl *PIDecl = 2233 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, 2234 property, 2235 (Synthesize ? 2236 ObjCPropertyImplDecl::Synthesize 2237 : ObjCPropertyImplDecl::Dynamic), 2238 Ivar); 2239 if (IC) { 2240 if (Synthesize) 2241 if (ObjCPropertyImplDecl *PPIDecl = 2242 IC->FindPropertyImplIvarDecl(PropertyIvar)) { 2243 Diag(PropertyLoc, diag::error_duplicate_ivar_use) 2244 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 2245 << PropertyIvar; 2246 Diag(PPIDecl->getLocation(), diag::note_previous_use); 2247 } 2248 2249 if (ObjCPropertyImplDecl *PPIDecl 2250 = IC->FindPropertyImplDecl(PropertyId)) { 2251 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; 2252 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 2253 return DeclPtrTy(); 2254 } 2255 IC->addPropertyImplementation(PIDecl); 2256 } else { 2257 if (Synthesize) 2258 if (ObjCPropertyImplDecl *PPIDecl = 2259 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { 2260 Diag(PropertyLoc, diag::error_duplicate_ivar_use) 2261 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 2262 << PropertyIvar; 2263 Diag(PPIDecl->getLocation(), diag::note_previous_use); 2264 } 2265 2266 if (ObjCPropertyImplDecl *PPIDecl = 2267 CatImplClass->FindPropertyImplDecl(PropertyId)) { 2268 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; 2269 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 2270 return DeclPtrTy(); 2271 } 2272 CatImplClass->addPropertyImplementation(PIDecl); 2273 } 2274 2275 return DeclPtrTy::make(PIDecl); 2276} 2277 2278bool Sema::CheckObjCDeclScope(Decl *D) { 2279 if (isa<TranslationUnitDecl>(CurContext->getLookupContext())) 2280 return false; 2281 2282 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); 2283 D->setInvalidDecl(); 2284 2285 return true; 2286} 2287 2288/// Called whenever @defs(ClassName) is encountered in the source. Inserts the 2289/// instance variables of ClassName into Decls. 2290void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart, 2291 IdentifierInfo *ClassName, 2292 llvm::SmallVectorImpl<DeclPtrTy> &Decls) { 2293 // Check that ClassName is a valid class 2294 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName); 2295 if (!Class) { 2296 Diag(DeclStart, diag::err_undef_interface) << ClassName; 2297 return; 2298 } 2299 if (LangOpts.ObjCNonFragileABI) { 2300 Diag(DeclStart, diag::err_atdef_nonfragile_interface); 2301 return; 2302 } 2303 2304 // Collect the instance variables 2305 llvm::SmallVector<FieldDecl*, 32> RecFields; 2306 Context.CollectObjCIvars(Class, RecFields); 2307 // For each ivar, create a fresh ObjCAtDefsFieldDecl. 2308 for (unsigned i = 0; i < RecFields.size(); i++) { 2309 FieldDecl* ID = RecFields[i]; 2310 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()); 2311 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(), 2312 ID->getIdentifier(), ID->getType(), 2313 ID->getBitWidth()); 2314 Decls.push_back(Sema::DeclPtrTy::make(FD)); 2315 } 2316 2317 // Introduce all of these fields into the appropriate scope. 2318 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin(); 2319 D != Decls.end(); ++D) { 2320 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>()); 2321 if (getLangOptions().CPlusPlus) 2322 PushOnScopeChains(cast<FieldDecl>(FD), S); 2323 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>())) 2324 Record->addDecl(FD); 2325 } 2326} 2327 2328