1 //===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements semantic analysis for Objective C declarations.
12 //===----------------------------------------------------------------------===//
15 #include "clang/Sema/ExternalSemaSource.h"
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/Parse/DeclSpec.h"
20 using namespace clang;
22 bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
23 ObjCMethodDecl *GetterMethod,
26 GetterMethod->getResultType() != property->getType()) {
27 AssignConvertType result = Incompatible;
28 if (property->getType()->isObjCObjectPointerType())
29 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
30 if (result != Compatible) {
31 Diag(Loc, diag::warn_accessor_property_type_mismatch)
32 << property->getDeclName()
33 << GetterMethod->getSelector();
34 Diag(GetterMethod->getLocation(), diag::note_declared_at);
41 /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
42 /// and user declared, in the method definition's AST.
43 void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
44 assert(getCurMethodDecl() == 0 && "Method parsing confused");
45 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
47 // If we don't have a valid method decl, simply return.
51 CurFunctionNeedsScopeChecking = false;
53 // Allow the rest of sema to find private method decl implementations.
54 if (MDecl->isInstanceMethod())
55 AddInstanceMethodToGlobalPool(MDecl);
57 AddFactoryMethodToGlobalPool(MDecl);
59 // Allow all of Sema to see that we are entering a method definition.
60 PushDeclContext(FnBodyScope, MDecl);
62 // Create Decl objects for each parameter, entrring them in the scope for
63 // binding to their use.
65 // Insert the invisible arguments, self and _cmd!
66 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
68 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
69 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
71 // Introduce all of the other parameters into this scope.
72 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
73 E = MDecl->param_end(); PI != E; ++PI)
74 if ((*PI)->getIdentifier())
75 PushOnScopeChains(*PI, FnBodyScope);
78 Sema::DeclPtrTy Sema::
79 ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
80 IdentifierInfo *ClassName, SourceLocation ClassLoc,
81 IdentifierInfo *SuperName, SourceLocation SuperLoc,
82 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
83 SourceLocation EndProtoLoc, AttributeList *AttrList) {
84 assert(ClassName && "Missing class identifier");
86 // Check for another declaration kind with the same name.
87 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
88 if (PrevDecl && PrevDecl->isTemplateParameter()) {
89 // Maybe we will complain about the shadowed template parameter.
90 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
91 // Just pretend that we didn't see the previous declaration.
95 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
96 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
97 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
100 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
102 // Class already seen. Is it a forward declaration?
103 if (!IDecl->isForwardDecl()) {
104 IDecl->setInvalidDecl();
105 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
106 Diag(IDecl->getLocation(), diag::note_previous_definition);
108 // Return the previous class interface.
109 // FIXME: don't leak the objects passed in!
110 return DeclPtrTy::make(IDecl);
112 IDecl->setLocation(AtInterfaceLoc);
113 IDecl->setForwardDecl(false);
114 IDecl->setClassLoc(ClassLoc);
117 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
118 ClassName, ClassLoc);
120 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
122 PushOnScopeChains(IDecl, TUScope);
126 // Check if a different kind of symbol declared in this scope.
127 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
128 if (PrevDecl == IDecl) {
129 Diag(SuperLoc, diag::err_recursive_superclass)
130 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
131 IDecl->setLocEnd(ClassLoc);
133 ObjCInterfaceDecl *SuperClassDecl =
134 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
136 // Diagnose classes that inherit from deprecated classes.
138 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
140 if (PrevDecl && SuperClassDecl == 0) {
141 // The previous declaration was not a class decl. Check if we have a
142 // typedef. If we do, get the underlying class type.
143 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
144 QualType T = TDecl->getUnderlyingType();
145 if (T->isObjCInterfaceType()) {
146 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
147 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
151 // This handles the following case:
153 // typedef int SuperClass;
154 // @interface MyClass : SuperClass {} @end
156 if (!SuperClassDecl) {
157 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
158 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
162 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
164 Diag(SuperLoc, diag::err_undef_superclass)
165 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
166 else if (SuperClassDecl->isForwardDecl())
167 Diag(SuperLoc, diag::err_undef_superclass)
168 << SuperClassDecl->getDeclName() << ClassName
169 << SourceRange(AtInterfaceLoc, ClassLoc);
171 IDecl->setSuperClass(SuperClassDecl);
172 IDecl->setSuperClassLoc(SuperLoc);
173 IDecl->setLocEnd(SuperLoc);
175 } else { // we have a root class.
176 IDecl->setLocEnd(ClassLoc);
179 /// Check then save referenced protocols.
181 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
183 IDecl->setLocEnd(EndProtoLoc);
186 CheckObjCDeclScope(IDecl);
187 return DeclPtrTy::make(IDecl);
190 /// ActOnCompatiblityAlias - this action is called after complete parsing of
191 /// @compatibility_alias declaration. It sets up the alias relationships.
192 Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
193 IdentifierInfo *AliasName,
194 SourceLocation AliasLocation,
195 IdentifierInfo *ClassName,
196 SourceLocation ClassLocation) {
197 // Look for previous declaration of alias name
198 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
200 if (isa<ObjCCompatibleAliasDecl>(ADecl))
201 Diag(AliasLocation, diag::warn_previous_alias_decl);
203 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
204 Diag(ADecl->getLocation(), diag::note_previous_declaration);
207 // Check for class declaration
208 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
209 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
210 QualType T = TDecl->getUnderlyingType();
211 if (T->isObjCInterfaceType()) {
212 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
213 ClassName = IDecl->getIdentifier();
214 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
218 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
220 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
222 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
226 // Everything checked out, instantiate a new alias declaration AST.
227 ObjCCompatibleAliasDecl *AliasDecl =
228 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
230 if (!CheckObjCDeclScope(AliasDecl))
231 PushOnScopeChains(AliasDecl, TUScope);
233 return DeclPtrTy::make(AliasDecl);
236 void Sema::CheckForwardProtocolDeclarationForCircularDependency(
237 IdentifierInfo *PName,
238 SourceLocation &Ploc, SourceLocation PrevLoc,
239 const ObjCList<ObjCProtocolDecl> &PList) {
240 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
241 E = PList.end(); I != E; ++I) {
243 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
244 if (PDecl->getIdentifier() == PName) {
245 Diag(Ploc, diag::err_protocol_has_circular_dependency);
246 Diag(PrevLoc, diag::note_previous_definition);
248 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
249 PDecl->getLocation(), PDecl->getReferencedProtocols());
255 Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
256 IdentifierInfo *ProtocolName,
257 SourceLocation ProtocolLoc,
258 const DeclPtrTy *ProtoRefs,
259 unsigned NumProtoRefs,
260 SourceLocation EndProtoLoc,
261 AttributeList *AttrList) {
262 // FIXME: Deal with AttrList.
263 assert(ProtocolName && "Missing protocol identifier");
264 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
266 // Protocol already seen. Better be a forward protocol declaration
267 if (!PDecl->isForwardDecl()) {
268 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
269 Diag(PDecl->getLocation(), diag::note_previous_definition);
270 // Just return the protocol we already had.
271 // FIXME: don't leak the objects passed in!
272 return DeclPtrTy::make(PDecl);
274 ObjCList<ObjCProtocolDecl> PList;
275 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
276 CheckForwardProtocolDeclarationForCircularDependency(
277 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
278 PList.Destroy(Context);
280 // Make sure the cached decl gets a valid start location.
281 PDecl->setLocation(AtProtoInterfaceLoc);
282 PDecl->setForwardDecl(false);
284 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
285 AtProtoInterfaceLoc,ProtocolName);
286 PushOnScopeChains(PDecl, TUScope);
287 PDecl->setForwardDecl(false);
290 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
292 /// Check then save referenced protocols.
293 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
294 PDecl->setLocEnd(EndProtoLoc);
297 CheckObjCDeclScope(PDecl);
298 return DeclPtrTy::make(PDecl);
301 /// FindProtocolDeclaration - This routine looks up protocols and
302 /// issues an error if they are not declared. It returns list of
303 /// protocol declarations in its 'Protocols' argument.
305 Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
306 const IdentifierLocPair *ProtocolId,
307 unsigned NumProtocols,
308 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
309 for (unsigned i = 0; i != NumProtocols; ++i) {
310 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
312 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
313 << ProtocolId[i].first;
317 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
319 // If this is a forward declaration and we are supposed to warn in this
321 if (WarnOnDeclarations && PDecl->isForwardDecl())
322 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
323 << ProtocolId[i].first;
324 Protocols.push_back(DeclPtrTy::make(PDecl));
328 /// DiagnosePropertyMismatch - Compares two properties for their
329 /// attributes and types and warns on a variety of inconsistencies.
332 Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
333 ObjCPropertyDecl *SuperProperty,
334 const IdentifierInfo *inheritedName) {
335 ObjCPropertyDecl::PropertyAttributeKind CAttr =
336 Property->getPropertyAttributes();
337 ObjCPropertyDecl::PropertyAttributeKind SAttr =
338 SuperProperty->getPropertyAttributes();
339 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
340 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
341 Diag(Property->getLocation(), diag::warn_readonly_property)
342 << Property->getDeclName() << inheritedName;
343 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
344 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
345 Diag(Property->getLocation(), diag::warn_property_attribute)
346 << Property->getDeclName() << "copy" << inheritedName;
347 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
348 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
349 Diag(Property->getLocation(), diag::warn_property_attribute)
350 << Property->getDeclName() << "retain" << inheritedName;
352 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
353 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
354 Diag(Property->getLocation(), diag::warn_property_attribute)
355 << Property->getDeclName() << "atomic" << inheritedName;
356 if (Property->getSetterName() != SuperProperty->getSetterName())
357 Diag(Property->getLocation(), diag::warn_property_attribute)
358 << Property->getDeclName() << "setter" << inheritedName;
359 if (Property->getGetterName() != SuperProperty->getGetterName())
360 Diag(Property->getLocation(), diag::warn_property_attribute)
361 << Property->getDeclName() << "getter" << inheritedName;
364 Context.getCanonicalType(SuperProperty->getType());
366 Context.getCanonicalType(Property->getType());
368 if (!Context.typesAreCompatible(LHSType, RHSType)) {
369 // FIXME: Incorporate this test with typesAreCompatible.
370 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
371 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
373 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
374 << Property->getType() << SuperProperty->getType() << inheritedName;
378 /// ComparePropertiesInBaseAndSuper - This routine compares property
379 /// declarations in base and its super class, if any, and issues
380 /// diagnostics in a variety of inconsistant situations.
382 void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
383 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
387 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
388 E = SDecl->prop_end(); S != E; ++S) {
389 ObjCPropertyDecl *SuperPDecl = (*S);
390 // Does property in super class has declaration in current class?
391 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
392 E = IDecl->prop_end(); I != E; ++I) {
393 ObjCPropertyDecl *PDecl = (*I);
394 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
395 DiagnosePropertyMismatch(PDecl, SuperPDecl,
396 SDecl->getIdentifier());
401 /// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
402 /// of properties declared in a protocol and adds them to the list
403 /// of properties for current class/category if it is not there already.
405 Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
406 ObjCProtocolDecl *PDecl) {
407 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
410 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
411 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
412 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
413 E = PDecl->prop_end(); P != E; ++P) {
414 ObjCPropertyDecl *Pr = (*P);
415 ObjCCategoryDecl::prop_iterator CP, CE;
416 // Is this property already in category's list of properties?
417 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
418 if ((*CP)->getIdentifier() == Pr->getIdentifier())
421 // Property protocol already exist in class. Diagnose any mismatch.
422 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
426 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
427 E = PDecl->prop_end(); P != E; ++P) {
428 ObjCPropertyDecl *Pr = (*P);
429 ObjCInterfaceDecl::prop_iterator CP, CE;
430 // Is this property already in class's list of properties?
431 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
432 if ((*CP)->getIdentifier() == Pr->getIdentifier())
435 // Property protocol already exist in class. Diagnose any mismatch.
436 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
440 /// MergeProtocolPropertiesIntoClass - This routine merges properties
441 /// declared in 'MergeItsProtocols' objects (which can be a class or an
442 /// inherited protocol into the list of properties for class/category 'CDecl'
444 void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
445 DeclPtrTy MergeItsProtocols) {
446 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
447 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
451 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
452 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
453 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
454 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
455 E = MDecl->protocol_end(); P != E; ++P)
456 // Merge properties of category (*P) into IDECL's
457 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
459 // Go thru the list of protocols for this category and recursively merge
460 // their properties into this class as well.
461 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
462 E = CatDecl->protocol_end(); P != E; ++P)
463 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
465 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
466 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
467 E = MD->protocol_end(); P != E; ++P)
468 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
473 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
474 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
475 E = MDecl->protocol_end(); P != E; ++P)
476 // Merge properties of class (*P) into IDECL's
477 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
479 // Go thru the list of protocols for this class and recursively merge
480 // their properties into this class as well.
481 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
482 E = IDecl->protocol_end(); P != E; ++P)
483 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
485 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
486 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
487 E = MD->protocol_end(); P != E; ++P)
488 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
492 /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
493 /// a class method in its extension.
495 void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
496 ObjCInterfaceDecl *ID) {
498 return; // Possibly due to previous error
500 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
501 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
502 e = ID->meth_end(); i != e; ++i) {
503 ObjCMethodDecl *MD = *i;
504 MethodMap[MD->getSelector()] = MD;
507 if (MethodMap.empty())
509 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
510 e = CAT->meth_end(); i != e; ++i) {
511 ObjCMethodDecl *Method = *i;
512 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
513 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
514 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
515 << Method->getDeclName();
516 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
521 /// ActOnForwardProtocolDeclaration - Handle @protocol foo;
523 Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
524 const IdentifierLocPair *IdentList,
526 AttributeList *attrList) {
527 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
529 for (unsigned i = 0; i != NumElts; ++i) {
530 IdentifierInfo *Ident = IdentList[i].first;
531 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
532 if (PDecl == 0) { // Not already seen?
533 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
534 IdentList[i].second, Ident);
535 PushOnScopeChains(PDecl, TUScope);
538 ProcessDeclAttributeList(TUScope, PDecl, attrList);
539 Protocols.push_back(PDecl);
542 ObjCForwardProtocolDecl *PDecl =
543 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
544 &Protocols[0], Protocols.size());
545 CurContext->addDecl(PDecl);
546 CheckObjCDeclScope(PDecl);
547 return DeclPtrTy::make(PDecl);
550 Sema::DeclPtrTy Sema::
551 ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
552 IdentifierInfo *ClassName, SourceLocation ClassLoc,
553 IdentifierInfo *CategoryName,
554 SourceLocation CategoryLoc,
555 const DeclPtrTy *ProtoRefs,
556 unsigned NumProtoRefs,
557 SourceLocation EndProtoLoc) {
558 ObjCCategoryDecl *CDecl =
559 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
560 // FIXME: PushOnScopeChains?
561 CurContext->addDecl(CDecl);
563 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
564 /// Check that class of this category is already completely declared.
565 if (!IDecl || IDecl->isForwardDecl()) {
566 CDecl->setInvalidDecl();
567 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
568 return DeclPtrTy::make(CDecl);
571 CDecl->setClassInterface(IDecl);
573 // If the interface is deprecated, warn about it.
574 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
576 /// Check for duplicate interface declaration for this category
577 ObjCCategoryDecl *CDeclChain;
578 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
579 CDeclChain = CDeclChain->getNextClassCategory()) {
580 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
581 Diag(CategoryLoc, diag::warn_dup_category_def)
582 << ClassName << CategoryName;
583 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
588 CDecl->insertNextClassCategory();
591 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
593 CDecl->setLocEnd(EndProtoLoc);
594 // Protocols in the class extension belong to the class.
595 if (!CDecl->getIdentifier())
596 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
597 NumProtoRefs,Context);
600 CheckObjCDeclScope(CDecl);
601 return DeclPtrTy::make(CDecl);
604 /// ActOnStartCategoryImplementation - Perform semantic checks on the
605 /// category implementation declaration and build an ObjCCategoryImplDecl
607 Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
608 SourceLocation AtCatImplLoc,
609 IdentifierInfo *ClassName, SourceLocation ClassLoc,
610 IdentifierInfo *CatName, SourceLocation CatLoc) {
611 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
612 ObjCCategoryDecl *CatIDecl = 0;
614 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
616 // Category @implementation with no corresponding @interface.
617 // Create and install one.
618 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
620 CatIDecl->setClassInterface(IDecl);
621 CatIDecl->insertNextClassCategory();
625 ObjCCategoryImplDecl *CDecl =
626 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
628 /// Check that class of this category is already completely declared.
629 if (!IDecl || IDecl->isForwardDecl())
630 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
632 // FIXME: PushOnScopeChains?
633 CurContext->addDecl(CDecl);
635 /// Check that CatName, category name, is not used in another implementation.
637 if (CatIDecl->getImplementation()) {
638 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
640 Diag(CatIDecl->getImplementation()->getLocation(),
641 diag::note_previous_definition);
643 CatIDecl->setImplementation(CDecl);
646 CheckObjCDeclScope(CDecl);
647 return DeclPtrTy::make(CDecl);
650 Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
651 SourceLocation AtClassImplLoc,
652 IdentifierInfo *ClassName, SourceLocation ClassLoc,
653 IdentifierInfo *SuperClassname,
654 SourceLocation SuperClassLoc) {
655 ObjCInterfaceDecl* IDecl = 0;
656 // Check for another declaration kind with the same name.
658 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
659 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
660 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
661 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
663 // Is there an interface declaration of this class; if not, warn!
664 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
665 if (!IDecl || IDecl->isForwardDecl()) {
666 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
671 // Check that super class name is valid class name
672 ObjCInterfaceDecl* SDecl = 0;
673 if (SuperClassname) {
674 // Check if a different kind of symbol declared in this scope.
675 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
676 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
677 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
679 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
681 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
683 Diag(SuperClassLoc, diag::err_undef_superclass)
684 << SuperClassname << ClassName;
685 else if (IDecl && IDecl->getSuperClass() != SDecl) {
686 // This implementation and its interface do not have the same
688 Diag(SuperClassLoc, diag::err_conflicting_super_class)
689 << SDecl->getDeclName();
690 Diag(SDecl->getLocation(), diag::note_previous_definition);
696 // Legacy case of @implementation with no corresponding @interface.
697 // Build, chain & install the interface decl into the identifier.
699 // FIXME: Do we support attributes on the @implementation? If so we should
701 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
702 ClassName, ClassLoc, false, true);
703 IDecl->setSuperClass(SDecl);
704 IDecl->setLocEnd(ClassLoc);
706 PushOnScopeChains(IDecl, TUScope);
708 // Mark the interface as being completed, even if it was just as
710 // declaration; the user cannot reopen it.
711 IDecl->setForwardDecl(false);
714 ObjCImplementationDecl* IMPDecl =
715 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
718 if (CheckObjCDeclScope(IMPDecl))
719 return DeclPtrTy::make(IMPDecl);
721 // Check that there is no duplicate implementation of this class.
722 if (IDecl->getImplementation()) {
723 // FIXME: Don't leak everything!
724 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
725 Diag(IDecl->getImplementation()->getLocation(),
726 diag::note_previous_definition);
727 } else { // add it to the list.
728 IDecl->setImplementation(IMPDecl);
729 PushOnScopeChains(IMPDecl, TUScope);
731 return DeclPtrTy::make(IMPDecl);
734 void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
735 ObjCIvarDecl **ivars, unsigned numIvars,
736 SourceLocation RBrace) {
737 assert(ImpDecl && "missing implementation decl");
738 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
741 /// Check case of non-existing @interface decl.
742 /// (legacy objective-c @implementation decl without an @interface decl).
743 /// Add implementations's ivar to the synthesize class's ivar list.
744 if (IDecl->isImplicitInterfaceDecl()) {
745 IDecl->setIVarList(ivars, numIvars, Context);
746 IDecl->setLocEnd(RBrace);
749 // If implementation has empty ivar list, just return.
753 assert(ivars && "missing @implementation ivars");
755 // Check interface's Ivar list against those in the implementation.
756 // names and types must match.
759 ObjCInterfaceDecl::ivar_iterator
760 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
761 for (; numIvars > 0 && IVI != IVE; ++IVI) {
762 ObjCIvarDecl* ImplIvar = ivars[j++];
763 ObjCIvarDecl* ClsIvar = *IVI;
764 assert (ImplIvar && "missing implementation ivar");
765 assert (ClsIvar && "missing class ivar");
767 // First, make sure the types match.
768 if (Context.getCanonicalType(ImplIvar->getType()) !=
769 Context.getCanonicalType(ClsIvar->getType())) {
770 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
771 << ImplIvar->getIdentifier()
772 << ImplIvar->getType() << ClsIvar->getType();
773 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
774 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
775 Expr *ImplBitWidth = ImplIvar->getBitWidth();
776 Expr *ClsBitWidth = ClsIvar->getBitWidth();
777 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
778 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
779 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
780 << ImplIvar->getIdentifier();
781 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
784 // Make sure the names are identical.
785 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
786 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
787 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
788 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
794 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
796 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
799 void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
800 bool &IncompleteImpl) {
801 if (!IncompleteImpl) {
802 Diag(ImpLoc, diag::warn_incomplete_impl);
803 IncompleteImpl = true;
805 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
808 void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
809 ObjCMethodDecl *IntfMethodDecl) {
810 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
811 ImpMethodDecl->getResultType()) &&
812 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
813 ImpMethodDecl->getResultType())) {
814 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
815 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
816 << ImpMethodDecl->getResultType();
817 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
820 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
821 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
822 IM != EM; ++IM, ++IF) {
823 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()) ||
824 Context.QualifiedIdConformsQualifiedId((*IF)->getType(),
828 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
829 << ImpMethodDecl->getDeclName() << (*IF)->getType()
831 Diag((*IF)->getLocation(), diag::note_previous_definition);
835 /// isPropertyReadonly - Return true if property is readonly, by searching
836 /// for the property in the class and in its categories and implementations
838 bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
839 ObjCInterfaceDecl *IDecl) {
840 // by far the most common case.
841 if (!PDecl->isReadOnly())
843 // Even if property is ready only, if interface has a user defined setter,
844 // it is not considered read only.
845 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
848 // Main class has the property as 'readonly'. Must search
849 // through the category list to see if the property's
850 // attribute has been over-ridden to 'readwrite'.
851 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
852 Category; Category = Category->getNextClassCategory()) {
853 // Even if property is ready only, if a category has a user defined setter,
854 // it is not considered read only.
855 if (Category->getInstanceMethod(PDecl->getSetterName()))
857 ObjCPropertyDecl *P =
858 Category->FindPropertyDeclaration(PDecl->getIdentifier());
859 if (P && !P->isReadOnly())
863 // Also, check for definition of a setter method in the implementation if
865 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
866 if (ObjCImplementationDecl *IMD =
867 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
868 if (IMD->getInstanceMethod(PDecl->getSetterName()))
870 } else if (ObjCCategoryImplDecl *CIMD =
871 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
872 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
876 // Lastly, look through the implementation (if one is in scope).
877 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
878 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
880 // If all fails, look at the super class.
881 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
882 return isPropertyReadonly(PDecl, SIDecl);
886 /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
887 /// improve the efficiency of selector lookups and type checking by associating
888 /// with each protocol / interface / category the flattened instance tables. If
889 /// we used an immutable set to keep the table then it wouldn't add significant
890 /// memory cost and it would be handy for lookups.
892 /// CheckProtocolMethodDefs - This routine checks unimplemented methods
893 /// Declared in protocol, and those referenced by it.
894 void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
895 ObjCProtocolDecl *PDecl,
896 bool& IncompleteImpl,
897 const llvm::DenseSet<Selector> &InsMap,
898 const llvm::DenseSet<Selector> &ClsMap,
899 ObjCInterfaceDecl *IDecl) {
900 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
901 ObjCInterfaceDecl *NSIDecl = 0;
902 if (getLangOptions().NeXTRuntime) {
903 // check to see if class implements forwardInvocation method and objects
904 // of this class are derived from 'NSProxy' so that to forward requests
905 // from one object to another.
906 // Under such conditions, which means that every method possible is
907 // implemented in the class, we should not issue "Method definition not
909 // FIXME: Use a general GetUnarySelector method for this.
910 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
911 Selector fISelector = Context.Selectors.getSelector(1, &II);
912 if (InsMap.count(fISelector))
913 // Is IDecl derived from 'NSProxy'? If so, no instance methods
914 // need be implemented in the implementation.
915 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
918 // If a method lookup fails locally we still need to look and see if
919 // the method was implemented by a base class or an inherited
920 // protocol. This lookup is slow, but occurs rarely in correct code
921 // and otherwise would terminate in a warning.
923 // check unimplemented instance methods.
925 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
926 E = PDecl->instmeth_end(); I != E; ++I) {
927 ObjCMethodDecl *method = *I;
928 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
929 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
931 !Super->lookupInstanceMethod(method->getSelector()))) {
932 // Ugly, but necessary. Method declared in protcol might have
933 // have been synthesized due to a property declared in the class which
934 // uses the protocol.
935 ObjCMethodDecl *MethodInClass =
936 IDecl->lookupInstanceMethod(method->getSelector());
937 if (!MethodInClass || !MethodInClass->isSynthesized())
938 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
941 // check unimplemented class methods
942 for (ObjCProtocolDecl::classmeth_iterator
943 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
945 ObjCMethodDecl *method = *I;
946 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
947 !ClsMap.count(method->getSelector()) &&
948 (!Super || !Super->lookupClassMethod(method->getSelector())))
949 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
951 // Check on this protocols's referenced protocols, recursively.
952 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
953 E = PDecl->protocol_end(); PI != E; ++PI)
954 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
957 /// MatchAllMethodDeclarations - Check methods declaraed in interface or
958 /// or protocol against those declared in their implementations.
960 void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
961 const llvm::DenseSet<Selector> &ClsMap,
962 llvm::DenseSet<Selector> &InsMapSeen,
963 llvm::DenseSet<Selector> &ClsMapSeen,
964 ObjCImplDecl* IMPDecl,
965 ObjCContainerDecl* CDecl,
966 bool &IncompleteImpl,
967 bool ImmediateClass) {
968 // Check and see if instance methods in class interface have been
969 // implemented in the implementation class. If so, their types match.
970 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
971 E = CDecl->instmeth_end(); I != E; ++I) {
972 if (InsMapSeen.count((*I)->getSelector()))
974 InsMapSeen.insert((*I)->getSelector());
975 if (!(*I)->isSynthesized() &&
976 !InsMap.count((*I)->getSelector())) {
978 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
981 ObjCMethodDecl *ImpMethodDecl =
982 IMPDecl->getInstanceMethod((*I)->getSelector());
983 ObjCMethodDecl *IntfMethodDecl =
984 CDecl->getInstanceMethod((*I)->getSelector());
985 assert(IntfMethodDecl &&
986 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
987 // ImpMethodDecl may be null as in a @dynamic property.
989 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
993 // Check and see if class methods in class interface have been
994 // implemented in the implementation class. If so, their types match.
995 for (ObjCInterfaceDecl::classmeth_iterator
996 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
997 if (ClsMapSeen.count((*I)->getSelector()))
999 ClsMapSeen.insert((*I)->getSelector());
1000 if (!ClsMap.count((*I)->getSelector())) {
1002 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1004 ObjCMethodDecl *ImpMethodDecl =
1005 IMPDecl->getClassMethod((*I)->getSelector());
1006 ObjCMethodDecl *IntfMethodDecl =
1007 CDecl->getClassMethod((*I)->getSelector());
1008 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1011 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1012 // Check for any implementation of a methods declared in protocol.
1013 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1014 E = I->protocol_end(); PI != E; ++PI)
1015 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1017 (*PI), IncompleteImpl, false);
1018 if (I->getSuperClass())
1019 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1021 I->getSuperClass(), IncompleteImpl, false);
1025 void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1026 ObjCContainerDecl* CDecl,
1027 bool IncompleteImpl) {
1028 llvm::DenseSet<Selector> InsMap;
1029 // Check and see if instance methods in class interface have been
1030 // implemented in the implementation class.
1031 for (ObjCImplementationDecl::instmeth_iterator
1032 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
1033 InsMap.insert((*I)->getSelector());
1035 // Check and see if properties declared in the interface have either 1)
1036 // an implementation or 2) there is a @synthesize/@dynamic implementation
1037 // of the property in the @implementation.
1038 if (isa<ObjCInterfaceDecl>(CDecl))
1039 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1040 E = CDecl->prop_end(); P != E; ++P) {
1041 ObjCPropertyDecl *Prop = (*P);
1042 if (Prop->isInvalidDecl())
1044 ObjCPropertyImplDecl *PI = 0;
1045 // Is there a matching propery synthesize/dynamic?
1046 for (ObjCImplDecl::propimpl_iterator
1047 I = IMPDecl->propimpl_begin(),
1048 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1049 if ((*I)->getPropertyDecl() == Prop) {
1055 if (!InsMap.count(Prop->getGetterName())) {
1056 Diag(Prop->getLocation(),
1057 diag::warn_setter_getter_impl_required)
1058 << Prop->getDeclName() << Prop->getGetterName();
1059 Diag(IMPDecl->getLocation(),
1060 diag::note_property_impl_required);
1063 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1064 Diag(Prop->getLocation(),
1065 diag::warn_setter_getter_impl_required)
1066 << Prop->getDeclName() << Prop->getSetterName();
1067 Diag(IMPDecl->getLocation(),
1068 diag::note_property_impl_required);
1072 llvm::DenseSet<Selector> ClsMap;
1073 for (ObjCImplementationDecl::classmeth_iterator
1074 I = IMPDecl->classmeth_begin(),
1075 E = IMPDecl->classmeth_end(); I != E; ++I)
1076 ClsMap.insert((*I)->getSelector());
1078 // Check for type conflict of methods declared in a class/protocol and
1079 // its implementation; if any.
1080 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1081 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1083 IncompleteImpl, true);
1085 // Check the protocol list for unimplemented methods in the @implementation
1087 // Check and see if class methods in class interface have been
1088 // implemented in the implementation class.
1090 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1091 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1092 E = I->protocol_end(); PI != E; ++PI)
1093 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1095 // Check class extensions (unnamed categories)
1096 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1097 Categories; Categories = Categories->getNextClassCategory()) {
1098 if (!Categories->getIdentifier()) {
1099 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1103 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1104 // For extended class, unimplemented methods in its protocols will
1105 // be reported in the primary class.
1106 if (C->getIdentifier()) {
1107 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1108 E = C->protocol_end(); PI != E; ++PI)
1109 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1110 InsMap, ClsMap, C->getClassInterface());
1113 assert(false && "invalid ObjCContainerDecl type.");
1116 /// ActOnForwardClassDeclaration -
1118 Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
1119 IdentifierInfo **IdentList,
1121 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
1123 for (unsigned i = 0; i != NumElts; ++i) {
1124 // Check for another declaration kind with the same name.
1126 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
1127 if (PrevDecl && PrevDecl->isTemplateParameter()) {
1128 // Maybe we will complain about the shadowed template parameter.
1129 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1130 // Just pretend that we didn't see the previous declaration.
1134 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
1135 // GCC apparently allows the following idiom:
1137 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1138 // @class XCElementToggler;
1140 // FIXME: Make an extension?
1141 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1142 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
1143 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
1144 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1146 // a forward class declaration matching a typedef name of a class refers
1147 // to the underlying class.
1148 if (ObjCInterfaceType * OI =
1149 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1150 PrevDecl = OI->getDecl();
1153 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1154 if (!IDecl) { // Not already seen? Make a forward decl.
1155 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1157 // FIXME: need to get the 'real'
1158 // identifier loc from the parser.
1160 PushOnScopeChains(IDecl, TUScope);
1163 Interfaces.push_back(IDecl);
1166 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1169 CurContext->addDecl(CDecl);
1170 CheckObjCDeclScope(CDecl);
1171 return DeclPtrTy::make(CDecl);
1175 /// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1176 /// returns true, or false, accordingly.
1177 /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
1178 bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
1179 const ObjCMethodDecl *PrevMethod,
1180 bool matchBasedOnSizeAndAlignment) {
1181 QualType T1 = Context.getCanonicalType(Method->getResultType());
1182 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1185 // The result types are different.
1186 if (!matchBasedOnSizeAndAlignment)
1188 // Incomplete types don't have a size and alignment.
1189 if (T1->isIncompleteType() || T2->isIncompleteType())
1191 // Check is based on size and alignment.
1192 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1196 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1197 E = Method->param_end();
1198 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1200 for (; ParamI != E; ++ParamI, ++PrevI) {
1201 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1202 T1 = Context.getCanonicalType((*ParamI)->getType());
1203 T2 = Context.getCanonicalType((*PrevI)->getType());
1205 // The result types are different.
1206 if (!matchBasedOnSizeAndAlignment)
1208 // Incomplete types don't have a size and alignment.
1209 if (T1->isIncompleteType() || T2->isIncompleteType())
1211 // Check is based on size and alignment.
1212 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1219 /// \brief Read the contents of the instance and factory method pools
1220 /// for a given selector from external storage.
1222 /// This routine should only be called once, when neither the instance
1223 /// nor the factory method pool has an entry for this selector.
1224 Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
1226 assert(ExternalSource && "We need an external AST source");
1227 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1228 "Selector data already loaded into the instance method pool");
1229 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1230 "Selector data already loaded into the factory method pool");
1232 // Read the method list from the external source.
1233 std::pair<ObjCMethodList, ObjCMethodList> Methods
1234 = ExternalSource->ReadMethodPool(Sel);
1237 if (Methods.second.Method)
1238 FactoryMethodPool[Sel] = Methods.second;
1239 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
1242 if (Methods.first.Method)
1243 InstanceMethodPool[Sel] = Methods.first;
1245 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1248 void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
1249 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1250 = InstanceMethodPool.find(Method->getSelector());
1251 if (Pos == InstanceMethodPool.end()) {
1252 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1253 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1255 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1256 ObjCMethodList())).first;
1259 ObjCMethodList &Entry = Pos->second;
1260 if (Entry.Method == 0) {
1261 // Haven't seen a method with this selector name yet - add it.
1262 Entry.Method = Method;
1267 // We've seen a method with this name, see if we have already seen this type
1269 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1270 if (MatchTwoMethodDeclarations(Method, List->Method))
1273 // We have a new signature for an existing method - add it.
1274 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1275 Entry.Next = new ObjCMethodList(Method, Entry.Next);
1278 // FIXME: Finish implementing -Wno-strict-selector-match.
1279 ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1282 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1283 = InstanceMethodPool.find(Sel);
1284 if (Pos == InstanceMethodPool.end()) {
1285 if (ExternalSource && !FactoryMethodPool.count(Sel))
1286 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1291 ObjCMethodList &MethList = Pos->second;
1292 bool issueWarning = false;
1294 if (MethList.Method && MethList.Next) {
1295 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1296 // This checks if the methods differ by size & alignment.
1297 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1298 issueWarning = warn;
1300 if (issueWarning && (MethList.Method && MethList.Next)) {
1301 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1302 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1303 << MethList.Method->getSourceRange();
1304 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1305 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1306 << Next->Method->getSourceRange();
1308 return MethList.Method;
1311 void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
1312 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1313 = FactoryMethodPool.find(Method->getSelector());
1314 if (Pos == FactoryMethodPool.end()) {
1315 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1316 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1318 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1319 ObjCMethodList())).first;
1322 ObjCMethodList &FirstMethod = Pos->second;
1323 if (!FirstMethod.Method) {
1324 // Haven't seen a method with this selector name yet - add it.
1325 FirstMethod.Method = Method;
1326 FirstMethod.Next = 0;
1328 // We've seen a method with this name, now check the type signature(s).
1329 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1331 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
1333 match = MatchTwoMethodDeclarations(Method, Next->Method);
1336 // We have a new signature for an existing method - add it.
1337 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1338 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
1339 FirstMethod.Next = OMI;
1344 ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
1346 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1347 = FactoryMethodPool.find(Sel);
1348 if (Pos == FactoryMethodPool.end()) {
1349 if (ExternalSource && !InstanceMethodPool.count(Sel))
1350 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1355 ObjCMethodList &MethList = Pos->second;
1356 bool issueWarning = false;
1358 if (MethList.Method && MethList.Next) {
1359 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1360 // This checks if the methods differ by size & alignment.
1361 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1362 issueWarning = true;
1364 if (issueWarning && (MethList.Method && MethList.Next)) {
1365 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1366 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1367 << MethList.Method->getSourceRange();
1368 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1369 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1370 << Next->Method->getSourceRange();
1372 return MethList.Method;
1375 /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1376 /// have the property type and issue diagnostics if they don't.
1377 /// Also synthesize a getter/setter method if none exist (and update the
1378 /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1379 /// methods is the "right" thing to do.
1380 void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1381 ObjCContainerDecl *CD) {
1382 ObjCMethodDecl *GetterMethod, *SetterMethod;
1384 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1385 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1386 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1387 property->getLocation());
1390 if (Context.getCanonicalType(SetterMethod->getResultType())
1392 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1393 if (SetterMethod->param_size() != 1 ||
1394 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1395 Diag(property->getLocation(),
1396 diag::warn_accessor_property_type_mismatch)
1397 << property->getDeclName()
1398 << SetterMethod->getSelector();
1399 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1403 // Synthesize getter/setter methods if none exist.
1404 // Find the default getter and if one not found, add one.
1405 // FIXME: The synthesized property we set here is misleading. We almost always
1406 // synthesize these methods unless the user explicitly provided prototypes
1407 // (which is odd, but allowed). Sema should be typechecking that the
1408 // declarations jive in that situation (which it is not currently).
1409 if (!GetterMethod) {
1410 // No instance method of same name as property getter name was found.
1411 // Declare a getter method and add it to the list of methods
1413 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1414 property->getLocation(), property->getGetterName(),
1415 property->getType(), CD, true, false, true,
1416 (property->getPropertyImplementation() ==
1417 ObjCPropertyDecl::Optional) ?
1418 ObjCMethodDecl::Optional :
1419 ObjCMethodDecl::Required);
1420 CD->addDecl(GetterMethod);
1422 // A user declared getter will be synthesize when @synthesize of
1423 // the property with the same name is seen in the @implementation
1424 GetterMethod->setSynthesized(true);
1425 property->setGetterMethodDecl(GetterMethod);
1427 // Skip setter if property is read-only.
1428 if (!property->isReadOnly()) {
1429 // Find the default setter and if one not found, add one.
1430 if (!SetterMethod) {
1431 // No instance method of same name as property setter name was found.
1432 // Declare a setter method and add it to the list of methods
1434 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1435 property->getLocation(),
1436 property->getSetterName(),
1437 Context.VoidTy, CD, true, false, true,
1438 (property->getPropertyImplementation() ==
1439 ObjCPropertyDecl::Optional) ?
1440 ObjCMethodDecl::Optional :
1441 ObjCMethodDecl::Required);
1442 // Invent the arguments for the setter. We don't bother making a
1443 // nice name for the argument.
1444 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1445 property->getLocation(),
1446 property->getIdentifier(),
1447 property->getType(),
1451 SetterMethod->setMethodParams(Context, &Argument, 1);
1452 CD->addDecl(SetterMethod);
1454 // A user declared setter will be synthesize when @synthesize of
1455 // the property with the same name is seen in the @implementation
1456 SetterMethod->setSynthesized(true);
1457 property->setSetterMethodDecl(SetterMethod);
1459 // Add any synthesized methods to the global pool. This allows us to
1460 // handle the following, which is supported by GCC (and part of the design).
1463 // @property double bar;
1466 // void thisIsUnfortunate() {
1468 // double bar = [foo bar];
1472 AddInstanceMethodToGlobalPool(GetterMethod);
1474 AddInstanceMethodToGlobalPool(SetterMethod);
1477 /// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1478 /// identical selector names in current and its super classes and issues
1479 /// a warning if any of their argument types are incompatible.
1480 void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1481 ObjCMethodDecl *Method,
1483 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1484 if (ID == 0) return;
1486 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
1487 ObjCMethodDecl *SuperMethodDecl =
1488 SD->lookupMethod(Method->getSelector(), IsInstance);
1489 if (SuperMethodDecl == 0) {
1493 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1494 E = Method->param_end();
1495 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1496 for (; ParamI != E; ++ParamI, ++PrevI) {
1497 // Number of parameters are the same and is guaranteed by selector match.
1498 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1499 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1500 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1501 // If type of arguement of method in this class does not match its
1502 // respective argument type in the super class method, issue warning;
1503 if (!Context.typesAreCompatible(T1, T2)) {
1504 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
1506 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1514 // Note: For class/category implemenations, allMethods/allProperties is
1516 void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1517 DeclPtrTy *allMethods, unsigned allNum,
1518 DeclPtrTy *allProperties, unsigned pNum,
1519 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
1520 Decl *ClassDecl = classDecl.getAs<Decl>();
1522 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1523 // always passing in a decl. If the decl has an error, isInvalidDecl()
1528 bool isInterfaceDeclKind =
1529 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1530 || isa<ObjCProtocolDecl>(ClassDecl);
1531 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
1533 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1535 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1536 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1537 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1539 for (unsigned i = 0; i < allNum; i++ ) {
1540 ObjCMethodDecl *Method =
1541 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
1543 if (!Method) continue; // Already issued a diagnostic.
1544 if (Method->isInstanceMethod()) {
1545 /// Check for instance method of the same name with incompatible types
1546 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
1547 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1549 if ((isInterfaceDeclKind && PrevMethod && !match)
1550 || (checkIdenticalMethods && match)) {
1551 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
1552 << Method->getDeclName();
1553 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
1555 DC->addDecl(Method);
1556 InsMap[Method->getSelector()] = Method;
1557 /// The following allows us to typecheck messages to "id".
1558 AddInstanceMethodToGlobalPool(Method);
1559 // verify that the instance method conforms to the same definition of
1560 // parent methods if it shadows one.
1561 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
1564 /// Check for class method of the same name with incompatible types
1565 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
1566 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1568 if ((isInterfaceDeclKind && PrevMethod && !match)
1569 || (checkIdenticalMethods && match)) {
1570 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
1571 << Method->getDeclName();
1572 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
1574 DC->addDecl(Method);
1575 ClsMap[Method->getSelector()] = Method;
1576 /// The following allows us to typecheck messages to "Class".
1577 AddFactoryMethodToGlobalPool(Method);
1578 // verify that the class method conforms to the same definition of
1579 // parent methods if it shadows one.
1580 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
1584 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
1585 // Compares properties declared in this class to those of its
1587 ComparePropertiesInBaseAndSuper(I);
1588 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
1589 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1590 // Categories are used to extend the class by declaring new methods.
1591 // By the same token, they are also used to add new properties. No
1592 // need to compare the added property to those in the class.
1594 // Merge protocol properties into category
1595 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
1596 if (C->getIdentifier() == 0)
1597 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
1599 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1600 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1601 // user-defined setter/getter. It also synthesizes setter/getter methods
1602 // and adds them to the DeclContext and global method pools.
1603 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1604 E = CDecl->prop_end();
1606 ProcessPropertyDecl(*I, CDecl);
1607 CDecl->setAtEndLoc(AtEndLoc);
1609 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
1610 IC->setAtEndLoc(AtEndLoc);
1611 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
1612 ImplMethodsVsClassMethods(IC, IDecl);
1613 } else if (ObjCCategoryImplDecl* CatImplClass =
1614 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
1615 CatImplClass->setAtEndLoc(AtEndLoc);
1617 // Find category interface decl and then check that all methods declared
1618 // in this interface are implemented in the category @implementation.
1619 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
1620 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
1621 Categories; Categories = Categories->getNextClassCategory()) {
1622 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1623 ImplMethodsVsClassMethods(CatImplClass, Categories);
1629 if (isInterfaceDeclKind) {
1630 // Reject invalid vardecls.
1631 for (unsigned i = 0; i != tuvNum; i++) {
1632 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1633 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1634 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
1635 if (!VDecl->hasExternalStorage())
1636 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
1643 /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1644 /// objective-c's type qualifier from the parser version of the same info.
1645 static Decl::ObjCDeclQualifier
1646 CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1647 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1648 if (PQTVal & ObjCDeclSpec::DQ_In)
1649 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1650 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1651 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1652 if (PQTVal & ObjCDeclSpec::DQ_Out)
1653 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1654 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1655 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1656 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1657 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1658 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1659 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
1664 Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
1665 SourceLocation MethodLoc, SourceLocation EndLoc,
1666 tok::TokenKind MethodType, DeclPtrTy classDecl,
1667 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
1669 // optional arguments. The number of types/arguments is obtained
1670 // from the Sel.getNumArgs().
1671 ObjCArgInfo *ArgInfo,
1672 llvm::SmallVectorImpl<Declarator> &Cdecls,
1673 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1675 Decl *ClassDecl = classDecl.getAs<Decl>();
1677 // Make sure we can establish a context for the method.
1679 Diag(MethodLoc, diag::error_missing_method_context);
1680 FunctionLabelMap.clear();
1683 QualType resultDeclType;
1686 resultDeclType = GetTypeFromParser(ReturnType);
1688 // Methods cannot return interface types. All ObjC objects are
1689 // passed by reference.
1690 if (resultDeclType->isObjCInterfaceType()) {
1691 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1692 << 0 << resultDeclType;
1695 } else // get the type for "id".
1696 resultDeclType = Context.getObjCIdType();
1698 ObjCMethodDecl* ObjCMethod =
1699 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
1700 cast<DeclContext>(ClassDecl),
1701 MethodType == tok::minus, isVariadic,
1703 MethodDeclKind == tok::objc_optional ?
1704 ObjCMethodDecl::Optional :
1705 ObjCMethodDecl::Required);
1707 llvm::SmallVector<ParmVarDecl*, 16> Params;
1709 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
1713 if (ArgInfo[i].Type == 0) {
1714 ArgType = Context.getObjCIdType();
1717 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
1718 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1719 ArgType = adjustParameterType(ArgType);
1723 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1724 ArgInfo[i].Name, ArgType, DI,
1727 if (ArgType->isObjCInterfaceType()) {
1728 Diag(ArgInfo[i].NameLoc,
1729 diag::err_object_cannot_be_passed_returned_by_value)
1731 Param->setInvalidDecl();
1734 Param->setObjCDeclQualifier(
1735 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
1737 // Apply the attributes to the parameter.
1738 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
1740 Params.push_back(Param);
1743 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
1744 ObjCMethod->setObjCDeclQualifier(
1745 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1746 const ObjCMethodDecl *PrevMethod = 0;
1749 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
1751 const ObjCMethodDecl *InterfaceMD = 0;
1753 // For implementations (which can be very "coarse grain"), we add the
1754 // method now. This allows the AST to implement lookup methods that work
1755 // incrementally (without waiting until we parse the @end). It also allows
1756 // us to flag multiple declaration errors as they occur.
1757 if (ObjCImplementationDecl *ImpDecl =
1758 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
1759 if (MethodType == tok::minus) {
1760 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1761 ImpDecl->addInstanceMethod(ObjCMethod);
1763 PrevMethod = ImpDecl->getClassMethod(Sel);
1764 ImpDecl->addClassMethod(ObjCMethod);
1766 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1767 MethodType == tok::minus);
1769 Diag(EndLoc, diag::warn_attribute_method_def);
1770 } else if (ObjCCategoryImplDecl *CatImpDecl =
1771 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
1772 if (MethodType == tok::minus) {
1773 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1774 CatImpDecl->addInstanceMethod(ObjCMethod);
1776 PrevMethod = CatImpDecl->getClassMethod(Sel);
1777 CatImpDecl->addClassMethod(ObjCMethod);
1780 Diag(EndLoc, diag::warn_attribute_method_def);
1783 // You can never have two method definitions with the same name.
1784 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
1785 << ObjCMethod->getDeclName();
1786 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
1789 // If the interface declared this method, and it was deprecated there,
1790 // mark it deprecated here.
1791 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1792 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1794 return DeclPtrTy::make(ObjCMethod);
1797 void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1799 unsigned &Attributes) {
1800 // FIXME: Improve the reported location.
1802 // readonly and readwrite/assign/retain/copy conflict.
1803 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1804 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1805 ObjCDeclSpec::DQ_PR_assign |
1806 ObjCDeclSpec::DQ_PR_copy |
1807 ObjCDeclSpec::DQ_PR_retain))) {
1808 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1810 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1812 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1815 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1816 diag::err_objc_property_attr_mutually_exclusive :
1817 diag::warn_objc_property_attr_mutually_exclusive)
1818 << "readonly" << which;
1821 // Check for copy or retain on non-object types.
1822 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1823 !PropertyTy->isObjCObjectPointerType() &&
1824 !PropertyTy->isBlockPointerType() &&
1825 !Context.isObjCNSObjectType(PropertyTy)) {
1826 Diag(Loc, diag::err_objc_property_requires_object)
1827 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1828 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1831 // Check for more than one of { assign, copy, retain }.
1832 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1833 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1834 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1835 << "assign" << "copy";
1836 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1838 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1839 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1840 << "assign" << "retain";
1841 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1843 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1844 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1845 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1846 << "copy" << "retain";
1847 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1851 // Warn if user supplied no assignment attribute, property is
1852 // readwrite, and this is an object type.
1853 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1854 ObjCDeclSpec::DQ_PR_retain)) &&
1855 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1856 PropertyTy->isObjCObjectPointerType()) {
1857 // Skip this warning in gc-only mode.
1858 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1859 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1861 // If non-gc code warn that this is likely inappropriate.
1862 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1863 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1865 // FIXME: Implement warning dependent on NSCopying being
1866 // implemented. See also:
1867 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1868 // (please trim this list while you are at it).
1871 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1872 && getLangOptions().getGCMode() == LangOptions::GCOnly
1873 && PropertyTy->isBlockPointerType())
1874 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1877 Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1878 FieldDeclarator &FD,
1882 DeclPtrTy ClassCategory,
1883 bool *isOverridingProperty,
1884 tok::ObjCKeywordKind MethodImplKind) {
1885 unsigned Attributes = ODS.getPropertyAttributes();
1886 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1887 // default is readwrite!
1888 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1889 // property is defaulted to 'assign' if it is readwrite and is
1890 // not retain or copy
1891 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1893 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1894 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1895 QualType T = GetTypeForDeclarator(FD.D, S);
1896 Decl *ClassDecl = ClassCategory.getAs<Decl>();
1897 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
1898 // May modify Attributes.
1899 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
1900 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1901 if (!CDecl->getIdentifier()) {
1902 // This is a continuation class. property requires special
1904 if ((CCPrimary = CDecl->getClassInterface())) {
1905 // Find the property in continuation class's primary class only.
1906 IdentifierInfo *PropertyId = FD.D.getIdentifier();
1907 if (ObjCPropertyDecl *PIDecl =
1908 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
1909 // property 'PIDecl's readonly attribute will be over-ridden
1910 // with continuation class's readwrite property attribute!
1911 unsigned PIkind = PIDecl->getPropertyAttributes();
1912 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
1913 unsigned assignRetainCopyNonatomic =
1914 (ObjCPropertyDecl::OBJC_PR_assign |
1915 ObjCPropertyDecl::OBJC_PR_retain |
1916 ObjCPropertyDecl::OBJC_PR_copy |
1917 ObjCPropertyDecl::OBJC_PR_nonatomic);
1918 if ((Attributes & assignRetainCopyNonatomic) !=
1919 (PIkind & assignRetainCopyNonatomic)) {
1920 Diag(AtLoc, diag::warn_property_attr_mismatch);
1921 Diag(PIDecl->getLocation(), diag::note_property_declare);
1923 PIDecl->makeitReadWriteAttribute();
1924 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1925 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1926 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1927 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1928 PIDecl->setSetterName(SetterSel);
1930 Diag(AtLoc, diag::err_use_continuation_class)
1931 << CCPrimary->getDeclName();
1932 Diag(PIDecl->getLocation(), diag::note_property_declare);
1934 *isOverridingProperty = true;
1935 // Make sure setter decl is synthesized, and added to primary
1937 ProcessPropertyDecl(PIDecl, CCPrimary);
1940 // No matching property found in the primary class. Just fall thru
1941 // and add property to continuation class's primary class.
1942 ClassDecl = CCPrimary;
1944 Diag(CDecl->getLocation(), diag::err_continuation_class);
1945 *isOverridingProperty = true;
1950 // Issue a warning if property is 'assign' as default and its object, which is
1951 // gc'able conforms to NSCopying protocol
1952 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
1953 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
1954 if (T->isObjCObjectPointerType()) {
1955 QualType InterfaceTy = T->getPointeeType();
1956 if (const ObjCInterfaceType *OIT =
1957 InterfaceTy->getAs<ObjCInterfaceType>()) {
1958 ObjCInterfaceDecl *IDecl = OIT->getDecl();
1960 if (ObjCProtocolDecl* PNSCopying =
1961 LookupProtocol(&Context.Idents.get("NSCopying")))
1962 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
1963 Diag(AtLoc, diag::warn_implements_nscopying)
1964 << FD.D.getIdentifier();
1967 if (T->isObjCInterfaceType())
1968 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
1970 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1971 assert(DC && "ClassDecl is not a DeclContext");
1972 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1973 FD.D.getIdentifierLoc(),
1974 FD.D.getIdentifier(), T);
1977 if (T->isArrayType() || T->isFunctionType()) {
1978 Diag(AtLoc, diag::err_property_type) << T;
1979 PDecl->setInvalidDecl();
1982 ProcessDeclAttributes(S, PDecl, FD.D);
1984 // Regardless of setter/getter attribute, we save the default getter/setter
1985 // selector names in anticipation of declaration of setter/getter methods.
1986 PDecl->setGetterName(GetterSel);
1987 PDecl->setSetterName(SetterSel);
1989 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
1990 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
1992 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
1993 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
1995 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
1996 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
1999 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
2001 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2002 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2004 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2005 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2008 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
2010 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
2011 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
2013 if (MethodImplKind == tok::objc_required)
2014 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2015 else if (MethodImplKind == tok::objc_optional)
2016 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
2017 // A case of continuation class adding a new property in the class. This
2018 // is not what it was meant for. However, gcc supports it and so should we.
2019 // Make sure setter/getters are declared here.
2021 ProcessPropertyDecl(PDecl, CCPrimary);
2023 return DeclPtrTy::make(PDecl);
2026 /// ActOnPropertyImplDecl - This routine performs semantic checks and
2027 /// builds the AST node for a property implementation declaration; declared
2028 /// as @synthesize or @dynamic.
2030 Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
2031 SourceLocation PropertyLoc,
2033 DeclPtrTy ClassCatImpDecl,
2034 IdentifierInfo *PropertyId,
2035 IdentifierInfo *PropertyIvar) {
2036 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
2037 // Make sure we have a context for the property implementation declaration.
2038 if (!ClassImpDecl) {
2039 Diag(AtLoc, diag::error_missing_property_context);
2042 ObjCPropertyDecl *property = 0;
2043 ObjCInterfaceDecl* IDecl = 0;
2044 // Find the class or category class where this property must have
2046 ObjCImplementationDecl *IC = 0;
2047 ObjCCategoryImplDecl* CatImplClass = 0;
2048 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
2049 IDecl = IC->getClassInterface();
2050 // We always synthesize an interface for an implementation
2051 // without an interface decl. So, IDecl is always non-zero.
2053 "ActOnPropertyImplDecl - @implementation without @interface");
2055 // Look for this property declaration in the @implementation's @interface
2056 property = IDecl->FindPropertyDeclaration(PropertyId);
2058 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
2061 if (const ObjCCategoryDecl *CD =
2062 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
2063 if (CD->getIdentifier()) {
2064 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
2065 Diag(property->getLocation(), diag::note_property_declare);
2069 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
2071 Diag(AtLoc, diag::error_synthesize_category_decl);
2074 IDecl = CatImplClass->getClassInterface();
2076 Diag(AtLoc, diag::error_missing_property_interface);
2079 ObjCCategoryDecl *Category =
2080 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
2082 // If category for this implementation not found, it is an error which
2083 // has already been reported eralier.
2086 // Look for this property declaration in @implementation's category
2087 property = Category->FindPropertyDeclaration(PropertyId);
2089 Diag(PropertyLoc, diag::error_bad_category_property_decl)
2090 << Category->getDeclName();
2094 Diag(AtLoc, diag::error_bad_property_context);
2097 ObjCIvarDecl *Ivar = 0;
2098 // Check that we have a valid, previously declared ivar for @synthesize
2102 PropertyIvar = PropertyId;
2103 QualType PropType = Context.getCanonicalType(property->getType());
2104 // Check that this is a previously declared 'ivar' in 'IDecl' interface
2105 ObjCInterfaceDecl *ClassDeclared;
2106 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
2108 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
2109 assert(EnclosingContext &&
2110 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
2111 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2112 PropertyIvar, PropType, /*Dinfo=*/0,
2113 ObjCIvarDecl::Public,
2115 Ivar->setLexicalDeclContext(IDecl);
2116 IDecl->addDecl(Ivar);
2117 property->setPropertyIvarDecl(Ivar);
2118 if (!getLangOptions().ObjCNonFragileABI)
2119 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
2120 // Note! I deliberately want it to fall thru so, we have a
2121 // a property implementation and to avoid future warnings.
2122 } else if (getLangOptions().ObjCNonFragileABI &&
2123 ClassDeclared != IDecl) {
2124 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
2125 << property->getDeclName() << Ivar->getDeclName()
2126 << ClassDeclared->getDeclName();
2127 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2128 << Ivar << Ivar->getNameAsCString();
2129 // Note! I deliberately want it to fall thru so more errors are caught.
2131 QualType IvarType = Context.getCanonicalType(Ivar->getType());
2133 // Check that type of property and its ivar are type compatible.
2134 if (PropType != IvarType) {
2135 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
2136 Diag(PropertyLoc, diag::error_property_ivar_type)
2137 << property->getDeclName() << Ivar->getDeclName();
2138 // Note! I deliberately want it to fall thru so, we have a
2139 // a property implementation and to avoid future warnings.
2142 // FIXME! Rules for properties are somewhat different that those
2143 // for assignments. Use a new routine to consolidate all cases;
2144 // specifically for property redeclarations as well as for ivars.
2145 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2146 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
2147 if (lhsType != rhsType &&
2148 lhsType->isArithmeticType()) {
2149 Diag(PropertyLoc, diag::error_property_ivar_type)
2150 << property->getDeclName() << Ivar->getDeclName();
2151 // Fall thru - see previous comment
2153 // __weak is explicit. So it works on Canonical type.
2154 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2155 getLangOptions().getGCMode() != LangOptions::NonGC) {
2156 Diag(PropertyLoc, diag::error_weak_property)
2157 << property->getDeclName() << Ivar->getDeclName();
2158 // Fall thru - see previous comment
2160 if ((property->getType()->isObjCObjectPointerType() ||
2161 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2162 getLangOptions().getGCMode() != LangOptions::NonGC) {
2163 Diag(PropertyLoc, diag::error_strong_property)
2164 << property->getDeclName() << Ivar->getDeclName();
2165 // Fall thru - see previous comment
2168 } else if (PropertyIvar)
2170 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
2171 assert (property && "ActOnPropertyImplDecl - property declaration missing");
2172 ObjCPropertyImplDecl *PIDecl =
2173 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2176 ObjCPropertyImplDecl::Synthesize
2177 : ObjCPropertyImplDecl::Dynamic),
2181 if (ObjCPropertyImplDecl *PPIDecl =
2182 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
2183 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2184 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2186 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2189 if (ObjCPropertyImplDecl *PPIDecl
2190 = IC->FindPropertyImplDecl(PropertyId)) {
2191 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2192 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
2195 IC->addPropertyImplementation(PIDecl);
2198 if (ObjCPropertyImplDecl *PPIDecl =
2199 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
2200 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2201 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2203 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2206 if (ObjCPropertyImplDecl *PPIDecl =
2207 CatImplClass->FindPropertyImplDecl(PropertyId)) {
2208 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2209 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
2212 CatImplClass->addPropertyImplementation(PIDecl);
2215 return DeclPtrTy::make(PIDecl);
2218 bool Sema::CheckObjCDeclScope(Decl *D) {
2219 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
2222 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2223 D->setInvalidDecl();
2228 /// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2229 /// instance variables of ClassName into Decls.
2230 void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
2231 IdentifierInfo *ClassName,
2232 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
2233 // Check that ClassName is a valid class
2234 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2236 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2239 if (LangOpts.ObjCNonFragileABI) {
2240 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2244 // Collect the instance variables
2245 llvm::SmallVector<FieldDecl*, 32> RecFields;
2246 Context.CollectObjCIvars(Class, RecFields);
2247 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2248 for (unsigned i = 0; i < RecFields.size(); i++) {
2249 FieldDecl* ID = RecFields[i];
2250 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2251 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2252 ID->getIdentifier(), ID->getType(),
2254 Decls.push_back(Sema::DeclPtrTy::make(FD));
2257 // Introduce all of these fields into the appropriate scope.
2258 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
2259 D != Decls.end(); ++D) {
2260 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
2261 if (getLangOptions().CPlusPlus)
2262 PushOnScopeChains(cast<FieldDecl>(FD), S);
2263 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
2264 Record->addDecl(FD);