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