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