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