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