SemaDeclObjC.cpp revision c9c7c4e06bba5dce053162ea1ead5743d7bba35b
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/Basic/Diagnostic.h" 18#include "clang/Parse/DeclSpec.h" 19 20using namespace clang; 21 22/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible 23/// and user declared, in the method definition's AST. 24void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) { 25 assert(getCurMethodDecl() == 0 && "Method parsing confused"); 26 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D); 27 28 // If we don't have a valid method decl, simply return. 29 if (!MDecl) 30 return; 31 32 // Allow the rest of sema to find private method decl implementations. 33 if (MDecl->isInstance()) 34 AddInstanceMethodToGlobalPool(MDecl); 35 else 36 AddFactoryMethodToGlobalPool(MDecl); 37 38 // Allow all of Sema to see that we are entering a method definition. 39 PushDeclContext(MDecl); 40 41 // Create Decl objects for each parameter, entrring them in the scope for 42 // binding to their use. 43 44 // Insert the invisible arguments, self and _cmd! 45 MDecl->createImplicitParams(Context); 46 47 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); 48 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); 49 50 // Introduce all of the other parameters into this scope. 51 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) { 52 ParmVarDecl *PDecl = MDecl->getParamDecl(i); 53 IdentifierInfo *II = PDecl->getIdentifier(); 54 if (II) 55 PushOnScopeChains(PDecl, FnBodyScope); 56 } 57} 58 59Sema::DeclTy *Sema:: 60ActOnStartClassInterface(SourceLocation AtInterfaceLoc, 61 IdentifierInfo *ClassName, SourceLocation ClassLoc, 62 IdentifierInfo *SuperName, SourceLocation SuperLoc, 63 DeclTy * const *ProtoRefs, unsigned NumProtoRefs, 64 SourceLocation EndProtoLoc, AttributeList *AttrList) { 65 assert(ClassName && "Missing class identifier"); 66 67 // Check for another declaration kind with the same name. 68 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); 69 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 70 Diag(ClassLoc, diag::err_redefinition_different_kind, 71 ClassName->getName()); 72 Diag(PrevDecl->getLocation(), diag::err_previous_definition); 73 } 74 75 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 76 if (IDecl) { 77 // Class already seen. Is it a forward declaration? 78 if (!IDecl->isForwardDecl()) { 79 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName()); 80 // Return the previous class interface. 81 // FIXME: don't leak the objects passed in! 82 return IDecl; 83 } else { 84 IDecl->setLocation(AtInterfaceLoc); 85 IDecl->setForwardDecl(false); 86 } 87 } else { 88 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, 89 ClassName, ClassLoc); 90 if (AttrList) 91 ProcessDeclAttributeList(IDecl, AttrList); 92 93 ObjCInterfaceDecls[ClassName] = IDecl; 94 // Remember that this needs to be removed when the scope is popped. 95 TUScope->AddDecl(IDecl); 96 } 97 98 if (SuperName) { 99 ObjCInterfaceDecl* SuperClassEntry = 0; 100 // Check if a different kind of symbol declared in this scope. 101 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope); 102 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 103 Diag(SuperLoc, diag::err_redefinition_different_kind, 104 SuperName->getName()); 105 Diag(PrevDecl->getLocation(), diag::err_previous_definition); 106 } 107 else { 108 // Check that super class is previously defined 109 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 110 111 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) { 112 Diag(SuperLoc, diag::err_undef_superclass, 113 SuperClassEntry ? SuperClassEntry->getName() 114 : SuperName->getName(), 115 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc)); 116 } 117 } 118 IDecl->setSuperClass(SuperClassEntry); 119 IDecl->setSuperClassLoc(SuperLoc); 120 IDecl->setLocEnd(SuperLoc); 121 } else { // we have a root class. 122 IDecl->setLocEnd(ClassLoc); 123 } 124 125 /// Check then save referenced protocols. 126 if (NumProtoRefs) { 127 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs); 128 IDecl->setLocEnd(EndProtoLoc); 129 } 130 131 CheckObjCDeclScope(IDecl); 132 return IDecl; 133} 134 135/// ActOnCompatiblityAlias - this action is called after complete parsing of 136/// @compatibility_alias declaration. It sets up the alias relationships. 137Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, 138 IdentifierInfo *AliasName, 139 SourceLocation AliasLocation, 140 IdentifierInfo *ClassName, 141 SourceLocation ClassLocation) { 142 // Look for previous declaration of alias name 143 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope); 144 if (ADecl) { 145 if (isa<ObjCCompatibleAliasDecl>(ADecl)) { 146 Diag(AliasLocation, diag::warn_previous_alias_decl); 147 Diag(ADecl->getLocation(), diag::warn_previous_declaration); 148 } 149 else { 150 Diag(AliasLocation, diag::err_conflicting_aliasing_type, 151 AliasName->getName()); 152 Diag(ADecl->getLocation(), diag::err_previous_declaration); 153 } 154 return 0; 155 } 156 // Check for class declaration 157 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); 158 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); 159 if (CDecl == 0) { 160 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName()); 161 if (CDeclU) 162 Diag(CDeclU->getLocation(), diag::warn_previous_declaration); 163 return 0; 164 } 165 166 // Everything checked out, instantiate a new alias declaration AST. 167 ObjCCompatibleAliasDecl *AliasDecl = 168 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl); 169 170 ObjCAliasDecls[AliasName] = AliasDecl; 171 172 if (!CheckObjCDeclScope(AliasDecl)) 173 TUScope->AddDecl(AliasDecl); 174 175 return AliasDecl; 176} 177 178Sema::DeclTy * 179Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, 180 IdentifierInfo *ProtocolName, 181 SourceLocation ProtocolLoc, 182 DeclTy * const *ProtoRefs, 183 unsigned NumProtoRefs, 184 SourceLocation EndProtoLoc, 185 AttributeList *AttrList) { 186 // FIXME: Deal with AttrList. 187 assert(ProtocolName && "Missing protocol identifier"); 188 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName]; 189 if (PDecl) { 190 // Protocol already seen. Better be a forward protocol declaration 191 if (!PDecl->isForwardDecl()) { 192 Diag(ProtocolLoc, diag::err_duplicate_protocol_def, 193 ProtocolName->getName()); 194 // Just return the protocol we already had. 195 // FIXME: don't leak the objects passed in! 196 return PDecl; 197 } 198 // Make sure the cached decl gets a valid start location. 199 PDecl->setLocation(AtProtoInterfaceLoc); 200 PDecl->setForwardDecl(false); 201 } else { 202 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName); 203 PDecl->setForwardDecl(false); 204 ObjCProtocols[ProtocolName] = PDecl; 205 } 206 207 if (NumProtoRefs) { 208 /// Check then save referenced protocols. 209 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs); 210 PDecl->setLocEnd(EndProtoLoc); 211 } 212 213 CheckObjCDeclScope(PDecl); 214 return PDecl; 215} 216 217/// FindProtocolDeclaration - This routine looks up protocols and 218/// issues an error if they are not declared. It returns list of 219/// protocol declarations in its 'Protocols' argument. 220void 221Sema::FindProtocolDeclaration(bool WarnOnDeclarations, 222 const IdentifierLocPair *ProtocolId, 223 unsigned NumProtocols, 224 llvm::SmallVectorImpl<DeclTy*> &Protocols) { 225 for (unsigned i = 0; i != NumProtocols; ++i) { 226 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first]; 227 if (!PDecl) { 228 Diag(ProtocolId[i].second, diag::err_undeclared_protocol, 229 ProtocolId[i].first->getName()); 230 continue; 231 } 232 233 // If this is a forward declaration and we are supposed to warn in this 234 // case, do it. 235 if (WarnOnDeclarations && PDecl->isForwardDecl()) 236 Diag(ProtocolId[i].second, diag::warn_undef_protocolref, 237 ProtocolId[i].first->getName()); 238 Protocols.push_back(PDecl); 239 } 240} 241 242/// DiagnosePropertyMismatch - Compares two properties for their 243/// attributes and types and warns on a variety of inconsistencies. 244/// 245void 246Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, 247 ObjCPropertyDecl *SuperProperty, 248 const char *inheritedName) { 249 ObjCPropertyDecl::PropertyAttributeKind CAttr = 250 Property->getPropertyAttributes(); 251 ObjCPropertyDecl::PropertyAttributeKind SAttr = 252 SuperProperty->getPropertyAttributes(); 253 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) 254 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) 255 Diag(Property->getLocation(), diag::warn_readonly_property, 256 Property->getName(), inheritedName); 257 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) 258 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) 259 Diag(Property->getLocation(), diag::warn_property_attribute) 260 << Property->getName() << "copy" << inheritedName; 261 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain) 262 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain)) 263 Diag(Property->getLocation(), diag::warn_property_attribute) 264 << Property->getName() << "retain" << inheritedName; 265 266 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) 267 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) 268 Diag(Property->getLocation(), diag::warn_property_attribute) 269 << Property->getName() << "atomic" << inheritedName; 270 if (Property->getSetterName() != SuperProperty->getSetterName()) 271 Diag(Property->getLocation(), diag::warn_property_attribute) 272 << Property->getName() << "setter" << inheritedName; 273 if (Property->getGetterName() != SuperProperty->getGetterName()) 274 Diag(Property->getLocation(), diag::warn_property_attribute) 275 << Property->getName() << "getter" << inheritedName; 276 277 if (Context.getCanonicalType(Property->getType()) != 278 Context.getCanonicalType(SuperProperty->getType())) 279 Diag(Property->getLocation(), diag::warn_property_type) 280 << Property->getType().getAsString() << inheritedName; 281 282} 283 284/// ComparePropertiesInBaseAndSuper - This routine compares property 285/// declarations in base and its super class, if any, and issues 286/// diagnostics in a variety of inconsistant situations. 287/// 288void 289Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { 290 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); 291 if (!SDecl) 292 return; 293 // FIXME: O(N^2) 294 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(), 295 E = SDecl->classprop_end(); S != E; ++S) { 296 ObjCPropertyDecl *SuperPDecl = (*S); 297 // Does property in super class has declaration in current class? 298 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(), 299 E = IDecl->classprop_end(); I != E; ++I) { 300 ObjCPropertyDecl *PDecl = (*I); 301 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) 302 DiagnosePropertyMismatch(PDecl, SuperPDecl, 303 SDecl->getIdentifierName()); 304 } 305 } 306} 307 308/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list 309/// of properties declared in a protocol and adds them to the list 310/// of properties for current class if it is not there already. 311void 312Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl, 313 ObjCProtocolDecl *PDecl) 314{ 315 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties; 316 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(), 317 E = PDecl->classprop_end(); P != E; ++P) { 318 ObjCPropertyDecl *Pr = (*P); 319 ObjCInterfaceDecl::classprop_iterator CP, CE; 320 // Is this property already in class's list of properties? 321 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end(); 322 CP != CE; ++CP) 323 if ((*CP)->getIdentifier() == Pr->getIdentifier()) 324 break; 325 if (CP == CE) 326 // Add this property to list of properties for thie class. 327 mergeProperties.push_back(Pr); 328 else 329 // Property protocol already exist in class. Diagnose any mismatch. 330 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifierName()); 331 } 332 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size()); 333} 334 335/// MergeProtocolPropertiesIntoClass - This routine merges properties 336/// declared in 'MergeItsProtocols' objects (which can be a class or an 337/// inherited protocol into the list of properties for class 'IDecl' 338/// 339 340void 341Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl, 342 DeclTy *MergeItsProtocols) { 343 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols); 344 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 345 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(), 346 E = MDecl->protocol_end(); P != E; ++P) 347 // Merge properties of class (*P) into IDECL's 348 MergeOneProtocolPropertiesIntoClass(IDecl, *P); 349 350 // Go thru the list of protocols for this class and recursively merge 351 // their properties into this class as well. 352 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(), 353 E = IDecl->protocol_end(); P != E; ++P) 354 MergeProtocolPropertiesIntoClass(IDecl, *P); 355 } else { 356 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); 357 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), 358 E = MD->protocol_end(); P != E; ++P) 359 MergeOneProtocolPropertiesIntoClass(IDecl, (*P)); 360 } 361} 362 363/// ActOnForwardProtocolDeclaration - 364Action::DeclTy * 365Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, 366 const IdentifierLocPair *IdentList, 367 unsigned NumElts) { 368 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; 369 370 for (unsigned i = 0; i != NumElts; ++i) { 371 IdentifierInfo *Ident = IdentList[i].first; 372 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident]; 373 if (PDecl == 0) // Not already seen? 374 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident); 375 376 Protocols.push_back(PDecl); 377 } 378 379 ObjCForwardProtocolDecl *PDecl = 380 ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc, 381 &Protocols[0], Protocols.size()); 382 383 CheckObjCDeclScope(PDecl); 384 return PDecl; 385} 386 387Sema::DeclTy *Sema:: 388ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, 389 IdentifierInfo *ClassName, SourceLocation ClassLoc, 390 IdentifierInfo *CategoryName, 391 SourceLocation CategoryLoc, 392 DeclTy * const *ProtoRefs, 393 unsigned NumProtoRefs, 394 SourceLocation EndProtoLoc) { 395 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); 396 397 ObjCCategoryDecl *CDecl = 398 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName); 399 CDecl->setClassInterface(IDecl); 400 401 /// Check that class of this category is already completely declared. 402 if (!IDecl || IDecl->isForwardDecl()) 403 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); 404 else { 405 /// Check for duplicate interface declaration for this category 406 ObjCCategoryDecl *CDeclChain; 407 for (CDeclChain = IDecl->getCategoryList(); CDeclChain; 408 CDeclChain = CDeclChain->getNextClassCategory()) { 409 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) { 410 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(), 411 CategoryName->getName()); 412 break; 413 } 414 } 415 if (!CDeclChain) 416 CDecl->insertNextClassCategory(); 417 } 418 419 if (NumProtoRefs) { 420 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs); 421 CDecl->setLocEnd(EndProtoLoc); 422 } 423 424 CheckObjCDeclScope(CDecl); 425 return CDecl; 426} 427 428/// ActOnStartCategoryImplementation - Perform semantic checks on the 429/// category implementation declaration and build an ObjCCategoryImplDecl 430/// object. 431Sema::DeclTy *Sema::ActOnStartCategoryImplementation( 432 SourceLocation AtCatImplLoc, 433 IdentifierInfo *ClassName, SourceLocation ClassLoc, 434 IdentifierInfo *CatName, SourceLocation CatLoc) { 435 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); 436 ObjCCategoryImplDecl *CDecl = 437 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl); 438 /// Check that class of this category is already completely declared. 439 if (!IDecl || IDecl->isForwardDecl()) 440 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); 441 442 /// TODO: Check that CatName, category name, is not used in another 443 // implementation. 444 ObjCCategoryImpls.push_back(CDecl); 445 446 CheckObjCDeclScope(CDecl); 447 return CDecl; 448} 449 450Sema::DeclTy *Sema::ActOnStartClassImplementation( 451 SourceLocation AtClassImplLoc, 452 IdentifierInfo *ClassName, SourceLocation ClassLoc, 453 IdentifierInfo *SuperClassname, 454 SourceLocation SuperClassLoc) { 455 ObjCInterfaceDecl* IDecl = 0; 456 // Check for another declaration kind with the same name. 457 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); 458 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 459 Diag(ClassLoc, diag::err_redefinition_different_kind, 460 ClassName->getName()); 461 Diag(PrevDecl->getLocation(), diag::err_previous_definition); 462 } 463 else { 464 // Is there an interface declaration of this class; if not, warn! 465 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 466 if (!IDecl) 467 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName()); 468 } 469 470 // Check that super class name is valid class name 471 ObjCInterfaceDecl* SDecl = 0; 472 if (SuperClassname) { 473 // Check if a different kind of symbol declared in this scope. 474 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope); 475 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 476 Diag(SuperClassLoc, diag::err_redefinition_different_kind, 477 SuperClassname->getName()); 478 Diag(PrevDecl->getLocation(), diag::err_previous_definition); 479 } 480 else { 481 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 482 if (!SDecl) 483 Diag(SuperClassLoc, diag::err_undef_superclass, 484 SuperClassname->getName(), ClassName->getName()); 485 else if (IDecl && IDecl->getSuperClass() != SDecl) { 486 // This implementation and its interface do not have the same 487 // super class. 488 Diag(SuperClassLoc, diag::err_conflicting_super_class, 489 SDecl->getName()); 490 Diag(SDecl->getLocation(), diag::err_previous_definition); 491 } 492 } 493 } 494 495 if (!IDecl) { 496 // Legacy case of @implementation with no corresponding @interface. 497 // Build, chain & install the interface decl into the identifier. 498 499 // FIXME: Do we support attributes on the @implementation? If so 500 // we should copy them over. 501 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName, 502 ClassLoc, false, true); 503 ObjCInterfaceDecls[ClassName] = IDecl; 504 IDecl->setSuperClass(SDecl); 505 IDecl->setLocEnd(ClassLoc); 506 507 // Remember that this needs to be removed when the scope is popped. 508 TUScope->AddDecl(IDecl); 509 } 510 511 ObjCImplementationDecl* IMPDecl = 512 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName, 513 IDecl, SDecl); 514 515 if (CheckObjCDeclScope(IMPDecl)) 516 return IMPDecl; 517 518 // Check that there is no duplicate implementation of this class. 519 if (ObjCImplementations[ClassName]) 520 // FIXME: Don't leak everything! 521 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName()); 522 else // add it to the list. 523 ObjCImplementations[ClassName] = IMPDecl; 524 return IMPDecl; 525} 526 527void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, 528 ObjCIvarDecl **ivars, unsigned numIvars, 529 SourceLocation RBrace) { 530 assert(ImpDecl && "missing implementation decl"); 531 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier()); 532 if (!IDecl) 533 return; 534 /// Check case of non-existing @interface decl. 535 /// (legacy objective-c @implementation decl without an @interface decl). 536 /// Add implementations's ivar to the synthesize class's ivar list. 537 if (IDecl->ImplicitInterfaceDecl()) { 538 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace); 539 return; 540 } 541 // If implementation has empty ivar list, just return. 542 if (numIvars == 0) 543 return; 544 545 assert(ivars && "missing @implementation ivars"); 546 547 // Check interface's Ivar list against those in the implementation. 548 // names and types must match. 549 // 550 unsigned j = 0; 551 ObjCInterfaceDecl::ivar_iterator 552 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); 553 for (; numIvars > 0 && IVI != IVE; ++IVI) { 554 ObjCIvarDecl* ImplIvar = ivars[j++]; 555 ObjCIvarDecl* ClsIvar = *IVI; 556 assert (ImplIvar && "missing implementation ivar"); 557 assert (ClsIvar && "missing class ivar"); 558 if (Context.getCanonicalType(ImplIvar->getType()) != 559 Context.getCanonicalType(ClsIvar->getType())) { 560 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type, 561 ImplIvar->getIdentifier()->getName()); 562 Diag(ClsIvar->getLocation(), diag::err_previous_definition, 563 ClsIvar->getIdentifier()->getName()); 564 } 565 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed 566 // as error. 567 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { 568 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name, 569 ImplIvar->getIdentifier()->getName()); 570 Diag(ClsIvar->getLocation(), diag::err_previous_definition, 571 ClsIvar->getIdentifier()->getName()); 572 return; 573 } 574 --numIvars; 575 } 576 577 if (numIvars > 0) 578 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); 579 else if (IVI != IVE) 580 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); 581} 582 583void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, 584 bool &IncompleteImpl) { 585 if (!IncompleteImpl) { 586 Diag(ImpLoc, diag::warn_incomplete_impl); 587 IncompleteImpl = true; 588 } 589 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName()); 590} 591 592/// FIXME: Type hierarchies in Objective-C can be deep. We could most 593/// likely improve the efficiency of selector lookups and type 594/// checking by associating with each protocol / interface / category 595/// the flattened instance tables. If we used an immutable set to keep 596/// the table then it wouldn't add significant memory cost and it 597/// would be handy for lookups. 598 599/// CheckProtocolMethodDefs - This routine checks unimplemented methods 600/// Declared in protocol, and those referenced by it. 601void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, 602 ObjCProtocolDecl *PDecl, 603 bool& IncompleteImpl, 604 const llvm::DenseSet<Selector> &InsMap, 605 const llvm::DenseSet<Selector> &ClsMap, 606 ObjCInterfaceDecl *IDecl) { 607 ObjCInterfaceDecl *Super = IDecl->getSuperClass(); 608 609 // If a method lookup fails locally we still need to look and see if 610 // the method was implemented by a base class or an inherited 611 // protocol. This lookup is slow, but occurs rarely in correct code 612 // and otherwise would terminate in a warning. 613 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 (method->getImplementationControl() != ObjCMethodDecl::Optional && 619 !InsMap.count(method->getSelector()) && 620 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) 621 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); 622 } 623 // check unimplemented class methods 624 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), 625 E = PDecl->classmeth_end(); I != E; ++I) { 626 ObjCMethodDecl *method = *I; 627 if (method->getImplementationControl() != ObjCMethodDecl::Optional && 628 !ClsMap.count(method->getSelector()) && 629 (!Super || !Super->lookupClassMethod(method->getSelector()))) 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, IDecl); 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, IDecl); 673} 674 675/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the 676/// category interface are 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, CatClassDecl->getClassInterface()); 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 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc, 751 &Interfaces[0], 752 Interfaces.size()); 753 754 CheckObjCDeclScope(CDecl); 755 return CDecl; 756} 757 758 759/// MatchTwoMethodDeclarations - Checks that two methods have matching type and 760/// returns true, or false, accordingly. 761/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons 762bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, 763 const ObjCMethodDecl *PrevMethod, 764 bool matchBasedOnSizeAndAlignment) { 765 QualType T1 = Context.getCanonicalType(Method->getResultType()); 766 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType()); 767 768 if (T1 != T2) { 769 // The result types are different. 770 if (!matchBasedOnSizeAndAlignment) 771 return false; 772 // Incomplete types don't have a size and alignment. 773 if (T1->isIncompleteType() || T2->isIncompleteType()) 774 return false; 775 // Check is based on size and alignment. 776 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) 777 return false; 778 } 779 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) { 780 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType()); 781 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType()); 782 if (T1 != T2) { 783 // The result types are different. 784 if (!matchBasedOnSizeAndAlignment) 785 return false; 786 // Incomplete types don't have a size and alignment. 787 if (T1->isIncompleteType() || T2->isIncompleteType()) 788 return false; 789 // Check is based on size and alignment. 790 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) 791 return false; 792 } 793 } 794 return true; 795} 796 797void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) { 798 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()]; 799 if (!FirstMethod.Method) { 800 // Haven't seen a method with this selector name yet - add it. 801 FirstMethod.Method = Method; 802 FirstMethod.Next = 0; 803 } else { 804 // We've seen a method with this name, now check the type signature(s). 805 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); 806 807 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; 808 Next = Next->Next) 809 match = MatchTwoMethodDeclarations(Method, Next->Method); 810 811 if (!match) { 812 // We have a new signature for an existing method - add it. 813 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". 814 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); 815 FirstMethod.Next = OMI; 816 } 817 } 818} 819 820// FIXME: Finish implementing -Wno-strict-selector-match. 821ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel, 822 SourceRange R) { 823 ObjCMethodList &MethList = InstanceMethodPool[Sel]; 824 bool issueWarning = false; 825 826 if (MethList.Method && MethList.Next) { 827 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 828 // This checks if the methods differ by size & alignment. 829 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) 830 issueWarning = true; 831 } 832 if (issueWarning && (MethList.Method && MethList.Next)) { 833 Diag(R.getBegin(), diag::warn_multiple_method_decl, Sel.getName(), R); 834 Diag(MethList.Method->getLocStart(), diag::warn_using_decl, 835 MethList.Method->getSourceRange()); 836 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 837 Diag(Next->Method->getLocStart(), diag::warn_also_found_decl, 838 Next->Method->getSourceRange()); 839 } 840 return MethList.Method; 841} 842 843void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { 844 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()]; 845 if (!FirstMethod.Method) { 846 // Haven't seen a method with this selector name yet - add it. 847 FirstMethod.Method = Method; 848 FirstMethod.Next = 0; 849 } else { 850 // We've seen a method with this name, now check the type signature(s). 851 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); 852 853 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; 854 Next = Next->Next) 855 match = MatchTwoMethodDeclarations(Method, Next->Method); 856 857 if (!match) { 858 // We have a new signature for an existing method - add it. 859 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". 860 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); 861 FirstMethod.Next = OMI; 862 } 863 } 864} 865 866// Note: For class/category implemenations, allMethods/allProperties is 867// always null. 868void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl, 869 DeclTy **allMethods, unsigned allNum, 870 DeclTy **allProperties, unsigned pNum) { 871 Decl *ClassDecl = static_cast<Decl *>(classDecl); 872 873 // FIXME: If we don't have a ClassDecl, we have an error. We should consider 874 // always passing in a decl. If the decl has an error, isInvalidDecl() 875 // should be true. 876 if (!ClassDecl) 877 return; 878 879 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods; 880 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods; 881 882 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; 883 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; 884 885 bool isInterfaceDeclKind = 886 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) 887 || isa<ObjCProtocolDecl>(ClassDecl); 888 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); 889 890 if (pNum != 0) { 891 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) 892 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); 893 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) 894 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); 895 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl)) 896 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); 897 else 898 assert(false && "ActOnAtEnd - property declaration misplaced"); 899 } 900 901 for (unsigned i = 0; i < allNum; i++ ) { 902 ObjCMethodDecl *Method = 903 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i])); 904 905 if (!Method) continue; // Already issued a diagnostic. 906 if (Method->isInstance()) { 907 /// Check for instance method of the same name with incompatible types 908 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; 909 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 910 : false; 911 if (isInterfaceDeclKind && PrevMethod && !match 912 || checkIdenticalMethods && match) { 913 Diag(Method->getLocation(), diag::error_duplicate_method_decl, 914 Method->getSelector().getName()); 915 Diag(PrevMethod->getLocation(), diag::err_previous_declaration); 916 } else { 917 insMethods.push_back(Method); 918 InsMap[Method->getSelector()] = Method; 919 /// The following allows us to typecheck messages to "id". 920 AddInstanceMethodToGlobalPool(Method); 921 } 922 } 923 else { 924 /// Check for class method of the same name with incompatible types 925 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; 926 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 927 : false; 928 if (isInterfaceDeclKind && PrevMethod && !match 929 || checkIdenticalMethods && match) { 930 Diag(Method->getLocation(), diag::error_duplicate_method_decl, 931 Method->getSelector().getName()); 932 Diag(PrevMethod->getLocation(), diag::err_previous_declaration); 933 } else { 934 clsMethods.push_back(Method); 935 ClsMap[Method->getSelector()] = Method; 936 /// The following allows us to typecheck messages to "Class". 937 AddFactoryMethodToGlobalPool(Method); 938 } 939 } 940 } 941 // Save the size so we can detect if we've added any property methods. 942 unsigned int insMethodsSizePriorToPropAdds = insMethods.size(); 943 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size(); 944 945 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 946 // Compares properties declared in this class to those of its 947 // super class. 948 ComparePropertiesInBaseAndSuper(I); 949 MergeProtocolPropertiesIntoClass(I, I); 950 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(), 951 e = I->classprop_end(); i != e; ++i) 952 I->addPropertyMethods(Context, *i, insMethods); 953 I->addMethods(&insMethods[0], insMethods.size(), 954 &clsMethods[0], clsMethods.size(), AtEndLoc); 955 956 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) { 957 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(), 958 e = P->classprop_end(); i != e; ++i) 959 P->addPropertyMethods(Context, *i, insMethods); 960 P->addMethods(&insMethods[0], insMethods.size(), 961 &clsMethods[0], clsMethods.size(), AtEndLoc); 962 } 963 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { 964 // FIXME: Need to compare properties to those in interface? 965 966 // FIXME: If we merge properties into class we should probably 967 // merge them into category as well? 968 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(), 969 e = C->classprop_end(); i != e; ++i) 970 C->addPropertyMethods(Context, *i, insMethods); 971 C->addMethods(&insMethods[0], insMethods.size(), 972 &clsMethods[0], clsMethods.size(), AtEndLoc); 973 } 974 else if (ObjCImplementationDecl *IC = 975 dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 976 IC->setLocEnd(AtEndLoc); 977 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier())) 978 ImplMethodsVsClassMethods(IC, IDecl); 979 } else { 980 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl); 981 CatImplClass->setLocEnd(AtEndLoc); 982 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface(); 983 // Find category interface decl and then check that all methods declared 984 // in this interface are implemented in the category @implementation. 985 if (IDecl) { 986 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); 987 Categories; Categories = Categories->getNextClassCategory()) { 988 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { 989 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories); 990 break; 991 } 992 } 993 } 994 } 995 // Add any synthesized methods to the global pool. This allows us to 996 // handle the following, which is supported by GCC (and part of the design). 997 // 998 // @interface Foo 999 // @property double bar; 1000 // @end 1001 // 1002 // void thisIsUnfortunate() { 1003 // id foo; 1004 // double bar = [foo bar]; 1005 // } 1006 // 1007 if (insMethodsSizePriorToPropAdds < insMethods.size()) 1008 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++) 1009 AddInstanceMethodToGlobalPool(insMethods[i]); 1010 if (clsMethodsSizePriorToPropAdds < clsMethods.size()) 1011 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++) 1012 AddFactoryMethodToGlobalPool(clsMethods[i]); 1013} 1014 1015 1016/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for 1017/// objective-c's type qualifier from the parser version of the same info. 1018static Decl::ObjCDeclQualifier 1019CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { 1020 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; 1021 if (PQTVal & ObjCDeclSpec::DQ_In) 1022 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); 1023 if (PQTVal & ObjCDeclSpec::DQ_Inout) 1024 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); 1025 if (PQTVal & ObjCDeclSpec::DQ_Out) 1026 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); 1027 if (PQTVal & ObjCDeclSpec::DQ_Bycopy) 1028 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); 1029 if (PQTVal & ObjCDeclSpec::DQ_Byref) 1030 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); 1031 if (PQTVal & ObjCDeclSpec::DQ_Oneway) 1032 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); 1033 1034 return ret; 1035} 1036 1037Sema::DeclTy *Sema::ActOnMethodDeclaration( 1038 SourceLocation MethodLoc, SourceLocation EndLoc, 1039 tok::TokenKind MethodType, DeclTy *classDecl, 1040 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, 1041 Selector Sel, 1042 // optional arguments. The number of types/arguments is obtained 1043 // from the Sel.getNumArgs(). 1044 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames, 1045 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, 1046 bool isVariadic) { 1047 Decl *ClassDecl = static_cast<Decl*>(classDecl); 1048 1049 // Make sure we can establish a context for the method. 1050 if (!ClassDecl) { 1051 Diag(MethodLoc, diag::error_missing_method_context); 1052 return 0; 1053 } 1054 QualType resultDeclType; 1055 1056 if (ReturnType) 1057 resultDeclType = QualType::getFromOpaquePtr(ReturnType); 1058 else // get the type for "id". 1059 resultDeclType = Context.getObjCIdType(); 1060 1061 ObjCMethodDecl* ObjCMethod = 1062 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, 1063 ClassDecl, 1064 MethodType == tok::minus, isVariadic, 1065 false, 1066 MethodDeclKind == tok::objc_optional ? 1067 ObjCMethodDecl::Optional : 1068 ObjCMethodDecl::Required); 1069 1070 llvm::SmallVector<ParmVarDecl*, 16> Params; 1071 1072 for (unsigned i = 0; i < Sel.getNumArgs(); i++) { 1073 // FIXME: arg->AttrList must be stored too! 1074 QualType argType; 1075 1076 if (ArgTypes[i]) 1077 argType = QualType::getFromOpaquePtr(ArgTypes[i]); 1078 else 1079 argType = Context.getObjCIdType(); 1080 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod, 1081 SourceLocation(/*FIXME*/), 1082 ArgNames[i], argType, 1083 VarDecl::None, 0, 0); 1084 Param->setObjCDeclQualifier( 1085 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier())); 1086 Params.push_back(Param); 1087 } 1088 1089 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs()); 1090 ObjCMethod->setObjCDeclQualifier( 1091 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); 1092 const ObjCMethodDecl *PrevMethod = 0; 1093 1094 if (AttrList) 1095 ProcessDeclAttributeList(ObjCMethod, AttrList); 1096 1097 // For implementations (which can be very "coarse grain"), we add the 1098 // method now. This allows the AST to implement lookup methods that work 1099 // incrementally (without waiting until we parse the @end). It also allows 1100 // us to flag multiple declaration errors as they occur. 1101 if (ObjCImplementationDecl *ImpDecl = 1102 dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 1103 if (MethodType == tok::minus) { 1104 PrevMethod = ImpDecl->getInstanceMethod(Sel); 1105 ImpDecl->addInstanceMethod(ObjCMethod); 1106 } else { 1107 PrevMethod = ImpDecl->getClassMethod(Sel); 1108 ImpDecl->addClassMethod(ObjCMethod); 1109 } 1110 } 1111 else if (ObjCCategoryImplDecl *CatImpDecl = 1112 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { 1113 if (MethodType == tok::minus) { 1114 PrevMethod = CatImpDecl->getInstanceMethod(Sel); 1115 CatImpDecl->addInstanceMethod(ObjCMethod); 1116 } else { 1117 PrevMethod = CatImpDecl->getClassMethod(Sel); 1118 CatImpDecl->addClassMethod(ObjCMethod); 1119 } 1120 } 1121 if (PrevMethod) { 1122 // You can never have two method definitions with the same name. 1123 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl, 1124 ObjCMethod->getSelector().getName()); 1125 Diag(PrevMethod->getLocation(), diag::err_previous_declaration); 1126 } 1127 return ObjCMethod; 1128} 1129 1130void Sema::CheckObjCPropertyAttributes(QualType PropertyTy, 1131 SourceLocation Loc, 1132 unsigned &Attributes) { 1133 // FIXME: Improve the reported location. 1134 1135 // readonly and readwrite conflict. 1136 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1137 (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) { 1138 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive, 1139 "readonly", "readwrite"); 1140 Attributes ^= ObjCDeclSpec::DQ_PR_readonly; 1141 } 1142 1143 // Check for copy or retain on non-object types. 1144 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) && 1145 !Context.isObjCObjectPointerType(PropertyTy)) { 1146 Diag(Loc, diag::err_objc_property_requires_object, 1147 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain"); 1148 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain); 1149 } 1150 1151 // Check for more than one of { assign, copy, retain }. 1152 if (Attributes & ObjCDeclSpec::DQ_PR_assign) { 1153 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1154 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive, 1155 "assign", "copy"); 1156 Attributes ^= ObjCDeclSpec::DQ_PR_copy; 1157 } 1158 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1159 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive, 1160 "assign", "retain"); 1161 Attributes ^= ObjCDeclSpec::DQ_PR_retain; 1162 } 1163 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1164 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1165 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive, 1166 "copy", "retain"); 1167 Attributes ^= ObjCDeclSpec::DQ_PR_retain; 1168 } 1169 } 1170 1171 // Warn if user supplied no assignment attribute, property is 1172 // readwrite, and this is an object type. 1173 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | 1174 ObjCDeclSpec::DQ_PR_retain)) && 1175 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1176 Context.isObjCObjectPointerType(PropertyTy)) { 1177 // Skip this warning in gc-only mode. 1178 if (getLangOptions().getGCMode() != LangOptions::GCOnly) 1179 Diag(Loc, diag::warn_objc_property_no_assignment_attribute); 1180 1181 // If non-gc code warn that this is likely inappropriate. 1182 if (getLangOptions().getGCMode() == LangOptions::NonGC) 1183 Diag(Loc, diag::warn_objc_property_default_assign_on_object); 1184 1185 // FIXME: Implement warning dependent on NSCopying being 1186 // implemented. See also: 1187 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> 1188 // (please trim this list while you are at it). 1189 } 1190} 1191 1192Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, 1193 FieldDeclarator &FD, 1194 ObjCDeclSpec &ODS, 1195 Selector GetterSel, 1196 Selector SetterSel, 1197 tok::ObjCKeywordKind MethodImplKind) { 1198 QualType T = GetTypeForDeclarator(FD.D, S); 1199 unsigned Attributes = ODS.getPropertyAttributes(); 1200 1201 // May modify Attributes. 1202 CheckObjCPropertyAttributes(T, AtLoc, Attributes); 1203 1204 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc, 1205 FD.D.getIdentifier(), T); 1206 // Regardless of setter/getter attribute, we save the default getter/setter 1207 // selector names in anticipation of declaration of setter/getter methods. 1208 PDecl->setGetterName(GetterSel); 1209 PDecl->setSetterName(SetterSel); 1210 1211 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) 1212 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); 1213 1214 if (Attributes & ObjCDeclSpec::DQ_PR_getter) 1215 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); 1216 1217 if (Attributes & ObjCDeclSpec::DQ_PR_setter) 1218 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); 1219 1220 if (Attributes & ObjCDeclSpec::DQ_PR_assign) 1221 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); 1222 1223 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite) 1224 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); 1225 1226 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 1227 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 1228 1229 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 1230 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 1231 1232 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) 1233 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); 1234 1235 if (MethodImplKind == tok::objc_required) 1236 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); 1237 else if (MethodImplKind == tok::objc_optional) 1238 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); 1239 1240 return PDecl; 1241} 1242 1243/// ActOnPropertyImplDecl - This routine performs semantic checks and 1244/// builds the AST node for a property implementation declaration; declared 1245/// as @synthesize or @dynamic. 1246/// 1247Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc, 1248 SourceLocation PropertyLoc, 1249 bool Synthesize, 1250 DeclTy *ClassCatImpDecl, 1251 IdentifierInfo *PropertyId, 1252 IdentifierInfo *PropertyIvar) { 1253 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl); 1254 // Make sure we have a context for the property implementation declaration. 1255 if (!ClassImpDecl) { 1256 Diag(AtLoc, diag::error_missing_property_context); 1257 return 0; 1258 } 1259 ObjCPropertyDecl *property = 0; 1260 ObjCInterfaceDecl* IDecl = 0; 1261 // Find the class or category class where this property must have 1262 // a declaration. 1263 ObjCImplementationDecl *IC = 0; 1264 ObjCCategoryImplDecl* CatImplClass = 0; 1265 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { 1266 IDecl = getObjCInterfaceDecl(IC->getIdentifier()); 1267 // We always synthesize an interface for an implementation 1268 // without an interface decl. So, IDecl is always non-zero. 1269 assert(IDecl && 1270 "ActOnPropertyImplDecl - @implementation without @interface"); 1271 1272 // Look for this property declaration in the @implementation's @interface 1273 property = IDecl->FindPropertyDeclaration(PropertyId); 1274 if (!property) { 1275 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName()); 1276 return 0; 1277 } 1278 } 1279 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { 1280 if (Synthesize) { 1281 Diag(AtLoc, diag::error_synthesize_category_decl); 1282 return 0; 1283 } 1284 IDecl = CatImplClass->getClassInterface(); 1285 if (!IDecl) { 1286 Diag(AtLoc, diag::error_missing_property_interface); 1287 return 0; 1288 } 1289 ObjCCategoryDecl *Category = 1290 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); 1291 1292 // If category for this implementation not found, it is an error which 1293 // has already been reported eralier. 1294 if (!Category) 1295 return 0; 1296 // Look for this property declaration in @implementation's category 1297 property = Category->FindPropertyDeclaration(PropertyId); 1298 if (!property) { 1299 Diag(PropertyLoc, diag::error_bad_category_property_decl, 1300 Category->getName()); 1301 return 0; 1302 } 1303 } 1304 else { 1305 Diag(AtLoc, diag::error_bad_property_context); 1306 return 0; 1307 } 1308 ObjCIvarDecl *Ivar = 0; 1309 // Check that we have a valid, previously declared ivar for @synthesize 1310 if (Synthesize) { 1311 // @synthesize 1312 if (!PropertyIvar) 1313 PropertyIvar = PropertyId; 1314 // Check that this is a previously declared 'ivar' in 'IDecl' interface 1315 Ivar = IDecl->FindIvarDeclaration(PropertyIvar); 1316 if (!Ivar) { 1317 Diag(PropertyLoc, diag::error_missing_property_ivar_decl, 1318 PropertyId->getName()); 1319 return 0; 1320 } 1321 QualType PropType = Context.getCanonicalType(property->getType()); 1322 QualType IvarType = Context.getCanonicalType(Ivar->getType()); 1323 1324 // Check that type of property and its ivar are type compatible. 1325 if (PropType != IvarType) { 1326 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) { 1327 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(), 1328 Ivar->getName()); 1329 return 0; 1330 } 1331 } 1332 } else if (PropertyIvar) { 1333 // @dynamic 1334 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); 1335 return 0; 1336 } 1337 assert (property && "ActOnPropertyImplDecl - property declaration missing"); 1338 ObjCPropertyImplDecl *PIDecl = 1339 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property, 1340 (Synthesize ? 1341 ObjCPropertyImplDecl::Synthesize 1342 : ObjCPropertyImplDecl::Dynamic), 1343 Ivar); 1344 if (IC) 1345 IC->addPropertyImplementation(PIDecl); 1346 else 1347 CatImplClass->addPropertyImplementation(PIDecl); 1348 1349 return PIDecl; 1350} 1351 1352bool Sema::CheckObjCDeclScope(Decl *D) 1353{ 1354 if (isa<TranslationUnitDecl>(CurContext)) 1355 return false; 1356 1357 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); 1358 D->setInvalidDecl(); 1359 1360 return true; 1361} 1362