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