SemaDeclObjC.cpp revision 3435096781465f6b8e69de4b35d9dd3a94a76468
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/// 250// TODO: Incomplete. 251// 252void 253Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, 254 ObjCPropertyDecl *SuperProperty, 255 ObjCInterfaceDecl *SuperIDecl) { 256 ObjCPropertyDecl::PropertyAttributeKind CAttr = 257 Property->getPropertyAttributes(); 258 ObjCPropertyDecl::PropertyAttributeKind SAttr = 259 SuperProperty->getPropertyAttributes(); 260 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) 261 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) 262 Diag(Property->getLocation(), diag::warn_readonly_property, 263 Property->getName(), SuperIDecl->getName()); 264 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) 265 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) 266 Diag(Property->getLocation(), diag::warn_property_attribute, 267 Property->getName(), "copy", SuperIDecl->getName(), 268 SourceRange()); 269 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain) 270 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain)) 271 Diag(Property->getLocation(), diag::warn_property_attribute, 272 Property->getName(), "retain", SuperIDecl->getName(), 273 SourceRange()); 274 275 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) 276 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) 277 Diag(Property->getLocation(), diag::warn_property_attribute, 278 Property->getName(), "atomic", SuperIDecl->getName(), 279 SourceRange()); 280 if (Property->getSetterName() != SuperProperty->getSetterName()) 281 Diag(Property->getLocation(), diag::warn_property_attribute, 282 Property->getName(), "setter", SuperIDecl->getName(), 283 SourceRange()); 284 if (Property->getGetterName() != SuperProperty->getGetterName()) 285 Diag(Property->getLocation(), diag::warn_property_attribute, 286 Property->getName(), "getter", SuperIDecl->getName(), 287 SourceRange()); 288 289 if (Property->getCanonicalType() != SuperProperty->getCanonicalType()) 290 Diag(Property->getLocation(), diag::warn_property_type, 291 Property->getType().getAsString(), 292 SuperIDecl->getName()); 293 294} 295 296/// ComparePropertiesInBaseAndSuper - This routine compares property 297/// declarations in base and its super class, if any, and issues 298/// diagnostics in a variety of inconsistant situations. 299/// 300void 301Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { 302 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); 303 if (!SDecl) 304 return; 305 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(), 306 E = SDecl->classprop_end(); S != E; ++S) { 307 ObjCPropertyDecl *SuperPDecl = (*S); 308 // Does property in super class has declaration in current class? 309 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(), 310 E = IDecl->classprop_end(); I != E; ++I) { 311 ObjCPropertyDecl *PDecl = (*I); 312 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) 313 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl); 314 } 315 } 316} 317 318/// ActOnForwardProtocolDeclaration - 319Action::DeclTy * 320Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, 321 IdentifierInfo **IdentList, unsigned NumElts) { 322 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; 323 324 for (unsigned i = 0; i != NumElts; ++i) { 325 IdentifierInfo *Ident = IdentList[i]; 326 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident]; 327 if (PDecl == 0) { // Not already seen? 328 // FIXME: Pass in the location of the identifier! 329 PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident); 330 } 331 332 Protocols.push_back(PDecl); 333 } 334 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc, 335 &Protocols[0], Protocols.size()); 336} 337 338Sema::DeclTy *Sema::ActOnStartCategoryInterface( 339 SourceLocation AtInterfaceLoc, 340 IdentifierInfo *ClassName, SourceLocation ClassLoc, 341 IdentifierInfo *CategoryName, SourceLocation CategoryLoc, 342 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs, 343 SourceLocation EndProtoLoc) { 344 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); 345 346 ObjCCategoryDecl *CDecl = 347 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName); 348 CDecl->setClassInterface(IDecl); 349 350 /// Check that class of this category is already completely declared. 351 if (!IDecl || IDecl->isForwardDecl()) 352 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); 353 else { 354 /// Check for duplicate interface declaration for this category 355 ObjCCategoryDecl *CDeclChain; 356 for (CDeclChain = IDecl->getCategoryList(); CDeclChain; 357 CDeclChain = CDeclChain->getNextClassCategory()) { 358 if (CDeclChain->getIdentifier() == CategoryName) { 359 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(), 360 CategoryName->getName()); 361 break; 362 } 363 } 364 if (!CDeclChain) 365 CDecl->insertNextClassCategory(); 366 } 367 368 if (NumProtoRefs) { 369 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols; 370 /// Check and then save the referenced protocols. 371 for (unsigned int i = 0; i != NumProtoRefs; i++) { 372 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]]; 373 if (!RefPDecl || RefPDecl->isForwardDecl()) { 374 Diag(CategoryLoc, diag::warn_undef_protocolref, 375 ProtoRefNames[i]->getName(), 376 CategoryName->getName()); 377 } 378 if (RefPDecl) 379 RefProtocols.push_back(RefPDecl); 380 } 381 if (!RefProtocols.empty()) 382 CDecl->setReferencedProtocolList(&RefProtocols[0], RefProtocols.size()); 383 } 384 CDecl->setLocEnd(EndProtoLoc); 385 return CDecl; 386} 387 388/// ActOnStartCategoryImplementation - Perform semantic checks on the 389/// category implementation declaration and build an ObjCCategoryImplDecl 390/// object. 391Sema::DeclTy *Sema::ActOnStartCategoryImplementation( 392 SourceLocation AtCatImplLoc, 393 IdentifierInfo *ClassName, SourceLocation ClassLoc, 394 IdentifierInfo *CatName, SourceLocation CatLoc) { 395 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); 396 ObjCCategoryImplDecl *CDecl = 397 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl); 398 /// Check that class of this category is already completely declared. 399 if (!IDecl || IDecl->isForwardDecl()) 400 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); 401 402 /// TODO: Check that CatName, category name, is not used in another 403 // implementation. 404 return CDecl; 405} 406 407Sema::DeclTy *Sema::ActOnStartClassImplementation( 408 SourceLocation AtClassImplLoc, 409 IdentifierInfo *ClassName, SourceLocation ClassLoc, 410 IdentifierInfo *SuperClassname, 411 SourceLocation SuperClassLoc) { 412 ObjCInterfaceDecl* IDecl = 0; 413 // Check for another declaration kind with the same name. 414 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); 415 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 416 Diag(ClassLoc, diag::err_redefinition_different_kind, 417 ClassName->getName()); 418 Diag(PrevDecl->getLocation(), diag::err_previous_definition); 419 } 420 else { 421 // Is there an interface declaration of this class; if not, warn! 422 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 423 if (!IDecl) 424 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName()); 425 } 426 427 // Check that super class name is valid class name 428 ObjCInterfaceDecl* SDecl = 0; 429 if (SuperClassname) { 430 // Check if a different kind of symbol declared in this scope. 431 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope); 432 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 433 Diag(SuperClassLoc, diag::err_redefinition_different_kind, 434 SuperClassname->getName()); 435 Diag(PrevDecl->getLocation(), diag::err_previous_definition); 436 } 437 else { 438 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 439 if (!SDecl) 440 Diag(SuperClassLoc, diag::err_undef_superclass, 441 SuperClassname->getName(), ClassName->getName()); 442 else if (IDecl && IDecl->getSuperClass() != SDecl) { 443 // This implementation and its interface do not have the same 444 // super class. 445 Diag(SuperClassLoc, diag::err_conflicting_super_class, 446 SDecl->getName()); 447 Diag(SDecl->getLocation(), diag::err_previous_definition); 448 } 449 } 450 } 451 452 if (!IDecl) { 453 // Legacy case of @implementation with no corresponding @interface. 454 // Build, chain & install the interface decl into the identifier. 455 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName, 456 ClassLoc, false, true); 457 ObjCInterfaceDecls[ClassName] = IDecl; 458 IDecl->setSuperClass(SDecl); 459 IDecl->setLocEnd(ClassLoc); 460 461 // Remember that this needs to be removed when the scope is popped. 462 TUScope->AddDecl(IDecl); 463 } 464 465 ObjCImplementationDecl* IMPDecl = 466 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName, 467 IDecl, SDecl); 468 469 // Check that there is no duplicate implementation of this class. 470 if (ObjCImplementations[ClassName]) 471 // FIXME: Don't leak everything! 472 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName()); 473 else // add it to the list. 474 ObjCImplementations[ClassName] = IMPDecl; 475 return IMPDecl; 476} 477 478void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, 479 ObjCIvarDecl **ivars, unsigned numIvars, 480 SourceLocation RBrace) { 481 assert(ImpDecl && "missing implementation decl"); 482 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier()); 483 if (!IDecl) 484 return; 485 /// Check case of non-existing @interface decl. 486 /// (legacy objective-c @implementation decl without an @interface decl). 487 /// Add implementations's ivar to the synthesize class's ivar list. 488 if (IDecl->ImplicitInterfaceDecl()) { 489 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace); 490 return; 491 } 492 // If implementation has empty ivar list, just return. 493 if (numIvars == 0) 494 return; 495 496 assert(ivars && "missing @implementation ivars"); 497 498 // Check interface's Ivar list against those in the implementation. 499 // names and types must match. 500 // 501 unsigned j = 0; 502 ObjCInterfaceDecl::ivar_iterator 503 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); 504 for (; numIvars > 0 && IVI != IVE; ++IVI) { 505 ObjCIvarDecl* ImplIvar = ivars[j++]; 506 ObjCIvarDecl* ClsIvar = *IVI; 507 assert (ImplIvar && "missing implementation ivar"); 508 assert (ClsIvar && "missing class ivar"); 509 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) { 510 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type, 511 ImplIvar->getIdentifier()->getName()); 512 Diag(ClsIvar->getLocation(), diag::err_previous_definition, 513 ClsIvar->getIdentifier()->getName()); 514 } 515 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed 516 // as error. 517 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { 518 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name, 519 ImplIvar->getIdentifier()->getName()); 520 Diag(ClsIvar->getLocation(), diag::err_previous_definition, 521 ClsIvar->getIdentifier()->getName()); 522 return; 523 } 524 --numIvars; 525 } 526 527 if (numIvars > 0) 528 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); 529 else if (IVI != IVE) 530 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); 531} 532 533void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, 534 bool &IncompleteImpl) { 535 if (!IncompleteImpl) { 536 Diag(ImpLoc, diag::warn_incomplete_impl); 537 IncompleteImpl = true; 538 } 539 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName()); 540} 541 542/// CheckProtocolMethodDefs - This routine checks unimplemented methods 543/// Declared in protocol, and those referenced by it. 544void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, 545 ObjCProtocolDecl *PDecl, 546 bool& IncompleteImpl, 547 const llvm::DenseSet<Selector> &InsMap, 548 const llvm::DenseSet<Selector> &ClsMap) { 549 // check unimplemented instance methods. 550 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), 551 E = PDecl->instmeth_end(); I != E; ++I) { 552 ObjCMethodDecl *method = *I; 553 if (!InsMap.count(method->getSelector()) && 554 method->getImplementationControl() != ObjCMethodDecl::Optional) 555 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); 556 } 557 // check unimplemented class methods 558 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), 559 E = PDecl->classmeth_end(); I != E; ++I) { 560 ObjCMethodDecl *method = *I; 561 if (!ClsMap.count(method->getSelector()) && 562 method->getImplementationControl() != ObjCMethodDecl::Optional) 563 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); 564 } 565 // Check on this protocols's referenced protocols, recursively 566 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols(); 567 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++) 568 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap); 569} 570 571void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl, 572 ObjCInterfaceDecl* IDecl) { 573 llvm::DenseSet<Selector> InsMap; 574 // Check and see if instance methods in class interface have been 575 // implemented in the implementation class. 576 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(), 577 E = IMPDecl->instmeth_end(); I != E; ++I) 578 InsMap.insert((*I)->getSelector()); 579 580 bool IncompleteImpl = false; 581 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(), 582 E = IDecl->instmeth_end(); I != E; ++I) 583 if (!InsMap.count((*I)->getSelector())) 584 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); 585 586 llvm::DenseSet<Selector> ClsMap; 587 // Check and see if class methods in class interface have been 588 // implemented in the implementation class. 589 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(), 590 E = IMPDecl->classmeth_end(); I != E; ++I) 591 ClsMap.insert((*I)->getSelector()); 592 593 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(), 594 E = IDecl->classmeth_end(); I != E; ++I) 595 if (!ClsMap.count((*I)->getSelector())) 596 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); 597 598 // Check the protocol list for unimplemented methods in the @implementation 599 // class. 600 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols(); 601 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) 602 CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i], 603 IncompleteImpl, InsMap, ClsMap); 604} 605 606/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the 607/// category interface is implemented in the category @implementation. 608void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl, 609 ObjCCategoryDecl *CatClassDecl) { 610 llvm::DenseSet<Selector> InsMap; 611 // Check and see if instance methods in category interface have been 612 // implemented in its implementation class. 613 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(), 614 E = CatImplDecl->instmeth_end(); I != E; ++I) 615 InsMap.insert((*I)->getSelector()); 616 617 bool IncompleteImpl = false; 618 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(), 619 E = CatClassDecl->instmeth_end(); I != E; ++I) 620 if (!InsMap.count((*I)->getSelector())) 621 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); 622 623 llvm::DenseSet<Selector> ClsMap; 624 // Check and see if class methods in category interface have been 625 // implemented in its implementation class. 626 for (ObjCCategoryImplDecl::classmeth_iterator 627 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end(); 628 I != E; ++I) 629 ClsMap.insert((*I)->getSelector()); 630 631 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(), 632 E = CatClassDecl->classmeth_end(); I != E; ++I) 633 if (!ClsMap.count((*I)->getSelector())) 634 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); 635 636 // Check the protocol list for unimplemented methods in the @implementation 637 // class. 638 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols(); 639 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) { 640 ObjCProtocolDecl* PDecl = protocols[i]; 641 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl, 642 InsMap, ClsMap); 643 } 644} 645 646/// ActOnForwardClassDeclaration - 647Action::DeclTy * 648Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, 649 IdentifierInfo **IdentList, unsigned NumElts) 650{ 651 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; 652 653 for (unsigned i = 0; i != NumElts; ++i) { 654 // Check for another declaration kind with the same name. 655 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope); 656 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 657 Diag(AtClassLoc, diag::err_redefinition_different_kind, 658 IdentList[i]->getName()); 659 Diag(PrevDecl->getLocation(), diag::err_previous_definition); 660 } 661 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 662 if (!IDecl) { // Not already seen? Make a forward decl. 663 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, 0, IdentList[i], 664 SourceLocation(), true); 665 ObjCInterfaceDecls[IdentList[i]] = IDecl; 666 667 // Remember that this needs to be removed when the scope is popped. 668 TUScope->AddDecl(IDecl); 669 } 670 671 Interfaces.push_back(IDecl); 672 } 673 674 return ObjCClassDecl::Create(Context, AtClassLoc, 675 &Interfaces[0], Interfaces.size()); 676} 677 678 679/// MatchTwoMethodDeclarations - Checks that two methods have matching type and 680/// returns true, or false, accordingly. 681/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons 682bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, 683 const ObjCMethodDecl *PrevMethod) { 684 if (Method->getResultType().getCanonicalType() != 685 PrevMethod->getResultType().getCanonicalType()) 686 return false; 687 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) { 688 ParmVarDecl *ParamDecl = Method->getParamDecl(i); 689 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i); 690 if (Context.getCanonicalType(ParamDecl->getType()) != 691 Context.getCanonicalType(PrevParamDecl->getType())) 692 return false; 693 } 694 return true; 695} 696 697void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) { 698 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()]; 699 if (!FirstMethod.Method) { 700 // Haven't seen a method with this selector name yet - add it. 701 FirstMethod.Method = Method; 702 FirstMethod.Next = 0; 703 } else { 704 // We've seen a method with this name, now check the type signature(s). 705 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); 706 707 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; 708 Next = Next->Next) 709 match = MatchTwoMethodDeclarations(Method, Next->Method); 710 711 if (!match) { 712 // We have a new signature for an existing method - add it. 713 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". 714 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); 715 FirstMethod.Next = OMI; 716 } 717 } 718} 719 720void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { 721 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()]; 722 if (!FirstMethod.Method) { 723 // Haven't seen a method with this selector name yet - add it. 724 FirstMethod.Method = Method; 725 FirstMethod.Next = 0; 726 } else { 727 // We've seen a method with this name, now check the type signature(s). 728 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); 729 730 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; 731 Next = Next->Next) 732 match = MatchTwoMethodDeclarations(Method, Next->Method); 733 734 if (!match) { 735 // We have a new signature for an existing method - add it. 736 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". 737 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); 738 FirstMethod.Next = OMI; 739 } 740 } 741} 742 743// Note: For class/category implemenations, allMethods/allProperties is 744// always null. 745void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl, 746 DeclTy **allMethods, unsigned allNum, 747 DeclTy **allProperties, unsigned pNum) { 748 Decl *ClassDecl = static_cast<Decl *>(classDecl); 749 750 // FIXME: If we don't have a ClassDecl, we have an error. We should consider 751 // always passing in a decl. If the decl has an error, isInvalidDecl() 752 // should be true. 753 if (!ClassDecl) 754 return; 755 756 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods; 757 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods; 758 759 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; 760 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; 761 762 bool isInterfaceDeclKind = 763 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) 764 || isa<ObjCProtocolDecl>(ClassDecl); 765 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); 766 767 if (pNum != 0) 768 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) 769 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); 770 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) 771 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); 772 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl)) 773 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); 774 else 775 assert(false && "ActOnAtEnd - property declaration misplaced"); 776 777 for (unsigned i = 0; i < allNum; i++ ) { 778 ObjCMethodDecl *Method = 779 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i])); 780 781 if (!Method) continue; // Already issued a diagnostic. 782 if (Method->isInstance()) { 783 /// Check for instance method of the same name with incompatible types 784 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; 785 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 786 : false; 787 if (isInterfaceDeclKind && PrevMethod && !match 788 || checkIdenticalMethods && match) { 789 Diag(Method->getLocation(), diag::error_duplicate_method_decl, 790 Method->getSelector().getName()); 791 Diag(PrevMethod->getLocation(), diag::err_previous_declaration); 792 } else { 793 insMethods.push_back(Method); 794 InsMap[Method->getSelector()] = Method; 795 /// The following allows us to typecheck messages to "id". 796 AddInstanceMethodToGlobalPool(Method); 797 } 798 } 799 else { 800 /// Check for class method of the same name with incompatible types 801 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; 802 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 803 : false; 804 if (isInterfaceDeclKind && PrevMethod && !match 805 || checkIdenticalMethods && match) { 806 Diag(Method->getLocation(), diag::error_duplicate_method_decl, 807 Method->getSelector().getName()); 808 Diag(PrevMethod->getLocation(), diag::err_previous_declaration); 809 } else { 810 clsMethods.push_back(Method); 811 ClsMap[Method->getSelector()] = Method; 812 /// The following allows us to typecheck messages to "Class". 813 AddFactoryMethodToGlobalPool(Method); 814 } 815 } 816 } 817 818 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 819 I->addMethods(&insMethods[0], insMethods.size(), 820 &clsMethods[0], clsMethods.size(), AtEndLoc); 821 // Compares properties declaraed in this class to those of its 822 // super class. 823 ComparePropertiesInBaseAndSuper (I); 824 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) { 825 P->addMethods(&insMethods[0], insMethods.size(), 826 &clsMethods[0], clsMethods.size(), AtEndLoc); 827 } 828 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { 829 C->addMethods(&insMethods[0], insMethods.size(), 830 &clsMethods[0], clsMethods.size(), AtEndLoc); 831 } 832 else if (ObjCImplementationDecl *IC = 833 dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 834 IC->setLocEnd(AtEndLoc); 835 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier())) 836 ImplMethodsVsClassMethods(IC, IDecl); 837 } else { 838 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl); 839 CatImplClass->setLocEnd(AtEndLoc); 840 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface(); 841 // Find category interface decl and then check that all methods declared 842 // in this interface is implemented in the category @implementation. 843 if (IDecl) { 844 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); 845 Categories; Categories = Categories->getNextClassCategory()) { 846 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { 847 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories); 848 break; 849 } 850 } 851 } 852 } 853} 854 855 856/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for 857/// objective-c's type qualifier from the parser version of the same info. 858static Decl::ObjCDeclQualifier 859CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { 860 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; 861 if (PQTVal & ObjCDeclSpec::DQ_In) 862 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); 863 if (PQTVal & ObjCDeclSpec::DQ_Inout) 864 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); 865 if (PQTVal & ObjCDeclSpec::DQ_Out) 866 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); 867 if (PQTVal & ObjCDeclSpec::DQ_Bycopy) 868 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); 869 if (PQTVal & ObjCDeclSpec::DQ_Byref) 870 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); 871 if (PQTVal & ObjCDeclSpec::DQ_Oneway) 872 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); 873 874 return ret; 875} 876 877Sema::DeclTy *Sema::ActOnMethodDeclaration( 878 SourceLocation MethodLoc, SourceLocation EndLoc, 879 tok::TokenKind MethodType, DeclTy *classDecl, 880 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, 881 Selector Sel, 882 // optional arguments. The number of types/arguments is obtained 883 // from the Sel.getNumArgs(). 884 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames, 885 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, 886 bool isVariadic) { 887 Decl *ClassDecl = static_cast<Decl*>(classDecl); 888 889 // Make sure we can establish a context for the method. 890 if (!ClassDecl) { 891 Diag(MethodLoc, diag::error_missing_method_context); 892 return 0; 893 } 894 QualType resultDeclType; 895 896 if (ReturnType) 897 resultDeclType = QualType::getFromOpaquePtr(ReturnType); 898 else // get the type for "id". 899 resultDeclType = Context.getObjCIdType(); 900 901 ObjCMethodDecl* ObjCMethod = 902 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, 903 ClassDecl, AttrList, 904 MethodType == tok::minus, isVariadic, 905 MethodDeclKind == tok::objc_optional ? 906 ObjCMethodDecl::Optional : 907 ObjCMethodDecl::Required); 908 909 llvm::SmallVector<ParmVarDecl*, 16> Params; 910 911 for (unsigned i = 0; i < Sel.getNumArgs(); i++) { 912 // FIXME: arg->AttrList must be stored too! 913 QualType argType; 914 915 if (ArgTypes[i]) 916 argType = QualType::getFromOpaquePtr(ArgTypes[i]); 917 else 918 argType = Context.getObjCIdType(); 919 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod, 920 SourceLocation(/*FIXME*/), 921 ArgNames[i], argType, 922 VarDecl::None, 0, 0); 923 Param->setObjCDeclQualifier( 924 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier())); 925 Params.push_back(Param); 926 } 927 928 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs()); 929 ObjCMethod->setObjCDeclQualifier( 930 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); 931 const ObjCMethodDecl *PrevMethod = 0; 932 933 // For implementations (which can be very "coarse grain"), we add the 934 // method now. This allows the AST to implement lookup methods that work 935 // incrementally (without waiting until we parse the @end). It also allows 936 // us to flag multiple declaration errors as they occur. 937 if (ObjCImplementationDecl *ImpDecl = 938 dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 939 if (MethodType == tok::minus) { 940 PrevMethod = ImpDecl->getInstanceMethod(Sel); 941 ImpDecl->addInstanceMethod(ObjCMethod); 942 } else { 943 PrevMethod = ImpDecl->getClassMethod(Sel); 944 ImpDecl->addClassMethod(ObjCMethod); 945 } 946 } 947 else if (ObjCCategoryImplDecl *CatImpDecl = 948 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { 949 if (MethodType == tok::minus) { 950 PrevMethod = CatImpDecl->getInstanceMethod(Sel); 951 CatImpDecl->addInstanceMethod(ObjCMethod); 952 } else { 953 PrevMethod = CatImpDecl->getClassMethod(Sel); 954 CatImpDecl->addClassMethod(ObjCMethod); 955 } 956 } 957 if (PrevMethod) { 958 // You can never have two method definitions with the same name. 959 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl, 960 ObjCMethod->getSelector().getName()); 961 Diag(PrevMethod->getLocation(), diag::err_previous_declaration); 962 } 963 return ObjCMethod; 964} 965 966Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, 967 FieldDeclarator &FD, 968 ObjCDeclSpec &ODS) { 969 QualType T = GetTypeForDeclarator(FD.D, S); 970 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc, 971 FD.D.getIdentifier(), T); 972 973 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly) 974 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); 975 976 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) { 977 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); 978 PDecl->setGetterName(ODS.getGetterName()); 979 } 980 981 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) { 982 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); 983 PDecl->setSetterName(ODS.getSetterName()); 984 } 985 986 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign) 987 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); 988 989 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite) 990 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); 991 992 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain) 993 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 994 995 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy) 996 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 997 998 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic) 999 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); 1000 1001 return PDecl; 1002} 1003 1004/// ActOnPropertyImplDecl - This routine performs semantic checks and 1005/// builds the AST node for a property implementation declaration; declared 1006/// as @synthesize or @dynamic. 1007/// 1008Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc, 1009 SourceLocation PropertyLoc, 1010 bool Synthesize, 1011 DeclTy *ClassCatImpDecl, 1012 IdentifierInfo *PropertyId, 1013 IdentifierInfo *PropertyIvar) { 1014 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl); 1015 // Make sure we have a context for the property implementation declaration. 1016 if (!ClassImpDecl) { 1017 Diag(AtLoc, diag::error_missing_property_context); 1018 return 0; 1019 } 1020 ObjCPropertyDecl *property = 0; 1021 ObjCInterfaceDecl* IDecl = 0; 1022 // Find the class or category class where this property must have 1023 // a declaration. 1024 ObjCImplementationDecl *IC = 0; 1025 ObjCCategoryImplDecl* CatImplClass = 0; 1026 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { 1027 IDecl = getObjCInterfaceDecl(IC->getIdentifier()); 1028 // We always synthesize an interface for an implementation 1029 // without an interface decl. So, IDecl is always non-zero. 1030 assert(IDecl && 1031 "ActOnPropertyImplDecl - @implementation without @interface"); 1032 1033 // Look for this property declaration in the @implementation's @interface 1034 property = IDecl->FindPropertyDeclaration(PropertyId); 1035 if (!property) { 1036 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName()); 1037 return 0; 1038 } 1039 } 1040 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { 1041 if (Synthesize) { 1042 Diag(AtLoc, diag::error_synthesize_category_decl); 1043 return 0; 1044 } 1045 IDecl = CatImplClass->getClassInterface(); 1046 if (!IDecl) { 1047 Diag(AtLoc, diag::error_missing_property_interface); 1048 return 0; 1049 } 1050 ObjCCategoryDecl *Category = 1051 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); 1052 1053 // If category for this implementation not found, it is an error which 1054 // has already been reported eralier. 1055 if (!Category) 1056 return 0; 1057 // Look for this property declaration in @implementation's category 1058 property = Category->FindPropertyDeclaration(PropertyId); 1059 if (!property) { 1060 Diag(PropertyLoc, diag::error_bad_category_property_decl, 1061 Category->getName()); 1062 return 0; 1063 } 1064 } 1065 else { 1066 Diag(AtLoc, diag::error_bad_property_context); 1067 return 0; 1068 } 1069 ObjCIvarDecl *Ivar = 0; 1070 // Check that we have a valid, previously declared ivar for @synthesize 1071 if (Synthesize) { 1072 // @synthesize 1073 if (!PropertyIvar) 1074 PropertyIvar = PropertyId; 1075 // Check that this is a previously declared 'ivar' in 'IDecl' interface 1076 Ivar = IDecl->FindIvarDeclaration(PropertyIvar); 1077 if (!Ivar) { 1078 Diag(PropertyLoc, diag::error_missing_property_ivar_decl, 1079 PropertyId->getName()); 1080 return 0; 1081 } 1082 // Check that type of property and its ivar match. 1083 if (Ivar->getCanonicalType() != property->getCanonicalType()) { 1084 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(), 1085 Ivar->getName()); 1086 return 0; 1087 } 1088 1089 } else if (PropertyIvar) { 1090 // @dynamic 1091 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); 1092 return 0; 1093 } 1094 assert (property && "ActOnPropertyImplDecl - property declaration missing"); 1095 ObjCPropertyImplDecl *PIDecl = 1096 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property, 1097 (Synthesize ? 1098 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE 1099 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC), 1100 Ivar); 1101 if (IC) 1102 IC->addPropertyImplementation(PIDecl); 1103 else 1104 CatImplClass->addPropertyImplementation(PIDecl); 1105 1106 return PIDecl; 1107} 1108 1109