SemaDeclObjC.cpp revision b33f3ad379f497c5fc6d0ada618745dd46d0e717
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/Sema/ExternalSemaSource.h" 16#include "clang/AST/Expr.h" 17#include "clang/AST/ASTContext.h" 18#include "clang/AST/DeclObjC.h" 19#include "clang/Parse/DeclSpec.h" 20using namespace clang; 21 22/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible 23/// and user declared, in the method definition's AST. 24void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) { 25 assert(getCurMethodDecl() == 0 && "Method parsing confused"); 26 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>()); 27 28 // If we don't have a valid method decl, simply return. 29 if (!MDecl) 30 return; 31 32 CurFunctionNeedsScopeChecking = false; 33 34 // Allow the rest of sema to find private method decl implementations. 35 if (MDecl->isInstanceMethod()) 36 AddInstanceMethodToGlobalPool(MDecl); 37 else 38 AddFactoryMethodToGlobalPool(MDecl); 39 40 // Allow all of Sema to see that we are entering a method definition. 41 PushDeclContext(FnBodyScope, MDecl); 42 43 // Create Decl objects for each parameter, entrring them in the scope for 44 // binding to their use. 45 46 // Insert the invisible arguments, self and _cmd! 47 MDecl->createImplicitParams(Context, MDecl->getClassInterface()); 48 49 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); 50 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); 51 52 // Introduce all of the other parameters into this scope. 53 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), 54 E = MDecl->param_end(); PI != E; ++PI) 55 if ((*PI)->getIdentifier()) 56 PushOnScopeChains(*PI, FnBodyScope); 57} 58 59Sema::DeclPtrTy Sema:: 60ActOnStartClassInterface(SourceLocation AtInterfaceLoc, 61 IdentifierInfo *ClassName, SourceLocation ClassLoc, 62 IdentifierInfo *SuperName, SourceLocation SuperLoc, 63 const DeclPtrTy *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 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName); 69 if (PrevDecl && PrevDecl->isTemplateParameter()) { 70 // Maybe we will complain about the shadowed template parameter. 71 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl); 72 // Just pretend that we didn't see the previous declaration. 73 PrevDecl = 0; 74 } 75 76 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 77 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; 78 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 79 } 80 81 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 82 if (IDecl) { 83 // Class already seen. Is it a forward declaration? 84 if (!IDecl->isForwardDecl()) { 85 IDecl->setInvalidDecl(); 86 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName(); 87 Diag(IDecl->getLocation(), diag::note_previous_definition); 88 89 // Return the previous class interface. 90 // FIXME: don't leak the objects passed in! 91 return DeclPtrTy::make(IDecl); 92 } else { 93 IDecl->setLocation(AtInterfaceLoc); 94 IDecl->setForwardDecl(false); 95 } 96 } else { 97 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, 98 ClassName, ClassLoc); 99 if (AttrList) 100 ProcessDeclAttributeList(IDecl, AttrList); 101 102 PushOnScopeChains(IDecl, TUScope); 103 } 104 105 if (SuperName) { 106 // Check if a different kind of symbol declared in this scope. 107 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName); 108 109 ObjCInterfaceDecl *SuperClassDecl = 110 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 111 112 // Diagnose classes that inherit from deprecated classes. 113 if (SuperClassDecl) 114 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); 115 116 if (PrevDecl && SuperClassDecl == 0) { 117 // The previous declaration was not a class decl. Check if we have a 118 // typedef. If we do, get the underlying class type. 119 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) { 120 QualType T = TDecl->getUnderlyingType(); 121 if (T->isObjCInterfaceType()) { 122 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) 123 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); 124 } 125 } 126 127 // This handles the following case: 128 // 129 // typedef int SuperClass; 130 // @interface MyClass : SuperClass {} @end 131 // 132 if (!SuperClassDecl) { 133 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; 134 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 135 } 136 } 137 138 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) { 139 if (!SuperClassDecl) 140 Diag(SuperLoc, diag::err_undef_superclass) 141 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); 142 else if (SuperClassDecl->isForwardDecl()) 143 Diag(SuperLoc, diag::err_undef_superclass) 144 << SuperClassDecl->getDeclName() << ClassName 145 << SourceRange(AtInterfaceLoc, ClassLoc); 146 } 147 IDecl->setSuperClass(SuperClassDecl); 148 IDecl->setSuperClassLoc(SuperLoc); 149 IDecl->setLocEnd(SuperLoc); 150 } else { // we have a root class. 151 IDecl->setLocEnd(ClassLoc); 152 } 153 154 /// Check then save referenced protocols. 155 if (NumProtoRefs) { 156 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, 157 Context); 158 IDecl->setLocEnd(EndProtoLoc); 159 } 160 161 CheckObjCDeclScope(IDecl); 162 return DeclPtrTy::make(IDecl); 163} 164 165/// ActOnCompatiblityAlias - this action is called after complete parsing of 166/// @compatibility_alias declaration. It sets up the alias relationships. 167Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, 168 IdentifierInfo *AliasName, 169 SourceLocation AliasLocation, 170 IdentifierInfo *ClassName, 171 SourceLocation ClassLocation) { 172 // Look for previous declaration of alias name 173 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName); 174 if (ADecl) { 175 if (isa<ObjCCompatibleAliasDecl>(ADecl)) 176 Diag(AliasLocation, diag::warn_previous_alias_decl); 177 else 178 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; 179 Diag(ADecl->getLocation(), diag::note_previous_declaration); 180 return DeclPtrTy(); 181 } 182 // Check for class declaration 183 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName); 184 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) { 185 QualType T = TDecl->getUnderlyingType(); 186 if (T->isObjCInterfaceType()) { 187 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) { 188 ClassName = IDecl->getIdentifier(); 189 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName); 190 } 191 } 192 } 193 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); 194 if (CDecl == 0) { 195 Diag(ClassLocation, diag::warn_undef_interface) << ClassName; 196 if (CDeclU) 197 Diag(CDeclU->getLocation(), diag::note_previous_declaration); 198 return DeclPtrTy(); 199 } 200 201 // Everything checked out, instantiate a new alias declaration AST. 202 ObjCCompatibleAliasDecl *AliasDecl = 203 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); 204 205 if (!CheckObjCDeclScope(AliasDecl)) 206 PushOnScopeChains(AliasDecl, TUScope); 207 208 return DeclPtrTy::make(AliasDecl); 209} 210 211void Sema::CheckForwardProtocolDeclarationForCircularDependency( 212 IdentifierInfo *PName, 213 SourceLocation &Ploc, SourceLocation PrevLoc, 214 const ObjCList<ObjCProtocolDecl> &PList) 215{ 216 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), 217 E = PList.end(); I != E; ++I) { 218 219 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) { 220 if (PDecl->getIdentifier() == PName) { 221 Diag(Ploc, diag::err_protocol_has_circular_dependency); 222 Diag(PrevLoc, diag::note_previous_definition); 223 } 224 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, 225 PDecl->getLocation(), PDecl->getReferencedProtocols()); 226 } 227 } 228} 229 230Sema::DeclPtrTy 231Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, 232 IdentifierInfo *ProtocolName, 233 SourceLocation ProtocolLoc, 234 const DeclPtrTy *ProtoRefs, 235 unsigned NumProtoRefs, 236 SourceLocation EndProtoLoc, 237 AttributeList *AttrList) { 238 // FIXME: Deal with AttrList. 239 assert(ProtocolName && "Missing protocol identifier"); 240 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName); 241 if (PDecl) { 242 // Protocol already seen. Better be a forward protocol declaration 243 if (!PDecl->isForwardDecl()) { 244 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; 245 Diag(PDecl->getLocation(), diag::note_previous_definition); 246 // Just return the protocol we already had. 247 // FIXME: don't leak the objects passed in! 248 return DeclPtrTy::make(PDecl); 249 } 250 ObjCList<ObjCProtocolDecl> PList; 251 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); 252 CheckForwardProtocolDeclarationForCircularDependency( 253 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList); 254 PList.Destroy(Context); 255 256 // Make sure the cached decl gets a valid start location. 257 PDecl->setLocation(AtProtoInterfaceLoc); 258 PDecl->setForwardDecl(false); 259 } else { 260 PDecl = ObjCProtocolDecl::Create(Context, CurContext, 261 AtProtoInterfaceLoc,ProtocolName); 262 PushOnScopeChains(PDecl, TUScope); 263 PDecl->setForwardDecl(false); 264 } 265 if (AttrList) 266 ProcessDeclAttributeList(PDecl, AttrList); 267 if (NumProtoRefs) { 268 /// Check then save referenced protocols. 269 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context); 270 PDecl->setLocEnd(EndProtoLoc); 271 } 272 273 CheckObjCDeclScope(PDecl); 274 return DeclPtrTy::make(PDecl); 275} 276 277/// FindProtocolDeclaration - This routine looks up protocols and 278/// issues an error if they are not declared. It returns list of 279/// protocol declarations in its 'Protocols' argument. 280void 281Sema::FindProtocolDeclaration(bool WarnOnDeclarations, 282 const IdentifierLocPair *ProtocolId, 283 unsigned NumProtocols, 284 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) { 285 for (unsigned i = 0; i != NumProtocols; ++i) { 286 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first); 287 if (!PDecl) { 288 Diag(ProtocolId[i].second, diag::err_undeclared_protocol) 289 << ProtocolId[i].first; 290 continue; 291 } 292 293 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second); 294 295 // If this is a forward declaration and we are supposed to warn in this 296 // case, do it. 297 if (WarnOnDeclarations && PDecl->isForwardDecl()) 298 Diag(ProtocolId[i].second, diag::warn_undef_protocolref) 299 << ProtocolId[i].first; 300 Protocols.push_back(DeclPtrTy::make(PDecl)); 301 } 302} 303 304/// DiagnosePropertyMismatch - Compares two properties for their 305/// attributes and types and warns on a variety of inconsistencies. 306/// 307void 308Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, 309 ObjCPropertyDecl *SuperProperty, 310 const IdentifierInfo *inheritedName) { 311 ObjCPropertyDecl::PropertyAttributeKind CAttr = 312 Property->getPropertyAttributes(); 313 ObjCPropertyDecl::PropertyAttributeKind SAttr = 314 SuperProperty->getPropertyAttributes(); 315 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) 316 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) 317 Diag(Property->getLocation(), diag::warn_readonly_property) 318 << Property->getDeclName() << inheritedName; 319 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) 320 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) 321 Diag(Property->getLocation(), diag::warn_property_attribute) 322 << Property->getDeclName() << "copy" << inheritedName; 323 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain) 324 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain)) 325 Diag(Property->getLocation(), diag::warn_property_attribute) 326 << Property->getDeclName() << "retain" << inheritedName; 327 328 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) 329 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) 330 Diag(Property->getLocation(), diag::warn_property_attribute) 331 << Property->getDeclName() << "atomic" << inheritedName; 332 if (Property->getSetterName() != SuperProperty->getSetterName()) 333 Diag(Property->getLocation(), diag::warn_property_attribute) 334 << Property->getDeclName() << "setter" << inheritedName; 335 if (Property->getGetterName() != SuperProperty->getGetterName()) 336 Diag(Property->getLocation(), diag::warn_property_attribute) 337 << Property->getDeclName() << "getter" << inheritedName; 338 339 QualType LHSType = 340 Context.getCanonicalType(SuperProperty->getType()); 341 QualType RHSType = 342 Context.getCanonicalType(Property->getType()); 343 344 if (!Context.typesAreCompatible(LHSType, RHSType)) { 345 // FIXME: Incorporate this test with typesAreCompatible. 346 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType()) 347 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false)) 348 return; 349 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) 350 << Property->getType() << SuperProperty->getType() << inheritedName; 351 } 352} 353 354/// ComparePropertiesInBaseAndSuper - This routine compares property 355/// declarations in base and its super class, if any, and issues 356/// diagnostics in a variety of inconsistant situations. 357/// 358void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { 359 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); 360 if (!SDecl) 361 return; 362 // FIXME: O(N^2) 363 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context), 364 E = SDecl->prop_end(Context); S != E; ++S) { 365 ObjCPropertyDecl *SuperPDecl = (*S); 366 // Does property in super class has declaration in current class? 367 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context), 368 E = IDecl->prop_end(Context); I != E; ++I) { 369 ObjCPropertyDecl *PDecl = (*I); 370 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) 371 DiagnosePropertyMismatch(PDecl, SuperPDecl, 372 SDecl->getIdentifier()); 373 } 374 } 375} 376 377/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list 378/// of properties declared in a protocol and adds them to the list 379/// of properties for current class/category if it is not there already. 380void 381Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl, 382 ObjCProtocolDecl *PDecl) { 383 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); 384 if (!IDecl) { 385 // Category 386 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); 387 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass"); 388 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context), 389 E = PDecl->prop_end(Context); P != E; ++P) { 390 ObjCPropertyDecl *Pr = (*P); 391 ObjCCategoryDecl::prop_iterator CP, CE; 392 // Is this property already in category's list of properties? 393 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context); 394 CP != CE; ++CP) 395 if ((*CP)->getIdentifier() == Pr->getIdentifier()) 396 break; 397 if (CP != CE) 398 // Property protocol already exist in class. Diagnose any mismatch. 399 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); 400 } 401 return; 402 } 403 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context), 404 E = PDecl->prop_end(Context); P != E; ++P) { 405 ObjCPropertyDecl *Pr = (*P); 406 ObjCInterfaceDecl::prop_iterator CP, CE; 407 // Is this property already in class's list of properties? 408 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context); 409 CP != CE; ++CP) 410 if ((*CP)->getIdentifier() == Pr->getIdentifier()) 411 break; 412 if (CP != CE) 413 // Property protocol already exist in class. Diagnose any mismatch. 414 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); 415 } 416} 417 418/// MergeProtocolPropertiesIntoClass - This routine merges properties 419/// declared in 'MergeItsProtocols' objects (which can be a class or an 420/// inherited protocol into the list of properties for class/category 'CDecl' 421/// 422void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl, 423 DeclPtrTy MergeItsProtocols) { 424 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>(); 425 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); 426 427 if (!IDecl) { 428 // Category 429 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); 430 assert (CatDecl && "MergeProtocolPropertiesIntoClass"); 431 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { 432 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(), 433 E = MDecl->protocol_end(); P != E; ++P) 434 // Merge properties of category (*P) into IDECL's 435 MergeOneProtocolPropertiesIntoClass(CatDecl, *P); 436 437 // Go thru the list of protocols for this category and recursively merge 438 // their properties into this class as well. 439 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(), 440 E = CatDecl->protocol_end(); P != E; ++P) 441 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P)); 442 } else { 443 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); 444 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), 445 E = MD->protocol_end(); P != E; ++P) 446 MergeOneProtocolPropertiesIntoClass(CatDecl, *P); 447 } 448 return; 449 } 450 451 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 452 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(), 453 E = MDecl->protocol_end(); P != E; ++P) 454 // Merge properties of class (*P) into IDECL's 455 MergeOneProtocolPropertiesIntoClass(IDecl, *P); 456 457 // Go thru the list of protocols for this class and recursively merge 458 // their properties into this class as well. 459 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(), 460 E = IDecl->protocol_end(); P != E; ++P) 461 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P)); 462 } else { 463 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); 464 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), 465 E = MD->protocol_end(); P != E; ++P) 466 MergeOneProtocolPropertiesIntoClass(IDecl, *P); 467 } 468} 469 470/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of 471/// a class method in its extension. 472/// 473void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, 474 ObjCInterfaceDecl *ID) { 475 if (!ID) 476 return; // Possibly due to previous error 477 478 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; 479 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context), 480 e = ID->meth_end(Context); i != e; ++i) { 481 ObjCMethodDecl *MD = *i; 482 MethodMap[MD->getSelector()] = MD; 483 } 484 485 if (MethodMap.empty()) 486 return; 487 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context), 488 e = CAT->meth_end(Context); i != e; ++i) { 489 ObjCMethodDecl *Method = *i; 490 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; 491 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) { 492 Diag(Method->getLocation(), diag::err_duplicate_method_decl) 493 << Method->getDeclName(); 494 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 495 } 496 } 497} 498 499/// ActOnForwardProtocolDeclaration - Handle @protocol foo; 500Action::DeclPtrTy 501Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, 502 const IdentifierLocPair *IdentList, 503 unsigned NumElts, 504 AttributeList *attrList) { 505 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; 506 507 for (unsigned i = 0; i != NumElts; ++i) { 508 IdentifierInfo *Ident = IdentList[i].first; 509 ObjCProtocolDecl *PDecl = LookupProtocol(Ident); 510 if (PDecl == 0) { // Not already seen? 511 PDecl = ObjCProtocolDecl::Create(Context, CurContext, 512 IdentList[i].second, Ident); 513 PushOnScopeChains(PDecl, TUScope); 514 } 515 if (attrList) 516 ProcessDeclAttributeList(PDecl, attrList); 517 Protocols.push_back(PDecl); 518 } 519 520 ObjCForwardProtocolDecl *PDecl = 521 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc, 522 &Protocols[0], Protocols.size()); 523 CurContext->addDecl(Context, PDecl); 524 CheckObjCDeclScope(PDecl); 525 return DeclPtrTy::make(PDecl); 526} 527 528Sema::DeclPtrTy Sema:: 529ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, 530 IdentifierInfo *ClassName, SourceLocation ClassLoc, 531 IdentifierInfo *CategoryName, 532 SourceLocation CategoryLoc, 533 const DeclPtrTy *ProtoRefs, 534 unsigned NumProtoRefs, 535 SourceLocation EndProtoLoc) { 536 ObjCCategoryDecl *CDecl = 537 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName); 538 // FIXME: PushOnScopeChains? 539 CurContext->addDecl(Context, CDecl); 540 541 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); 542 /// Check that class of this category is already completely declared. 543 if (!IDecl || IDecl->isForwardDecl()) { 544 CDecl->setInvalidDecl(); 545 Diag(ClassLoc, diag::err_undef_interface) << ClassName; 546 return DeclPtrTy::make(CDecl); 547 } 548 549 CDecl->setClassInterface(IDecl); 550 551 // If the interface is deprecated, warn about it. 552 (void)DiagnoseUseOfDecl(IDecl, ClassLoc); 553 554 /// Check for duplicate interface declaration for this category 555 ObjCCategoryDecl *CDeclChain; 556 for (CDeclChain = IDecl->getCategoryList(); CDeclChain; 557 CDeclChain = CDeclChain->getNextClassCategory()) { 558 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) { 559 Diag(CategoryLoc, diag::warn_dup_category_def) 560 << ClassName << CategoryName; 561 Diag(CDeclChain->getLocation(), diag::note_previous_definition); 562 break; 563 } 564 } 565 if (!CDeclChain) 566 CDecl->insertNextClassCategory(); 567 568 if (NumProtoRefs) { 569 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context); 570 CDecl->setLocEnd(EndProtoLoc); 571 } 572 573 CheckObjCDeclScope(CDecl); 574 return DeclPtrTy::make(CDecl); 575} 576 577/// ActOnStartCategoryImplementation - Perform semantic checks on the 578/// category implementation declaration and build an ObjCCategoryImplDecl 579/// object. 580Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation( 581 SourceLocation AtCatImplLoc, 582 IdentifierInfo *ClassName, SourceLocation ClassLoc, 583 IdentifierInfo *CatName, SourceLocation CatLoc) { 584 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); 585 ObjCCategoryImplDecl *CDecl = 586 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName, 587 IDecl); 588 /// Check that class of this category is already completely declared. 589 if (!IDecl || IDecl->isForwardDecl()) 590 Diag(ClassLoc, diag::err_undef_interface) << ClassName; 591 592 // FIXME: PushOnScopeChains? 593 CurContext->addDecl(Context, CDecl); 594 595 /// TODO: Check that CatName, category name, is not used in another 596 // implementation. 597 ObjCCategoryImpls.push_back(CDecl); 598 599 CheckObjCDeclScope(CDecl); 600 return DeclPtrTy::make(CDecl); 601} 602 603Sema::DeclPtrTy Sema::ActOnStartClassImplementation( 604 SourceLocation AtClassImplLoc, 605 IdentifierInfo *ClassName, SourceLocation ClassLoc, 606 IdentifierInfo *SuperClassname, 607 SourceLocation SuperClassLoc) { 608 ObjCInterfaceDecl* IDecl = 0; 609 // Check for another declaration kind with the same name. 610 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName); 611 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 612 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; 613 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 614 } else { 615 // Is there an interface declaration of this class; if not, warn! 616 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 617 if (!IDecl || IDecl->isForwardDecl()) { 618 Diag(ClassLoc, diag::warn_undef_interface) << ClassName; 619 IDecl = 0; 620 } 621 } 622 623 // Check that super class name is valid class name 624 ObjCInterfaceDecl* SDecl = 0; 625 if (SuperClassname) { 626 // Check if a different kind of symbol declared in this scope. 627 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName); 628 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 629 Diag(SuperClassLoc, diag::err_redefinition_different_kind) 630 << SuperClassname; 631 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 632 } else { 633 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 634 if (!SDecl) 635 Diag(SuperClassLoc, diag::err_undef_superclass) 636 << SuperClassname << ClassName; 637 else if (IDecl && IDecl->getSuperClass() != SDecl) { 638 // This implementation and its interface do not have the same 639 // super class. 640 Diag(SuperClassLoc, diag::err_conflicting_super_class) 641 << SDecl->getDeclName(); 642 Diag(SDecl->getLocation(), diag::note_previous_definition); 643 } 644 } 645 } 646 647 if (!IDecl) { 648 // Legacy case of @implementation with no corresponding @interface. 649 // Build, chain & install the interface decl into the identifier. 650 651 // FIXME: Do we support attributes on the @implementation? If so 652 // we should copy them over. 653 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, 654 ClassName, ClassLoc, false, true); 655 IDecl->setSuperClass(SDecl); 656 IDecl->setLocEnd(ClassLoc); 657 658 PushOnScopeChains(IDecl, TUScope); 659 } else { 660 // Mark the interface as being completed, even if it was just as 661 // @class ....; 662 // declaration; the user cannot reopen it. 663 IDecl->setForwardDecl(false); 664 } 665 666 ObjCImplementationDecl* IMPDecl = 667 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc, 668 IDecl, SDecl); 669 670 if (CheckObjCDeclScope(IMPDecl)) 671 return DeclPtrTy::make(IMPDecl); 672 673 // Check that there is no duplicate implementation of this class. 674 if (LookupObjCImplementation(ClassName)) 675 // FIXME: Don't leak everything! 676 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; 677 else // add it to the list. 678 PushOnScopeChains(IMPDecl, TUScope); 679 return DeclPtrTy::make(IMPDecl); 680} 681 682void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, 683 ObjCIvarDecl **ivars, unsigned numIvars, 684 SourceLocation RBrace) { 685 assert(ImpDecl && "missing implementation decl"); 686 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); 687 if (!IDecl) 688 return; 689 /// Check case of non-existing @interface decl. 690 /// (legacy objective-c @implementation decl without an @interface decl). 691 /// Add implementations's ivar to the synthesize class's ivar list. 692 if (IDecl->isImplicitInterfaceDecl()) { 693 IDecl->setIVarList(ivars, numIvars, Context); 694 IDecl->setLocEnd(RBrace); 695 return; 696 } 697 // If implementation has empty ivar list, just return. 698 if (numIvars == 0) 699 return; 700 701 assert(ivars && "missing @implementation ivars"); 702 703 // Check interface's Ivar list against those in the implementation. 704 // names and types must match. 705 // 706 unsigned j = 0; 707 ObjCInterfaceDecl::ivar_iterator 708 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); 709 for (; numIvars > 0 && IVI != IVE; ++IVI) { 710 ObjCIvarDecl* ImplIvar = ivars[j++]; 711 ObjCIvarDecl* ClsIvar = *IVI; 712 assert (ImplIvar && "missing implementation ivar"); 713 assert (ClsIvar && "missing class ivar"); 714 715 // First, make sure the types match. 716 if (Context.getCanonicalType(ImplIvar->getType()) != 717 Context.getCanonicalType(ClsIvar->getType())) { 718 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) 719 << ImplIvar->getIdentifier() 720 << ImplIvar->getType() << ClsIvar->getType(); 721 Diag(ClsIvar->getLocation(), diag::note_previous_definition); 722 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) { 723 Expr *ImplBitWidth = ImplIvar->getBitWidth(); 724 Expr *ClsBitWidth = ClsIvar->getBitWidth(); 725 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() != 726 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) { 727 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth) 728 << ImplIvar->getIdentifier(); 729 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition); 730 } 731 } 732 // Make sure the names are identical. 733 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { 734 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) 735 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); 736 Diag(ClsIvar->getLocation(), diag::note_previous_definition); 737 } 738 --numIvars; 739 } 740 741 if (numIvars > 0) 742 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); 743 else if (IVI != IVE) 744 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); 745} 746 747void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, 748 bool &IncompleteImpl) { 749 if (!IncompleteImpl) { 750 Diag(ImpLoc, diag::warn_incomplete_impl); 751 IncompleteImpl = true; 752 } 753 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName(); 754} 755 756void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, 757 ObjCMethodDecl *IntfMethodDecl) { 758 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(), 759 ImpMethodDecl->getResultType())) { 760 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types) 761 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType() 762 << ImpMethodDecl->getResultType(); 763 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition); 764 } 765 766 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), 767 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end(); 768 IM != EM; ++IM, ++IF) { 769 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType())) 770 continue; 771 772 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types) 773 << ImpMethodDecl->getDeclName() << (*IF)->getType() 774 << (*IM)->getType(); 775 Diag((*IF)->getLocation(), diag::note_previous_definition); 776 } 777} 778 779/// isPropertyReadonly - Return true if property is readonly, by searching 780/// for the property in the class and in its categories and implementations 781/// 782bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, 783 ObjCInterfaceDecl *IDecl) { 784 // by far the most common case. 785 if (!PDecl->isReadOnly()) 786 return false; 787 // Even if property is ready only, if interface has a user defined setter, 788 // it is not considered read only. 789 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName())) 790 return false; 791 792 // Main class has the property as 'readonly'. Must search 793 // through the category list to see if the property's 794 // attribute has been over-ridden to 'readwrite'. 795 for (ObjCCategoryDecl *Category = IDecl->getCategoryList(); 796 Category; Category = Category->getNextClassCategory()) { 797 // Even if property is ready only, if a category has a user defined setter, 798 // it is not considered read only. 799 if (Category->getInstanceMethod(Context, PDecl->getSetterName())) 800 return false; 801 ObjCPropertyDecl *P = 802 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier()); 803 if (P && !P->isReadOnly()) 804 return false; 805 } 806 807 // Also, check for definition of a setter method in the implementation if 808 // all else failed. 809 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { 810 if (ObjCImplementationDecl *IMD = 811 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { 812 if (IMD->getInstanceMethod(Context, PDecl->getSetterName())) 813 return false; 814 } 815 else if (ObjCCategoryImplDecl *CIMD = 816 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { 817 if (CIMD->getInstanceMethod(Context, PDecl->getSetterName())) 818 return false; 819 } 820 } 821 // Lastly, look through the implementation (if one is in scope). 822 if (ObjCImplementationDecl *ImpDecl 823 = LookupObjCImplementation(IDecl->getIdentifier())) 824 if (ImpDecl->getInstanceMethod(Context, PDecl->getSetterName())) 825 return false; 826 // If all fails, look at the super class. 827 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass()) 828 return isPropertyReadonly(PDecl, SIDecl); 829 return true; 830} 831 832/// FIXME: Type hierarchies in Objective-C can be deep. We could most 833/// likely improve the efficiency of selector lookups and type 834/// checking by associating with each protocol / interface / category 835/// the flattened instance tables. If we used an immutable set to keep 836/// the table then it wouldn't add significant memory cost and it 837/// would be handy for lookups. 838 839/// CheckProtocolMethodDefs - This routine checks unimplemented methods 840/// Declared in protocol, and those referenced by it. 841void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, 842 ObjCProtocolDecl *PDecl, 843 bool& IncompleteImpl, 844 const llvm::DenseSet<Selector> &InsMap, 845 const llvm::DenseSet<Selector> &ClsMap, 846 ObjCInterfaceDecl *IDecl) { 847 ObjCInterfaceDecl *Super = IDecl->getSuperClass(); 848 849 // If a method lookup fails locally we still need to look and see if 850 // the method was implemented by a base class or an inherited 851 // protocol. This lookup is slow, but occurs rarely in correct code 852 // and otherwise would terminate in a warning. 853 854 // check unimplemented instance methods. 855 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context), 856 E = PDecl->instmeth_end(Context); I != E; ++I) { 857 ObjCMethodDecl *method = *I; 858 if (method->getImplementationControl() != ObjCMethodDecl::Optional && 859 !method->isSynthesized() && !InsMap.count(method->getSelector()) && 860 (!Super || 861 !Super->lookupInstanceMethod(Context, method->getSelector()))) { 862 // Ugly, but necessary. Method declared in protcol might have 863 // have been synthesized due to a property declared in the class which 864 // uses the protocol. 865 ObjCMethodDecl *MethodInClass = 866 IDecl->lookupInstanceMethod(Context, method->getSelector()); 867 if (!MethodInClass || !MethodInClass->isSynthesized()) 868 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); 869 } 870 } 871 // check unimplemented class methods 872 for (ObjCProtocolDecl::classmeth_iterator 873 I = PDecl->classmeth_begin(Context), 874 E = PDecl->classmeth_end(Context); 875 I != E; ++I) { 876 ObjCMethodDecl *method = *I; 877 if (method->getImplementationControl() != ObjCMethodDecl::Optional && 878 !ClsMap.count(method->getSelector()) && 879 (!Super || !Super->lookupClassMethod(Context, method->getSelector()))) 880 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); 881 } 882 // Check on this protocols's referenced protocols, recursively. 883 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), 884 E = PDecl->protocol_end(); PI != E; ++PI) 885 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl); 886} 887 888/// MatchAllMethodDeclarations - Check methods declaraed in interface or 889/// or protocol against those declared in their implementations. 890/// 891void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap, 892 const llvm::DenseSet<Selector> &ClsMap, 893 llvm::DenseSet<Selector> &InsMapSeen, 894 llvm::DenseSet<Selector> &ClsMapSeen, 895 ObjCImplDecl* IMPDecl, 896 ObjCContainerDecl* CDecl, 897 bool &IncompleteImpl, 898 bool ImmediateClass) 899{ 900 // Check and see if instance methods in class interface have been 901 // implemented in the implementation class. If so, their types match. 902 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context), 903 E = CDecl->instmeth_end(Context); I != E; ++I) { 904 if (InsMapSeen.count((*I)->getSelector())) 905 continue; 906 InsMapSeen.insert((*I)->getSelector()); 907 if (!(*I)->isSynthesized() && 908 !InsMap.count((*I)->getSelector())) { 909 if (ImmediateClass) 910 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); 911 continue; 912 } 913 else { 914 ObjCMethodDecl *ImpMethodDecl = 915 IMPDecl->getInstanceMethod(Context, (*I)->getSelector()); 916 ObjCMethodDecl *IntfMethodDecl = 917 CDecl->getInstanceMethod(Context, (*I)->getSelector()); 918 assert(IntfMethodDecl && 919 "IntfMethodDecl is null in ImplMethodsVsClassMethods"); 920 // ImpMethodDecl may be null as in a @dynamic property. 921 if (ImpMethodDecl) 922 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); 923 } 924 } 925 926 // Check and see if class methods in class interface have been 927 // implemented in the implementation class. If so, their types match. 928 for (ObjCInterfaceDecl::classmeth_iterator 929 I = CDecl->classmeth_begin(Context), 930 E = CDecl->classmeth_end(Context); 931 I != E; ++I) { 932 if (ClsMapSeen.count((*I)->getSelector())) 933 continue; 934 ClsMapSeen.insert((*I)->getSelector()); 935 if (!ClsMap.count((*I)->getSelector())) { 936 if (ImmediateClass) 937 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); 938 } 939 else { 940 ObjCMethodDecl *ImpMethodDecl = 941 IMPDecl->getClassMethod(Context, (*I)->getSelector()); 942 ObjCMethodDecl *IntfMethodDecl = 943 CDecl->getClassMethod(Context, (*I)->getSelector()); 944 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); 945 } 946 } 947 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { 948 // Check for any implementation of a methods declared in protocol. 949 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(), 950 E = I->protocol_end(); PI != E; ++PI) 951 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, 952 IMPDecl, 953 (*PI), IncompleteImpl, false); 954 if (I->getSuperClass()) 955 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, 956 IMPDecl, 957 I->getSuperClass(), IncompleteImpl, false); 958 } 959} 960 961void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl, 962 ObjCContainerDecl* CDecl, 963 bool IncompleteImpl) { 964 llvm::DenseSet<Selector> InsMap; 965 // Check and see if instance methods in class interface have been 966 // implemented in the implementation class. 967 for (ObjCImplementationDecl::instmeth_iterator 968 I = IMPDecl->instmeth_begin(Context), 969 E = IMPDecl->instmeth_end(Context); I != E; ++I) 970 InsMap.insert((*I)->getSelector()); 971 972 // Check and see if properties declared in the interface have either 1) 973 // an implementation or 2) there is a @synthesize/@dynamic implementation 974 // of the property in the @implementation. 975 if (isa<ObjCInterfaceDecl>(CDecl)) 976 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context), 977 E = CDecl->prop_end(Context); P != E; ++P) { 978 ObjCPropertyDecl *Prop = (*P); 979 if (Prop->isInvalidDecl()) 980 continue; 981 ObjCPropertyImplDecl *PI = 0; 982 // Is there a matching propery synthesize/dynamic? 983 for (ObjCImplDecl::propimpl_iterator 984 I = IMPDecl->propimpl_begin(Context), 985 EI = IMPDecl->propimpl_end(Context); I != EI; ++I) 986 if ((*I)->getPropertyDecl() == Prop) { 987 PI = (*I); 988 break; 989 } 990 if (PI) 991 continue; 992 if (!InsMap.count(Prop->getGetterName())) { 993 Diag(Prop->getLocation(), 994 diag::warn_setter_getter_impl_required) 995 << Prop->getDeclName() << Prop->getGetterName(); 996 Diag(IMPDecl->getLocation(), 997 diag::note_property_impl_required); 998 } 999 1000 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) { 1001 Diag(Prop->getLocation(), 1002 diag::warn_setter_getter_impl_required) 1003 << Prop->getDeclName() << Prop->getSetterName(); 1004 Diag(IMPDecl->getLocation(), 1005 diag::note_property_impl_required); 1006 } 1007 } 1008 1009 llvm::DenseSet<Selector> ClsMap; 1010 for (ObjCImplementationDecl::classmeth_iterator 1011 I = IMPDecl->classmeth_begin(Context), 1012 E = IMPDecl->classmeth_end(Context); I != E; ++I) 1013 ClsMap.insert((*I)->getSelector()); 1014 1015 // Check for type conflict of methods declared in a class/protocol and 1016 // its implementation; if any. 1017 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen; 1018 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, 1019 IMPDecl, CDecl, 1020 IncompleteImpl, true); 1021 1022 // Check the protocol list for unimplemented methods in the @implementation 1023 // class. 1024 // Check and see if class methods in class interface have been 1025 // implemented in the implementation class. 1026 1027 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { 1028 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(), 1029 E = I->protocol_end(); PI != E; ++PI) 1030 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, 1031 InsMap, ClsMap, I); 1032 // Check class extensions (unnamed categories) 1033 for (ObjCCategoryDecl *Categories = I->getCategoryList(); 1034 Categories; Categories = Categories->getNextClassCategory()) { 1035 if (!Categories->getIdentifier()) { 1036 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl); 1037 break; 1038 } 1039 } 1040 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { 1041 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(), 1042 E = C->protocol_end(); PI != E; ++PI) 1043 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, 1044 InsMap, ClsMap, C->getClassInterface()); 1045 } else 1046 assert(false && "invalid ObjCContainerDecl type."); 1047} 1048 1049/// ActOnForwardClassDeclaration - 1050Action::DeclPtrTy 1051Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, 1052 IdentifierInfo **IdentList, 1053 unsigned NumElts) { 1054 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; 1055 1056 for (unsigned i = 0; i != NumElts; ++i) { 1057 // Check for another declaration kind with the same name. 1058 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName); 1059 if (PrevDecl && PrevDecl->isTemplateParameter()) { 1060 // Maybe we will complain about the shadowed template parameter. 1061 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl); 1062 // Just pretend that we didn't see the previous declaration. 1063 PrevDecl = 0; 1064 } 1065 1066 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { 1067 // GCC apparently allows the following idiom: 1068 // 1069 // typedef NSObject < XCElementTogglerP > XCElementToggler; 1070 // @class XCElementToggler; 1071 // 1072 // FIXME: Make an extension? 1073 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl); 1074 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) { 1075 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; 1076 Diag(PrevDecl->getLocation(), diag::note_previous_definition); 1077 } 1078 } 1079 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); 1080 if (!IDecl) { // Not already seen? Make a forward decl. 1081 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, 1082 IdentList[i], SourceLocation(), true); 1083 PushOnScopeChains(IDecl, TUScope); 1084 } 1085 1086 Interfaces.push_back(IDecl); 1087 } 1088 1089 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc, 1090 &Interfaces[0], 1091 Interfaces.size()); 1092 CurContext->addDecl(Context, CDecl); 1093 CheckObjCDeclScope(CDecl); 1094 return DeclPtrTy::make(CDecl); 1095} 1096 1097 1098/// MatchTwoMethodDeclarations - Checks that two methods have matching type and 1099/// returns true, or false, accordingly. 1100/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons 1101bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, 1102 const ObjCMethodDecl *PrevMethod, 1103 bool matchBasedOnSizeAndAlignment) { 1104 QualType T1 = Context.getCanonicalType(Method->getResultType()); 1105 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType()); 1106 1107 if (T1 != T2) { 1108 // The result types are different. 1109 if (!matchBasedOnSizeAndAlignment) 1110 return false; 1111 // Incomplete types don't have a size and alignment. 1112 if (T1->isIncompleteType() || T2->isIncompleteType()) 1113 return false; 1114 // Check is based on size and alignment. 1115 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) 1116 return false; 1117 } 1118 1119 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), 1120 E = Method->param_end(); 1121 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin(); 1122 1123 for (; ParamI != E; ++ParamI, ++PrevI) { 1124 assert(PrevI != PrevMethod->param_end() && "Param mismatch"); 1125 T1 = Context.getCanonicalType((*ParamI)->getType()); 1126 T2 = Context.getCanonicalType((*PrevI)->getType()); 1127 if (T1 != T2) { 1128 // The result types are different. 1129 if (!matchBasedOnSizeAndAlignment) 1130 return false; 1131 // Incomplete types don't have a size and alignment. 1132 if (T1->isIncompleteType() || T2->isIncompleteType()) 1133 return false; 1134 // Check is based on size and alignment. 1135 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) 1136 return false; 1137 } 1138 } 1139 return true; 1140} 1141 1142/// \brief Read the contents of the instance and factory method pools 1143/// for a given selector from external storage. 1144/// 1145/// This routine should only be called once, when neither the instance 1146/// nor the factory method pool has an entry for this selector. 1147Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel, 1148 bool isInstance) { 1149 assert(ExternalSource && "We need an external AST source"); 1150 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() && 1151 "Selector data already loaded into the instance method pool"); 1152 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() && 1153 "Selector data already loaded into the factory method pool"); 1154 1155 // Read the method list from the external source. 1156 std::pair<ObjCMethodList, ObjCMethodList> Methods 1157 = ExternalSource->ReadMethodPool(Sel); 1158 1159 if (isInstance) { 1160 if (Methods.second.Method) 1161 FactoryMethodPool[Sel] = Methods.second; 1162 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first; 1163 } 1164 1165 if (Methods.first.Method) 1166 InstanceMethodPool[Sel] = Methods.first; 1167 1168 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first; 1169} 1170 1171void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) { 1172 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos 1173 = InstanceMethodPool.find(Method->getSelector()); 1174 if (Pos == InstanceMethodPool.end()) { 1175 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector())) 1176 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true); 1177 else 1178 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(), 1179 ObjCMethodList())).first; 1180 } 1181 1182 ObjCMethodList &Entry = Pos->second; 1183 if (Entry.Method == 0) { 1184 // Haven't seen a method with this selector name yet - add it. 1185 Entry.Method = Method; 1186 Entry.Next = 0; 1187 return; 1188 } 1189 1190 // We've seen a method with this name, see if we have already seen this type 1191 // signature. 1192 for (ObjCMethodList *List = &Entry; List; List = List->Next) 1193 if (MatchTwoMethodDeclarations(Method, List->Method)) 1194 return; 1195 1196 // We have a new signature for an existing method - add it. 1197 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". 1198 Entry.Next = new ObjCMethodList(Method, Entry.Next); 1199} 1200 1201// FIXME: Finish implementing -Wno-strict-selector-match. 1202ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel, 1203 SourceRange R) { 1204 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos 1205 = InstanceMethodPool.find(Sel); 1206 if (Pos == InstanceMethodPool.end()) { 1207 if (ExternalSource && !FactoryMethodPool.count(Sel)) 1208 Pos = ReadMethodPool(Sel, /*isInstance=*/true); 1209 else 1210 return 0; 1211 } 1212 1213 ObjCMethodList &MethList = Pos->second; 1214 bool issueWarning = false; 1215 1216 if (MethList.Method && MethList.Next) { 1217 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 1218 // This checks if the methods differ by size & alignment. 1219 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) 1220 issueWarning = true; 1221 } 1222 if (issueWarning && (MethList.Method && MethList.Next)) { 1223 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; 1224 Diag(MethList.Method->getLocStart(), diag::note_using_decl) 1225 << MethList.Method->getSourceRange(); 1226 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 1227 Diag(Next->Method->getLocStart(), diag::note_also_found_decl) 1228 << Next->Method->getSourceRange(); 1229 } 1230 return MethList.Method; 1231} 1232 1233void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { 1234 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos 1235 = FactoryMethodPool.find(Method->getSelector()); 1236 if (Pos == FactoryMethodPool.end()) { 1237 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector())) 1238 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false); 1239 else 1240 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(), 1241 ObjCMethodList())).first; 1242 } 1243 1244 ObjCMethodList &FirstMethod = Pos->second; 1245 if (!FirstMethod.Method) { 1246 // Haven't seen a method with this selector name yet - add it. 1247 FirstMethod.Method = Method; 1248 FirstMethod.Next = 0; 1249 } else { 1250 // We've seen a method with this name, now check the type signature(s). 1251 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); 1252 1253 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; 1254 Next = Next->Next) 1255 match = MatchTwoMethodDeclarations(Method, Next->Method); 1256 1257 if (!match) { 1258 // We have a new signature for an existing method - add it. 1259 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". 1260 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); 1261 FirstMethod.Next = OMI; 1262 } 1263 } 1264} 1265 1266ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel, 1267 SourceRange R) { 1268 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos 1269 = FactoryMethodPool.find(Sel); 1270 if (Pos == FactoryMethodPool.end()) { 1271 if (ExternalSource && !InstanceMethodPool.count(Sel)) 1272 Pos = ReadMethodPool(Sel, /*isInstance=*/false); 1273 else 1274 return 0; 1275 } 1276 1277 ObjCMethodList &MethList = Pos->second; 1278 bool issueWarning = false; 1279 1280 if (MethList.Method && MethList.Next) { 1281 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 1282 // This checks if the methods differ by size & alignment. 1283 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) 1284 issueWarning = true; 1285 } 1286 if (issueWarning && (MethList.Method && MethList.Next)) { 1287 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; 1288 Diag(MethList.Method->getLocStart(), diag::note_using_decl) 1289 << MethList.Method->getSourceRange(); 1290 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) 1291 Diag(Next->Method->getLocStart(), diag::note_also_found_decl) 1292 << Next->Method->getSourceRange(); 1293 } 1294 return MethList.Method; 1295} 1296 1297/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods 1298/// have the property type and issue diagnostics if they don't. 1299/// Also synthesize a getter/setter method if none exist (and update the 1300/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized 1301/// methods is the "right" thing to do. 1302void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, 1303 ObjCContainerDecl *CD) { 1304 ObjCMethodDecl *GetterMethod, *SetterMethod; 1305 1306 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName()); 1307 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName()); 1308 1309 if (GetterMethod && 1310 GetterMethod->getResultType() != property->getType()) { 1311 Diag(property->getLocation(), 1312 diag::err_accessor_property_type_mismatch) 1313 << property->getDeclName() 1314 << GetterMethod->getSelector(); 1315 Diag(GetterMethod->getLocation(), diag::note_declared_at); 1316 } 1317 1318 if (SetterMethod) { 1319 if (Context.getCanonicalType(SetterMethod->getResultType()) 1320 != Context.VoidTy) 1321 Diag(SetterMethod->getLocation(), diag::err_setter_type_void); 1322 if (SetterMethod->param_size() != 1 || 1323 ((*SetterMethod->param_begin())->getType() != property->getType())) { 1324 Diag(property->getLocation(), 1325 diag::err_accessor_property_type_mismatch) 1326 << property->getDeclName() 1327 << SetterMethod->getSelector(); 1328 Diag(SetterMethod->getLocation(), diag::note_declared_at); 1329 } 1330 } 1331 1332 // Synthesize getter/setter methods if none exist. 1333 // Find the default getter and if one not found, add one. 1334 // FIXME: The synthesized property we set here is misleading. We 1335 // almost always synthesize these methods unless the user explicitly 1336 // provided prototypes (which is odd, but allowed). Sema should be 1337 // typechecking that the declarations jive in that situation (which 1338 // it is not currently). 1339 if (!GetterMethod) { 1340 // No instance method of same name as property getter name was found. 1341 // Declare a getter method and add it to the list of methods 1342 // for this class. 1343 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), 1344 property->getLocation(), property->getGetterName(), 1345 property->getType(), CD, true, false, true, 1346 (property->getPropertyImplementation() == 1347 ObjCPropertyDecl::Optional) ? 1348 ObjCMethodDecl::Optional : 1349 ObjCMethodDecl::Required); 1350 CD->addDecl(Context, GetterMethod); 1351 } else 1352 // A user declared getter will be synthesize when @synthesize of 1353 // the property with the same name is seen in the @implementation 1354 GetterMethod->setSynthesized(true); 1355 property->setGetterMethodDecl(GetterMethod); 1356 1357 // Skip setter if property is read-only. 1358 if (!property->isReadOnly()) { 1359 // Find the default setter and if one not found, add one. 1360 if (!SetterMethod) { 1361 // No instance method of same name as property setter name was found. 1362 // Declare a setter method and add it to the list of methods 1363 // for this class. 1364 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), 1365 property->getLocation(), 1366 property->getSetterName(), 1367 Context.VoidTy, CD, true, false, true, 1368 (property->getPropertyImplementation() == 1369 ObjCPropertyDecl::Optional) ? 1370 ObjCMethodDecl::Optional : 1371 ObjCMethodDecl::Required); 1372 // Invent the arguments for the setter. We don't bother making a 1373 // nice name for the argument. 1374 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, 1375 property->getLocation(), 1376 property->getIdentifier(), 1377 property->getType(), 1378 VarDecl::None, 1379 0); 1380 SetterMethod->setMethodParams(Context, &Argument, 1); 1381 CD->addDecl(Context, SetterMethod); 1382 } else 1383 // A user declared setter will be synthesize when @synthesize of 1384 // the property with the same name is seen in the @implementation 1385 SetterMethod->setSynthesized(true); 1386 property->setSetterMethodDecl(SetterMethod); 1387 } 1388 // Add any synthesized methods to the global pool. This allows us to 1389 // handle the following, which is supported by GCC (and part of the design). 1390 // 1391 // @interface Foo 1392 // @property double bar; 1393 // @end 1394 // 1395 // void thisIsUnfortunate() { 1396 // id foo; 1397 // double bar = [foo bar]; 1398 // } 1399 // 1400 if (GetterMethod) 1401 AddInstanceMethodToGlobalPool(GetterMethod); 1402 if (SetterMethod) 1403 AddInstanceMethodToGlobalPool(SetterMethod); 1404} 1405 1406// Note: For class/category implemenations, allMethods/allProperties is 1407// always null. 1408void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl, 1409 DeclPtrTy *allMethods, unsigned allNum, 1410 DeclPtrTy *allProperties, unsigned pNum, 1411 DeclGroupPtrTy *allTUVars, unsigned tuvNum) { 1412 Decl *ClassDecl = classDecl.getAs<Decl>(); 1413 1414 // FIXME: If we don't have a ClassDecl, we have an error. We should consider 1415 // always passing in a decl. If the decl has an error, isInvalidDecl() 1416 // should be true. 1417 if (!ClassDecl) 1418 return; 1419 1420 bool isInterfaceDeclKind = 1421 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) 1422 || isa<ObjCProtocolDecl>(ClassDecl); 1423 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); 1424 1425 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); 1426 1427 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. 1428 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; 1429 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; 1430 1431 for (unsigned i = 0; i < allNum; i++ ) { 1432 ObjCMethodDecl *Method = 1433 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>()); 1434 1435 if (!Method) continue; // Already issued a diagnostic. 1436 if (Method->isInstanceMethod()) { 1437 /// Check for instance method of the same name with incompatible types 1438 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; 1439 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 1440 : false; 1441 if ((isInterfaceDeclKind && PrevMethod && !match) 1442 || (checkIdenticalMethods && match)) { 1443 Diag(Method->getLocation(), diag::err_duplicate_method_decl) 1444 << Method->getDeclName(); 1445 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1446 } else { 1447 DC->addDecl(Context, Method); 1448 InsMap[Method->getSelector()] = Method; 1449 /// The following allows us to typecheck messages to "id". 1450 AddInstanceMethodToGlobalPool(Method); 1451 } 1452 } 1453 else { 1454 /// Check for class method of the same name with incompatible types 1455 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; 1456 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) 1457 : false; 1458 if ((isInterfaceDeclKind && PrevMethod && !match) 1459 || (checkIdenticalMethods && match)) { 1460 Diag(Method->getLocation(), diag::err_duplicate_method_decl) 1461 << Method->getDeclName(); 1462 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1463 } else { 1464 DC->addDecl(Context, Method); 1465 ClsMap[Method->getSelector()] = Method; 1466 /// The following allows us to typecheck messages to "Class". 1467 AddFactoryMethodToGlobalPool(Method); 1468 } 1469 } 1470 } 1471 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 1472 // Compares properties declared in this class to those of its 1473 // super class. 1474 ComparePropertiesInBaseAndSuper(I); 1475 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I)); 1476 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { 1477 // Categories are used to extend the class by declaring new methods. 1478 // By the same token, they are also used to add new properties. No 1479 // need to compare the added property to those in the class. 1480 1481 // Merge protocol properties into category 1482 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C)); 1483 if (C->getIdentifier() == 0) 1484 DiagnoseClassExtensionDupMethods(C, C->getClassInterface()); 1485 } 1486 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { 1487 // ProcessPropertyDecl is responsible for diagnosing conflicts with any 1488 // user-defined setter/getter. It also synthesizes setter/getter methods 1489 // and adds them to the DeclContext and global method pools. 1490 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context), 1491 E = CDecl->prop_end(Context); 1492 I != E; ++I) 1493 ProcessPropertyDecl(*I, CDecl); 1494 CDecl->setAtEndLoc(AtEndLoc); 1495 } 1496 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 1497 IC->setLocEnd(AtEndLoc); 1498 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) 1499 ImplMethodsVsClassMethods(IC, IDecl); 1500 } else if (ObjCCategoryImplDecl* CatImplClass = 1501 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { 1502 CatImplClass->setLocEnd(AtEndLoc); 1503 1504 // Find category interface decl and then check that all methods declared 1505 // in this interface are implemented in the category @implementation. 1506 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { 1507 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); 1508 Categories; Categories = Categories->getNextClassCategory()) { 1509 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { 1510 ImplMethodsVsClassMethods(CatImplClass, Categories); 1511 break; 1512 } 1513 } 1514 } 1515 } 1516 if (isInterfaceDeclKind) { 1517 // Reject invalid vardecls. 1518 for (unsigned i = 0; i != tuvNum; i++) { 1519 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>(); 1520 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) 1521 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { 1522 if (!VDecl->hasExternalStorage()) 1523 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); 1524 } 1525 } 1526 } 1527} 1528 1529 1530/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for 1531/// objective-c's type qualifier from the parser version of the same info. 1532static Decl::ObjCDeclQualifier 1533CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { 1534 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; 1535 if (PQTVal & ObjCDeclSpec::DQ_In) 1536 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); 1537 if (PQTVal & ObjCDeclSpec::DQ_Inout) 1538 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); 1539 if (PQTVal & ObjCDeclSpec::DQ_Out) 1540 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); 1541 if (PQTVal & ObjCDeclSpec::DQ_Bycopy) 1542 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); 1543 if (PQTVal & ObjCDeclSpec::DQ_Byref) 1544 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); 1545 if (PQTVal & ObjCDeclSpec::DQ_Oneway) 1546 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); 1547 1548 return ret; 1549} 1550 1551Sema::DeclPtrTy Sema::ActOnMethodDeclaration( 1552 SourceLocation MethodLoc, SourceLocation EndLoc, 1553 tok::TokenKind MethodType, DeclPtrTy classDecl, 1554 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, 1555 Selector Sel, 1556 // optional arguments. The number of types/arguments is obtained 1557 // from the Sel.getNumArgs(). 1558 ObjCArgInfo *ArgInfo, 1559 llvm::SmallVectorImpl<Declarator> &Cdecls, 1560 AttributeList *ReturnAttrList, 1561 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, 1562 bool isVariadic) { 1563 Decl *ClassDecl = classDecl.getAs<Decl>(); 1564 1565 // Make sure we can establish a context for the method. 1566 if (!ClassDecl) { 1567 Diag(MethodLoc, diag::error_missing_method_context); 1568 return DeclPtrTy(); 1569 } 1570 QualType resultDeclType; 1571 1572 if (ReturnType) { 1573 resultDeclType = QualType::getFromOpaquePtr(ReturnType); 1574 1575 // Methods cannot return interface types. All ObjC objects are 1576 // passed by reference. 1577 if (resultDeclType->isObjCInterfaceType()) { 1578 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value) 1579 << 0 << resultDeclType; 1580 return DeclPtrTy(); 1581 } 1582 } else // get the type for "id". 1583 resultDeclType = Context.getObjCIdType(); 1584 1585 ObjCMethodDecl* ObjCMethod = 1586 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, 1587 cast<DeclContext>(ClassDecl), 1588 MethodType == tok::minus, isVariadic, 1589 false, 1590 MethodDeclKind == tok::objc_optional ? 1591 ObjCMethodDecl::Optional : 1592 ObjCMethodDecl::Required); 1593 1594 llvm::SmallVector<ParmVarDecl*, 16> Params; 1595 1596 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { 1597 QualType ArgType, UnpromotedArgType; 1598 1599 if (ArgInfo[i].Type == 0) { 1600 UnpromotedArgType = ArgType = Context.getObjCIdType(); 1601 } else { 1602 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type); 1603 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). 1604 ArgType = adjustParameterType(ArgType); 1605 } 1606 1607 ParmVarDecl* Param; 1608 if (ArgType == UnpromotedArgType) 1609 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc, 1610 ArgInfo[i].Name, ArgType, 1611 VarDecl::None, 0); 1612 else 1613 Param = OriginalParmVarDecl::Create(Context, ObjCMethod, 1614 ArgInfo[i].NameLoc, 1615 ArgInfo[i].Name, ArgType, 1616 UnpromotedArgType, 1617 VarDecl::None, 0); 1618 1619 if (ArgType->isObjCInterfaceType()) { 1620 Diag(ArgInfo[i].NameLoc, 1621 diag::err_object_cannot_be_passed_returned_by_value) 1622 << 1 << ArgType; 1623 Param->setInvalidDecl(); 1624 } 1625 1626 Param->setObjCDeclQualifier( 1627 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); 1628 1629 // Apply the attributes to the parameter. 1630 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs); 1631 1632 Params.push_back(Param); 1633 } 1634 1635 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs()); 1636 ObjCMethod->setObjCDeclQualifier( 1637 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); 1638 const ObjCMethodDecl *PrevMethod = 0; 1639 1640 if (AttrList) 1641 ProcessDeclAttributeList(ObjCMethod, AttrList); 1642 1643 if (ReturnAttrList) 1644 ProcessObjCMethDeclReturnAttributeList(ObjCMethod, ReturnAttrList); 1645 1646 // For implementations (which can be very "coarse grain"), we add the 1647 // method now. This allows the AST to implement lookup methods that work 1648 // incrementally (without waiting until we parse the @end). It also allows 1649 // us to flag multiple declaration errors as they occur. 1650 if (ObjCImplementationDecl *ImpDecl = 1651 dyn_cast<ObjCImplementationDecl>(ClassDecl)) { 1652 if (MethodType == tok::minus) { 1653 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel); 1654 ImpDecl->addInstanceMethod(Context, ObjCMethod); 1655 } else { 1656 PrevMethod = ImpDecl->getClassMethod(Context, Sel); 1657 ImpDecl->addClassMethod(Context, ObjCMethod); 1658 } 1659 } 1660 else if (ObjCCategoryImplDecl *CatImpDecl = 1661 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { 1662 if (MethodType == tok::minus) { 1663 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel); 1664 CatImpDecl->addInstanceMethod(Context, ObjCMethod); 1665 } else { 1666 PrevMethod = CatImpDecl->getClassMethod(Context, Sel); 1667 CatImpDecl->addClassMethod(Context, ObjCMethod); 1668 } 1669 } 1670 if (PrevMethod) { 1671 // You can never have two method definitions with the same name. 1672 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) 1673 << ObjCMethod->getDeclName(); 1674 Diag(PrevMethod->getLocation(), diag::note_previous_declaration); 1675 } 1676 return DeclPtrTy::make(ObjCMethod); 1677} 1678 1679void Sema::CheckObjCPropertyAttributes(QualType PropertyTy, 1680 SourceLocation Loc, 1681 unsigned &Attributes) { 1682 // FIXME: Improve the reported location. 1683 1684 // readonly and readwrite/assign/retain/copy conflict. 1685 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1686 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | 1687 ObjCDeclSpec::DQ_PR_assign | 1688 ObjCDeclSpec::DQ_PR_copy | 1689 ObjCDeclSpec::DQ_PR_retain))) { 1690 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? 1691 "readwrite" : 1692 (Attributes & ObjCDeclSpec::DQ_PR_assign) ? 1693 "assign" : 1694 (Attributes & ObjCDeclSpec::DQ_PR_copy) ? 1695 "copy" : "retain"; 1696 1697 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? 1698 diag::err_objc_property_attr_mutually_exclusive : 1699 diag::warn_objc_property_attr_mutually_exclusive) 1700 << "readonly" << which; 1701 } 1702 1703 // Check for copy or retain on non-object types. 1704 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) && 1705 !Context.isObjCObjectPointerType(PropertyTy)) { 1706 Diag(Loc, diag::err_objc_property_requires_object) 1707 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain"); 1708 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain); 1709 } 1710 1711 // Check for more than one of { assign, copy, retain }. 1712 if (Attributes & ObjCDeclSpec::DQ_PR_assign) { 1713 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1714 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1715 << "assign" << "copy"; 1716 Attributes &= ~ObjCDeclSpec::DQ_PR_copy; 1717 } 1718 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1719 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1720 << "assign" << "retain"; 1721 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1722 } 1723 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1724 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1725 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1726 << "copy" << "retain"; 1727 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1728 } 1729 } 1730 1731 // Warn if user supplied no assignment attribute, property is 1732 // readwrite, and this is an object type. 1733 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | 1734 ObjCDeclSpec::DQ_PR_retain)) && 1735 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1736 Context.isObjCObjectPointerType(PropertyTy)) { 1737 // Skip this warning in gc-only mode. 1738 if (getLangOptions().getGCMode() != LangOptions::GCOnly) 1739 Diag(Loc, diag::warn_objc_property_no_assignment_attribute); 1740 1741 // If non-gc code warn that this is likely inappropriate. 1742 if (getLangOptions().getGCMode() == LangOptions::NonGC) 1743 Diag(Loc, diag::warn_objc_property_default_assign_on_object); 1744 1745 // FIXME: Implement warning dependent on NSCopying being 1746 // implemented. See also: 1747 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> 1748 // (please trim this list while you are at it). 1749 } 1750} 1751 1752Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, 1753 FieldDeclarator &FD, 1754 ObjCDeclSpec &ODS, 1755 Selector GetterSel, 1756 Selector SetterSel, 1757 DeclPtrTy ClassCategory, 1758 bool *isOverridingProperty, 1759 tok::ObjCKeywordKind MethodImplKind) { 1760 unsigned Attributes = ODS.getPropertyAttributes(); 1761 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || 1762 // default is readwrite! 1763 !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); 1764 // property is defaulted to 'assign' if it is readwrite and is 1765 // not retain or copy 1766 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || 1767 (isReadWrite && 1768 !(Attributes & ObjCDeclSpec::DQ_PR_retain) && 1769 !(Attributes & ObjCDeclSpec::DQ_PR_copy))); 1770 QualType T = GetTypeForDeclarator(FD.D, S); 1771 Decl *ClassDecl = ClassCategory.getAs<Decl>(); 1772 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class 1773 // May modify Attributes. 1774 CheckObjCPropertyAttributes(T, AtLoc, Attributes); 1775 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) 1776 if (!CDecl->getIdentifier()) { 1777 // This is a continuation class. property requires special 1778 // handling. 1779 if ((CCPrimary = CDecl->getClassInterface())) { 1780 // Find the property in continuation class's primary class only. 1781 ObjCPropertyDecl *PIDecl = 0; 1782 IdentifierInfo *PropertyId = FD.D.getIdentifier(); 1783 for (ObjCInterfaceDecl::prop_iterator 1784 I = CCPrimary->prop_begin(Context), 1785 E = CCPrimary->prop_end(Context); 1786 I != E; ++I) 1787 if ((*I)->getIdentifier() == PropertyId) { 1788 PIDecl = *I; 1789 break; 1790 } 1791 1792 if (PIDecl) { 1793 // property 'PIDecl's readonly attribute will be over-ridden 1794 // with continuation class's readwrite property attribute! 1795 unsigned PIkind = PIDecl->getPropertyAttributes(); 1796 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { 1797 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) != 1798 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic)) 1799 Diag(AtLoc, diag::warn_property_attr_mismatch); 1800 PIDecl->makeitReadWriteAttribute(); 1801 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 1802 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 1803 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 1804 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 1805 PIDecl->setSetterName(SetterSel); 1806 } 1807 else 1808 Diag(AtLoc, diag::err_use_continuation_class) 1809 << CCPrimary->getDeclName(); 1810 *isOverridingProperty = true; 1811 // Make sure setter decl is synthesized, and added to primary 1812 // class's list. 1813 ProcessPropertyDecl(PIDecl, CCPrimary); 1814 return DeclPtrTy(); 1815 } 1816 // No matching property found in the primary class. Just fall thru 1817 // and add property to continuation class's primary class. 1818 ClassDecl = CCPrimary; 1819 } else { 1820 Diag(CDecl->getLocation(), diag::err_continuation_class); 1821 *isOverridingProperty = true; 1822 return DeclPtrTy(); 1823 } 1824 } 1825 1826 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); 1827 assert(DC && "ClassDecl is not a DeclContext"); 1828 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, 1829 FD.D.getIdentifierLoc(), 1830 FD.D.getIdentifier(), T); 1831 DC->addDecl(Context, PDecl); 1832 1833 if (T->isArrayType() || T->isFunctionType()) { 1834 Diag(AtLoc, diag::err_property_type) << T; 1835 PDecl->setInvalidDecl(); 1836 } 1837 1838 ProcessDeclAttributes(PDecl, FD.D); 1839 1840 // Regardless of setter/getter attribute, we save the default getter/setter 1841 // selector names in anticipation of declaration of setter/getter methods. 1842 PDecl->setGetterName(GetterSel); 1843 PDecl->setSetterName(SetterSel); 1844 1845 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) 1846 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); 1847 1848 if (Attributes & ObjCDeclSpec::DQ_PR_getter) 1849 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); 1850 1851 if (Attributes & ObjCDeclSpec::DQ_PR_setter) 1852 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); 1853 1854 if (isReadWrite) 1855 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); 1856 1857 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 1858 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 1859 1860 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 1861 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 1862 1863 if (isAssign) 1864 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); 1865 1866 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) 1867 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); 1868 1869 if (MethodImplKind == tok::objc_required) 1870 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); 1871 else if (MethodImplKind == tok::objc_optional) 1872 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); 1873 // A case of continuation class adding a new property in the class. This 1874 // is not what it was meant for. However, gcc supports it and so should we. 1875 // Make sure setter/getters are declared here. 1876 if (CCPrimary) 1877 ProcessPropertyDecl(PDecl, CCPrimary); 1878 1879 return DeclPtrTy::make(PDecl); 1880} 1881 1882/// ActOnPropertyImplDecl - This routine performs semantic checks and 1883/// builds the AST node for a property implementation declaration; declared 1884/// as @synthesize or @dynamic. 1885/// 1886Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc, 1887 SourceLocation PropertyLoc, 1888 bool Synthesize, 1889 DeclPtrTy ClassCatImpDecl, 1890 IdentifierInfo *PropertyId, 1891 IdentifierInfo *PropertyIvar) { 1892 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>(); 1893 // Make sure we have a context for the property implementation declaration. 1894 if (!ClassImpDecl) { 1895 Diag(AtLoc, diag::error_missing_property_context); 1896 return DeclPtrTy(); 1897 } 1898 ObjCPropertyDecl *property = 0; 1899 ObjCInterfaceDecl* IDecl = 0; 1900 // Find the class or category class where this property must have 1901 // a declaration. 1902 ObjCImplementationDecl *IC = 0; 1903 ObjCCategoryImplDecl* CatImplClass = 0; 1904 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { 1905 IDecl = IC->getClassInterface(); 1906 // We always synthesize an interface for an implementation 1907 // without an interface decl. So, IDecl is always non-zero. 1908 assert(IDecl && 1909 "ActOnPropertyImplDecl - @implementation without @interface"); 1910 1911 // Look for this property declaration in the @implementation's @interface 1912 property = IDecl->FindPropertyDeclaration(Context, PropertyId); 1913 if (!property) { 1914 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); 1915 return DeclPtrTy(); 1916 } 1917 } 1918 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { 1919 if (Synthesize) { 1920 Diag(AtLoc, diag::error_synthesize_category_decl); 1921 return DeclPtrTy(); 1922 } 1923 IDecl = CatImplClass->getClassInterface(); 1924 if (!IDecl) { 1925 Diag(AtLoc, diag::error_missing_property_interface); 1926 return DeclPtrTy(); 1927 } 1928 ObjCCategoryDecl *Category = 1929 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); 1930 1931 // If category for this implementation not found, it is an error which 1932 // has already been reported eralier. 1933 if (!Category) 1934 return DeclPtrTy(); 1935 // Look for this property declaration in @implementation's category 1936 property = Category->FindPropertyDeclaration(Context, PropertyId); 1937 if (!property) { 1938 Diag(PropertyLoc, diag::error_bad_category_property_decl) 1939 << Category->getDeclName(); 1940 return DeclPtrTy(); 1941 } 1942 } else { 1943 Diag(AtLoc, diag::error_bad_property_context); 1944 return DeclPtrTy(); 1945 } 1946 ObjCIvarDecl *Ivar = 0; 1947 // Check that we have a valid, previously declared ivar for @synthesize 1948 if (Synthesize) { 1949 // @synthesize 1950 if (!PropertyIvar) 1951 PropertyIvar = PropertyId; 1952 QualType PropType = Context.getCanonicalType(property->getType()); 1953 // Check that this is a previously declared 'ivar' in 'IDecl' interface 1954 ObjCInterfaceDecl *ClassDeclared; 1955 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared); 1956 if (!Ivar) { 1957 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc, 1958 PropertyIvar, PropType, 1959 ObjCIvarDecl::Public, 1960 (Expr *)0); 1961 property->setPropertyIvarDecl(Ivar); 1962 if (!getLangOptions().ObjCNonFragileABI) 1963 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId; 1964 // Note! I deliberately want it to fall thru so, we have a 1965 // a property implementation and to avoid future warnings. 1966 } 1967 else if (getLangOptions().ObjCNonFragileABI && 1968 ClassDeclared != IDecl) { 1969 Diag(PropertyLoc, diag::error_ivar_in_superclass_use) 1970 << property->getDeclName() << Ivar->getDeclName() 1971 << ClassDeclared->getDeclName(); 1972 Diag(Ivar->getLocation(), diag::note_previous_access_declaration) 1973 << Ivar << Ivar->getNameAsCString(); 1974 // Note! I deliberately want it to fall thru so more errors are caught. 1975 } 1976 QualType IvarType = Context.getCanonicalType(Ivar->getType()); 1977 1978 // Check that type of property and its ivar are type compatible. 1979 if (PropType != IvarType) { 1980 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) { 1981 Diag(PropertyLoc, diag::error_property_ivar_type) 1982 << property->getDeclName() << Ivar->getDeclName(); 1983 // Note! I deliberately want it to fall thru so, we have a 1984 // a property implementation and to avoid future warnings. 1985 } 1986 1987 // FIXME! Rules for properties are somewhat different that those 1988 // for assignments. Use a new routine to consolidate all cases; 1989 // specifically for property redeclarations as well as for ivars. 1990 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType(); 1991 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); 1992 if (lhsType != rhsType && 1993 lhsType->isArithmeticType()) { 1994 Diag(PropertyLoc, diag::error_property_ivar_type) 1995 << property->getDeclName() << Ivar->getDeclName(); 1996 // Fall thru - see previous comment 1997 } 1998 // __weak is explicit. So it works on Canonical type. 1999 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && 2000 getLangOptions().getGCMode() != LangOptions::NonGC) { 2001 Diag(PropertyLoc, diag::error_weak_property) 2002 << property->getDeclName() << Ivar->getDeclName(); 2003 // Fall thru - see previous comment 2004 } 2005 if ((Context.isObjCObjectPointerType(property->getType()) || 2006 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && 2007 getLangOptions().getGCMode() != LangOptions::NonGC) { 2008 Diag(PropertyLoc, diag::error_strong_property) 2009 << property->getDeclName() << Ivar->getDeclName(); 2010 // Fall thru - see previous comment 2011 } 2012 } 2013 } else if (PropertyIvar) 2014 // @dynamic 2015 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); 2016 assert (property && "ActOnPropertyImplDecl - property declaration missing"); 2017 ObjCPropertyImplDecl *PIDecl = 2018 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, 2019 property, 2020 (Synthesize ? 2021 ObjCPropertyImplDecl::Synthesize 2022 : ObjCPropertyImplDecl::Dynamic), 2023 Ivar); 2024 if (IC) { 2025 if (Synthesize) 2026 if (ObjCPropertyImplDecl *PPIDecl = 2027 IC->FindPropertyImplIvarDecl(Context, PropertyIvar)) { 2028 Diag(PropertyLoc, diag::error_duplicate_ivar_use) 2029 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 2030 << PropertyIvar; 2031 Diag(PPIDecl->getLocation(), diag::note_previous_use); 2032 } 2033 2034 if (ObjCPropertyImplDecl *PPIDecl 2035 = IC->FindPropertyImplDecl(Context, PropertyId)) { 2036 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; 2037 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 2038 return DeclPtrTy(); 2039 } 2040 IC->addPropertyImplementation(Context, PIDecl); 2041 } 2042 else { 2043 if (Synthesize) 2044 if (ObjCPropertyImplDecl *PPIDecl = 2045 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) { 2046 Diag(PropertyLoc, diag::error_duplicate_ivar_use) 2047 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 2048 << PropertyIvar; 2049 Diag(PPIDecl->getLocation(), diag::note_previous_use); 2050 } 2051 2052 if (ObjCPropertyImplDecl *PPIDecl = 2053 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) { 2054 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; 2055 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 2056 return DeclPtrTy(); 2057 } 2058 CatImplClass->addPropertyImplementation(Context, PIDecl); 2059 } 2060 2061 return DeclPtrTy::make(PIDecl); 2062} 2063 2064bool Sema::CheckObjCDeclScope(Decl *D) { 2065 if (isa<TranslationUnitDecl>(CurContext->getLookupContext())) 2066 return false; 2067 2068 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); 2069 D->setInvalidDecl(); 2070 2071 return true; 2072} 2073 2074/// Collect the instance variables declared in an Objective-C object. Used in 2075/// the creation of structures from objects using the @defs directive. 2076/// FIXME: This should be consolidated with CollectObjCIvars as it is also 2077/// part of the AST generation logic of @defs. 2078static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record, 2079 ASTContext& Ctx, 2080 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) { 2081 if (Class->getSuperClass()) 2082 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars); 2083 2084 // For each ivar, create a fresh ObjCAtDefsFieldDecl. 2085 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(), 2086 E = Class->ivar_end(); I != E; ++I) { 2087 ObjCIvarDecl* ID = *I; 2088 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(), 2089 ID->getIdentifier(), ID->getType(), 2090 ID->getBitWidth()); 2091 ivars.push_back(Sema::DeclPtrTy::make(FD)); 2092 } 2093} 2094 2095/// Called whenever @defs(ClassName) is encountered in the source. Inserts the 2096/// instance variables of ClassName into Decls. 2097void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart, 2098 IdentifierInfo *ClassName, 2099 llvm::SmallVectorImpl<DeclPtrTy> &Decls) { 2100 // Check that ClassName is a valid class 2101 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName); 2102 if (!Class) { 2103 Diag(DeclStart, diag::err_undef_interface) << ClassName; 2104 return; 2105 } 2106 if (LangOpts.ObjCNonFragileABI) { 2107 Diag(DeclStart, diag::err_atdef_nonfragile_interface); 2108 return; 2109 } 2110 2111 // Collect the instance variables 2112 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls); 2113 2114 // Introduce all of these fields into the appropriate scope. 2115 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin(); 2116 D != Decls.end(); ++D) { 2117 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>()); 2118 if (getLangOptions().CPlusPlus) 2119 PushOnScopeChains(cast<FieldDecl>(FD), S); 2120 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>())) 2121 Record->addDecl(Context, FD); 2122 } 2123} 2124 2125