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