SemaDeclObjC.cpp revision 761b72ff014980a0c52c752cfac6cc83e60964dd
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; i < Sel.getNumArgs(); i++) { 1435 // FIXME: arg->AttrList must be stored too! 1436 QualType ArgType, UnpromotedArgType; 1437 1438 if (ArgInfo[i].Type == 0) { 1439 UnpromotedArgType = ArgType = Context.getObjCIdType(); 1440 } else { 1441 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type); 1442 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). 1443 ArgType = adjustParameterType(ArgType); 1444 } 1445 1446 ParmVarDecl* Param; 1447 if (ArgType == UnpromotedArgType) 1448 Param = ParmVarDecl::Create(Context, ObjCMethod, 1449 SourceLocation(/*FIXME*/), 1450 ArgInfo[i].Name, ArgType, 1451 VarDecl::None, 0); 1452 else 1453 Param = OriginalParmVarDecl::Create(Context, ObjCMethod, 1454 SourceLocation(/*FIXME*/), 1455 ArgInfo[i].Name, ArgType, 1456 UnpromotedArgType, 1457 VarDecl::None, 0); 1458 1459 if (ArgType->isObjCInterfaceType()) { 1460 Diag(ArgInfo[i].NameLoc, 1461 diag::err_object_cannot_be_passed_returned_by_value) 1462 << 1 << ArgType; 1463 Param->setInvalidDecl(); 1464 } 1465 1466 Param->setObjCDeclQualifier( 1467 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); 1468 1469 // Apply the attributes to the parameter. 1470 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs); 1471 1472 Params.push_back(Param); 1473 } 1474 1475 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context); 1476 ObjCMethod->setObjCDeclQualifier( 1477 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); 1478 const ObjCMethodDecl *PrevMethod = 0; 1479 1480 if (AttrList) 1481 ProcessDeclAttributeList(ObjCMethod, AttrList); 1482 1483 // For implementations (which can be very "coarse grain"), we add the 1484 // method now. This allows the AST to implement lookup methods that work 1485 // incrementally (without waiting until we parse the @end). It also allows 1486 // us to flag multiple declaration errors as they occur. 1487 if (ObjCImplementationDecl *ImpDecl = 1488 dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 1489 if (MethodType == tok::minus) { 1490 PrevMethod = ImpDecl->getInstanceMethod(Sel); 1491 ImpDecl->addInstanceMethod(ObjCMethod); 1492 } else { 1493 PrevMethod = ImpDecl->getClassMethod(Sel); 1494 ImpDecl->addClassMethod(ObjCMethod); 1495 } 1496 } 1497 else if (ObjCCategoryImplDecl *CatImpDecl = 1498 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { 1499 if (MethodType == tok::minus) { 1500 PrevMethod = CatImpDecl->getInstanceMethod(Sel); 1501 CatImpDecl->addInstanceMethod(ObjCMethod); 1502 } else { 1503 PrevMethod = CatImpDecl->getClassMethod(Sel); 1504 CatImpDecl->addClassMethod(ObjCMethod); 1505 } 1506 } 1507 if (PrevMethod) { 1508 // You can never have two method definitions with the same name. 1509 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) 1510 << ObjCMethod->getDeclName(); 1511 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1512 } 1513 return DeclPtrTy::make(ObjCMethod); 1514} 1515 1516void Sema::CheckObjCPropertyAttributes(QualType PropertyTy, 1517 SourceLocation Loc, 1518 unsigned &Attributes) { 1519 // FIXME: Improve the reported location. 1520 1521 // readonly and readwrite/assign/retain/copy conflict. 1522 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1523 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | 1524 ObjCDeclSpec::DQ_PR_assign | 1525 ObjCDeclSpec::DQ_PR_copy | 1526 ObjCDeclSpec::DQ_PR_retain))) { 1527 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? 1528 "readwrite" : 1529 (Attributes & ObjCDeclSpec::DQ_PR_assign) ? 1530 "assign" : 1531 (Attributes & ObjCDeclSpec::DQ_PR_copy) ? 1532 "copy" : "retain"; 1533 1534 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? 1535 diag::err_objc_property_attr_mutually_exclusive : 1536 diag::warn_objc_property_attr_mutually_exclusive) 1537 << "readonly" << which; 1538 } 1539 1540 // Check for copy or retain on non-object types. 1541 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) && 1542 !Context.isObjCObjectPointerType(PropertyTy)) { 1543 Diag(Loc, diag::err_objc_property_requires_object) 1544 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain"); 1545 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain); 1546 } 1547 1548 // Check for more than one of { assign, copy, retain }. 1549 if (Attributes & ObjCDeclSpec::DQ_PR_assign) { 1550 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1551 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1552 << "assign" << "copy"; 1553 Attributes &= ~ObjCDeclSpec::DQ_PR_copy; 1554 } 1555 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1556 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1557 << "assign" << "retain"; 1558 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1559 } 1560 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1561 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1562 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1563 << "copy" << "retain"; 1564 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1565 } 1566 } 1567 1568 // Warn if user supplied no assignment attribute, property is 1569 // readwrite, and this is an object type. 1570 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | 1571 ObjCDeclSpec::DQ_PR_retain)) && 1572 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1573 Context.isObjCObjectPointerType(PropertyTy)) { 1574 // Skip this warning in gc-only mode. 1575 if (getLangOptions().getGCMode() != LangOptions::GCOnly) 1576 Diag(Loc, diag::warn_objc_property_no_assignment_attribute); 1577 1578 // If non-gc code warn that this is likely inappropriate. 1579 if (getLangOptions().getGCMode() == LangOptions::NonGC) 1580 Diag(Loc, diag::warn_objc_property_default_assign_on_object); 1581 1582 // FIXME: Implement warning dependent on NSCopying being 1583 // implemented. See also: 1584 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> 1585 // (please trim this list while you are at it). 1586 } 1587} 1588 1589Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, 1590 FieldDeclarator &FD, 1591 ObjCDeclSpec &ODS, 1592 Selector GetterSel, 1593 Selector SetterSel, 1594 DeclPtrTy ClassCategory, 1595 bool *isOverridingProperty, 1596 tok::ObjCKeywordKind MethodImplKind) { 1597 unsigned Attributes = ODS.getPropertyAttributes(); 1598 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || 1599 // default is readwrite! 1600 !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); 1601 // property is defaulted to 'assign' if it is readwrite and is 1602 // not retain or copy 1603 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || 1604 (isReadWrite && 1605 !(Attributes & ObjCDeclSpec::DQ_PR_retain) && 1606 !(Attributes & ObjCDeclSpec::DQ_PR_copy))); 1607 QualType T = GetTypeForDeclarator(FD.D, S); 1608 Decl *ClassDecl = ClassCategory.getAs<Decl>(); 1609 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class 1610 // May modify Attributes. 1611 CheckObjCPropertyAttributes(T, AtLoc, Attributes); 1612 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) 1613 if (!CDecl->getIdentifier()) { 1614 // This is a continuation class. property requires special 1615 // handling. 1616 if ((CCPrimary = CDecl->getClassInterface())) { 1617 // Find the property in continuation class's primary class only. 1618 ObjCPropertyDecl *PIDecl = 0; 1619 IdentifierInfo *PropertyId = FD.D.getIdentifier(); 1620 for (ObjCInterfaceDecl::prop_iterator 1621 I = CCPrimary->prop_begin(Context), 1622 E = CCPrimary->prop_end(Context); 1623 I != E; ++I) 1624 if ((*I)->getIdentifier() == PropertyId) { 1625 PIDecl = *I; 1626 break; 1627 } 1628 1629 if (PIDecl) { 1630 // property 'PIDecl's readonly attribute will be over-ridden 1631 // with continuation class's readwrite property attribute! 1632 unsigned PIkind = PIDecl->getPropertyAttributes(); 1633 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { 1634 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) != 1635 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic)) 1636 Diag(AtLoc, diag::warn_property_attr_mismatch); 1637 PIDecl->makeitReadWriteAttribute(); 1638 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 1639 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 1640 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 1641 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 1642 PIDecl->setSetterName(SetterSel); 1643 // FIXME: use a common routine with addPropertyMethods. 1644 ObjCMethodDecl *SetterDecl = 1645 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel, 1646 Context.VoidTy, 1647 CCPrimary, 1648 true, false, true, 1649 ObjCMethodDecl::Required); 1650 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl, 1651 SourceLocation(), 1652 PropertyId, 1653 T, VarDecl::None, 0); 1654 SetterDecl->setMethodParams(&Argument, 1, Context); 1655 PIDecl->setSetterMethodDecl(SetterDecl); 1656 } 1657 else 1658 Diag(AtLoc, diag::err_use_continuation_class) 1659 << CCPrimary->getDeclName(); 1660 *isOverridingProperty = true; 1661 return DeclPtrTy(); 1662 } 1663 // No matching property found in the primary class. Just fall thru 1664 // and add property to continuation class's primary class. 1665 ClassDecl = CCPrimary; 1666 } else { 1667 Diag(CDecl->getLocation(), diag::err_continuation_class); 1668 *isOverridingProperty = true; 1669 return DeclPtrTy(); 1670 } 1671 } 1672 1673 Type *t = T.getTypePtr(); 1674 if (t->isArrayType() || t->isFunctionType()) 1675 Diag(AtLoc, diag::err_property_type) << T; 1676 1677 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); 1678 assert(DC && "ClassDecl is not a DeclContext"); 1679 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc, 1680 FD.D.getIdentifier(), T); 1681 DC->addDecl(Context, PDecl); 1682 1683 ProcessDeclAttributes(PDecl, FD.D); 1684 1685 // Regardless of setter/getter attribute, we save the default getter/setter 1686 // selector names in anticipation of declaration of setter/getter methods. 1687 PDecl->setGetterName(GetterSel); 1688 PDecl->setSetterName(SetterSel); 1689 1690 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) 1691 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); 1692 1693 if (Attributes & ObjCDeclSpec::DQ_PR_getter) 1694 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); 1695 1696 if (Attributes & ObjCDeclSpec::DQ_PR_setter) 1697 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); 1698 1699 if (isReadWrite) 1700 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); 1701 1702 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 1703 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 1704 1705 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 1706 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 1707 1708 if (isAssign) 1709 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); 1710 1711 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) 1712 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); 1713 1714 if (MethodImplKind == tok::objc_required) 1715 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); 1716 else if (MethodImplKind == tok::objc_optional) 1717 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); 1718 // A case of continuation class adding a new property in the class. This 1719 // is not what it was meant for. However, gcc supports it and so should we. 1720 // Make sure setter/getters are declared here. 1721 if (CCPrimary) 1722 ProcessPropertyDecl(PDecl, CCPrimary); 1723 1724 return DeclPtrTy::make(PDecl); 1725} 1726 1727/// ActOnPropertyImplDecl - This routine performs semantic checks and 1728/// builds the AST node for a property implementation declaration; declared 1729/// as @synthesize or @dynamic. 1730/// 1731Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc, 1732 SourceLocation PropertyLoc, 1733 bool Synthesize, 1734 DeclPtrTy ClassCatImpDecl, 1735 IdentifierInfo *PropertyId, 1736 IdentifierInfo *PropertyIvar) { 1737 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>(); 1738 // Make sure we have a context for the property implementation declaration. 1739 if (!ClassImpDecl) { 1740 Diag(AtLoc, diag::error_missing_property_context); 1741 return DeclPtrTy(); 1742 } 1743 ObjCPropertyDecl *property = 0; 1744 ObjCInterfaceDecl* IDecl = 0; 1745 // Find the class or category class where this property must have 1746 // a declaration. 1747 ObjCImplementationDecl *IC = 0; 1748 ObjCCategoryImplDecl* CatImplClass = 0; 1749 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { 1750 IDecl = IC->getClassInterface(); 1751 // We always synthesize an interface for an implementation 1752 // without an interface decl. So, IDecl is always non-zero. 1753 assert(IDecl && 1754 "ActOnPropertyImplDecl - @implementation without @interface"); 1755 1756 // Look for this property declaration in the @implementation's @interface 1757 property = IDecl->FindPropertyDeclaration(Context, PropertyId); 1758 if (!property) { 1759 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); 1760 return DeclPtrTy(); 1761 } 1762 } 1763 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { 1764 if (Synthesize) { 1765 Diag(AtLoc, diag::error_synthesize_category_decl); 1766 return DeclPtrTy(); 1767 } 1768 IDecl = CatImplClass->getClassInterface(); 1769 if (!IDecl) { 1770 Diag(AtLoc, diag::error_missing_property_interface); 1771 return DeclPtrTy(); 1772 } 1773 ObjCCategoryDecl *Category = 1774 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); 1775 1776 // If category for this implementation not found, it is an error which 1777 // has already been reported eralier. 1778 if (!Category) 1779 return DeclPtrTy(); 1780 // Look for this property declaration in @implementation's category 1781 property = Category->FindPropertyDeclaration(Context, PropertyId); 1782 if (!property) { 1783 Diag(PropertyLoc, diag::error_bad_category_property_decl) 1784 << Category->getDeclName(); 1785 return DeclPtrTy(); 1786 } 1787 } else { 1788 Diag(AtLoc, diag::error_bad_property_context); 1789 return DeclPtrTy(); 1790 } 1791 ObjCIvarDecl *Ivar = 0; 1792 // Check that we have a valid, previously declared ivar for @synthesize 1793 if (Synthesize) { 1794 // @synthesize 1795 if (!PropertyIvar) 1796 PropertyIvar = PropertyId; 1797 QualType PropType = Context.getCanonicalType(property->getType()); 1798 // Check that this is a previously declared 'ivar' in 'IDecl' interface 1799 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar); 1800 if (!Ivar) { 1801 if (getLangOptions().ObjCNonFragileABI) { 1802 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc, 1803 PropertyIvar, PropType, 1804 ObjCIvarDecl::Public, 1805 (Expr *)0); 1806 property->setPropertyIvarDecl(Ivar); 1807 } 1808 else { 1809 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId; 1810 return DeclPtrTy(); 1811 } 1812 } 1813 QualType IvarType = Context.getCanonicalType(Ivar->getType()); 1814 1815 // Check that type of property and its ivar are type compatible. 1816 if (PropType != IvarType) { 1817 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) { 1818 Diag(PropertyLoc, diag::error_property_ivar_type) 1819 << property->getDeclName() << Ivar->getDeclName(); 1820 return DeclPtrTy(); 1821 } 1822 1823 // FIXME! Rules for properties are somewhat different that those 1824 // for assignments. Use a new routine to consolidate all cases; 1825 // specifically for property redeclarations as well as for ivars. 1826 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType(); 1827 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); 1828 if (lhsType != rhsType && 1829 lhsType->isArithmeticType()) { 1830 Diag(PropertyLoc, diag::error_property_ivar_type) 1831 << property->getDeclName() << Ivar->getDeclName(); 1832 return DeclPtrTy(); 1833 } 1834 // __weak is explicit. So it works on Canonical type. 1835 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && 1836 getLangOptions().getGCMode() != LangOptions::NonGC) { 1837 Diag(PropertyLoc, diag::error_weak_property) 1838 << property->getDeclName() << Ivar->getDeclName(); 1839 return DeclPtrTy(); 1840 } 1841 if ((Context.isObjCObjectPointerType(property->getType()) || 1842 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && 1843 getLangOptions().getGCMode() != LangOptions::NonGC) { 1844 Diag(PropertyLoc, diag::error_strong_property) 1845 << property->getDeclName() << Ivar->getDeclName(); 1846 return DeclPtrTy(); 1847 } 1848 } 1849 } else if (PropertyIvar) { 1850 // @dynamic 1851 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); 1852 return DeclPtrTy(); 1853 } 1854 assert (property && "ActOnPropertyImplDecl - property declaration missing"); 1855 ObjCPropertyImplDecl *PIDecl = 1856 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, 1857 property, 1858 (Synthesize ? 1859 ObjCPropertyImplDecl::Synthesize 1860 : ObjCPropertyImplDecl::Dynamic), 1861 Ivar); 1862 CurContext->addDecl(Context, PIDecl); 1863 if (IC) { 1864 if (Synthesize) 1865 if (ObjCPropertyImplDecl *PPIDecl = 1866 IC->FindPropertyImplIvarDecl(PropertyIvar)) { 1867 Diag(PropertyLoc, diag::error_duplicate_ivar_use) 1868 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 1869 << PropertyIvar; 1870 Diag(PPIDecl->getLocation(), diag::note_previous_use); 1871 } 1872 1873 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) { 1874 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; 1875 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 1876 return DeclPtrTy(); 1877 } 1878 IC->addPropertyImplementation(PIDecl); 1879 } 1880 else { 1881 if (Synthesize) 1882 if (ObjCPropertyImplDecl *PPIDecl = 1883 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { 1884 Diag(PropertyLoc, diag::error_duplicate_ivar_use) 1885 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 1886 << PropertyIvar; 1887 Diag(PPIDecl->getLocation(), diag::note_previous_use); 1888 } 1889 1890 if (ObjCPropertyImplDecl *PPIDecl = 1891 CatImplClass->FindPropertyImplDecl(PropertyId)) { 1892 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; 1893 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 1894 return DeclPtrTy(); 1895 } 1896 CatImplClass->addPropertyImplementation(PIDecl); 1897 } 1898 1899 return DeclPtrTy::make(PIDecl); 1900} 1901 1902bool Sema::CheckObjCDeclScope(Decl *D) { 1903 if (isa<TranslationUnitDecl>(CurContext->getLookupContext())) 1904 return false; 1905 1906 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); 1907 D->setInvalidDecl(); 1908 1909 return true; 1910} 1911 1912/// Collect the instance variables declared in an Objective-C object. Used in 1913/// the creation of structures from objects using the @defs directive. 1914/// FIXME: This should be consolidated with CollectObjCIvars as it is also 1915/// part of the AST generation logic of @defs. 1916static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record, 1917 ASTContext& Ctx, 1918 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) { 1919 if (Class->getSuperClass()) 1920 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars); 1921 1922 // For each ivar, create a fresh ObjCAtDefsFieldDecl. 1923 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(), 1924 E = Class->ivar_end(); I != E; ++I) { 1925 ObjCIvarDecl* ID = *I; 1926 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(), 1927 ID->getIdentifier(), ID->getType(), 1928 ID->getBitWidth()); 1929 ivars.push_back(Sema::DeclPtrTy::make(FD)); 1930 } 1931} 1932 1933/// Called whenever @defs(ClassName) is encountered in the source. Inserts the 1934/// instance variables of ClassName into Decls. 1935void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart, 1936 IdentifierInfo *ClassName, 1937 llvm::SmallVectorImpl<DeclPtrTy> &Decls) { 1938 // Check that ClassName is a valid class 1939 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName); 1940 if (!Class) { 1941 Diag(DeclStart, diag::err_undef_interface) << ClassName; 1942 return; 1943 } 1944 // Collect the instance variables 1945 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls); 1946 1947 // Introduce all of these fields into the appropriate scope. 1948 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin(); 1949 D != Decls.end(); ++D) { 1950 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>()); 1951 if (getLangOptions().CPlusPlus) 1952 PushOnScopeChains(cast<FieldDecl>(FD), S); 1953 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>())) 1954 Record->addDecl(Context, FD); 1955 } 1956} 1957 1958