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