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