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