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