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