SemaDeclObjC.cpp revision faca797cb3a8c1a41c2ba4728f26bb93b902b01b
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/Basic/Diagnostic.h" 18#include "clang/Parse/DeclSpec.h" 19 20using namespace clang; 21 22/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible 23/// and user declared, in the method definition's AST. 24void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) { 25 assert(getCurMethodDecl() == 0 && "Method parsing confused"); 26 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D); 27 28 // If we don't have a valid method decl, simply return. 29 if (!MDecl) 30 return; 31 32 // Allow the rest of sema to find private method decl implementations. 33 if (MDecl->isInstance()) 34 AddInstanceMethodToGlobalPool(MDecl); 35 else 36 AddFactoryMethodToGlobalPool(MDecl); 37 38 // Allow all of Sema to see that we are entering a method definition. 39 PushDeclContext(MDecl); 40 41 // Create Decl objects for each parameter, entrring them in the scope for 42 // binding to their use. 43 44 // Insert the invisible arguments, self and _cmd! 45 MDecl->createImplicitParams(Context); 46 47 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); 48 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); 49 50 // Introduce all of the other parameters into this scope. 51 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) { 52 ParmVarDecl *PDecl = MDecl->getParamDecl(i); 53 IdentifierInfo *II = PDecl->getIdentifier(); 54 if (II) 55 PushOnScopeChains(PDecl, FnBodyScope); 56 } 57} 58 59Sema::DeclTy *Sema:: 60ActOnStartClassInterface(SourceLocation AtInterfaceLoc, 61 IdentifierInfo *ClassName, SourceLocation ClassLoc, 62 IdentifierInfo *SuperName, SourceLocation SuperLoc, 63 DeclTy * const *ProtoRefs, unsigned NumProtoRefs, 64 SourceLocation EndProtoLoc, AttributeList *AttrList) { 65 assert(ClassName && "Missing class identifier"); 66 67 // Check for another declaration kind with the same name. 68 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); 69 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 70 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; 71 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 72 } 73 74 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 75 if (IDecl) { 76 // Class already seen. Is it a forward declaration? 77 if (!IDecl->isForwardDecl()) { 78 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName(); 79 Diag(IDecl->getLocation(), diag::note_previous_definition); 80 81 // Return the previous class interface. 82 // FIXME: don't leak the objects passed in! 83 return IDecl; 84 } else { 85 IDecl->setLocation(AtInterfaceLoc); 86 IDecl->setForwardDecl(false); 87 } 88 } else { 89 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, 90 ClassName, ClassLoc); 91 if (AttrList) 92 ProcessDeclAttributeList(IDecl, AttrList); 93 94 ObjCInterfaceDecls[ClassName] = IDecl; 95 // Remember that this needs to be removed when the scope is popped. 96 TUScope->AddDecl(IDecl); 97 } 98 99 if (SuperName) { 100 ObjCInterfaceDecl* SuperClassEntry = 0; 101 // Check if a different kind of symbol declared in this scope. 102 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope); 103 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 104 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; 105 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 106 } 107 else { 108 // Check that super class is previously defined 109 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 110 111 if (!SuperClassEntry) 112 Diag(SuperLoc, diag::err_undef_superclass) 113 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); 114 else if (SuperClassEntry->isForwardDecl()) 115 Diag(SuperLoc, diag::err_undef_superclass) 116 << SuperClassEntry->getDeclName() << ClassName 117 << SourceRange(AtInterfaceLoc, ClassLoc); 118 } 119 IDecl->setSuperClass(SuperClassEntry); 120 IDecl->setSuperClassLoc(SuperLoc); 121 IDecl->setLocEnd(SuperLoc); 122 } else { // we have a root class. 123 IDecl->setLocEnd(ClassLoc); 124 } 125 126 /// Check then save referenced protocols. 127 if (NumProtoRefs) { 128 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs); 129 IDecl->setLocEnd(EndProtoLoc); 130 } 131 132 CheckObjCDeclScope(IDecl); 133 return IDecl; 134} 135 136/// ActOnCompatiblityAlias - this action is called after complete parsing of 137/// @compatibility_alias declaration. It sets up the alias relationships. 138Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, 139 IdentifierInfo *AliasName, 140 SourceLocation AliasLocation, 141 IdentifierInfo *ClassName, 142 SourceLocation ClassLocation) { 143 // Look for previous declaration of alias name 144 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope); 145 if (ADecl) { 146 if (isa<ObjCCompatibleAliasDecl>(ADecl)) 147 Diag(AliasLocation, diag::warn_previous_alias_decl); 148 else 149 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; 150 Diag(ADecl->getLocation(), diag::note_previous_declaration); 151 return 0; 152 } 153 // Check for class declaration 154 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); 155 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); 156 if (CDecl == 0) { 157 Diag(ClassLocation, diag::warn_undef_interface) << ClassName; 158 if (CDeclU) 159 Diag(CDeclU->getLocation(), diag::note_previous_declaration); 160 return 0; 161 } 162 163 // Everything checked out, instantiate a new alias declaration AST. 164 ObjCCompatibleAliasDecl *AliasDecl = 165 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl); 166 167 ObjCAliasDecls[AliasName] = AliasDecl; 168 169 if (!CheckObjCDeclScope(AliasDecl)) 170 TUScope->AddDecl(AliasDecl); 171 172 return AliasDecl; 173} 174 175Sema::DeclTy * 176Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, 177 IdentifierInfo *ProtocolName, 178 SourceLocation ProtocolLoc, 179 DeclTy * const *ProtoRefs, 180 unsigned NumProtoRefs, 181 SourceLocation EndProtoLoc, 182 AttributeList *AttrList) { 183 // FIXME: Deal with AttrList. 184 assert(ProtocolName && "Missing protocol identifier"); 185 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName]; 186 if (PDecl) { 187 // Protocol already seen. Better be a forward protocol declaration 188 if (!PDecl->isForwardDecl()) { 189 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName; 190 Diag(PDecl->getLocation(), diag::note_previous_definition); 191 // Just return the protocol we already had. 192 // FIXME: don't leak the objects passed in! 193 return PDecl; 194 } 195 // Make sure the cached decl gets a valid start location. 196 PDecl->setLocation(AtProtoInterfaceLoc); 197 PDecl->setForwardDecl(false); 198 } else { 199 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName); 200 PDecl->setForwardDecl(false); 201 ObjCProtocols[ProtocolName] = PDecl; 202 } 203 204 if (NumProtoRefs) { 205 /// Check then save referenced protocols. 206 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs); 207 PDecl->setLocEnd(EndProtoLoc); 208 } 209 210 CheckObjCDeclScope(PDecl); 211 return PDecl; 212} 213 214/// FindProtocolDeclaration - This routine looks up protocols and 215/// issues an error if they are not declared. It returns list of 216/// protocol declarations in its 'Protocols' argument. 217void 218Sema::FindProtocolDeclaration(bool WarnOnDeclarations, 219 const IdentifierLocPair *ProtocolId, 220 unsigned NumProtocols, 221 llvm::SmallVectorImpl<DeclTy*> &Protocols) { 222 for (unsigned i = 0; i != NumProtocols; ++i) { 223 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first]; 224 if (!PDecl) { 225 Diag(ProtocolId[i].second, diag::err_undeclared_protocol) 226 << ProtocolId[i].first; 227 continue; 228 } 229 230 // If this is a forward declaration and we are supposed to warn in this 231 // case, do it. 232 if (WarnOnDeclarations && PDecl->isForwardDecl()) 233 Diag(ProtocolId[i].second, diag::warn_undef_protocolref) 234 << ProtocolId[i].first; 235 Protocols.push_back(PDecl); 236 } 237} 238 239/// DiagnosePropertyMismatch - Compares two properties for their 240/// attributes and types and warns on a variety of inconsistencies. 241/// 242void 243Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, 244 ObjCPropertyDecl *SuperProperty, 245 const IdentifierInfo *inheritedName) { 246 ObjCPropertyDecl::PropertyAttributeKind CAttr = 247 Property->getPropertyAttributes(); 248 ObjCPropertyDecl::PropertyAttributeKind SAttr = 249 SuperProperty->getPropertyAttributes(); 250 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) 251 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) 252 Diag(Property->getLocation(), diag::warn_readonly_property) 253 << Property->getDeclName() << inheritedName; 254 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) 255 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) 256 Diag(Property->getLocation(), diag::warn_property_attribute) 257 << Property->getDeclName() << "copy" << inheritedName; 258 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain) 259 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain)) 260 Diag(Property->getLocation(), diag::warn_property_attribute) 261 << Property->getDeclName() << "retain" << inheritedName; 262 263 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) 264 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) 265 Diag(Property->getLocation(), diag::warn_property_attribute) 266 << Property->getDeclName() << "atomic" << inheritedName; 267 if (Property->getSetterName() != SuperProperty->getSetterName()) 268 Diag(Property->getLocation(), diag::warn_property_attribute) 269 << Property->getDeclName() << "setter" << inheritedName; 270 if (Property->getGetterName() != SuperProperty->getGetterName()) 271 Diag(Property->getLocation(), diag::warn_property_attribute) 272 << Property->getDeclName() << "getter" << inheritedName; 273 274 if (Context.getCanonicalType(Property->getType()) != 275 Context.getCanonicalType(SuperProperty->getType())) 276 Diag(Property->getLocation(), diag::warn_property_type) 277 << Property->getType() << inheritedName; 278 279} 280 281/// ComparePropertiesInBaseAndSuper - This routine compares property 282/// declarations in base and its super class, if any, and issues 283/// diagnostics in a variety of inconsistant situations. 284/// 285void 286Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { 287 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); 288 if (!SDecl) 289 return; 290 // FIXME: O(N^2) 291 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(), 292 E = SDecl->classprop_end(); S != E; ++S) { 293 ObjCPropertyDecl *SuperPDecl = (*S); 294 // Does property in super class has declaration in current class? 295 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(), 296 E = IDecl->classprop_end(); I != E; ++I) { 297 ObjCPropertyDecl *PDecl = (*I); 298 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) 299 DiagnosePropertyMismatch(PDecl, SuperPDecl, 300 SDecl->getIdentifier()); 301 } 302 } 303} 304 305/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list 306/// of properties declared in a protocol and adds them to the list 307/// of properties for current class if it is not there already. 308void 309Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl, 310 ObjCProtocolDecl *PDecl) { 311 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties; 312 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(), 313 E = PDecl->classprop_end(); P != E; ++P) { 314 ObjCPropertyDecl *Pr = (*P); 315 ObjCInterfaceDecl::classprop_iterator CP, CE; 316 // Is this property already in class's list of properties? 317 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end(); 318 CP != CE; ++CP) 319 if ((*CP)->getIdentifier() == Pr->getIdentifier()) 320 break; 321 if (CP == CE) 322 // Add this property to list of properties for thie class. 323 mergeProperties.push_back(Pr); 324 else 325 // Property protocol already exist in class. Diagnose any mismatch. 326 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); 327 } 328 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size()); 329} 330 331/// MergeProtocolPropertiesIntoClass - This routine merges properties 332/// declared in 'MergeItsProtocols' objects (which can be a class or an 333/// inherited protocol into the list of properties for class 'IDecl' 334/// 335 336void 337Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl, 338 DeclTy *MergeItsProtocols) { 339 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols); 340 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 341 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(), 342 E = MDecl->protocol_end(); P != E; ++P) 343 // Merge properties of class (*P) into IDECL's 344 MergeOneProtocolPropertiesIntoClass(IDecl, *P); 345 346 // Go thru the list of protocols for this class and recursively merge 347 // their properties into this class as well. 348 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(), 349 E = IDecl->protocol_end(); P != E; ++P) 350 MergeProtocolPropertiesIntoClass(IDecl, *P); 351 } else { 352 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); 353 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), 354 E = MD->protocol_end(); P != E; ++P) 355 MergeOneProtocolPropertiesIntoClass(IDecl, (*P)); 356 } 357} 358 359/// ActOnForwardProtocolDeclaration - 360Action::DeclTy * 361Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, 362 const IdentifierLocPair *IdentList, 363 unsigned NumElts) { 364 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; 365 366 for (unsigned i = 0; i != NumElts; ++i) { 367 IdentifierInfo *Ident = IdentList[i].first; 368 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident]; 369 if (PDecl == 0) // Not already seen? 370 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident); 371 372 Protocols.push_back(PDecl); 373 } 374 375 ObjCForwardProtocolDecl *PDecl = 376 ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc, 377 &Protocols[0], Protocols.size()); 378 379 CheckObjCDeclScope(PDecl); 380 return PDecl; 381} 382 383Sema::DeclTy *Sema:: 384ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, 385 IdentifierInfo *ClassName, SourceLocation ClassLoc, 386 IdentifierInfo *CategoryName, 387 SourceLocation CategoryLoc, 388 DeclTy * const *ProtoRefs, 389 unsigned NumProtoRefs, 390 SourceLocation EndProtoLoc) { 391 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); 392 393 ObjCCategoryDecl *CDecl = 394 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName); 395 CDecl->setClassInterface(IDecl); 396 397 /// Check that class of this category is already completely declared. 398 if (!IDecl || IDecl->isForwardDecl()) 399 Diag(ClassLoc, diag::err_undef_interface) << ClassName; 400 else { 401 /// Check for duplicate interface declaration for this category 402 ObjCCategoryDecl *CDeclChain; 403 for (CDeclChain = IDecl->getCategoryList(); CDeclChain; 404 CDeclChain = CDeclChain->getNextClassCategory()) { 405 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) { 406 Diag(CategoryLoc, diag::warn_dup_category_def) 407 << ClassName << CategoryName; 408 Diag(CDeclChain->getLocation(), diag::note_previous_definition); 409 break; 410 } 411 } 412 if (!CDeclChain) 413 CDecl->insertNextClassCategory(); 414 } 415 416 if (NumProtoRefs) { 417 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs); 418 CDecl->setLocEnd(EndProtoLoc); 419 } 420 421 CheckObjCDeclScope(CDecl); 422 return CDecl; 423} 424 425/// ActOnStartCategoryImplementation - Perform semantic checks on the 426/// category implementation declaration and build an ObjCCategoryImplDecl 427/// object. 428Sema::DeclTy *Sema::ActOnStartCategoryImplementation( 429 SourceLocation AtCatImplLoc, 430 IdentifierInfo *ClassName, SourceLocation ClassLoc, 431 IdentifierInfo *CatName, SourceLocation CatLoc) { 432 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); 433 ObjCCategoryImplDecl *CDecl = 434 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl); 435 /// Check that class of this category is already completely declared. 436 if (!IDecl || IDecl->isForwardDecl()) 437 Diag(ClassLoc, diag::err_undef_interface) << ClassName; 438 439 /// TODO: Check that CatName, category name, is not used in another 440 // implementation. 441 ObjCCategoryImpls.push_back(CDecl); 442 443 CheckObjCDeclScope(CDecl); 444 return CDecl; 445} 446 447Sema::DeclTy *Sema::ActOnStartClassImplementation( 448 SourceLocation AtClassImplLoc, 449 IdentifierInfo *ClassName, SourceLocation ClassLoc, 450 IdentifierInfo *SuperClassname, 451 SourceLocation SuperClassLoc) { 452 ObjCInterfaceDecl* IDecl = 0; 453 // Check for another declaration kind with the same name. 454 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); 455 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 456 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; 457 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 458 } 459 else { 460 // Is there an interface declaration of this class; if not, warn! 461 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 462 if (!IDecl) 463 Diag(ClassLoc, diag::warn_undef_interface) << ClassName; 464 } 465 466 // Check that super class name is valid class name 467 ObjCInterfaceDecl* SDecl = 0; 468 if (SuperClassname) { 469 // Check if a different kind of symbol declared in this scope. 470 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope); 471 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 472 Diag(SuperClassLoc, diag::err_redefinition_different_kind) 473 << SuperClassname; 474 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 475 } else { 476 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 477 if (!SDecl) 478 Diag(SuperClassLoc, diag::err_undef_superclass) 479 << SuperClassname << ClassName; 480 else if (IDecl && IDecl->getSuperClass() != SDecl) { 481 // This implementation and its interface do not have the same 482 // super class. 483 Diag(SuperClassLoc, diag::err_conflicting_super_class) 484 << SDecl->getDeclName(); 485 Diag(SDecl->getLocation(), diag::note_previous_definition); 486 } 487 } 488 } 489 490 if (!IDecl) { 491 // Legacy case of @implementation with no corresponding @interface. 492 // Build, chain & install the interface decl into the identifier. 493 494 // FIXME: Do we support attributes on the @implementation? If so 495 // we should copy them over. 496 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName, 497 ClassLoc, false, true); 498 ObjCInterfaceDecls[ClassName] = IDecl; 499 IDecl->setSuperClass(SDecl); 500 IDecl->setLocEnd(ClassLoc); 501 502 // Remember that this needs to be removed when the scope is popped. 503 TUScope->AddDecl(IDecl); 504 } 505 506 ObjCImplementationDecl* IMPDecl = 507 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName, 508 IDecl, SDecl); 509 510 if (CheckObjCDeclScope(IMPDecl)) 511 return IMPDecl; 512 513 // Check that there is no duplicate implementation of this class. 514 if (ObjCImplementations[ClassName]) 515 // FIXME: Don't leak everything! 516 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; 517 else // add it to the list. 518 ObjCImplementations[ClassName] = IMPDecl; 519 return IMPDecl; 520} 521 522void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, 523 ObjCIvarDecl **ivars, unsigned numIvars, 524 SourceLocation RBrace) { 525 assert(ImpDecl && "missing implementation decl"); 526 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier()); 527 if (!IDecl) 528 return; 529 /// Check case of non-existing @interface decl. 530 /// (legacy objective-c @implementation decl without an @interface decl). 531 /// Add implementations's ivar to the synthesize class's ivar list. 532 if (IDecl->ImplicitInterfaceDecl()) { 533 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace); 534 return; 535 } 536 // If implementation has empty ivar list, just return. 537 if (numIvars == 0) 538 return; 539 540 assert(ivars && "missing @implementation ivars"); 541 542 // Check interface's Ivar list against those in the implementation. 543 // names and types must match. 544 // 545 unsigned j = 0; 546 ObjCInterfaceDecl::ivar_iterator 547 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); 548 for (; numIvars > 0 && IVI != IVE; ++IVI) { 549 ObjCIvarDecl* ImplIvar = ivars[j++]; 550 ObjCIvarDecl* ClsIvar = *IVI; 551 assert (ImplIvar && "missing implementation ivar"); 552 assert (ClsIvar && "missing class ivar"); 553 if (Context.getCanonicalType(ImplIvar->getType()) != 554 Context.getCanonicalType(ClsIvar->getType())) { 555 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) 556 << ImplIvar->getIdentifier() 557 << ImplIvar->getType() << ClsIvar->getType(); 558 Diag(ClsIvar->getLocation(), diag::note_previous_definition); 559 } 560 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed 561 // as error. 562 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { 563 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) 564 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); 565 Diag(ClsIvar->getLocation(), diag::note_previous_definition); 566 return; 567 } 568 --numIvars; 569 } 570 571 if (numIvars > 0) 572 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); 573 else if (IVI != IVE) 574 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); 575} 576 577void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, 578 bool &IncompleteImpl) { 579 if (!IncompleteImpl) { 580 Diag(ImpLoc, diag::warn_incomplete_impl); 581 IncompleteImpl = true; 582 } 583 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName(); 584} 585 586/// FIXME: Type hierarchies in Objective-C can be deep. We could most 587/// likely improve the efficiency of selector lookups and type 588/// checking by associating with each protocol / interface / category 589/// the flattened instance tables. If we used an immutable set to keep 590/// the table then it wouldn't add significant memory cost and it 591/// would be handy for lookups. 592 593/// CheckProtocolMethodDefs - This routine checks unimplemented methods 594/// Declared in protocol, and those referenced by it. 595void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, 596 ObjCProtocolDecl *PDecl, 597 bool& IncompleteImpl, 598 const llvm::DenseSet<Selector> &InsMap, 599 const llvm::DenseSet<Selector> &ClsMap, 600 ObjCInterfaceDecl *IDecl) { 601 ObjCInterfaceDecl *Super = IDecl->getSuperClass(); 602 603 // If a method lookup fails locally we still need to look and see if 604 // the method was implemented by a base class or an inherited 605 // protocol. This lookup is slow, but occurs rarely in correct code 606 // and otherwise would terminate in a warning. 607 608 // check unimplemented instance methods. 609 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), 610 E = PDecl->instmeth_end(); I != E; ++I) { 611 ObjCMethodDecl *method = *I; 612 if (method->getImplementationControl() != ObjCMethodDecl::Optional && 613 !method->isSynthesized() && !InsMap.count(method->getSelector()) && 614 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) 615 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); 616 } 617 // check unimplemented class methods 618 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), 619 E = PDecl->classmeth_end(); I != E; ++I) { 620 ObjCMethodDecl *method = *I; 621 if (method->getImplementationControl() != ObjCMethodDecl::Optional && 622 !ClsMap.count(method->getSelector()) && 623 (!Super || !Super->lookupClassMethod(method->getSelector()))) 624 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); 625 } 626 // Check on this protocols's referenced protocols, recursively. 627 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), 628 E = PDecl->protocol_end(); PI != E; ++PI) 629 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl); 630} 631 632void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl, 633 ObjCInterfaceDecl* IDecl) { 634 llvm::DenseSet<Selector> InsMap; 635 // Check and see if instance methods in class interface have been 636 // implemented in the implementation class. 637 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(), 638 E = IMPDecl->instmeth_end(); I != E; ++I) 639 InsMap.insert((*I)->getSelector()); 640 641 bool IncompleteImpl = false; 642 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(), 643 E = IDecl->instmeth_end(); I != E; ++I) 644 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) 645 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); 646 647 llvm::DenseSet<Selector> ClsMap; 648 // Check and see if class methods in class interface have been 649 // implemented in the implementation class. 650 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(), 651 E = IMPDecl->classmeth_end(); I != E; ++I) 652 ClsMap.insert((*I)->getSelector()); 653 654 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(), 655 E = IDecl->classmeth_end(); I != E; ++I) 656 if (!ClsMap.count((*I)->getSelector())) 657 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); 658 659 // Check the protocol list for unimplemented methods in the @implementation 660 // class. 661 const ObjCList<ObjCProtocolDecl> &Protocols = 662 IDecl->getReferencedProtocols(); 663 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 664 E = Protocols.end(); I != E; ++I) 665 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I, 666 IncompleteImpl, InsMap, ClsMap, IDecl); 667} 668 669/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the 670/// category interface are implemented in the category @implementation. 671void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl, 672 ObjCCategoryDecl *CatClassDecl) { 673 llvm::DenseSet<Selector> InsMap; 674 // Check and see if instance methods in category interface have been 675 // implemented in its implementation class. 676 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(), 677 E = CatImplDecl->instmeth_end(); I != E; ++I) 678 InsMap.insert((*I)->getSelector()); 679 680 bool IncompleteImpl = false; 681 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(), 682 E = CatClassDecl->instmeth_end(); I != E; ++I) 683 if (!InsMap.count((*I)->getSelector())) 684 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); 685 686 llvm::DenseSet<Selector> ClsMap; 687 // Check and see if class methods in category interface have been 688 // implemented in its implementation class. 689 for (ObjCCategoryImplDecl::classmeth_iterator 690 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end(); 691 I != E; ++I) 692 ClsMap.insert((*I)->getSelector()); 693 694 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(), 695 E = CatClassDecl->classmeth_end(); I != E; ++I) 696 if (!ClsMap.count((*I)->getSelector())) 697 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); 698 699 // Check the protocol list for unimplemented methods in the @implementation 700 // class. 701 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(), 702 E = CatClassDecl->protocol_end(); PI != E; ++PI) 703 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl, 704 InsMap, ClsMap, CatClassDecl->getClassInterface()); 705} 706 707/// ActOnForwardClassDeclaration - 708Action::DeclTy * 709Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, 710 IdentifierInfo **IdentList, unsigned NumElts) 711{ 712 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; 713 714 for (unsigned i = 0; i != NumElts; ++i) { 715 // Check for another declaration kind with the same name. 716 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope); 717 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 718 // GCC apparently allows the following idiom: 719 // 720 // typedef NSObject < XCElementTogglerP > XCElementToggler; 721 // @class XCElementToggler; 722 // 723 // FIXME: Make an extension? 724 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl); 725 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) { 726 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; 727 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 728 } 729 } 730 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 731 if (!IDecl) { // Not already seen? Make a forward decl. 732 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i], 733 SourceLocation(), true); 734 ObjCInterfaceDecls[IdentList[i]] = IDecl; 735 736 // Remember that this needs to be removed when the scope is popped. 737 TUScope->AddDecl(IDecl); 738 } 739 740 Interfaces.push_back(IDecl); 741 } 742 743 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc, 744 &Interfaces[0], 745 Interfaces.size()); 746 747 CheckObjCDeclScope(CDecl); 748 return CDecl; 749} 750 751 752/// MatchTwoMethodDeclarations - Checks that two methods have matching type and 753/// returns true, or false, accordingly. 754/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons 755bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, 756 const ObjCMethodDecl *PrevMethod, 757 bool matchBasedOnSizeAndAlignment) { 758 QualType T1 = Context.getCanonicalType(Method->getResultType()); 759 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType()); 760 761 if (T1 != T2) { 762 // The result types are different. 763 if (!matchBasedOnSizeAndAlignment) 764 return false; 765 // Incomplete types don't have a size and alignment. 766 if (T1->isIncompleteType() || T2->isIncompleteType()) 767 return false; 768 // Check is based on size and alignment. 769 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) 770 return false; 771 } 772 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) { 773 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType()); 774 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType()); 775 if (T1 != T2) { 776 // The result types are different. 777 if (!matchBasedOnSizeAndAlignment) 778 return false; 779 // Incomplete types don't have a size and alignment. 780 if (T1->isIncompleteType() || T2->isIncompleteType()) 781 return false; 782 // Check is based on size and alignment. 783 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) 784 return false; 785 } 786 } 787 return true; 788} 789 790void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) { 791 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()]; 792 if (!FirstMethod.Method) { 793 // Haven't seen a method with this selector name yet - add it. 794 FirstMethod.Method = Method; 795 FirstMethod.Next = 0; 796 } else { 797 // We've seen a method with this name, now check the type signature(s). 798 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); 799 800 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; 801 Next = Next->Next) 802 match = MatchTwoMethodDeclarations(Method, Next->Method); 803 804 if (!match) { 805 // We have a new signature for an existing method - add it. 806 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". 807 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);; 808 } 809 } 810} 811 812// FIXME: Finish implementing -Wno-strict-selector-match. 813ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel, 814 SourceRange R) { 815 ObjCMethodList &MethList = InstanceMethodPool[Sel]; 816 bool issueWarning = false; 817 818 if (MethList.Method && MethList.Next) { 819 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 820 // This checks if the methods differ by size & alignment. 821 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) 822 issueWarning = true; 823 } 824 if (issueWarning && (MethList.Method && MethList.Next)) { 825 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; 826 Diag(MethList.Method->getLocStart(), diag::note_using_decl) 827 << MethList.Method->getSourceRange(); 828 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 829 Diag(Next->Method->getLocStart(), diag::note_also_found_decl) 830 << Next->Method->getSourceRange(); 831 } 832 return MethList.Method; 833} 834 835void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { 836 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()]; 837 if (!FirstMethod.Method) { 838 // Haven't seen a method with this selector name yet - add it. 839 FirstMethod.Method = Method; 840 FirstMethod.Next = 0; 841 } else { 842 // We've seen a method with this name, now check the type signature(s). 843 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); 844 845 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; 846 Next = Next->Next) 847 match = MatchTwoMethodDeclarations(Method, Next->Method); 848 849 if (!match) { 850 // We have a new signature for an existing method - add it. 851 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". 852 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); 853 FirstMethod.Next = OMI; 854 } 855 } 856} 857 858/// diagnosePropertySetterGetterMismatch - Make sure that use-defined 859/// setter/getter methods have the property type and issue diagnostics 860/// if they don't. 861/// 862void 863Sema::diagnosePropertySetterGetterMismatch(ObjCPropertyDecl *property, 864 const ObjCMethodDecl *GetterMethod, 865 const ObjCMethodDecl *SetterMethod) { 866 if (GetterMethod && 867 GetterMethod->getResultType() != property->getType()) 868 Diag(property->getLocation(), 869 diag::err_accessor_property_type_mismatch) 870 << property->getDeclName() 871 << GetterMethod->getSelector().getAsIdentifierInfo(); 872 873 if (SetterMethod) { 874 if (SetterMethod->getResultType() != Context.VoidPtrTy) 875 Diag(SetterMethod->getLocation(), diag::err_setter_type_void); 876 if (SetterMethod->getNumParams() != 1 || 877 (SetterMethod->getParamDecl(0)->getType() != property->getType())) 878 Diag(property->getLocation(), 879 diag::err_accessor_property_type_mismatch) 880 << property->getDeclName() 881 << SetterMethod->getSelector().getAsIdentifierInfo(); 882 } 883} 884 885// Note: For class/category implemenations, allMethods/allProperties is 886// always null. 887void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl, 888 DeclTy **allMethods, unsigned allNum, 889 DeclTy **allProperties, unsigned pNum) { 890 Decl *ClassDecl = static_cast<Decl *>(classDecl); 891 892 // FIXME: If we don't have a ClassDecl, we have an error. We should consider 893 // always passing in a decl. If the decl has an error, isInvalidDecl() 894 // should be true. 895 if (!ClassDecl) 896 return; 897 898 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods; 899 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods; 900 901 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; 902 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; 903 904 bool isInterfaceDeclKind = 905 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) 906 || isa<ObjCProtocolDecl>(ClassDecl); 907 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); 908 909 if (pNum != 0) { 910 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) 911 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); 912 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) 913 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); 914 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl)) 915 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); 916 else 917 assert(false && "ActOnAtEnd - property declaration misplaced"); 918 } 919 920 for (unsigned i = 0; i < allNum; i++ ) { 921 ObjCMethodDecl *Method = 922 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i])); 923 924 if (!Method) continue; // Already issued a diagnostic. 925 if (Method->isInstance()) { 926 /// Check for instance method of the same name with incompatible types 927 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; 928 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 929 : false; 930 if (isInterfaceDeclKind && PrevMethod && !match 931 || checkIdenticalMethods && match) { 932 Diag(Method->getLocation(), diag::err_duplicate_method_decl) 933 << Method->getDeclName(); 934 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 935 } else { 936 insMethods.push_back(Method); 937 InsMap[Method->getSelector()] = Method; 938 /// The following allows us to typecheck messages to "id". 939 AddInstanceMethodToGlobalPool(Method); 940 } 941 } 942 else { 943 /// Check for class method of the same name with incompatible types 944 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; 945 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 946 : false; 947 if (isInterfaceDeclKind && PrevMethod && !match 948 || checkIdenticalMethods && match) { 949 Diag(Method->getLocation(), diag::err_duplicate_method_decl) 950 << Method->getDeclName(); 951 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 952 } else { 953 clsMethods.push_back(Method); 954 ClsMap[Method->getSelector()] = Method; 955 /// The following allows us to typecheck messages to "Class". 956 AddFactoryMethodToGlobalPool(Method); 957 } 958 } 959 } 960 // Save the size so we can detect if we've added any property methods. 961 unsigned int insMethodsSizePriorToPropAdds = insMethods.size(); 962 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size(); 963 964 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 965 // Compares properties declared in this class to those of its 966 // super class. 967 ComparePropertiesInBaseAndSuper(I); 968 MergeProtocolPropertiesIntoClass(I, I); 969 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(), 970 e = I->classprop_end(); i != e; ++i) { 971 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()], 972 InsMap[(*i)->getSetterName()]); 973 I->addPropertyMethods(Context, *i, insMethods, InsMap); 974 } 975 I->addMethods(&insMethods[0], insMethods.size(), 976 &clsMethods[0], clsMethods.size(), AtEndLoc); 977 978 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) { 979 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(), 980 e = P->classprop_end(); i != e; ++i) { 981 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()], 982 InsMap[(*i)->getSetterName()]); 983 P->addPropertyMethods(Context, *i, insMethods, InsMap); 984 } 985 P->addMethods(&insMethods[0], insMethods.size(), 986 &clsMethods[0], clsMethods.size(), AtEndLoc); 987 } 988 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { 989 // FIXME: Need to compare properties to those in interface? 990 991 // FIXME: If we merge properties into class we should probably 992 // merge them into category as well? 993 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(), 994 e = C->classprop_end(); i != e; ++i) { 995 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()], 996 InsMap[(*i)->getSetterName()]); 997 C->addPropertyMethods(Context, *i, insMethods, InsMap); 998 } 999 C->addMethods(&insMethods[0], insMethods.size(), 1000 &clsMethods[0], clsMethods.size(), AtEndLoc); 1001 } 1002 else if (ObjCImplementationDecl *IC = 1003 dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 1004 IC->setLocEnd(AtEndLoc); 1005 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier())) 1006 ImplMethodsVsClassMethods(IC, IDecl); 1007 } else { 1008 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl); 1009 CatImplClass->setLocEnd(AtEndLoc); 1010 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface(); 1011 // Find category interface decl and then check that all methods declared 1012 // in this interface are implemented in the category @implementation. 1013 if (IDecl) { 1014 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); 1015 Categories; Categories = Categories->getNextClassCategory()) { 1016 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { 1017 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories); 1018 break; 1019 } 1020 } 1021 } 1022 } 1023 // Add any synthesized methods to the global pool. This allows us to 1024 // handle the following, which is supported by GCC (and part of the design). 1025 // 1026 // @interface Foo 1027 // @property double bar; 1028 // @end 1029 // 1030 // void thisIsUnfortunate() { 1031 // id foo; 1032 // double bar = [foo bar]; 1033 // } 1034 // 1035 if (insMethodsSizePriorToPropAdds < insMethods.size()) 1036 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++) 1037 AddInstanceMethodToGlobalPool(insMethods[i]); 1038 if (clsMethodsSizePriorToPropAdds < clsMethods.size()) 1039 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++) 1040 AddFactoryMethodToGlobalPool(clsMethods[i]); 1041} 1042 1043 1044/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for 1045/// objective-c's type qualifier from the parser version of the same info. 1046static Decl::ObjCDeclQualifier 1047CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { 1048 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; 1049 if (PQTVal & ObjCDeclSpec::DQ_In) 1050 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); 1051 if (PQTVal & ObjCDeclSpec::DQ_Inout) 1052 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); 1053 if (PQTVal & ObjCDeclSpec::DQ_Out) 1054 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); 1055 if (PQTVal & ObjCDeclSpec::DQ_Bycopy) 1056 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); 1057 if (PQTVal & ObjCDeclSpec::DQ_Byref) 1058 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); 1059 if (PQTVal & ObjCDeclSpec::DQ_Oneway) 1060 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); 1061 1062 return ret; 1063} 1064 1065Sema::DeclTy *Sema::ActOnMethodDeclaration( 1066 SourceLocation MethodLoc, SourceLocation EndLoc, 1067 tok::TokenKind MethodType, DeclTy *classDecl, 1068 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, 1069 Selector Sel, 1070 // optional arguments. The number of types/arguments is obtained 1071 // from the Sel.getNumArgs(). 1072 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames, 1073 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, 1074 bool isVariadic) { 1075 Decl *ClassDecl = static_cast<Decl*>(classDecl); 1076 1077 // Make sure we can establish a context for the method. 1078 if (!ClassDecl) { 1079 Diag(MethodLoc, diag::error_missing_method_context); 1080 return 0; 1081 } 1082 QualType resultDeclType; 1083 1084 if (ReturnType) 1085 resultDeclType = QualType::getFromOpaquePtr(ReturnType); 1086 else // get the type for "id". 1087 resultDeclType = Context.getObjCIdType(); 1088 1089 ObjCMethodDecl* ObjCMethod = 1090 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, 1091 ClassDecl, 1092 MethodType == tok::minus, isVariadic, 1093 false, 1094 MethodDeclKind == tok::objc_optional ? 1095 ObjCMethodDecl::Optional : 1096 ObjCMethodDecl::Required); 1097 1098 llvm::SmallVector<ParmVarDecl*, 16> Params; 1099 1100 for (unsigned i = 0; i < Sel.getNumArgs(); i++) { 1101 // FIXME: arg->AttrList must be stored too! 1102 QualType argType; 1103 1104 if (ArgTypes[i]) 1105 argType = QualType::getFromOpaquePtr(ArgTypes[i]); 1106 else 1107 argType = Context.getObjCIdType(); 1108 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod, 1109 SourceLocation(/*FIXME*/), 1110 ArgNames[i], argType, 1111 VarDecl::None, 0, 0); 1112 Param->setObjCDeclQualifier( 1113 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier())); 1114 Params.push_back(Param); 1115 } 1116 1117 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs()); 1118 ObjCMethod->setObjCDeclQualifier( 1119 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); 1120 const ObjCMethodDecl *PrevMethod = 0; 1121 1122 if (AttrList) 1123 ProcessDeclAttributeList(ObjCMethod, AttrList); 1124 1125 // For implementations (which can be very "coarse grain"), we add the 1126 // method now. This allows the AST to implement lookup methods that work 1127 // incrementally (without waiting until we parse the @end). It also allows 1128 // us to flag multiple declaration errors as they occur. 1129 if (ObjCImplementationDecl *ImpDecl = 1130 dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 1131 if (MethodType == tok::minus) { 1132 PrevMethod = ImpDecl->getInstanceMethod(Sel); 1133 ImpDecl->addInstanceMethod(ObjCMethod); 1134 } else { 1135 PrevMethod = ImpDecl->getClassMethod(Sel); 1136 ImpDecl->addClassMethod(ObjCMethod); 1137 } 1138 } 1139 else if (ObjCCategoryImplDecl *CatImpDecl = 1140 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { 1141 if (MethodType == tok::minus) { 1142 PrevMethod = CatImpDecl->getInstanceMethod(Sel); 1143 CatImpDecl->addInstanceMethod(ObjCMethod); 1144 } else { 1145 PrevMethod = CatImpDecl->getClassMethod(Sel); 1146 CatImpDecl->addClassMethod(ObjCMethod); 1147 } 1148 } 1149 if (PrevMethod) { 1150 // You can never have two method definitions with the same name. 1151 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) 1152 << ObjCMethod->getDeclName(); 1153 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1154 } 1155 return ObjCMethod; 1156} 1157 1158void Sema::CheckObjCPropertyAttributes(QualType PropertyTy, 1159 SourceLocation Loc, 1160 unsigned &Attributes) { 1161 // FIXME: Improve the reported location. 1162 1163 // readonly and readwrite conflict. 1164 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1165 (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) { 1166 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1167 << "readonly" << "readwrite"; 1168 Attributes &= ~ObjCDeclSpec::DQ_PR_readonly; 1169 } 1170 1171 // Check for copy or retain on non-object types. 1172 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) && 1173 !Context.isObjCObjectPointerType(PropertyTy)) { 1174 Diag(Loc, diag::err_objc_property_requires_object) 1175 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain"); 1176 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain); 1177 } 1178 1179 // Check for more than one of { assign, copy, retain }. 1180 if (Attributes & ObjCDeclSpec::DQ_PR_assign) { 1181 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1182 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1183 << "assign" << "copy"; 1184 Attributes &= ~ObjCDeclSpec::DQ_PR_copy; 1185 } 1186 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1187 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1188 << "assign" << "retain"; 1189 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1190 } 1191 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1192 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1193 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1194 << "copy" << "retain"; 1195 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1196 } 1197 } 1198 1199 // Warn if user supplied no assignment attribute, property is 1200 // readwrite, and this is an object type. 1201 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | 1202 ObjCDeclSpec::DQ_PR_retain)) && 1203 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1204 Context.isObjCObjectPointerType(PropertyTy)) { 1205 // Skip this warning in gc-only mode. 1206 if (getLangOptions().getGCMode() != LangOptions::GCOnly) 1207 Diag(Loc, diag::warn_objc_property_no_assignment_attribute); 1208 1209 // If non-gc code warn that this is likely inappropriate. 1210 if (getLangOptions().getGCMode() == LangOptions::NonGC) 1211 Diag(Loc, diag::warn_objc_property_default_assign_on_object); 1212 1213 // FIXME: Implement warning dependent on NSCopying being 1214 // implemented. See also: 1215 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> 1216 // (please trim this list while you are at it). 1217 } 1218} 1219 1220Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, 1221 FieldDeclarator &FD, 1222 ObjCDeclSpec &ODS, 1223 Selector GetterSel, 1224 Selector SetterSel, 1225 DeclTy *ClassCategory, 1226 bool *isOverridingProperty, 1227 tok::ObjCKeywordKind MethodImplKind) { 1228 unsigned Attributes = ODS.getPropertyAttributes(); 1229 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || 1230 // default is readwrite! 1231 !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); 1232 // property is defaulted to 'assign' if it is readwrite and is 1233 // not retain or copy 1234 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || 1235 (isReadWrite && 1236 !(Attributes & ObjCDeclSpec::DQ_PR_retain) && 1237 !(Attributes & ObjCDeclSpec::DQ_PR_copy))); 1238 QualType T = GetTypeForDeclarator(FD.D, S); 1239 Decl *ClassDecl = static_cast<Decl *>(ClassCategory); 1240 1241 // May modify Attributes. 1242 CheckObjCPropertyAttributes(T, AtLoc, Attributes); 1243 1244 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) 1245 if (!CDecl->getIdentifier()) { 1246 // This is an anonymous category. property requires special 1247 // handling. 1248 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) { 1249 if (ObjCPropertyDecl *PIDecl = 1250 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) { 1251 // property 'PIDecl's readonly attribute will be over-ridden 1252 // with anonymous category's readwrite property attribute! 1253 unsigned PIkind = PIDecl->getPropertyAttributes(); 1254 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { 1255 if ((Attributes & ObjCPropertyDecl::OBJC_PR_retain) != 1256 (PIkind & ObjCPropertyDecl::OBJC_PR_retain) || 1257 (Attributes & ObjCPropertyDecl::OBJC_PR_copy) != 1258 (PIkind & ObjCPropertyDecl::OBJC_PR_copy) || 1259 (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) != 1260 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic)) 1261 Diag(AtLoc, diag::warn_property_attr_mismatch); 1262 PIDecl->makeitReadWriteAttribute(); 1263 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 1264 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 1265 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 1266 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 1267 PIDecl->setSetterName(SetterSel); 1268 // FIXME: use a common routine with addPropertyMethods. 1269 ObjCMethodDecl *SetterDecl = 1270 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel, 1271 Context.VoidTy, 1272 ICDecl, 1273 true, false, true, 1274 ObjCMethodDecl::Required); 1275 ParmVarDecl *Argument = ParmVarDecl::Create(Context, 1276 SetterDecl, 1277 SourceLocation(), 1278 FD.D.getIdentifier(), 1279 T, 1280 VarDecl::None, 1281 0, 0); 1282 SetterDecl->setMethodParams(&Argument, 1); 1283 PIDecl->setSetterMethodDecl(SetterDecl); 1284 } 1285 else 1286 Diag(AtLoc, diag::err_use_continuation_class); 1287 *isOverridingProperty = true; 1288 return 0; 1289 } 1290 // No matching property found in the main class. Just fall thru 1291 // and add property to the anonymous category. It looks like 1292 // it works as is. This category becomes just like a category 1293 // for its primary class. 1294 } else { 1295 Diag(CDecl->getLocation(), diag::err_continuation_class); 1296 *isOverridingProperty = true; 1297 return 0; 1298 } 1299 } 1300 1301 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc, 1302 FD.D.getIdentifier(), T); 1303 // Regardless of setter/getter attribute, we save the default getter/setter 1304 // selector names in anticipation of declaration of setter/getter methods. 1305 PDecl->setGetterName(GetterSel); 1306 PDecl->setSetterName(SetterSel); 1307 1308 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) 1309 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); 1310 1311 if (Attributes & ObjCDeclSpec::DQ_PR_getter) 1312 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); 1313 1314 if (Attributes & ObjCDeclSpec::DQ_PR_setter) 1315 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); 1316 1317 if (isReadWrite) 1318 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); 1319 1320 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 1321 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 1322 1323 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 1324 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 1325 1326 if (isAssign) 1327 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); 1328 1329 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) 1330 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); 1331 1332 if (MethodImplKind == tok::objc_required) 1333 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); 1334 else if (MethodImplKind == tok::objc_optional) 1335 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); 1336 1337 return PDecl; 1338} 1339 1340/// ActOnPropertyImplDecl - This routine performs semantic checks and 1341/// builds the AST node for a property implementation declaration; declared 1342/// as @synthesize or @dynamic. 1343/// 1344Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc, 1345 SourceLocation PropertyLoc, 1346 bool Synthesize, 1347 DeclTy *ClassCatImpDecl, 1348 IdentifierInfo *PropertyId, 1349 IdentifierInfo *PropertyIvar) { 1350 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl); 1351 // Make sure we have a context for the property implementation declaration. 1352 if (!ClassImpDecl) { 1353 Diag(AtLoc, diag::error_missing_property_context); 1354 return 0; 1355 } 1356 ObjCPropertyDecl *property = 0; 1357 ObjCInterfaceDecl* IDecl = 0; 1358 // Find the class or category class where this property must have 1359 // a declaration. 1360 ObjCImplementationDecl *IC = 0; 1361 ObjCCategoryImplDecl* CatImplClass = 0; 1362 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { 1363 IDecl = getObjCInterfaceDecl(IC->getIdentifier()); 1364 // We always synthesize an interface for an implementation 1365 // without an interface decl. So, IDecl is always non-zero. 1366 assert(IDecl && 1367 "ActOnPropertyImplDecl - @implementation without @interface"); 1368 1369 // Look for this property declaration in the @implementation's @interface 1370 property = IDecl->FindPropertyDeclaration(PropertyId); 1371 if (!property) { 1372 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); 1373 return 0; 1374 } 1375 } 1376 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { 1377 if (Synthesize) { 1378 Diag(AtLoc, diag::error_synthesize_category_decl); 1379 return 0; 1380 } 1381 IDecl = CatImplClass->getClassInterface(); 1382 if (!IDecl) { 1383 Diag(AtLoc, diag::error_missing_property_interface); 1384 return 0; 1385 } 1386 ObjCCategoryDecl *Category = 1387 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); 1388 1389 // If category for this implementation not found, it is an error which 1390 // has already been reported eralier. 1391 if (!Category) 1392 return 0; 1393 // Look for this property declaration in @implementation's category 1394 property = Category->FindPropertyDeclaration(PropertyId); 1395 if (!property) { 1396 Diag(PropertyLoc, diag::error_bad_category_property_decl) 1397 << Category->getDeclName(); 1398 return 0; 1399 } 1400 } 1401 else { 1402 Diag(AtLoc, diag::error_bad_property_context); 1403 return 0; 1404 } 1405 ObjCIvarDecl *Ivar = 0; 1406 // Check that we have a valid, previously declared ivar for @synthesize 1407 if (Synthesize) { 1408 // @synthesize 1409 if (!PropertyIvar) 1410 PropertyIvar = PropertyId; 1411 // Check that this is a previously declared 'ivar' in 'IDecl' interface 1412 Ivar = IDecl->FindIvarDeclaration(PropertyIvar); 1413 if (!Ivar) { 1414 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId; 1415 return 0; 1416 } 1417 QualType PropType = Context.getCanonicalType(property->getType()); 1418 QualType IvarType = Context.getCanonicalType(Ivar->getType()); 1419 1420 // Check that type of property and its ivar are type compatible. 1421 if (PropType != IvarType) { 1422 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) { 1423 Diag(PropertyLoc, diag::error_property_ivar_type) 1424 << property->getDeclName() << Ivar->getDeclName(); 1425 return 0; 1426 } 1427 } 1428 } else if (PropertyIvar) { 1429 // @dynamic 1430 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); 1431 return 0; 1432 } 1433 assert (property && "ActOnPropertyImplDecl - property declaration missing"); 1434 ObjCPropertyImplDecl *PIDecl = 1435 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property, 1436 (Synthesize ? 1437 ObjCPropertyImplDecl::Synthesize 1438 : ObjCPropertyImplDecl::Dynamic), 1439 Ivar); 1440 if (IC) 1441 IC->addPropertyImplementation(PIDecl); 1442 else 1443 CatImplClass->addPropertyImplementation(PIDecl); 1444 1445 return PIDecl; 1446} 1447 1448bool Sema::CheckObjCDeclScope(Decl *D) 1449{ 1450 if (isa<TranslationUnitDecl>(CurContext)) 1451 return false; 1452 1453 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); 1454 D->setInvalidDecl(); 1455 1456 return true; 1457} 1458