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