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