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