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