SemaDeclObjC.cpp revision e705e5ec34e4ab296c2a3831505fde62270fb778
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:: 72ActOnStartClassInterface(SourceLocation AtInterfaceLoc, 73 IdentifierInfo *ClassName, SourceLocation ClassLoc, 74 IdentifierInfo *SuperName, SourceLocation SuperLoc, 75 const IdentifierLocPair *ProtocolNames, 76 unsigned NumProtocols, 77 SourceLocation EndProtoLoc, AttributeList *AttrList) { 78 assert(ClassName && "Missing class identifier"); 79 80 // Check for another declaration kind with the same name. 81 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); 82 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 83 Diag(ClassLoc, diag::err_redefinition_different_kind, 84 ClassName->getName()); 85 Diag(PrevDecl->getLocation(), diag::err_previous_definition); 86 } 87 88 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 89 if (IDecl) { 90 // Class already seen. Is it a forward declaration? 91 if (!IDecl->isForwardDecl()) 92 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName()); 93 else { 94 IDecl->setLocation(AtInterfaceLoc); 95 IDecl->setForwardDecl(false); 96 } 97 } else { 98 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, 99 ClassName, ClassLoc); 100 101 ObjCInterfaceDecls[ClassName] = IDecl; 102 // Remember that this needs to be removed when the scope is popped. 103 TUScope->AddDecl(IDecl); 104 } 105 106 if (SuperName) { 107 ObjCInterfaceDecl* SuperClassEntry = 0; 108 // Check if a different kind of symbol declared in this scope. 109 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope); 110 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 111 Diag(SuperLoc, diag::err_redefinition_different_kind, 112 SuperName->getName()); 113 Diag(PrevDecl->getLocation(), diag::err_previous_definition); 114 } 115 else { 116 // Check that super class is previously defined 117 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 118 119 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) { 120 Diag(SuperLoc, diag::err_undef_superclass, 121 SuperClassEntry ? SuperClassEntry->getName() 122 : SuperName->getName(), 123 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc)); 124 } 125 } 126 IDecl->setSuperClass(SuperClassEntry); 127 IDecl->setSuperClassLoc(SuperLoc); 128 IDecl->setLocEnd(SuperLoc); 129 } else { // we have a root class. 130 IDecl->setLocEnd(ClassLoc); 131 } 132 133 /// Check then save referenced protocols 134 if (NumProtocols) { 135 llvm::SmallVector<ObjCProtocolDecl*, 8> RefProtos; 136 for (unsigned int i = 0; i != NumProtocols; i++) { 137 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i].first]; 138 if (!RefPDecl) 139 Diag(ProtocolNames[i].second, diag::err_undef_protocolref, 140 ProtocolNames[i].first->getName(), ClassName->getName()); 141 else { 142 if (RefPDecl->isForwardDecl()) 143 Diag(ProtocolNames[i].second, diag::warn_undef_protocolref, 144 ProtocolNames[i].first->getName(), ClassName->getName()); 145 RefProtos.push_back(RefPDecl); 146 } 147 } 148 if (!RefProtos.empty()) 149 IDecl->addReferencedProtocols(&RefProtos[0], RefProtos.size()); 150 IDecl->setLocEnd(EndProtoLoc); 151 } 152 return IDecl; 153} 154 155/// ActOnCompatiblityAlias - this action is called after complete parsing of 156/// @compaatibility_alias declaration. It sets up the alias relationships. 157Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, 158 IdentifierInfo *AliasName, 159 SourceLocation AliasLocation, 160 IdentifierInfo *ClassName, 161 SourceLocation ClassLocation) { 162 // Look for previous declaration of alias name 163 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope); 164 if (ADecl) { 165 if (isa<ObjCCompatibleAliasDecl>(ADecl)) { 166 Diag(AliasLocation, diag::warn_previous_alias_decl); 167 Diag(ADecl->getLocation(), diag::warn_previous_declaration); 168 } 169 else { 170 Diag(AliasLocation, diag::err_conflicting_aliasing_type, 171 AliasName->getName()); 172 Diag(ADecl->getLocation(), diag::err_previous_declaration); 173 } 174 return 0; 175 } 176 // Check for class declaration 177 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); 178 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); 179 if (CDecl == 0) { 180 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName()); 181 if (CDeclU) 182 Diag(CDeclU->getLocation(), diag::warn_previous_declaration); 183 return 0; 184 } 185 186 // Everything checked out, instantiate a new alias declaration AST. 187 ObjCCompatibleAliasDecl *AliasDecl = 188 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl); 189 190 ObjCAliasDecls[AliasName] = AliasDecl; 191 TUScope->AddDecl(AliasDecl); 192 return AliasDecl; 193} 194 195Sema::DeclTy *Sema::ActOnStartProtocolInterface( 196 SourceLocation AtProtoInterfaceLoc, 197 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc, 198 const IdentifierLocPair *ProtoRefNames, unsigned NumProtoRefs, 199 SourceLocation EndProtoLoc) { 200 assert(ProtocolName && "Missing protocol identifier"); 201 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName]; 202 if (PDecl) { 203 // Protocol already seen. Better be a forward protocol declaration 204 if (!PDecl->isForwardDecl()) { 205 Diag(ProtocolLoc, diag::err_duplicate_protocol_def, 206 ProtocolName->getName()); 207 // Just return the protocol we already had. 208 // FIXME: don't leak the objects passed in! 209 return PDecl; 210 } 211 212 PDecl->setForwardDecl(false); 213 } else { 214 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName); 215 PDecl->setForwardDecl(false); 216 ObjCProtocols[ProtocolName] = PDecl; 217 } 218 219 if (NumProtoRefs) { 220 /// Check then save referenced protocols. 221 llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols; 222 for (unsigned int i = 0; i != NumProtoRefs; i++) { 223 ObjCProtocolDecl *RefPDecl = ObjCProtocols[ProtoRefNames[i].first]; 224 if (!RefPDecl) 225 Diag(ProtoRefNames[i].second, diag::err_undef_protocolref, 226 ProtoRefNames[i].first->getName(), ProtocolName->getName()); 227 else { 228 if (RefPDecl->isForwardDecl()) 229 Diag(ProtoRefNames[i].second, diag::warn_undef_protocolref, 230 ProtoRefNames[i].first->getName(), ProtocolName->getName()); 231 Protocols.push_back(RefPDecl); 232 } 233 } 234 if (!Protocols.empty()) 235 PDecl->addReferencedProtocols(&Protocols[0], Protocols.size()); 236 PDecl->setLocEnd(EndProtoLoc); 237 } 238 return PDecl; 239} 240 241/// FindProtocolDeclaration - This routine looks up protocols and 242/// issuer error if they are not declared. It returns list of protocol 243/// declarations in its 'Protocols' argument. 244void 245Sema::FindProtocolDeclaration(SourceLocation TypeLoc, 246 const IdentifierLocPair *ProtocolId, 247 unsigned NumProtocols, 248 llvm::SmallVectorImpl<DeclTy*> &Protocols) { 249 for (unsigned i = 0; i != NumProtocols; ++i) { 250 if (ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first]) 251 Protocols.push_back(PDecl); 252 else 253 Diag(ProtocolId[i].second, diag::err_undeclared_protocol, 254 ProtocolId[i].first->getName()); 255 } 256} 257 258/// DiagnosePropertyMismatch - Compares two properties for their 259/// attributes and types and warns on a variety of inconsistancies. 260/// 261void 262Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, 263 ObjCPropertyDecl *SuperProperty, 264 const char *inheritedName) { 265 ObjCPropertyDecl::PropertyAttributeKind CAttr = 266 Property->getPropertyAttributes(); 267 ObjCPropertyDecl::PropertyAttributeKind SAttr = 268 SuperProperty->getPropertyAttributes(); 269 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) 270 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) 271 Diag(Property->getLocation(), diag::warn_readonly_property, 272 Property->getName(), inheritedName); 273 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) 274 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) 275 Diag(Property->getLocation(), diag::warn_property_attribute, 276 Property->getName(), "copy", inheritedName, 277 SourceRange()); 278 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain) 279 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain)) 280 Diag(Property->getLocation(), diag::warn_property_attribute, 281 Property->getName(), "retain", inheritedName, 282 SourceRange()); 283 284 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) 285 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) 286 Diag(Property->getLocation(), diag::warn_property_attribute, 287 Property->getName(), "atomic", inheritedName, 288 SourceRange()); 289 if (Property->getSetterName() != SuperProperty->getSetterName()) 290 Diag(Property->getLocation(), diag::warn_property_attribute, 291 Property->getName(), "setter", inheritedName, 292 SourceRange()); 293 if (Property->getGetterName() != SuperProperty->getGetterName()) 294 Diag(Property->getLocation(), diag::warn_property_attribute, 295 Property->getName(), "getter", inheritedName, 296 SourceRange()); 297 298 if (Property->getCanonicalType() != SuperProperty->getCanonicalType()) 299 Diag(Property->getLocation(), diag::warn_property_type, 300 Property->getType().getAsString(), 301 inheritedName); 302 303} 304 305/// ComparePropertiesInBaseAndSuper - This routine compares property 306/// declarations in base and its super class, if any, and issues 307/// diagnostics in a variety of inconsistant situations. 308/// 309void 310Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { 311 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); 312 if (!SDecl) 313 return; 314 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(), 315 E = SDecl->classprop_end(); S != E; ++S) { 316 ObjCPropertyDecl *SuperPDecl = (*S); 317 // Does property in super class has declaration in current class? 318 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(), 319 E = IDecl->classprop_end(); I != E; ++I) { 320 ObjCPropertyDecl *PDecl = (*I); 321 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) 322 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName()); 323 } 324 } 325} 326 327/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list 328/// of properties declared in a protocol and adds them to the list 329/// of properties for current class if it is not there already. 330void 331Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl, 332 ObjCProtocolDecl *PDecl) 333{ 334 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties; 335 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(), 336 E = PDecl->classprop_end(); P != E; ++P) { 337 ObjCPropertyDecl *Pr = (*P); 338 ObjCInterfaceDecl::classprop_iterator CP, CE; 339 // Is this property already in class's list of properties? 340 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end(); 341 CP != CE; ++CP) 342 if ((*CP)->getIdentifier() == Pr->getIdentifier()) 343 break; 344 if (CP == CE) 345 // Add this property to list of properties for thie class. 346 mergeProperties.push_back(Pr); 347 else 348 // Property protocol already exist in class. Diagnose any mismatch. 349 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName()); 350 } 351 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size()); 352} 353 354/// MergeProtocolPropertiesIntoClass - This routine merges properties 355/// declared in 'MergeItsProtocols' objects (which can be a class or an 356/// inherited protocol into the list of properties for class 'IDecl' 357/// 358 359void 360Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl, 361 DeclTy *MergeItsProtocols) { 362 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols); 363 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 364 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(), 365 E = MDecl->protocol_end(); P != E; ++P) 366 // Merge properties of class (*P) into IDECL's 367 MergeOneProtocolPropertiesIntoClass(IDecl, *P); 368 369 // Go thru the list of protocols for this class and recursively merge 370 // their properties into this class as well. 371 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(), 372 E = IDecl->protocol_end(); P != E; ++P) 373 MergeProtocolPropertiesIntoClass(IDecl, *P); 374 } else { 375 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); 376 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), 377 E = MD->protocol_end(); P != E; ++P) 378 MergeOneProtocolPropertiesIntoClass(IDecl, (*P)); 379 } 380} 381 382/// ActOnForwardProtocolDeclaration - 383Action::DeclTy * 384Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, 385 const IdentifierLocPair *IdentList, 386 unsigned NumElts) { 387 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; 388 389 for (unsigned i = 0; i != NumElts; ++i) { 390 IdentifierInfo *Ident = IdentList[i].first; 391 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident]; 392 if (PDecl == 0) // Not already seen? 393 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident); 394 395 Protocols.push_back(PDecl); 396 } 397 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc, 398 &Protocols[0], Protocols.size()); 399} 400 401Sema::DeclTy *Sema:: 402ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, 403 IdentifierInfo *ClassName, SourceLocation ClassLoc, 404 IdentifierInfo *CategoryName, 405 SourceLocation CategoryLoc, 406 const IdentifierLocPair *ProtoRefNames, 407 unsigned NumProtoRefs, 408 SourceLocation EndProtoLoc) { 409 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); 410 411 ObjCCategoryDecl *CDecl = 412 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName); 413 CDecl->setClassInterface(IDecl); 414 415 /// Check that class of this category is already completely declared. 416 if (!IDecl || IDecl->isForwardDecl()) 417 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); 418 else { 419 /// Check for duplicate interface declaration for this category 420 ObjCCategoryDecl *CDeclChain; 421 for (CDeclChain = IDecl->getCategoryList(); CDeclChain; 422 CDeclChain = CDeclChain->getNextClassCategory()) { 423 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) { 424 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(), 425 CategoryName->getName()); 426 break; 427 } 428 } 429 if (!CDeclChain) 430 CDecl->insertNextClassCategory(); 431 } 432 433 if (NumProtoRefs) { 434 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols; 435 /// Check and then save the referenced protocols. 436 for (unsigned int i = 0; i != NumProtoRefs; i++) { 437 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i].first]; 438 if (!RefPDecl) 439 Diag(ProtoRefNames[i].second, diag::err_undef_protocolref, 440 ProtoRefNames[i].first->getName(), CategoryName->getName()); 441 else { 442 if (RefPDecl->isForwardDecl()) 443 Diag(ProtoRefNames[i].second, diag::warn_undef_protocolref, 444 ProtoRefNames[i].first->getName(), CategoryName->getName()); 445 RefProtocols.push_back(RefPDecl); 446 } 447 } 448 if (!RefProtocols.empty()) 449 CDecl->addReferencedProtocols(&RefProtocols[0], RefProtocols.size()); 450 } 451 CDecl->setLocEnd(EndProtoLoc); 452 return CDecl; 453} 454 455/// ActOnStartCategoryImplementation - Perform semantic checks on the 456/// category implementation declaration and build an ObjCCategoryImplDecl 457/// object. 458Sema::DeclTy *Sema::ActOnStartCategoryImplementation( 459 SourceLocation AtCatImplLoc, 460 IdentifierInfo *ClassName, SourceLocation ClassLoc, 461 IdentifierInfo *CatName, SourceLocation CatLoc) { 462 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); 463 ObjCCategoryImplDecl *CDecl = 464 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl); 465 /// Check that class of this category is already completely declared. 466 if (!IDecl || IDecl->isForwardDecl()) 467 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); 468 469 /// TODO: Check that CatName, category name, is not used in another 470 // implementation. 471 return CDecl; 472} 473 474Sema::DeclTy *Sema::ActOnStartClassImplementation( 475 SourceLocation AtClassImplLoc, 476 IdentifierInfo *ClassName, SourceLocation ClassLoc, 477 IdentifierInfo *SuperClassname, 478 SourceLocation SuperClassLoc) { 479 ObjCInterfaceDecl* IDecl = 0; 480 // Check for another declaration kind with the same name. 481 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); 482 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 483 Diag(ClassLoc, diag::err_redefinition_different_kind, 484 ClassName->getName()); 485 Diag(PrevDecl->getLocation(), diag::err_previous_definition); 486 } 487 else { 488 // Is there an interface declaration of this class; if not, warn! 489 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 490 if (!IDecl) 491 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName()); 492 } 493 494 // Check that super class name is valid class name 495 ObjCInterfaceDecl* SDecl = 0; 496 if (SuperClassname) { 497 // Check if a different kind of symbol declared in this scope. 498 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope); 499 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 500 Diag(SuperClassLoc, diag::err_redefinition_different_kind, 501 SuperClassname->getName()); 502 Diag(PrevDecl->getLocation(), diag::err_previous_definition); 503 } 504 else { 505 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 506 if (!SDecl) 507 Diag(SuperClassLoc, diag::err_undef_superclass, 508 SuperClassname->getName(), ClassName->getName()); 509 else if (IDecl && IDecl->getSuperClass() != SDecl) { 510 // This implementation and its interface do not have the same 511 // super class. 512 Diag(SuperClassLoc, diag::err_conflicting_super_class, 513 SDecl->getName()); 514 Diag(SDecl->getLocation(), diag::err_previous_definition); 515 } 516 } 517 } 518 519 if (!IDecl) { 520 // Legacy case of @implementation with no corresponding @interface. 521 // Build, chain & install the interface decl into the identifier. 522 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName, 523 ClassLoc, false, true); 524 ObjCInterfaceDecls[ClassName] = IDecl; 525 IDecl->setSuperClass(SDecl); 526 IDecl->setLocEnd(ClassLoc); 527 528 // Remember that this needs to be removed when the scope is popped. 529 TUScope->AddDecl(IDecl); 530 } 531 532 ObjCImplementationDecl* IMPDecl = 533 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName, 534 IDecl, SDecl); 535 536 // Check that there is no duplicate implementation of this class. 537 if (ObjCImplementations[ClassName]) 538 // FIXME: Don't leak everything! 539 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName()); 540 else // add it to the list. 541 ObjCImplementations[ClassName] = IMPDecl; 542 return IMPDecl; 543} 544 545void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, 546 ObjCIvarDecl **ivars, unsigned numIvars, 547 SourceLocation RBrace) { 548 assert(ImpDecl && "missing implementation decl"); 549 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier()); 550 if (!IDecl) 551 return; 552 /// Check case of non-existing @interface decl. 553 /// (legacy objective-c @implementation decl without an @interface decl). 554 /// Add implementations's ivar to the synthesize class's ivar list. 555 if (IDecl->ImplicitInterfaceDecl()) { 556 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace); 557 return; 558 } 559 // If implementation has empty ivar list, just return. 560 if (numIvars == 0) 561 return; 562 563 assert(ivars && "missing @implementation ivars"); 564 565 // Check interface's Ivar list against those in the implementation. 566 // names and types must match. 567 // 568 unsigned j = 0; 569 ObjCInterfaceDecl::ivar_iterator 570 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); 571 for (; numIvars > 0 && IVI != IVE; ++IVI) { 572 ObjCIvarDecl* ImplIvar = ivars[j++]; 573 ObjCIvarDecl* ClsIvar = *IVI; 574 assert (ImplIvar && "missing implementation ivar"); 575 assert (ClsIvar && "missing class ivar"); 576 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) { 577 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type, 578 ImplIvar->getIdentifier()->getName()); 579 Diag(ClsIvar->getLocation(), diag::err_previous_definition, 580 ClsIvar->getIdentifier()->getName()); 581 } 582 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed 583 // as error. 584 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { 585 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name, 586 ImplIvar->getIdentifier()->getName()); 587 Diag(ClsIvar->getLocation(), diag::err_previous_definition, 588 ClsIvar->getIdentifier()->getName()); 589 return; 590 } 591 --numIvars; 592 } 593 594 if (numIvars > 0) 595 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); 596 else if (IVI != IVE) 597 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); 598} 599 600void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, 601 bool &IncompleteImpl) { 602 if (!IncompleteImpl) { 603 Diag(ImpLoc, diag::warn_incomplete_impl); 604 IncompleteImpl = true; 605 } 606 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName()); 607} 608 609/// CheckProtocolMethodDefs - This routine checks unimplemented methods 610/// Declared in protocol, and those referenced by it. 611void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, 612 ObjCProtocolDecl *PDecl, 613 bool& IncompleteImpl, 614 const llvm::DenseSet<Selector> &InsMap, 615 const llvm::DenseSet<Selector> &ClsMap) { 616 // check unimplemented instance methods. 617 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), 618 E = PDecl->instmeth_end(); I != E; ++I) { 619 ObjCMethodDecl *method = *I; 620 if (!InsMap.count(method->getSelector()) && 621 method->getImplementationControl() != ObjCMethodDecl::Optional) 622 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); 623 } 624 // check unimplemented class methods 625 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), 626 E = PDecl->classmeth_end(); I != E; ++I) { 627 ObjCMethodDecl *method = *I; 628 if (!ClsMap.count(method->getSelector()) && 629 method->getImplementationControl() != ObjCMethodDecl::Optional) 630 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); 631 } 632 // Check on this protocols's referenced protocols, recursively. 633 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), 634 E = PDecl->protocol_end(); PI != E; ++PI) 635 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap); 636} 637 638void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl, 639 ObjCInterfaceDecl* IDecl) { 640 llvm::DenseSet<Selector> InsMap; 641 // Check and see if instance methods in class interface have been 642 // implemented in the implementation class. 643 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(), 644 E = IMPDecl->instmeth_end(); I != E; ++I) 645 InsMap.insert((*I)->getSelector()); 646 647 bool IncompleteImpl = false; 648 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(), 649 E = IDecl->instmeth_end(); I != E; ++I) 650 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) 651 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); 652 653 llvm::DenseSet<Selector> ClsMap; 654 // Check and see if class methods in class interface have been 655 // implemented in the implementation class. 656 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(), 657 E = IMPDecl->classmeth_end(); I != E; ++I) 658 ClsMap.insert((*I)->getSelector()); 659 660 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(), 661 E = IDecl->classmeth_end(); I != E; ++I) 662 if (!ClsMap.count((*I)->getSelector())) 663 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); 664 665 // Check the protocol list for unimplemented methods in the @implementation 666 // class. 667 const ObjCList<ObjCProtocolDecl> &Protocols = 668 IDecl->getReferencedProtocols(); 669 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 670 E = Protocols.end(); I != E; ++I) 671 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I, 672 IncompleteImpl, InsMap, ClsMap); 673} 674 675/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the 676/// category interface is implemented in the category @implementation. 677void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl, 678 ObjCCategoryDecl *CatClassDecl) { 679 llvm::DenseSet<Selector> InsMap; 680 // Check and see if instance methods in category interface have been 681 // implemented in its implementation class. 682 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(), 683 E = CatImplDecl->instmeth_end(); I != E; ++I) 684 InsMap.insert((*I)->getSelector()); 685 686 bool IncompleteImpl = false; 687 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(), 688 E = CatClassDecl->instmeth_end(); I != E; ++I) 689 if (!InsMap.count((*I)->getSelector())) 690 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); 691 692 llvm::DenseSet<Selector> ClsMap; 693 // Check and see if class methods in category interface have been 694 // implemented in its implementation class. 695 for (ObjCCategoryImplDecl::classmeth_iterator 696 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end(); 697 I != E; ++I) 698 ClsMap.insert((*I)->getSelector()); 699 700 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(), 701 E = CatClassDecl->classmeth_end(); I != E; ++I) 702 if (!ClsMap.count((*I)->getSelector())) 703 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); 704 705 // Check the protocol list for unimplemented methods in the @implementation 706 // class. 707 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(), 708 E = CatClassDecl->protocol_end(); PI != E; ++PI) 709 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl, 710 InsMap, ClsMap); 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