SemaDeclObjC.cpp revision 32844b35f61e04b23618e19c5ea41d57b975f9ac
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 bool warn) { 1269 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos 1270 = InstanceMethodPool.find(Sel); 1271 if (Pos == InstanceMethodPool.end()) { 1272 if (ExternalSource && !FactoryMethodPool.count(Sel)) 1273 Pos = ReadMethodPool(Sel, /*isInstance=*/true); 1274 else 1275 return 0; 1276 } 1277 1278 ObjCMethodList &MethList = Pos->second; 1279 bool issueWarning = false; 1280 1281 if (MethList.Method && MethList.Next) { 1282 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 1283 // This checks if the methods differ by size & alignment. 1284 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) 1285 issueWarning = warn; 1286 } 1287 if (issueWarning && (MethList.Method && MethList.Next)) { 1288 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; 1289 Diag(MethList.Method->getLocStart(), diag::note_using_decl) 1290 << MethList.Method->getSourceRange(); 1291 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 1292 Diag(Next->Method->getLocStart(), diag::note_also_found_decl) 1293 << Next->Method->getSourceRange(); 1294 } 1295 return MethList.Method; 1296} 1297 1298void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { 1299 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos 1300 = FactoryMethodPool.find(Method->getSelector()); 1301 if (Pos == FactoryMethodPool.end()) { 1302 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector())) 1303 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false); 1304 else 1305 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(), 1306 ObjCMethodList())).first; 1307 } 1308 1309 ObjCMethodList &FirstMethod = Pos->second; 1310 if (!FirstMethod.Method) { 1311 // Haven't seen a method with this selector name yet - add it. 1312 FirstMethod.Method = Method; 1313 FirstMethod.Next = 0; 1314 } else { 1315 // We've seen a method with this name, now check the type signature(s). 1316 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); 1317 1318 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; 1319 Next = Next->Next) 1320 match = MatchTwoMethodDeclarations(Method, Next->Method); 1321 1322 if (!match) { 1323 // We have a new signature for an existing method - add it. 1324 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". 1325 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); 1326 FirstMethod.Next = OMI; 1327 } 1328 } 1329} 1330 1331ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel, 1332 SourceRange R) { 1333 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos 1334 = FactoryMethodPool.find(Sel); 1335 if (Pos == FactoryMethodPool.end()) { 1336 if (ExternalSource && !InstanceMethodPool.count(Sel)) 1337 Pos = ReadMethodPool(Sel, /*isInstance=*/false); 1338 else 1339 return 0; 1340 } 1341 1342 ObjCMethodList &MethList = Pos->second; 1343 bool issueWarning = false; 1344 1345 if (MethList.Method && MethList.Next) { 1346 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 1347 // This checks if the methods differ by size & alignment. 1348 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) 1349 issueWarning = true; 1350 } 1351 if (issueWarning && (MethList.Method && MethList.Next)) { 1352 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; 1353 Diag(MethList.Method->getLocStart(), diag::note_using_decl) 1354 << MethList.Method->getSourceRange(); 1355 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 1356 Diag(Next->Method->getLocStart(), diag::note_also_found_decl) 1357 << Next->Method->getSourceRange(); 1358 } 1359 return MethList.Method; 1360} 1361 1362/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods 1363/// have the property type and issue diagnostics if they don't. 1364/// Also synthesize a getter/setter method if none exist (and update the 1365/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized 1366/// methods is the "right" thing to do. 1367void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, 1368 ObjCContainerDecl *CD) { 1369 ObjCMethodDecl *GetterMethod, *SetterMethod; 1370 1371 GetterMethod = CD->getInstanceMethod(property->getGetterName()); 1372 SetterMethod = CD->getInstanceMethod(property->getSetterName()); 1373 DiagnosePropertyAccessorMismatch(property, GetterMethod, 1374 property->getLocation()); 1375 1376 if (SetterMethod) { 1377 if (Context.getCanonicalType(SetterMethod->getResultType()) 1378 != Context.VoidTy) 1379 Diag(SetterMethod->getLocation(), diag::err_setter_type_void); 1380 if (SetterMethod->param_size() != 1 || 1381 ((*SetterMethod->param_begin())->getType() != property->getType())) { 1382 Diag(property->getLocation(), 1383 diag::warn_accessor_property_type_mismatch) 1384 << property->getDeclName() 1385 << SetterMethod->getSelector(); 1386 Diag(SetterMethod->getLocation(), diag::note_declared_at); 1387 } 1388 } 1389 1390 // Synthesize getter/setter methods if none exist. 1391 // Find the default getter and if one not found, add one. 1392 // FIXME: The synthesized property we set here is misleading. We almost always 1393 // synthesize these methods unless the user explicitly provided prototypes 1394 // (which is odd, but allowed). Sema should be typechecking that the 1395 // declarations jive in that situation (which it is not currently). 1396 if (!GetterMethod) { 1397 // No instance method of same name as property getter name was found. 1398 // Declare a getter method and add it to the list of methods 1399 // for this class. 1400 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), 1401 property->getLocation(), property->getGetterName(), 1402 property->getType(), CD, true, false, true, 1403 (property->getPropertyImplementation() == 1404 ObjCPropertyDecl::Optional) ? 1405 ObjCMethodDecl::Optional : 1406 ObjCMethodDecl::Required); 1407 CD->addDecl(GetterMethod); 1408 } else 1409 // A user declared getter will be synthesize when @synthesize of 1410 // the property with the same name is seen in the @implementation 1411 GetterMethod->setSynthesized(true); 1412 property->setGetterMethodDecl(GetterMethod); 1413 1414 // Skip setter if property is read-only. 1415 if (!property->isReadOnly()) { 1416 // Find the default setter and if one not found, add one. 1417 if (!SetterMethod) { 1418 // No instance method of same name as property setter name was found. 1419 // Declare a setter method and add it to the list of methods 1420 // for this class. 1421 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), 1422 property->getLocation(), 1423 property->getSetterName(), 1424 Context.VoidTy, CD, true, false, true, 1425 (property->getPropertyImplementation() == 1426 ObjCPropertyDecl::Optional) ? 1427 ObjCMethodDecl::Optional : 1428 ObjCMethodDecl::Required); 1429 // Invent the arguments for the setter. We don't bother making a 1430 // nice name for the argument. 1431 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, 1432 property->getLocation(), 1433 property->getIdentifier(), 1434 property->getType(), 1435 /*DInfo=*/0, 1436 VarDecl::None, 1437 0); 1438 SetterMethod->setMethodParams(Context, &Argument, 1); 1439 CD->addDecl(SetterMethod); 1440 } else 1441 // A user declared setter will be synthesize when @synthesize of 1442 // the property with the same name is seen in the @implementation 1443 SetterMethod->setSynthesized(true); 1444 property->setSetterMethodDecl(SetterMethod); 1445 } 1446 // Add any synthesized methods to the global pool. This allows us to 1447 // handle the following, which is supported by GCC (and part of the design). 1448 // 1449 // @interface Foo 1450 // @property double bar; 1451 // @end 1452 // 1453 // void thisIsUnfortunate() { 1454 // id foo; 1455 // double bar = [foo bar]; 1456 // } 1457 // 1458 if (GetterMethod) 1459 AddInstanceMethodToGlobalPool(GetterMethod); 1460 if (SetterMethod) 1461 AddInstanceMethodToGlobalPool(SetterMethod); 1462} 1463 1464/// CompareMethodParamsInBaseAndSuper - This routine compares methods with 1465/// identical selector names in current and its super classes and issues 1466/// a warning if any of their argument types are incompatible. 1467void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl, 1468 ObjCMethodDecl *Method, 1469 bool IsInstance) { 1470 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl); 1471 if (ID == 0) return; 1472 1473 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) { 1474 ObjCMethodDecl *SuperMethodDecl = 1475 SD->lookupMethod(Method->getSelector(), IsInstance); 1476 if (SuperMethodDecl == 0) { 1477 ID = SD; 1478 continue; 1479 } 1480 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), 1481 E = Method->param_end(); 1482 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin(); 1483 for (; ParamI != E; ++ParamI, ++PrevI) { 1484 // Number of parameters are the same and is guaranteed by selector match. 1485 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch"); 1486 QualType T1 = Context.getCanonicalType((*ParamI)->getType()); 1487 QualType T2 = Context.getCanonicalType((*PrevI)->getType()); 1488 // If type of arguement of method in this class does not match its 1489 // respective argument type in the super class method, issue warning; 1490 if (!Context.typesAreCompatible(T1, T2)) { 1491 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) 1492 << T1 << T2; 1493 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration); 1494 return; 1495 } 1496 } 1497 ID = SD; 1498 } 1499} 1500 1501// Note: For class/category implemenations, allMethods/allProperties is 1502// always null. 1503void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl, 1504 DeclPtrTy *allMethods, unsigned allNum, 1505 DeclPtrTy *allProperties, unsigned pNum, 1506 DeclGroupPtrTy *allTUVars, unsigned tuvNum) { 1507 Decl *ClassDecl = classDecl.getAs<Decl>(); 1508 1509 // FIXME: If we don't have a ClassDecl, we have an error. We should consider 1510 // always passing in a decl. If the decl has an error, isInvalidDecl() 1511 // should be true. 1512 if (!ClassDecl) 1513 return; 1514 1515 bool isInterfaceDeclKind = 1516 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) 1517 || isa<ObjCProtocolDecl>(ClassDecl); 1518 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); 1519 1520 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); 1521 1522 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. 1523 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; 1524 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; 1525 1526 for (unsigned i = 0; i < allNum; i++ ) { 1527 ObjCMethodDecl *Method = 1528 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>()); 1529 1530 if (!Method) continue; // Already issued a diagnostic. 1531 if (Method->isInstanceMethod()) { 1532 /// Check for instance method of the same name with incompatible types 1533 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; 1534 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 1535 : false; 1536 if ((isInterfaceDeclKind && PrevMethod && !match) 1537 || (checkIdenticalMethods && match)) { 1538 Diag(Method->getLocation(), diag::err_duplicate_method_decl) 1539 << Method->getDeclName(); 1540 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1541 } else { 1542 DC->addDecl(Method); 1543 InsMap[Method->getSelector()] = Method; 1544 /// The following allows us to typecheck messages to "id". 1545 AddInstanceMethodToGlobalPool(Method); 1546 // verify that the instance method conforms to the same definition of 1547 // parent methods if it shadows one. 1548 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true); 1549 } 1550 } else { 1551 /// Check for class method of the same name with incompatible types 1552 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; 1553 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 1554 : false; 1555 if ((isInterfaceDeclKind && PrevMethod && !match) 1556 || (checkIdenticalMethods && match)) { 1557 Diag(Method->getLocation(), diag::err_duplicate_method_decl) 1558 << Method->getDeclName(); 1559 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1560 } else { 1561 DC->addDecl(Method); 1562 ClsMap[Method->getSelector()] = Method; 1563 /// The following allows us to typecheck messages to "Class". 1564 AddFactoryMethodToGlobalPool(Method); 1565 // verify that the class method conforms to the same definition of 1566 // parent methods if it shadows one. 1567 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false); 1568 } 1569 } 1570 } 1571 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 1572 // Compares properties declared in this class to those of its 1573 // super class. 1574 ComparePropertiesInBaseAndSuper(I); 1575 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I)); 1576 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { 1577 // Categories are used to extend the class by declaring new methods. 1578 // By the same token, they are also used to add new properties. No 1579 // need to compare the added property to those in the class. 1580 1581 // Merge protocol properties into category 1582 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C)); 1583 if (C->getIdentifier() == 0) 1584 DiagnoseClassExtensionDupMethods(C, C->getClassInterface()); 1585 } 1586 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { 1587 // ProcessPropertyDecl is responsible for diagnosing conflicts with any 1588 // user-defined setter/getter. It also synthesizes setter/getter methods 1589 // and adds them to the DeclContext and global method pools. 1590 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), 1591 E = CDecl->prop_end(); 1592 I != E; ++I) 1593 ProcessPropertyDecl(*I, CDecl); 1594 CDecl->setAtEndLoc(AtEndLoc); 1595 } 1596 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 1597 IC->setAtEndLoc(AtEndLoc); 1598 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) 1599 ImplMethodsVsClassMethods(IC, IDecl); 1600 } else if (ObjCCategoryImplDecl* CatImplClass = 1601 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { 1602 CatImplClass->setAtEndLoc(AtEndLoc); 1603 1604 // Find category interface decl and then check that all methods declared 1605 // in this interface are implemented in the category @implementation. 1606 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { 1607 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); 1608 Categories; Categories = Categories->getNextClassCategory()) { 1609 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { 1610 ImplMethodsVsClassMethods(CatImplClass, Categories); 1611 break; 1612 } 1613 } 1614 } 1615 } 1616 if (isInterfaceDeclKind) { 1617 // Reject invalid vardecls. 1618 for (unsigned i = 0; i != tuvNum; i++) { 1619 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>(); 1620 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) 1621 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { 1622 if (!VDecl->hasExternalStorage()) 1623 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); 1624 } 1625 } 1626 } 1627} 1628 1629 1630/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for 1631/// objective-c's type qualifier from the parser version of the same info. 1632static Decl::ObjCDeclQualifier 1633CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { 1634 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; 1635 if (PQTVal & ObjCDeclSpec::DQ_In) 1636 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); 1637 if (PQTVal & ObjCDeclSpec::DQ_Inout) 1638 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); 1639 if (PQTVal & ObjCDeclSpec::DQ_Out) 1640 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); 1641 if (PQTVal & ObjCDeclSpec::DQ_Bycopy) 1642 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); 1643 if (PQTVal & ObjCDeclSpec::DQ_Byref) 1644 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); 1645 if (PQTVal & ObjCDeclSpec::DQ_Oneway) 1646 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); 1647 1648 return ret; 1649} 1650 1651Sema::DeclPtrTy Sema::ActOnMethodDeclaration( 1652 SourceLocation MethodLoc, SourceLocation EndLoc, 1653 tok::TokenKind MethodType, DeclPtrTy classDecl, 1654 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, 1655 Selector Sel, 1656 // optional arguments. The number of types/arguments is obtained 1657 // from the Sel.getNumArgs(). 1658 ObjCArgInfo *ArgInfo, 1659 llvm::SmallVectorImpl<Declarator> &Cdecls, 1660 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, 1661 bool isVariadic) { 1662 Decl *ClassDecl = classDecl.getAs<Decl>(); 1663 1664 // Make sure we can establish a context for the method. 1665 if (!ClassDecl) { 1666 Diag(MethodLoc, diag::error_missing_method_context); 1667 FunctionLabelMap.clear(); 1668 return DeclPtrTy(); 1669 } 1670 QualType resultDeclType; 1671 1672 if (ReturnType) { 1673 resultDeclType = GetTypeFromParser(ReturnType); 1674 1675 // Methods cannot return interface types. All ObjC objects are 1676 // passed by reference. 1677 if (resultDeclType->isObjCInterfaceType()) { 1678 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value) 1679 << 0 << resultDeclType; 1680 return DeclPtrTy(); 1681 } 1682 } else // get the type for "id". 1683 resultDeclType = Context.getObjCIdType(); 1684 1685 ObjCMethodDecl* ObjCMethod = 1686 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, 1687 cast<DeclContext>(ClassDecl), 1688 MethodType == tok::minus, isVariadic, 1689 false, 1690 MethodDeclKind == tok::objc_optional ? 1691 ObjCMethodDecl::Optional : 1692 ObjCMethodDecl::Required); 1693 1694 llvm::SmallVector<ParmVarDecl*, 16> Params; 1695 1696 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { 1697 QualType ArgType, UnpromotedArgType; 1698 1699 if (ArgInfo[i].Type == 0) { 1700 UnpromotedArgType = ArgType = Context.getObjCIdType(); 1701 } else { 1702 UnpromotedArgType = ArgType = GetTypeFromParser(ArgInfo[i].Type); 1703 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). 1704 ArgType = adjustParameterType(ArgType); 1705 } 1706 1707 ParmVarDecl* Param; 1708 if (ArgType == UnpromotedArgType) 1709 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc, 1710 ArgInfo[i].Name, ArgType, 1711 /*DInfo=*/0, //FIXME: Pass info here. 1712 VarDecl::None, 0); 1713 else 1714 Param = OriginalParmVarDecl::Create(Context, ObjCMethod, 1715 ArgInfo[i].NameLoc, 1716 ArgInfo[i].Name, ArgType, 1717 /*DInfo=*/0, //FIXME: Pass info here. 1718 UnpromotedArgType, 1719 VarDecl::None, 0); 1720 1721 if (ArgType->isObjCInterfaceType()) { 1722 Diag(ArgInfo[i].NameLoc, 1723 diag::err_object_cannot_be_passed_returned_by_value) 1724 << 1 << ArgType; 1725 Param->setInvalidDecl(); 1726 } 1727 1728 Param->setObjCDeclQualifier( 1729 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); 1730 1731 // Apply the attributes to the parameter. 1732 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); 1733 1734 Params.push_back(Param); 1735 } 1736 1737 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs()); 1738 ObjCMethod->setObjCDeclQualifier( 1739 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); 1740 const ObjCMethodDecl *PrevMethod = 0; 1741 1742 if (AttrList) 1743 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); 1744 1745 // For implementations (which can be very "coarse grain"), we add the 1746 // method now. This allows the AST to implement lookup methods that work 1747 // incrementally (without waiting until we parse the @end). It also allows 1748 // us to flag multiple declaration errors as they occur. 1749 if (ObjCImplementationDecl *ImpDecl = 1750 dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 1751 if (MethodType == tok::minus) { 1752 PrevMethod = ImpDecl->getInstanceMethod(Sel); 1753 ImpDecl->addInstanceMethod(ObjCMethod); 1754 } else { 1755 PrevMethod = ImpDecl->getClassMethod(Sel); 1756 ImpDecl->addClassMethod(ObjCMethod); 1757 } 1758 if (AttrList) 1759 Diag(EndLoc, diag::warn_attribute_method_def); 1760 } else if (ObjCCategoryImplDecl *CatImpDecl = 1761 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { 1762 if (MethodType == tok::minus) { 1763 PrevMethod = CatImpDecl->getInstanceMethod(Sel); 1764 CatImpDecl->addInstanceMethod(ObjCMethod); 1765 } else { 1766 PrevMethod = CatImpDecl->getClassMethod(Sel); 1767 CatImpDecl->addClassMethod(ObjCMethod); 1768 } 1769 if (AttrList) 1770 Diag(EndLoc, diag::warn_attribute_method_def); 1771 } 1772 if (PrevMethod) { 1773 // You can never have two method definitions with the same name. 1774 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) 1775 << ObjCMethod->getDeclName(); 1776 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1777 } 1778 return DeclPtrTy::make(ObjCMethod); 1779} 1780 1781void Sema::CheckObjCPropertyAttributes(QualType PropertyTy, 1782 SourceLocation Loc, 1783 unsigned &Attributes) { 1784 // FIXME: Improve the reported location. 1785 1786 // readonly and readwrite/assign/retain/copy conflict. 1787 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1788 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | 1789 ObjCDeclSpec::DQ_PR_assign | 1790 ObjCDeclSpec::DQ_PR_copy | 1791 ObjCDeclSpec::DQ_PR_retain))) { 1792 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? 1793 "readwrite" : 1794 (Attributes & ObjCDeclSpec::DQ_PR_assign) ? 1795 "assign" : 1796 (Attributes & ObjCDeclSpec::DQ_PR_copy) ? 1797 "copy" : "retain"; 1798 1799 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? 1800 diag::err_objc_property_attr_mutually_exclusive : 1801 diag::warn_objc_property_attr_mutually_exclusive) 1802 << "readonly" << which; 1803 } 1804 1805 // Check for copy or retain on non-object types. 1806 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) && 1807 !PropertyTy->isObjCObjectPointerType() && 1808 !PropertyTy->isBlockPointerType() && 1809 !Context.isObjCNSObjectType(PropertyTy)) { 1810 Diag(Loc, diag::err_objc_property_requires_object) 1811 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain"); 1812 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain); 1813 } 1814 1815 // Check for more than one of { assign, copy, retain }. 1816 if (Attributes & ObjCDeclSpec::DQ_PR_assign) { 1817 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1818 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1819 << "assign" << "copy"; 1820 Attributes &= ~ObjCDeclSpec::DQ_PR_copy; 1821 } 1822 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1823 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1824 << "assign" << "retain"; 1825 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1826 } 1827 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1828 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1829 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1830 << "copy" << "retain"; 1831 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1832 } 1833 } 1834 1835 // Warn if user supplied no assignment attribute, property is 1836 // readwrite, and this is an object type. 1837 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | 1838 ObjCDeclSpec::DQ_PR_retain)) && 1839 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1840 PropertyTy->isObjCObjectPointerType()) { 1841 // Skip this warning in gc-only mode. 1842 if (getLangOptions().getGCMode() != LangOptions::GCOnly) 1843 Diag(Loc, diag::warn_objc_property_no_assignment_attribute); 1844 1845 // If non-gc code warn that this is likely inappropriate. 1846 if (getLangOptions().getGCMode() == LangOptions::NonGC) 1847 Diag(Loc, diag::warn_objc_property_default_assign_on_object); 1848 1849 // FIXME: Implement warning dependent on NSCopying being 1850 // implemented. See also: 1851 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> 1852 // (please trim this list while you are at it). 1853 } 1854 1855 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) 1856 && getLangOptions().getGCMode() == LangOptions::GCOnly 1857 && PropertyTy->isBlockPointerType()) 1858 Diag(Loc, diag::warn_objc_property_copy_missing_on_block); 1859} 1860 1861Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, 1862 FieldDeclarator &FD, 1863 ObjCDeclSpec &ODS, 1864 Selector GetterSel, 1865 Selector SetterSel, 1866 DeclPtrTy ClassCategory, 1867 bool *isOverridingProperty, 1868 tok::ObjCKeywordKind MethodImplKind) { 1869 unsigned Attributes = ODS.getPropertyAttributes(); 1870 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || 1871 // default is readwrite! 1872 !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); 1873 // property is defaulted to 'assign' if it is readwrite and is 1874 // not retain or copy 1875 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || 1876 (isReadWrite && 1877 !(Attributes & ObjCDeclSpec::DQ_PR_retain) && 1878 !(Attributes & ObjCDeclSpec::DQ_PR_copy))); 1879 QualType T = GetTypeForDeclarator(FD.D, S); 1880 Decl *ClassDecl = ClassCategory.getAs<Decl>(); 1881 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class 1882 // May modify Attributes. 1883 CheckObjCPropertyAttributes(T, AtLoc, Attributes); 1884 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) 1885 if (!CDecl->getIdentifier()) { 1886 // This is a continuation class. property requires special 1887 // handling. 1888 if ((CCPrimary = CDecl->getClassInterface())) { 1889 // Find the property in continuation class's primary class only. 1890 ObjCPropertyDecl *PIDecl = 0; 1891 IdentifierInfo *PropertyId = FD.D.getIdentifier(); 1892 for (ObjCInterfaceDecl::prop_iterator 1893 I = CCPrimary->prop_begin(), E = CCPrimary->prop_end(); 1894 I != E; ++I) 1895 if ((*I)->getIdentifier() == PropertyId) { 1896 PIDecl = *I; 1897 break; 1898 } 1899 1900 if (PIDecl) { 1901 // property 'PIDecl's readonly attribute will be over-ridden 1902 // with continuation class's readwrite property attribute! 1903 unsigned PIkind = PIDecl->getPropertyAttributes(); 1904 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { 1905 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) != 1906 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic)) 1907 Diag(AtLoc, diag::warn_property_attr_mismatch); 1908 PIDecl->makeitReadWriteAttribute(); 1909 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 1910 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 1911 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 1912 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 1913 PIDecl->setSetterName(SetterSel); 1914 } else 1915 Diag(AtLoc, diag::err_use_continuation_class) 1916 << CCPrimary->getDeclName(); 1917 *isOverridingProperty = true; 1918 // Make sure setter decl is synthesized, and added to primary 1919 // class's list. 1920 ProcessPropertyDecl(PIDecl, CCPrimary); 1921 return DeclPtrTy(); 1922 } 1923 // No matching property found in the primary class. Just fall thru 1924 // and add property to continuation class's primary class. 1925 ClassDecl = CCPrimary; 1926 } else { 1927 Diag(CDecl->getLocation(), diag::err_continuation_class); 1928 *isOverridingProperty = true; 1929 return DeclPtrTy(); 1930 } 1931 } 1932 1933 // Issue a warning if property is 'assign' as default and its object, which is 1934 // gc'able conforms to NSCopying protocol 1935 if (getLangOptions().getGCMode() != LangOptions::NonGC && 1936 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) 1937 if (T->isObjCObjectPointerType()) { 1938 QualType InterfaceTy = T->getPointeeType(); 1939 if (const ObjCInterfaceType *OIT = 1940 InterfaceTy->getAsObjCInterfaceType()) { 1941 ObjCInterfaceDecl *IDecl = OIT->getDecl(); 1942 if (IDecl) 1943 if (ObjCProtocolDecl* PNSCopying = 1944 LookupProtocol(&Context.Idents.get("NSCopying"))) 1945 if (IDecl->ClassImplementsProtocol(PNSCopying, true)) 1946 Diag(AtLoc, diag::warn_implements_nscopying) 1947 << FD.D.getIdentifier(); 1948 } 1949 } 1950 if (T->isObjCInterfaceType()) 1951 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object); 1952 1953 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); 1954 assert(DC && "ClassDecl is not a DeclContext"); 1955 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, 1956 FD.D.getIdentifierLoc(), 1957 FD.D.getIdentifier(), T); 1958 DC->addDecl(PDecl); 1959 1960 if (T->isArrayType() || T->isFunctionType()) { 1961 Diag(AtLoc, diag::err_property_type) << T; 1962 PDecl->setInvalidDecl(); 1963 } 1964 1965 ProcessDeclAttributes(S, PDecl, FD.D); 1966 1967 // Regardless of setter/getter attribute, we save the default getter/setter 1968 // selector names in anticipation of declaration of setter/getter methods. 1969 PDecl->setGetterName(GetterSel); 1970 PDecl->setSetterName(SetterSel); 1971 1972 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) 1973 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); 1974 1975 if (Attributes & ObjCDeclSpec::DQ_PR_getter) 1976 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); 1977 1978 if (Attributes & ObjCDeclSpec::DQ_PR_setter) 1979 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); 1980 1981 if (isReadWrite) 1982 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); 1983 1984 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 1985 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 1986 1987 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 1988 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 1989 1990 if (isAssign) 1991 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); 1992 1993 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) 1994 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); 1995 1996 if (MethodImplKind == tok::objc_required) 1997 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); 1998 else if (MethodImplKind == tok::objc_optional) 1999 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); 2000 // A case of continuation class adding a new property in the class. This 2001 // is not what it was meant for. However, gcc supports it and so should we. 2002 // Make sure setter/getters are declared here. 2003 if (CCPrimary) 2004 ProcessPropertyDecl(PDecl, CCPrimary); 2005 2006 return DeclPtrTy::make(PDecl); 2007} 2008 2009/// ActOnPropertyImplDecl - This routine performs semantic checks and 2010/// builds the AST node for a property implementation declaration; declared 2011/// as @synthesize or @dynamic. 2012/// 2013Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc, 2014 SourceLocation PropertyLoc, 2015 bool Synthesize, 2016 DeclPtrTy ClassCatImpDecl, 2017 IdentifierInfo *PropertyId, 2018 IdentifierInfo *PropertyIvar) { 2019 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>(); 2020 // Make sure we have a context for the property implementation declaration. 2021 if (!ClassImpDecl) { 2022 Diag(AtLoc, diag::error_missing_property_context); 2023 return DeclPtrTy(); 2024 } 2025 ObjCPropertyDecl *property = 0; 2026 ObjCInterfaceDecl* IDecl = 0; 2027 // Find the class or category class where this property must have 2028 // a declaration. 2029 ObjCImplementationDecl *IC = 0; 2030 ObjCCategoryImplDecl* CatImplClass = 0; 2031 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { 2032 IDecl = IC->getClassInterface(); 2033 // We always synthesize an interface for an implementation 2034 // without an interface decl. So, IDecl is always non-zero. 2035 assert(IDecl && 2036 "ActOnPropertyImplDecl - @implementation without @interface"); 2037 2038 // Look for this property declaration in the @implementation's @interface 2039 property = IDecl->FindPropertyDeclaration(PropertyId); 2040 if (!property) { 2041 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); 2042 return DeclPtrTy(); 2043 } 2044 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { 2045 if (Synthesize) { 2046 Diag(AtLoc, diag::error_synthesize_category_decl); 2047 return DeclPtrTy(); 2048 } 2049 IDecl = CatImplClass->getClassInterface(); 2050 if (!IDecl) { 2051 Diag(AtLoc, diag::error_missing_property_interface); 2052 return DeclPtrTy(); 2053 } 2054 ObjCCategoryDecl *Category = 2055 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); 2056 2057 // If category for this implementation not found, it is an error which 2058 // has already been reported eralier. 2059 if (!Category) 2060 return DeclPtrTy(); 2061 // Look for this property declaration in @implementation's category 2062 property = Category->FindPropertyDeclaration(PropertyId); 2063 if (!property) { 2064 Diag(PropertyLoc, diag::error_bad_category_property_decl) 2065 << Category->getDeclName(); 2066 return DeclPtrTy(); 2067 } 2068 } else { 2069 Diag(AtLoc, diag::error_bad_property_context); 2070 return DeclPtrTy(); 2071 } 2072 ObjCIvarDecl *Ivar = 0; 2073 // Check that we have a valid, previously declared ivar for @synthesize 2074 if (Synthesize) { 2075 // @synthesize 2076 if (!PropertyIvar) 2077 PropertyIvar = PropertyId; 2078 QualType PropType = Context.getCanonicalType(property->getType()); 2079 // Check that this is a previously declared 'ivar' in 'IDecl' interface 2080 ObjCInterfaceDecl *ClassDeclared; 2081 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); 2082 if (!Ivar) { 2083 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl); 2084 assert(EnclosingContext && 2085 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl"); 2086 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc, 2087 PropertyIvar, PropType, /*Dinfo=*/0, 2088 ObjCIvarDecl::Public, 2089 (Expr *)0); 2090 Ivar->setLexicalDeclContext(IDecl); 2091 IDecl->addDecl(Ivar); 2092 property->setPropertyIvarDecl(Ivar); 2093 if (!getLangOptions().ObjCNonFragileABI) 2094 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId; 2095 // Note! I deliberately want it to fall thru so, we have a 2096 // a property implementation and to avoid future warnings. 2097 } else if (getLangOptions().ObjCNonFragileABI && 2098 ClassDeclared != IDecl) { 2099 Diag(PropertyLoc, diag::error_ivar_in_superclass_use) 2100 << property->getDeclName() << Ivar->getDeclName() 2101 << ClassDeclared->getDeclName(); 2102 Diag(Ivar->getLocation(), diag::note_previous_access_declaration) 2103 << Ivar << Ivar->getNameAsCString(); 2104 // Note! I deliberately want it to fall thru so more errors are caught. 2105 } 2106 QualType IvarType = Context.getCanonicalType(Ivar->getType()); 2107 2108 // Check that type of property and its ivar are type compatible. 2109 if (PropType != IvarType) { 2110 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) { 2111 Diag(PropertyLoc, diag::error_property_ivar_type) 2112 << property->getDeclName() << Ivar->getDeclName(); 2113 // Note! I deliberately want it to fall thru so, we have a 2114 // a property implementation and to avoid future warnings. 2115 } 2116 2117 // FIXME! Rules for properties are somewhat different that those 2118 // for assignments. Use a new routine to consolidate all cases; 2119 // specifically for property redeclarations as well as for ivars. 2120 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType(); 2121 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); 2122 if (lhsType != rhsType && 2123 lhsType->isArithmeticType()) { 2124 Diag(PropertyLoc, diag::error_property_ivar_type) 2125 << property->getDeclName() << Ivar->getDeclName(); 2126 // Fall thru - see previous comment 2127 } 2128 // __weak is explicit. So it works on Canonical type. 2129 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && 2130 getLangOptions().getGCMode() != LangOptions::NonGC) { 2131 Diag(PropertyLoc, diag::error_weak_property) 2132 << property->getDeclName() << Ivar->getDeclName(); 2133 // Fall thru - see previous comment 2134 } 2135 if ((property->getType()->isObjCObjectPointerType() || 2136 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && 2137 getLangOptions().getGCMode() != LangOptions::NonGC) { 2138 Diag(PropertyLoc, diag::error_strong_property) 2139 << property->getDeclName() << Ivar->getDeclName(); 2140 // Fall thru - see previous comment 2141 } 2142 } 2143 } else if (PropertyIvar) 2144 // @dynamic 2145 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); 2146 assert (property && "ActOnPropertyImplDecl - property declaration missing"); 2147 ObjCPropertyImplDecl *PIDecl = 2148 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, 2149 property, 2150 (Synthesize ? 2151 ObjCPropertyImplDecl::Synthesize 2152 : ObjCPropertyImplDecl::Dynamic), 2153 Ivar); 2154 if (IC) { 2155 if (Synthesize) 2156 if (ObjCPropertyImplDecl *PPIDecl = 2157 IC->FindPropertyImplIvarDecl(PropertyIvar)) { 2158 Diag(PropertyLoc, diag::error_duplicate_ivar_use) 2159 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 2160 << PropertyIvar; 2161 Diag(PPIDecl->getLocation(), diag::note_previous_use); 2162 } 2163 2164 if (ObjCPropertyImplDecl *PPIDecl 2165 = IC->FindPropertyImplDecl(PropertyId)) { 2166 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; 2167 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 2168 return DeclPtrTy(); 2169 } 2170 IC->addPropertyImplementation(PIDecl); 2171 } else { 2172 if (Synthesize) 2173 if (ObjCPropertyImplDecl *PPIDecl = 2174 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { 2175 Diag(PropertyLoc, diag::error_duplicate_ivar_use) 2176 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 2177 << PropertyIvar; 2178 Diag(PPIDecl->getLocation(), diag::note_previous_use); 2179 } 2180 2181 if (ObjCPropertyImplDecl *PPIDecl = 2182 CatImplClass->FindPropertyImplDecl(PropertyId)) { 2183 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; 2184 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 2185 return DeclPtrTy(); 2186 } 2187 CatImplClass->addPropertyImplementation(PIDecl); 2188 } 2189 2190 return DeclPtrTy::make(PIDecl); 2191} 2192 2193bool Sema::CheckObjCDeclScope(Decl *D) { 2194 if (isa<TranslationUnitDecl>(CurContext->getLookupContext())) 2195 return false; 2196 2197 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); 2198 D->setInvalidDecl(); 2199 2200 return true; 2201} 2202 2203/// Called whenever @defs(ClassName) is encountered in the source. Inserts the 2204/// instance variables of ClassName into Decls. 2205void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart, 2206 IdentifierInfo *ClassName, 2207 llvm::SmallVectorImpl<DeclPtrTy> &Decls) { 2208 // Check that ClassName is a valid class 2209 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName); 2210 if (!Class) { 2211 Diag(DeclStart, diag::err_undef_interface) << ClassName; 2212 return; 2213 } 2214 if (LangOpts.ObjCNonFragileABI) { 2215 Diag(DeclStart, diag::err_atdef_nonfragile_interface); 2216 return; 2217 } 2218 2219 // Collect the instance variables 2220 llvm::SmallVector<FieldDecl*, 32> RecFields; 2221 Context.CollectObjCIvars(Class, RecFields); 2222 // For each ivar, create a fresh ObjCAtDefsFieldDecl. 2223 for (unsigned i = 0; i < RecFields.size(); i++) { 2224 FieldDecl* ID = RecFields[i]; 2225 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()); 2226 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(), 2227 ID->getIdentifier(), ID->getType(), 2228 ID->getBitWidth()); 2229 Decls.push_back(Sema::DeclPtrTy::make(FD)); 2230 } 2231 2232 // Introduce all of these fields into the appropriate scope. 2233 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin(); 2234 D != Decls.end(); ++D) { 2235 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>()); 2236 if (getLangOptions().CPlusPlus) 2237 PushOnScopeChains(cast<FieldDecl>(FD), S); 2238 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>())) 2239 Record->addDecl(FD); 2240 } 2241} 2242 2243