blob: 0ec793360b05a44cebc302708442dca93137f98e [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Douglas Gregorf06cdae2010-01-03 18:01:57 +000015#include "Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
Steve Naroffca331292009-03-03 14:49:36 +000017#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000021using namespace clang;
22
Fariborz Jahanianc001e892009-05-08 20:20:55 +000023bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
24 ObjCMethodDecl *GetterMethod,
25 SourceLocation Loc) {
26 if (GetterMethod &&
27 GetterMethod->getResultType() != property->getType()) {
28 AssignConvertType result = Incompatible;
Steve Narofff4954562009-07-16 15:41:00 +000029 if (property->getType()->isObjCObjectPointerType())
Fariborz Jahanian7aaa4092009-05-08 21:10:00 +000030 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
Fariborz Jahanianc001e892009-05-08 20:20:55 +000031 if (result != Compatible) {
Mike Stump1eb44332009-09-09 15:08:12 +000032 Diag(Loc, diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianc001e892009-05-08 20:20:55 +000033 << property->getDeclName()
34 << GetterMethod->getSelector();
35 Diag(GetterMethod->getLocation(), diag::note_declared_at);
36 return true;
37 }
38 }
39 return false;
40}
41
Steve Naroffebf64432009-02-28 16:59:13 +000042/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000043/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000044void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000045 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000046 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump1eb44332009-09-09 15:08:12 +000047
Steve Naroff394f3f42008-07-25 17:57:26 +000048 // If we don't have a valid method decl, simply return.
49 if (!MDecl)
50 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000051
Chris Lattner38c5ebd2009-04-19 05:21:20 +000052 CurFunctionNeedsScopeChecking = false;
Chris Lattner6d97e5e2010-03-01 20:59:53 +000053 NumErrorsAtStartOfFunction = getDiagnostics().getNumErrors();
Chris Lattner38c5ebd2009-04-19 05:21:20 +000054
Steve Naroffa56f6162007-12-18 01:30:32 +000055 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000056 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000057 AddInstanceMethodToGlobalPool(MDecl);
58 else
59 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000060
Chris Lattner4d391482007-12-12 07:09:47 +000061 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000062 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000063
64 // Create Decl objects for each parameter, entrring them in the scope for
65 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000066
67 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000068 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +000069
Daniel Dunbar451318c2008-08-26 06:07:48 +000070 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
71 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000072
Chris Lattner8123a952008-04-10 02:22:51 +000073 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000074 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
75 E = MDecl->param_end(); PI != E; ++PI)
76 if ((*PI)->getIdentifier())
77 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000078}
79
Chris Lattnerb28317a2009-03-28 19:18:32 +000080Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000081ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
82 IdentifierInfo *ClassName, SourceLocation ClassLoc,
83 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000084 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +000085 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000086 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000087 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner4d391482007-12-12 07:09:47 +000089 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +000090 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000091 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000092 // Maybe we will complain about the shadowed template parameter.
93 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
94 // Just pretend that we didn't see the previous declaration.
95 PrevDecl = 0;
96 }
97
Ted Kremeneka526c5c2008-01-07 19:49:32 +000098 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000099 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000100 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000103 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000104 if (IDecl) {
105 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000106 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000107 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000108 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000109 Diag(IDecl->getLocation(), diag::note_previous_definition);
110
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000111 // Return the previous class interface.
112 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000113 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000114 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000115 IDecl->setLocation(AtInterfaceLoc);
116 IDecl->setForwardDecl(false);
Steve Naroff8b26cbd2009-09-11 00:12:01 +0000117 IDecl->setClassLoc(ClassLoc);
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000118
119 // Since this ObjCInterfaceDecl was created by a forward declaration,
120 // we now add it to the DeclContext since it wasn't added before
121 // (see ActOnForwardClassDeclaration).
122 CurContext->addDecl(IDecl);
123
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000124 if (AttrList)
125 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000126 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000127 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000128 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000129 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000130 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000131 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Steve Naroffa7503a72009-04-23 15:15:40 +0000133 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000134 }
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Chris Lattner4d391482007-12-12 07:09:47 +0000136 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000137 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000138 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000139
140 if (!PrevDecl) {
141 // Try to correct for a typo in the superclass name.
142 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
143 if (CorrectTypo(R, TUScope, 0) &&
144 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
145 Diag(SuperLoc, diag::err_undef_superclass_suggest)
146 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000147 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
148 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000149 }
150 }
151
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000152 if (PrevDecl == IDecl) {
153 Diag(SuperLoc, diag::err_recursive_superclass)
154 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
155 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000156 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000157 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000158 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000159
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000160 // Diagnose classes that inherit from deprecated classes.
161 if (SuperClassDecl)
162 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000164 if (PrevDecl && SuperClassDecl == 0) {
165 // The previous declaration was not a class decl. Check if we have a
166 // typedef. If we do, get the underlying class type.
167 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
168 QualType T = TDecl->getUnderlyingType();
169 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000170 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000171 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
172 }
173 }
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000175 // This handles the following case:
176 //
177 // typedef int SuperClass;
178 // @interface MyClass : SuperClass {} @end
179 //
180 if (!SuperClassDecl) {
181 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
182 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000183 }
184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000186 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
187 if (!SuperClassDecl)
188 Diag(SuperLoc, diag::err_undef_superclass)
189 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
190 else if (SuperClassDecl->isForwardDecl())
191 Diag(SuperLoc, diag::err_undef_superclass)
192 << SuperClassDecl->getDeclName() << ClassName
193 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000194 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000195 IDecl->setSuperClass(SuperClassDecl);
196 IDecl->setSuperClassLoc(SuperLoc);
197 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000198 }
Chris Lattner4d391482007-12-12 07:09:47 +0000199 } else { // we have a root class.
200 IDecl->setLocEnd(ClassLoc);
201 }
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000203 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000204 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000205 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000206 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000207 IDecl->setLocEnd(EndProtoLoc);
208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Anders Carlsson15281452008-11-04 16:57:32 +0000210 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000211 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000212}
213
214/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000215/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000216Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000217 IdentifierInfo *AliasName,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000218 SourceLocation AliasLocation,
219 IdentifierInfo *ClassName,
220 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000221 // Look for previous declaration of alias name
John McCallf36e02d2009-10-09 21:13:30 +0000222 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000223 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000224 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000225 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000226 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000227 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000228 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000229 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000230 }
231 // Check for class declaration
John McCallf36e02d2009-10-09 21:13:30 +0000232 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000233 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
234 QualType T = TDecl->getUnderlyingType();
235 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000236 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000237 ClassName = IDecl->getIdentifier();
John McCallf36e02d2009-10-09 21:13:30 +0000238 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000239 }
240 }
241 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000242 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
243 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000244 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000245 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000246 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000247 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000248 }
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000250 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000251 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000252 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Anders Carlsson15281452008-11-04 16:57:32 +0000254 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000255 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000256
Chris Lattnerb28317a2009-03-28 19:18:32 +0000257 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000258}
259
Steve Naroff61d68522009-03-05 15:22:01 +0000260void Sema::CheckForwardProtocolDeclarationForCircularDependency(
261 IdentifierInfo *PName,
262 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000263 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000264 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
265 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Douglas Gregor6e378de2009-04-23 23:18:26 +0000267 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000268 if (PDecl->getIdentifier() == PName) {
269 Diag(Ploc, diag::err_protocol_has_circular_dependency);
270 Diag(PrevLoc, diag::note_previous_definition);
271 }
Mike Stump1eb44332009-09-09 15:08:12 +0000272 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000273 PDecl->getLocation(), PDecl->getReferencedProtocols());
274 }
275 }
276}
277
Chris Lattnerb28317a2009-03-28 19:18:32 +0000278Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000279Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
280 IdentifierInfo *ProtocolName,
281 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000282 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000283 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000284 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000285 SourceLocation EndProtoLoc,
286 AttributeList *AttrList) {
287 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000288 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000289 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000290 if (PDecl) {
291 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000292 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000293 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000294 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000295 // Just return the protocol we already had.
296 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000297 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000298 }
Steve Naroff61d68522009-03-05 15:22:01 +0000299 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000300 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000301 CheckForwardProtocolDeclarationForCircularDependency(
302 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
303 PList.Destroy(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Steve Narofff11b5082008-08-13 16:39:22 +0000305 // Make sure the cached decl gets a valid start location.
306 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000307 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000308 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000309 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000310 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000311 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000312 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000313 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000314 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000315 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000316 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000317 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000318 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
319 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000320 PDecl->setLocEnd(EndProtoLoc);
321 }
Mike Stump1eb44332009-09-09 15:08:12 +0000322
323 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000324 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000325}
326
327/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000328/// issues an error if they are not declared. It returns list of
329/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000330void
Chris Lattnere13b9592008-07-26 04:03:38 +0000331Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000332 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000333 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000334 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000335 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000336 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000337 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000338 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
339 LookupObjCProtocolName);
340 if (CorrectTypo(R, TUScope, 0) &&
341 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
342 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
343 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000344 Diag(PDecl->getLocation(), diag::note_previous_decl)
345 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000346 }
347 }
348
349 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000350 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000351 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000352 continue;
353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000355 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000356
357 // If this is a forward declaration and we are supposed to warn in this
358 // case, do it.
359 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000360 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000361 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000362 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000363 }
364}
365
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000366/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000367/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000368///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000369void
Mike Stump1eb44332009-09-09 15:08:12 +0000370Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000371 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000372 const IdentifierInfo *inheritedName) {
Mike Stump1eb44332009-09-09 15:08:12 +0000373 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000374 Property->getPropertyAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000375 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000376 SuperProperty->getPropertyAttributes();
377 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
378 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000379 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000380 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000381 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
382 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000383 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000384 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000385 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
386 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000387 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000388 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000390 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
391 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000392 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000393 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000394 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000395 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump1eb44332009-09-09 15:08:12 +0000396 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000397 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000398 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000399 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000400
Mike Stump1eb44332009-09-09 15:08:12 +0000401 QualType LHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000402 Context.getCanonicalType(SuperProperty->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000403 QualType RHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000404 Context.getCanonicalType(Property->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Steve Naroff15edf0d2009-03-03 15:43:24 +0000406 if (!Context.typesAreCompatible(LHSType, RHSType)) {
407 // FIXME: Incorporate this test with typesAreCompatible.
408 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff4084c302009-07-23 01:01:38 +0000409 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff15edf0d2009-03-03 15:43:24 +0000410 return;
411 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
412 << Property->getType() << SuperProperty->getType() << inheritedName;
413 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000414}
415
416/// ComparePropertiesInBaseAndSuper - This routine compares property
417/// declarations in base and its super class, if any, and issues
418/// diagnostics in a variety of inconsistant situations.
419///
Chris Lattner70f19542009-02-16 21:26:43 +0000420void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000421 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
422 if (!SDecl)
423 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000424 // FIXME: O(N^2)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000425 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
426 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000427 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000428 // Does property in super class has declaration in current class?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000429 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
430 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000431 ObjCPropertyDecl *PDecl = (*I);
432 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump1eb44332009-09-09 15:08:12 +0000433 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000434 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000435 }
436 }
437}
438
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000439/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
440/// of properties declared in a protocol and compares their attribute against
441/// the same property declared in the class or category.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000442void
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000443Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000444 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000445 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
446 if (!IDecl) {
447 // Category
448 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000449 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
Fariborz Jahanian25760612010-02-15 21:55:26 +0000450 if (!CatDecl->IsClassExtension())
451 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
452 E = PDecl->prop_end(); P != E; ++P) {
453 ObjCPropertyDecl *Pr = (*P);
454 ObjCCategoryDecl::prop_iterator CP, CE;
455 // Is this property already in category's list of properties?
456 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
457 if ((*CP)->getIdentifier() == Pr->getIdentifier())
458 break;
459 if (CP != CE)
460 // Property protocol already exist in class. Diagnose any mismatch.
461 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
462 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000463 return;
464 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000465 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
466 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000467 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000468 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000469 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000470 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000471 if ((*CP)->getIdentifier() == Pr->getIdentifier())
472 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000473 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000474 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000475 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000476 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000477}
478
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000479/// CompareProperties - This routine compares properties
480/// declared in 'ClassOrProtocol' objects (which can be a class or an
481/// inherited protocol with the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000482///
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000483void Sema::CompareProperties(Decl *CDecl,
484 DeclPtrTy ClassOrProtocol) {
485 Decl *ClassDecl = ClassOrProtocol.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000486 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
487
488 if (!IDecl) {
489 // Category
490 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000491 assert (CatDecl && "CompareProperties");
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000492 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
493 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
494 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000495 // Match properties of category with those of protocol (*P)
496 MatchOneProtocolPropertiesInClass(CatDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000498 // Go thru the list of protocols for this category and recursively match
499 // their properties with those in the category.
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000500 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
501 E = CatDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000502 CompareProperties(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000503 } else {
504 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
505 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
506 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000507 MatchOneProtocolPropertiesInClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000508 }
509 return;
510 }
511
Chris Lattnerb752f282008-07-21 07:06:49 +0000512 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000513 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
514 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000515 // Match properties of class IDecl with those of protocol (*P).
516 MatchOneProtocolPropertiesInClass(IDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000518 // Go thru the list of protocols for this class and recursively match
519 // their properties with those declared in the class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000520 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
521 E = IDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000522 CompareProperties(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000523 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000524 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
525 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
526 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000527 MatchOneProtocolPropertiesInClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000528 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000529}
530
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000531/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000532/// a class method in its extension.
533///
Mike Stump1eb44332009-09-09 15:08:12 +0000534void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000535 ObjCInterfaceDecl *ID) {
536 if (!ID)
537 return; // Possibly due to previous error
538
539 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000540 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
541 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000542 ObjCMethodDecl *MD = *i;
543 MethodMap[MD->getSelector()] = MD;
544 }
545
546 if (MethodMap.empty())
547 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000548 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
549 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000550 ObjCMethodDecl *Method = *i;
551 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
552 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
553 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
554 << Method->getDeclName();
555 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
556 }
557 }
558}
559
Chris Lattner58fe03b2009-04-12 08:43:13 +0000560/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000561Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000562Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000563 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000564 unsigned NumElts,
565 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000566 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000567 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Chris Lattner4d391482007-12-12 07:09:47 +0000569 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000570 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000571 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000572 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000573 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000574 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000575 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000576 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000577 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000578 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000579 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000580 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000581 }
Mike Stump1eb44332009-09-09 15:08:12 +0000582
583 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000584 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000585 Protocols.data(), Protocols.size(),
586 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000587 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000588 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000589 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000590}
591
Chris Lattnerb28317a2009-03-28 19:18:32 +0000592Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000593ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
594 IdentifierInfo *ClassName, SourceLocation ClassLoc,
595 IdentifierInfo *CategoryName,
596 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000597 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000598 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000599 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000600 SourceLocation EndProtoLoc) {
Fariborz Jahanian25760612010-02-15 21:55:26 +0000601 ObjCCategoryDecl *CDecl = 0;
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000602 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Ted Kremenek09b68972010-02-23 19:39:46 +0000603
604 /// Check that class of this category is already completely declared.
605 if (!IDecl || IDecl->isForwardDecl()) {
606 // Create an invalid ObjCCategoryDecl to serve as context for
607 // the enclosing method declarations. We mark the decl invalid
608 // to make it clear that this isn't a valid AST.
609 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
610 ClassLoc, CategoryLoc, CategoryName);
611 CDecl->setInvalidDecl();
612 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
613 return DeclPtrTy::make(CDecl);
614 }
615
616 if (!CategoryName) {
Fariborz Jahanian25760612010-02-15 21:55:26 +0000617 // Class extensions require a special treatment. Use an existing one.
Ted Kremenek09b68972010-02-23 19:39:46 +0000618 // Note that 'getClassExtension()' can return NULL.
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000619 CDecl = IDecl->getClassExtension();
Ted Kremenek09b68972010-02-23 19:39:46 +0000620 }
621
Fariborz Jahanian25760612010-02-15 21:55:26 +0000622 if (!CDecl) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000623 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
624 ClassLoc, CategoryLoc, CategoryName);
Fariborz Jahanian25760612010-02-15 21:55:26 +0000625 // FIXME: PushOnScopeChains?
626 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000627
Fariborz Jahanian25760612010-02-15 21:55:26 +0000628 CDecl->setClassInterface(IDecl);
629 // Insert first use of class extension to the list of class's categories.
630 if (!CategoryName)
631 CDecl->insertNextClassCategory();
632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Chris Lattner16b34b42009-02-16 21:30:01 +0000634 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000635 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000636
Fariborz Jahanian25760612010-02-15 21:55:26 +0000637 if (CategoryName) {
638 /// Check for duplicate interface declaration for this category
639 ObjCCategoryDecl *CDeclChain;
640 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
641 CDeclChain = CDeclChain->getNextClassCategory()) {
642 if (CDeclChain->getIdentifier() == CategoryName) {
643 // Class extensions can be declared multiple times.
644 Diag(CategoryLoc, diag::warn_dup_category_def)
645 << ClassName << CategoryName;
646 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
647 break;
648 }
Chris Lattner70f19542009-02-16 21:26:43 +0000649 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000650 if (!CDeclChain)
651 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000652 }
Chris Lattner70f19542009-02-16 21:26:43 +0000653
Chris Lattner4d391482007-12-12 07:09:47 +0000654 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000655 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000656 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000657 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000658 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000659 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000660 NumProtoRefs, ProtoLocs,
661 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000662 }
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Anders Carlsson15281452008-11-04 16:57:32 +0000664 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000665 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000666}
667
668/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000669/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000670/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000671Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000672 SourceLocation AtCatImplLoc,
673 IdentifierInfo *ClassName, SourceLocation ClassLoc,
674 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000675 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000676 ObjCCategoryDecl *CatIDecl = 0;
677 if (IDecl) {
678 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
679 if (!CatIDecl) {
680 // Category @implementation with no corresponding @interface.
681 // Create and install one.
682 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000683 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000684 CatName);
685 CatIDecl->setClassInterface(IDecl);
686 CatIDecl->insertNextClassCategory();
687 }
688 }
689
Mike Stump1eb44332009-09-09 15:08:12 +0000690 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000691 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
692 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000693 /// Check that class of this category is already completely declared.
694 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000695 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000696
Douglas Gregord0434102009-01-09 00:49:46 +0000697 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000698 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000699
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000700 /// Check that CatName, category name, is not used in another implementation.
701 if (CatIDecl) {
702 if (CatIDecl->getImplementation()) {
703 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
704 << CatName;
705 Diag(CatIDecl->getImplementation()->getLocation(),
706 diag::note_previous_definition);
707 } else
708 CatIDecl->setImplementation(CDecl);
709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Anders Carlsson15281452008-11-04 16:57:32 +0000711 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000712 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000713}
714
Chris Lattnerb28317a2009-03-28 19:18:32 +0000715Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000716 SourceLocation AtClassImplLoc,
717 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000718 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000719 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000720 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000721 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000722 NamedDecl *PrevDecl
723 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000724 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000725 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000726 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000727 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
728 // If this is a forward declaration of an interface, warn.
729 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000730 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000731 IDecl = 0;
732 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000733 } else {
734 // We did not find anything with the name ClassName; try to correct for
735 // typos in the class name.
736 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
737 if (CorrectTypo(R, TUScope, 0) &&
738 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000739 // Suggest the (potentially) correct interface name. However, put the
740 // fix-it hint itself in a separate note, since changing the name in
741 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000742 // provide a code-modification hint or use the typo name for recovery,
743 // because this is just a warning. The program may actually be correct.
744 Diag(ClassLoc, diag::warn_undef_interface_suggest)
745 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000746 Diag(IDecl->getLocation(), diag::note_previous_decl)
747 << R.getLookupName()
748 << CodeModificationHint::CreateReplacement(ClassLoc,
749 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000750 IDecl = 0;
751 } else {
752 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
753 }
Chris Lattner4d391482007-12-12 07:09:47 +0000754 }
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Chris Lattner4d391482007-12-12 07:09:47 +0000756 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000757 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000758 if (SuperClassname) {
759 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000760 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000761 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000762 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
763 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000764 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000765 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000766 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000767 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000768 Diag(SuperClassLoc, diag::err_undef_superclass)
769 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000770 else if (IDecl && IDecl->getSuperClass() != SDecl) {
771 // This implementation and its interface do not have the same
772 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000773 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000774 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000775 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000776 }
777 }
778 }
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Chris Lattner4d391482007-12-12 07:09:47 +0000780 if (!IDecl) {
781 // Legacy case of @implementation with no corresponding @interface.
782 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000783
Mike Stump390b4cc2009-05-16 07:39:55 +0000784 // FIXME: Do we support attributes on the @implementation? If so we should
785 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000786 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000787 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000788 IDecl->setSuperClass(SDecl);
789 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000790
791 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000792 } else {
793 // Mark the interface as being completed, even if it was just as
794 // @class ....;
795 // declaration; the user cannot reopen it.
796 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000797 }
Mike Stump1eb44332009-09-09 15:08:12 +0000798
799 ObjCImplementationDecl* IMPDecl =
800 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000801 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Anders Carlsson15281452008-11-04 16:57:32 +0000803 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000804 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Chris Lattner4d391482007-12-12 07:09:47 +0000806 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000807 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000808 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000809 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000810 Diag(IDecl->getImplementation()->getLocation(),
811 diag::note_previous_definition);
812 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000813 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000814 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000815 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000816 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000817}
818
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000819void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
820 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000821 SourceLocation RBrace) {
822 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000823 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000824 if (!IDecl)
825 return;
826 /// Check case of non-existing @interface decl.
827 /// (legacy objective-c @implementation decl without an @interface decl).
828 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000829 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000830 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000831 // Add ivar's to class's DeclContext.
832 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000833 ivars[i]->setLexicalDeclContext(ImpDecl);
834 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000835 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000836 }
837
Chris Lattner4d391482007-12-12 07:09:47 +0000838 return;
839 }
840 // If implementation has empty ivar list, just return.
841 if (numIvars == 0)
842 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Chris Lattner4d391482007-12-12 07:09:47 +0000844 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000845 if (LangOpts.ObjCNonFragileABI2) {
846 if (ImpDecl->getSuperClass())
847 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
848 for (unsigned i = 0; i < numIvars; i++) {
849 ObjCIvarDecl* ImplIvar = ivars[i];
850 if (const ObjCIvarDecl *ClsIvar =
851 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
852 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
853 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
854 continue;
855 }
856 if (ImplIvar->getAccessControl() != ObjCIvarDecl::Private)
857 Diag(ImplIvar->getLocation(), diag::err_non_private_ivar_declaration);
858 // Instance ivar to Implementation's DeclContext.
859 ImplIvar->setLexicalDeclContext(ImpDecl);
860 IDecl->makeDeclVisibleInContext(ImplIvar, false);
861 ImpDecl->addDecl(ImplIvar);
862 }
863 return;
864 }
Chris Lattner4d391482007-12-12 07:09:47 +0000865 // Check interface's Ivar list against those in the implementation.
866 // names and types must match.
867 //
Chris Lattner4d391482007-12-12 07:09:47 +0000868 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000869 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000870 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
871 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000872 ObjCIvarDecl* ImplIvar = ivars[j++];
873 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000874 assert (ImplIvar && "missing implementation ivar");
875 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Steve Naroffca331292009-03-03 14:49:36 +0000877 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000878 if (Context.getCanonicalType(ImplIvar->getType()) !=
879 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000880 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000881 << ImplIvar->getIdentifier()
882 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000883 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000884 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
885 Expr *ImplBitWidth = ImplIvar->getBitWidth();
886 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000887 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
888 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000889 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
890 << ImplIvar->getIdentifier();
891 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893 }
Steve Naroffca331292009-03-03 14:49:36 +0000894 // Make sure the names are identical.
895 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000896 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000897 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000898 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000899 }
900 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000901 }
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Chris Lattner609e4c72007-12-12 18:11:49 +0000903 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000904 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000905 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000906 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000907}
908
Steve Naroff3c2eb662008-02-10 21:38:56 +0000909void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
910 bool &IncompleteImpl) {
911 if (!IncompleteImpl) {
912 Diag(ImpLoc, diag::warn_incomplete_impl);
913 IncompleteImpl = true;
914 }
Chris Lattner08631c52008-11-23 21:45:46 +0000915 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000916}
917
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000918void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
919 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000920 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000921 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000922 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
923 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000924 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000925 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
926 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000927 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
928 }
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Chris Lattner3aff9192009-04-11 19:58:42 +0000930 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
931 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
932 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000933 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
934 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
935 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
936 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000937 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000938
939 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000940 << ImpMethodDecl->getDeclName() << (*IF)->getType()
941 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000942 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000943 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000944}
945
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000946/// isPropertyReadonly - Return true if property is readonly, by searching
947/// for the property in the class and in its categories and implementations
948///
949bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000950 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000951 // by far the most common case.
952 if (!PDecl->isReadOnly())
953 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000954 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000955 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000956 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000957 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000959 // Main class has the property as 'readonly'. Must search
Mike Stump1eb44332009-09-09 15:08:12 +0000960 // through the category list to see if the property's
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000961 // attribute has been over-ridden to 'readwrite'.
962 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
963 Category; Category = Category->getNextClassCategory()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000964 // Even if property is ready only, if a category has a user defined setter,
965 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000966 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000967 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000968 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000969 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000970 if (P && !P->isReadOnly())
971 return false;
972 }
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000974 // Also, check for definition of a setter method in the implementation if
975 // all else failed.
976 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000977 if (ObjCImplementationDecl *IMD =
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000978 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000979 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000980 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000981 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000982 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000983 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000984 return false;
985 }
986 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000987 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000988 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000989 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000990 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000991 // If all fails, look at the super class.
992 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
993 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000994 return true;
995}
996
Mike Stump390b4cc2009-05-16 07:39:55 +0000997/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
998/// improve the efficiency of selector lookups and type checking by associating
999/// with each protocol / interface / category the flattened instance tables. If
1000/// we used an immutable set to keep the table then it wouldn't add significant
1001/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001002
Steve Naroffefe7f362008-02-08 22:06:17 +00001003/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001004/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001005void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1006 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001007 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001008 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001009 const llvm::DenseSet<Selector> &ClsMap,
1010 ObjCInterfaceDecl *IDecl) {
1011 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001012 ObjCInterfaceDecl *NSIDecl = 0;
1013 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001014 // check to see if class implements forwardInvocation method and objects
1015 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001016 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001017 // Under such conditions, which means that every method possible is
1018 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001019 // found" warnings.
1020 // FIXME: Use a general GetUnarySelector method for this.
1021 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1022 Selector fISelector = Context.Selectors.getSelector(1, &II);
1023 if (InsMap.count(fISelector))
1024 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1025 // need be implemented in the implementation.
1026 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1027 }
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001029 // If a method lookup fails locally we still need to look and see if
1030 // the method was implemented by a base class or an inherited
1031 // protocol. This lookup is slow, but occurs rarely in correct code
1032 // and otherwise would terminate in a warning.
1033
Chris Lattner4d391482007-12-12 07:09:47 +00001034 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001035 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001036 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001037 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001038 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001039 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001040 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001041 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001042 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001043 // Ugly, but necessary. Method declared in protcol might have
1044 // have been synthesized due to a property declared in the class which
1045 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001046 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001047 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001048 if (!MethodInClass || !MethodInClass->isSynthesized())
1049 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
1050 }
1051 }
Chris Lattner4d391482007-12-12 07:09:47 +00001052 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001053 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001054 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001055 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001056 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001057 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1058 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001059 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +00001060 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001061 }
Chris Lattner780f3292008-07-21 21:32:27 +00001062 // Check on this protocols's referenced protocols, recursively.
1063 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1064 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001065 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001066}
1067
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001068/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1069/// or protocol against those declared in their implementations.
1070///
1071void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1072 const llvm::DenseSet<Selector> &ClsMap,
1073 llvm::DenseSet<Selector> &InsMapSeen,
1074 llvm::DenseSet<Selector> &ClsMapSeen,
1075 ObjCImplDecl* IMPDecl,
1076 ObjCContainerDecl* CDecl,
1077 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001078 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001079 // Check and see if instance methods in class interface have been
1080 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001081 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1082 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001083 if (InsMapSeen.count((*I)->getSelector()))
1084 continue;
1085 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001086 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001087 !InsMap.count((*I)->getSelector())) {
1088 if (ImmediateClass)
1089 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1090 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001091 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001092 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001093 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001094 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001095 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001096 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001097 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1098 // ImpMethodDecl may be null as in a @dynamic property.
1099 if (ImpMethodDecl)
1100 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1101 }
1102 }
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001104 // Check and see if class methods in class interface have been
1105 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001106 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001107 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001108 if (ClsMapSeen.count((*I)->getSelector()))
1109 continue;
1110 ClsMapSeen.insert((*I)->getSelector());
1111 if (!ClsMap.count((*I)->getSelector())) {
1112 if (ImmediateClass)
1113 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001114 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001115 ObjCMethodDecl *ImpMethodDecl =
1116 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001117 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001118 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001119 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1120 }
1121 }
1122 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1123 // Check for any implementation of a methods declared in protocol.
1124 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1125 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001126 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1127 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001128 (*PI), IncompleteImpl, false);
1129 if (I->getSuperClass())
1130 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001131 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001132 I->getSuperClass(), IncompleteImpl, false);
1133 }
1134}
1135
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001136/// CollectImmediateProperties - This routine collects all properties in
1137/// the class and its conforming protocols; but not those it its super class.
1138void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
1139 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1140 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1141 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1142 E = IDecl->prop_end(); P != E; ++P) {
1143 ObjCPropertyDecl *Prop = (*P);
1144 PropMap[Prop->getIdentifier()] = Prop;
1145 }
1146 // scan through class's protocols.
1147 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
1148 E = IDecl->protocol_end(); PI != E; ++PI)
1149 CollectImmediateProperties((*PI), PropMap);
1150 }
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001151 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001152 if (!CATDecl->IsClassExtension())
1153 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1154 E = CATDecl->prop_end(); P != E; ++P) {
1155 ObjCPropertyDecl *Prop = (*P);
1156 PropMap[Prop->getIdentifier()] = Prop;
1157 }
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001158 // scan through class's protocols.
1159 for (ObjCInterfaceDecl::protocol_iterator PI = CATDecl->protocol_begin(),
1160 E = CATDecl->protocol_end(); PI != E; ++PI)
1161 CollectImmediateProperties((*PI), PropMap);
1162 }
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001163 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1164 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1165 E = PDecl->prop_end(); P != E; ++P) {
1166 ObjCPropertyDecl *Prop = (*P);
1167 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1168 if (!PropEntry)
1169 PropEntry = Prop;
1170 }
1171 // scan through protocol's protocols.
1172 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1173 E = PDecl->protocol_end(); PI != E; ++PI)
1174 CollectImmediateProperties((*PI), PropMap);
1175 }
1176}
1177
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001178/// LookupPropertyDecl - Looks up a property in the current class and all
1179/// its protocols.
1180ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1181 IdentifierInfo *II) {
1182 if (const ObjCInterfaceDecl *IDecl =
1183 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1184 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1185 E = IDecl->prop_end(); P != E; ++P) {
1186 ObjCPropertyDecl *Prop = (*P);
1187 if (Prop->getIdentifier() == II)
1188 return Prop;
1189 }
1190 // scan through class's protocols.
1191 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
1192 E = IDecl->protocol_end(); PI != E; ++PI) {
1193 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1194 if (Prop)
1195 return Prop;
1196 }
1197 }
1198 else if (const ObjCProtocolDecl *PDecl =
1199 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1200 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1201 E = PDecl->prop_end(); P != E; ++P) {
1202 ObjCPropertyDecl *Prop = (*P);
1203 if (Prop->getIdentifier() == II)
1204 return Prop;
1205 }
1206 // scan through protocol's protocols.
1207 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1208 E = PDecl->protocol_end(); PI != E; ++PI) {
1209 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1210 if (Prop)
1211 return Prop;
1212 }
1213 }
1214 return 0;
1215}
1216
1217
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001218void Sema::DiagnoseUnimplementedProperties(ObjCImplDecl* IMPDecl,
1219 ObjCContainerDecl *CDecl,
1220 const llvm::DenseSet<Selector>& InsMap) {
1221 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1222 CollectImmediateProperties(CDecl, PropMap);
Fariborz Jahaniana84f2e42010-01-20 17:27:59 +00001223 if (PropMap.empty())
1224 return;
1225
1226 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1227 for (ObjCImplDecl::propimpl_iterator
1228 I = IMPDecl->propimpl_begin(),
1229 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1230 PropImplMap.insert((*I)->getPropertyDecl());
1231
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001232 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1233 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1234 ObjCPropertyDecl *Prop = P->second;
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001235 // Is there a matching propery synthesize/dynamic?
Fariborz Jahaniana84f2e42010-01-20 17:27:59 +00001236 if (Prop->isInvalidDecl() ||
1237 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1238 PropImplMap.count(Prop))
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001239 continue;
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001240 if (LangOpts.ObjCNonFragileABI2) {
1241 ActOnPropertyImplDecl(IMPDecl->getLocation(),
1242 SourceLocation(),
1243 true, DeclPtrTy::make(IMPDecl),
1244 Prop->getIdentifier(),
1245 Prop->getIdentifier());
1246 continue;
1247 }
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001248 if (!InsMap.count(Prop->getGetterName())) {
1249 Diag(Prop->getLocation(),
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001250 isa<ObjCCategoryDecl>(CDecl) ?
1251 diag::warn_setter_getter_impl_required_in_category :
1252 diag::warn_setter_getter_impl_required)
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001253 << Prop->getDeclName() << Prop->getGetterName();
1254 Diag(IMPDecl->getLocation(),
1255 diag::note_property_impl_required);
1256 }
1257
1258 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1259 Diag(Prop->getLocation(),
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001260 isa<ObjCCategoryDecl>(CDecl) ?
1261 diag::warn_setter_getter_impl_required_in_category :
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001262 diag::warn_setter_getter_impl_required)
1263 << Prop->getDeclName() << Prop->getSetterName();
1264 Diag(IMPDecl->getLocation(),
1265 diag::note_property_impl_required);
1266 }
1267 }
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001268}
1269
Mike Stump1eb44332009-09-09 15:08:12 +00001270void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1271 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001272 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001273 llvm::DenseSet<Selector> InsMap;
1274 // Check and see if instance methods in class interface have been
1275 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001276 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001277 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001278 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001280 // Check and see if properties declared in the interface have either 1)
1281 // an implementation or 2) there is a @synthesize/@dynamic implementation
1282 // of the property in the @implementation.
1283 if (isa<ObjCInterfaceDecl>(CDecl))
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001284 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
1285
Chris Lattner4d391482007-12-12 07:09:47 +00001286 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001287 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001288 I = IMPDecl->classmeth_begin(),
1289 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001290 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001292 // Check for type conflict of methods declared in a class/protocol and
1293 // its implementation; if any.
1294 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001295 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1296 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001297 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Chris Lattner4d391482007-12-12 07:09:47 +00001299 // Check the protocol list for unimplemented methods in the @implementation
1300 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001301 // Check and see if class methods in class interface have been
1302 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Chris Lattnercddc8882009-03-01 00:56:52 +00001304 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001305 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001306 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001307 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001308 InsMap, ClsMap, I);
1309 // Check class extensions (unnamed categories)
1310 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1311 Categories; Categories = Categories->getNextClassCategory()) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001312 if (Categories->IsClassExtension()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001313 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1314 break;
1315 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001316 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001317 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001318 // For extended class, unimplemented methods in its protocols will
1319 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001320 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001321 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1322 E = C->protocol_end(); PI != E; ++PI)
1323 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1324 InsMap, ClsMap, C->getClassInterface());
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001325 // Report unimplemented properties in the category as well.
1326 // When reporting on missing setter/getters, do not report when
1327 // setter/getter is implemented in category's primary class
1328 // implementation.
1329 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1330 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1331 for (ObjCImplementationDecl::instmeth_iterator
1332 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1333 InsMap.insert((*I)->getSelector());
1334 }
1335 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
1336 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001337 } else
1338 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001339}
1340
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001341void
1342Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1343 ObjCContainerDecl* IDecl) {
1344 // Rules apply in non-GC mode only
1345 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1346 return;
1347 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1348 E = IDecl->prop_end();
1349 I != E; ++I) {
1350 ObjCPropertyDecl *Property = (*I);
1351 unsigned Attributes = Property->getPropertyAttributes();
1352 // We only care about readwrite atomic property.
1353 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1354 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1355 continue;
1356 if (const ObjCPropertyImplDecl *PIDecl
1357 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1358 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1359 continue;
1360 ObjCMethodDecl *GetterMethod =
1361 IMPDecl->getInstanceMethod(Property->getGetterName());
1362 ObjCMethodDecl *SetterMethod =
1363 IMPDecl->getInstanceMethod(Property->getSetterName());
1364 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1365 SourceLocation MethodLoc =
1366 (GetterMethod ? GetterMethod->getLocation()
1367 : SetterMethod->getLocation());
1368 Diag(MethodLoc, diag::warn_atomic_property_rule)
1369 << Property->getIdentifier();
1370 Diag(Property->getLocation(), diag::note_property_declare);
1371 }
1372 }
1373 }
1374}
1375
Mike Stump1eb44332009-09-09 15:08:12 +00001376/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001377Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001378Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001379 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001380 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001381 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001382 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Chris Lattner4d391482007-12-12 07:09:47 +00001384 for (unsigned i = 0; i != NumElts; ++i) {
1385 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001386 NamedDecl *PrevDecl
1387 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001388 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001389 // Maybe we will complain about the shadowed template parameter.
1390 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1391 // Just pretend that we didn't see the previous declaration.
1392 PrevDecl = 0;
1393 }
1394
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001395 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001396 // GCC apparently allows the following idiom:
1397 //
1398 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1399 // @class XCElementToggler;
1400 //
Mike Stump1eb44332009-09-09 15:08:12 +00001401 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001402 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1403 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001404 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001405 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001406 } else if (TDD) {
1407 // a forward class declaration matching a typedef name of a class refers
1408 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001409 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001410 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1411 PrevDecl = OI->getDecl();
1412 }
Chris Lattner4d391482007-12-12 07:09:47 +00001413 }
Mike Stump1eb44332009-09-09 15:08:12 +00001414 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001415 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001416 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001417 IdentList[i], IdentLocs[i], true);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001418
1419 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1420 // the current DeclContext. This prevents clients that walk DeclContext
1421 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1422 // declared later (if at all). We also take care to explicitly make
1423 // sure this declaration is visible for name lookup.
1424 PushOnScopeChains(IDecl, TUScope, false);
1425 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001426 }
1427
1428 Interfaces.push_back(IDecl);
1429 }
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Ted Kremenek321c22f2009-11-18 00:28:11 +00001431 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001432 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001433 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001434 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001435 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001436 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001437 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001438}
1439
1440
1441/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1442/// returns true, or false, accordingly.
1443/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001444bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001445 const ObjCMethodDecl *PrevMethod,
1446 bool matchBasedOnSizeAndAlignment) {
1447 QualType T1 = Context.getCanonicalType(Method->getResultType());
1448 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001450 if (T1 != T2) {
1451 // The result types are different.
1452 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001453 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001454 // Incomplete types don't have a size and alignment.
1455 if (T1->isIncompleteType() || T2->isIncompleteType())
1456 return false;
1457 // Check is based on size and alignment.
1458 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1459 return false;
1460 }
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Chris Lattner89951a82009-02-20 18:43:26 +00001462 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1463 E = Method->param_end();
1464 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Chris Lattner89951a82009-02-20 18:43:26 +00001466 for (; ParamI != E; ++ParamI, ++PrevI) {
1467 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1468 T1 = Context.getCanonicalType((*ParamI)->getType());
1469 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001470 if (T1 != T2) {
1471 // The result types are different.
1472 if (!matchBasedOnSizeAndAlignment)
1473 return false;
1474 // Incomplete types don't have a size and alignment.
1475 if (T1->isIncompleteType() || T2->isIncompleteType())
1476 return false;
1477 // Check is based on size and alignment.
1478 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1479 return false;
1480 }
Chris Lattner4d391482007-12-12 07:09:47 +00001481 }
1482 return true;
1483}
1484
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001485/// \brief Read the contents of the instance and factory method pools
1486/// for a given selector from external storage.
1487///
1488/// This routine should only be called once, when neither the instance
1489/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001490Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001491 bool isInstance) {
1492 assert(ExternalSource && "We need an external AST source");
1493 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1494 "Selector data already loaded into the instance method pool");
1495 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1496 "Selector data already loaded into the factory method pool");
1497
1498 // Read the method list from the external source.
1499 std::pair<ObjCMethodList, ObjCMethodList> Methods
1500 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001502 if (isInstance) {
1503 if (Methods.second.Method)
1504 FactoryMethodPool[Sel] = Methods.second;
1505 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001506 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001507
1508 if (Methods.first.Method)
1509 InstanceMethodPool[Sel] = Methods.first;
1510
1511 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1512}
1513
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001514void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001515 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1516 = InstanceMethodPool.find(Method->getSelector());
1517 if (Pos == InstanceMethodPool.end()) {
1518 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1519 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1520 else
1521 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1522 ObjCMethodList())).first;
1523 }
1524
1525 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001526 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001527 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001528 Entry.Method = Method;
1529 Entry.Next = 0;
1530 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001531 }
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Chris Lattnerb25df352009-03-04 05:16:45 +00001533 // We've seen a method with this name, see if we have already seen this type
1534 // signature.
1535 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1536 if (MatchTwoMethodDeclarations(Method, List->Method))
1537 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Chris Lattnerb25df352009-03-04 05:16:45 +00001539 // We have a new signature for an existing method - add it.
1540 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001541 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1542 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001543}
1544
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001545// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001546ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001547 SourceRange R,
1548 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001549 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1550 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001551 if (Pos == InstanceMethodPool.end()) {
1552 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001553 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1554 else
1555 return 0;
1556 }
1557
1558 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001559 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Steve Naroff037cda52008-09-30 14:38:43 +00001561 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001562 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1563 // This checks if the methods differ by size & alignment.
1564 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001565 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001566 }
1567 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001568 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001569 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001570 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001571 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001572 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001573 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001574 }
1575 return MethList.Method;
1576}
1577
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001578void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001579 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1580 = FactoryMethodPool.find(Method->getSelector());
1581 if (Pos == FactoryMethodPool.end()) {
1582 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1583 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1584 else
1585 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1586 ObjCMethodList())).first;
1587 }
1588
1589 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001590 if (!FirstMethod.Method) {
1591 // Haven't seen a method with this selector name yet - add it.
1592 FirstMethod.Method = Method;
1593 FirstMethod.Next = 0;
1594 } else {
1595 // We've seen a method with this name, now check the type signature(s).
1596 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001597
1598 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001599 Next = Next->Next)
1600 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Chris Lattner4d391482007-12-12 07:09:47 +00001602 if (!match) {
1603 // We have a new signature for an existing method - add it.
1604 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001605 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1606 ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001607 FirstMethod.Next = OMI;
1608 }
1609 }
1610}
1611
Mike Stump1eb44332009-09-09 15:08:12 +00001612ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001613 SourceRange R) {
1614 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1615 = FactoryMethodPool.find(Sel);
1616 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001617 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001618 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1619 else
1620 return 0;
1621 }
1622
1623 ObjCMethodList &MethList = Pos->second;
1624 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001626 if (MethList.Method && MethList.Next) {
1627 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1628 // This checks if the methods differ by size & alignment.
1629 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1630 issueWarning = true;
1631 }
1632 if (issueWarning && (MethList.Method && MethList.Next)) {
1633 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001634 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001635 << MethList.Method->getSourceRange();
1636 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001637 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001638 << Next->Method->getSourceRange();
1639 }
1640 return MethList.Method;
1641}
1642
Mike Stump1eb44332009-09-09 15:08:12 +00001643/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff0701bbb2009-01-08 17:28:14 +00001644/// have the property type and issue diagnostics if they don't.
1645/// Also synthesize a getter/setter method if none exist (and update the
1646/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1647/// methods is the "right" thing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001648void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001649 ObjCContainerDecl *CD) {
1650 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00001651
1652 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001653 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump1eb44332009-09-09 15:08:12 +00001654 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001655 property->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001657 if (SetterMethod) {
Fariborz Jahanian02deae82010-01-06 00:18:12 +00001658 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1659 property->getPropertyAttributes();
1660 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1661 Context.getCanonicalType(SetterMethod->getResultType()) !=
1662 Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001663 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001664 if (SetterMethod->param_size() != 1 ||
1665 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001666 Diag(property->getLocation(),
1667 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001668 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001669 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001670 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1671 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001672 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001673
1674 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001675 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001676 // FIXME: The synthesized property we set here is misleading. We almost always
1677 // synthesize these methods unless the user explicitly provided prototypes
1678 // (which is odd, but allowed). Sema should be typechecking that the
1679 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001680 if (!GetterMethod) {
1681 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001682 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001683 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001684 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1685 property->getLocation(), property->getGetterName(),
1686 property->getType(), CD, true, false, true,
1687 (property->getPropertyImplementation() ==
1688 ObjCPropertyDecl::Optional) ?
1689 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001690 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001691 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001692 } else
1693 // A user declared getter will be synthesize when @synthesize of
1694 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001695 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001696 property->setGetterMethodDecl(GetterMethod);
1697
1698 // Skip setter if property is read-only.
1699 if (!property->isReadOnly()) {
1700 // Find the default setter and if one not found, add one.
1701 if (!SetterMethod) {
1702 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001703 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001704 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001705 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1706 property->getLocation(),
1707 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001708 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001709 (property->getPropertyImplementation() ==
1710 ObjCPropertyDecl::Optional) ?
1711 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001712 ObjCMethodDecl::Required);
1713 // Invent the arguments for the setter. We don't bother making a
1714 // nice name for the argument.
1715 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001716 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001717 property->getIdentifier(),
1718 property->getType(),
John McCalla93c9342009-12-07 02:54:59 +00001719 /*TInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001720 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001721 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001722 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001723 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001724 } else
1725 // A user declared setter will be synthesize when @synthesize of
1726 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001727 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001728 property->setSetterMethodDecl(SetterMethod);
1729 }
Mike Stump1eb44332009-09-09 15:08:12 +00001730 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001731 // handle the following, which is supported by GCC (and part of the design).
1732 //
1733 // @interface Foo
1734 // @property double bar;
1735 // @end
1736 //
1737 // void thisIsUnfortunate() {
1738 // id foo;
1739 // double bar = [foo bar];
1740 // }
1741 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001742 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001743 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001744 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001745 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001746}
1747
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001748/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1749/// identical selector names in current and its super classes and issues
1750/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001751void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1752 ObjCMethodDecl *Method,
1753 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001754 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1755 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001757 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001758 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001759 SD->lookupMethod(Method->getSelector(), IsInstance);
1760 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001761 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001762 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001763 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001764 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1765 E = Method->param_end();
1766 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1767 for (; ParamI != E; ++ParamI, ++PrevI) {
1768 // Number of parameters are the same and is guaranteed by selector match.
1769 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1770 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1771 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1772 // If type of arguement of method in this class does not match its
1773 // respective argument type in the super class method, issue warning;
1774 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001775 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001776 << T1 << T2;
1777 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1778 return;
1779 }
1780 }
1781 ID = SD;
1782 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001783}
1784
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001785/// DiagnoseDuplicateIvars -
1786/// Check for duplicate ivars in the entire class at the start of
1787/// @implementation. This becomes necesssary because class extension can
1788/// add ivars to a class in random order which will not be known until
1789/// class's @implementation is seen.
1790void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1791 ObjCInterfaceDecl *SID) {
1792 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1793 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1794 ObjCIvarDecl* Ivar = (*IVI);
1795 if (Ivar->isInvalidDecl())
1796 continue;
1797 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1798 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1799 if (prevIvar) {
1800 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1801 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1802 Ivar->setInvalidDecl();
1803 }
1804 }
1805 }
1806}
1807
Steve Naroffa56f6162007-12-18 01:30:32 +00001808// Note: For class/category implemenations, allMethods/allProperties is
1809// always null.
Ted Kremenek782f2f52010-01-07 01:20:12 +00001810void Sema::ActOnAtEnd(SourceRange AtEnd,
1811 DeclPtrTy classDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001812 DeclPtrTy *allMethods, unsigned allNum,
1813 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001814 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001815 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001816
Steve Naroffa56f6162007-12-18 01:30:32 +00001817 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1818 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001819 // should be true.
1820 if (!ClassDecl)
1821 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001822
Mike Stump1eb44332009-09-09 15:08:12 +00001823 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001824 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1825 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001826 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001827
Ted Kremenek782f2f52010-01-07 01:20:12 +00001828 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1829 // FIXME: This is wrong. We shouldn't be pretending that there is
1830 // an '@end' in the declaration.
1831 SourceLocation L = ClassDecl->getLocation();
1832 AtEnd.setBegin(L);
1833 AtEnd.setEnd(L);
1834 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001835 }
1836
Steve Naroff0701bbb2009-01-08 17:28:14 +00001837 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001838
1839 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1840 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1841 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1842
Chris Lattner4d391482007-12-12 07:09:47 +00001843 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001844 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001845 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001846
1847 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001848 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001849 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001850 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001851 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001852 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001853 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001854 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001855 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001856 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001857 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001858 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001859 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001860 InsMap[Method->getSelector()] = Method;
1861 /// The following allows us to typecheck messages to "id".
1862 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001863 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001864 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001865 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001866 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001867 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001868 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001869 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001870 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001871 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001872 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001873 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001874 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001875 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001876 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001877 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001878 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001879 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001880 /// The following allows us to typecheck messages to "Class".
1881 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001882 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001883 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001884 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001885 }
1886 }
1887 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001888 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001889 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001890 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001891 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001892 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001893 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001894 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001895 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001896 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001897
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001898 // Compare protocol properties with those in category
1899 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian25760612010-02-15 21:55:26 +00001900 if (C->IsClassExtension())
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001901 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001902 }
Steve Naroff09c47192009-01-09 15:36:25 +00001903 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001904 if (CDecl->getIdentifier())
1905 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1906 // user-defined setter/getter. It also synthesizes setter/getter methods
1907 // and adds them to the DeclContext and global method pools.
1908 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1909 E = CDecl->prop_end();
1910 I != E; ++I)
1911 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001912 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001913 }
1914 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001915 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001916 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001917 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001918 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001919 if (LangOpts.ObjCNonFragileABI2)
1920 while (IDecl->getSuperClass()) {
1921 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1922 IDecl = IDecl->getSuperClass();
1923 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001924 }
Mike Stump1eb44332009-09-09 15:08:12 +00001925 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001926 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001927 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001928
Chris Lattner4d391482007-12-12 07:09:47 +00001929 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001930 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001931 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001932 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001933 Categories; Categories = Categories->getNextClassCategory()) {
1934 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001935 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001936 break;
1937 }
1938 }
1939 }
1940 }
Chris Lattner682bf922009-03-29 16:50:03 +00001941 if (isInterfaceDeclKind) {
1942 // Reject invalid vardecls.
1943 for (unsigned i = 0; i != tuvNum; i++) {
1944 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1945 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1946 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001947 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001948 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001949 }
Chris Lattner682bf922009-03-29 16:50:03 +00001950 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001951 }
Chris Lattner4d391482007-12-12 07:09:47 +00001952}
1953
1954
1955/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1956/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001957static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001958CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1959 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1960 if (PQTVal & ObjCDeclSpec::DQ_In)
1961 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1962 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1963 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1964 if (PQTVal & ObjCDeclSpec::DQ_Out)
1965 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1966 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1967 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1968 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1969 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1970 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1971 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001972
1973 return ret;
1974}
1975
Chris Lattnerb28317a2009-03-28 19:18:32 +00001976Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001977 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001978 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001979 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001980 Selector Sel,
1981 // optional arguments. The number of types/arguments is obtained
1982 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001983 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001984 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001985 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1986 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001987 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001988
1989 // Make sure we can establish a context for the method.
1990 if (!ClassDecl) {
1991 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanian32844b32009-08-28 17:52:37 +00001992 FunctionLabelMap.clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001993 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001994 }
Chris Lattner4d391482007-12-12 07:09:47 +00001995 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001996
Steve Naroffccef3712009-02-20 22:59:16 +00001997 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001998 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Steve Naroffccef3712009-02-20 22:59:16 +00002000 // Methods cannot return interface types. All ObjC objects are
2001 // passed by reference.
2002 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002003 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2004 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002005 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00002006 }
2007 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002008 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00002009
2010 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002011 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00002012 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002013 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00002014 false,
Mike Stump1eb44332009-09-09 15:08:12 +00002015 MethodDeclKind == tok::objc_optional ?
2016 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002017 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Chris Lattner0ed844b2008-04-04 06:12:32 +00002019 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Chris Lattner7db638d2009-04-11 19:42:43 +00002021 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002022 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002023 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Chris Lattnere294d3f2009-04-11 18:57:04 +00002025 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002026 ArgType = Context.getObjCIdType();
2027 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002028 } else {
John McCall58e46772009-10-23 21:48:59 +00002029 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002030 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002031 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002032 }
Mike Stump1eb44332009-09-09 15:08:12 +00002033
John McCall58e46772009-10-23 21:48:59 +00002034 ParmVarDecl* Param
2035 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
2036 ArgInfo[i].Name, ArgType, DI,
2037 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002038
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002039 if (ArgType->isObjCInterfaceType()) {
2040 Diag(ArgInfo[i].NameLoc,
2041 diag::err_object_cannot_be_passed_returned_by_value)
2042 << 1 << ArgType;
2043 Param->setInvalidDecl();
2044 }
Mike Stump1eb44332009-09-09 15:08:12 +00002045
Chris Lattner0ed844b2008-04-04 06:12:32 +00002046 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002047 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002048
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002049 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002050 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002051
Chris Lattner0ed844b2008-04-04 06:12:32 +00002052 Params.push_back(Param);
2053 }
2054
Jay Foadbeaaccd2009-05-21 09:52:38 +00002055 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002056 ObjCMethod->setObjCDeclQualifier(
2057 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
2058 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00002059
2060 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002061 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002062
John McCall54abf7d2009-11-04 02:18:39 +00002063 const ObjCMethodDecl *InterfaceMD = 0;
2064
Mike Stump1eb44332009-09-09 15:08:12 +00002065 // For implementations (which can be very "coarse grain"), we add the
2066 // method now. This allows the AST to implement lookup methods that work
2067 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00002068 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002069 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002070 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002071 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002072 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2073 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002074 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002075 PrevMethod = ImpDecl->getClassMethod(Sel);
2076 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002077 }
John McCall54abf7d2009-11-04 02:18:39 +00002078 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
2079 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002080 if (AttrList)
2081 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00002082 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002083 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002084 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002085 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
2086 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002087 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002088 PrevMethod = CatImpDecl->getClassMethod(Sel);
2089 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002090 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002091 if (AttrList)
2092 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00002093 }
2094 if (PrevMethod) {
2095 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002096 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002097 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002098 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002099 }
John McCall54abf7d2009-11-04 02:18:39 +00002100
2101 // If the interface declared this method, and it was deprecated there,
2102 // mark it deprecated here.
2103 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
2104 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
2105
Chris Lattnerb28317a2009-03-28 19:18:32 +00002106 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002107}
2108
Mike Stump1eb44332009-09-09 15:08:12 +00002109void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002110 SourceLocation Loc,
2111 unsigned &Attributes) {
2112 // FIXME: Improve the reported location.
2113
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002114 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002115 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002116 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
2117 ObjCDeclSpec::DQ_PR_assign |
2118 ObjCDeclSpec::DQ_PR_copy |
2119 ObjCDeclSpec::DQ_PR_retain))) {
2120 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
2121 "readwrite" :
2122 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
2123 "assign" :
2124 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
2125 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00002126
2127 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00002128 diag::err_objc_property_attr_mutually_exclusive :
2129 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002130 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002131 }
2132
2133 // Check for copy or retain on non-object types.
2134 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00002135 !PropertyTy->isObjCObjectPointerType() &&
2136 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00002137 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002138 Diag(Loc, diag::err_objc_property_requires_object)
2139 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002140 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
2141 }
2142
2143 // Check for more than one of { assign, copy, retain }.
2144 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2145 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002146 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2147 << "assign" << "copy";
2148 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00002149 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002150 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002151 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2152 << "assign" << "retain";
2153 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002154 }
2155 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2156 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002157 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2158 << "copy" << "retain";
2159 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002160 }
2161 }
2162
2163 // Warn if user supplied no assignment attribute, property is
2164 // readwrite, and this is an object type.
2165 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
2166 ObjCDeclSpec::DQ_PR_retain)) &&
2167 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00002168 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002169 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00002170 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002171 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
2172
2173 // If non-gc code warn that this is likely inappropriate.
2174 if (getLangOptions().getGCMode() == LangOptions::NonGC)
2175 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002176
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002177 // FIXME: Implement warning dependent on NSCopying being
2178 // implemented. See also:
2179 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2180 // (please trim this list while you are at it).
2181 }
Mike Stump046efd92009-05-07 23:06:50 +00002182
2183 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2184 && getLangOptions().getGCMode() == LangOptions::GCOnly
2185 && PropertyTy->isBlockPointerType())
2186 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002187}
2188
Mike Stump1eb44332009-09-09 15:08:12 +00002189Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002190 FieldDeclarator &FD,
2191 ObjCDeclSpec &ODS,
2192 Selector GetterSel,
2193 Selector SetterSel,
2194 DeclPtrTy ClassCategory,
2195 bool *isOverridingProperty,
2196 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002197 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002198 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
2199 // default is readwrite!
2200 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00002201 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002202 // not retain or copy
2203 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00002204 (isReadWrite &&
2205 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002206 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
2207 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahaniandd69aae2009-12-16 18:03:30 +00002208 if (T->isReferenceType()) {
2209 Diag(AtLoc, diag::error_reference_property);
2210 return DeclPtrTy();
2211 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002212 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002213 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002214 // May modify Attributes.
2215 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002216 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanian25760612010-02-15 21:55:26 +00002217 if (CDecl->IsClassExtension()) {
2218 // Diagnose if this property is already in continuation class.
2219 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2220 assert(DC && "ClassDecl is not a DeclContext");
2221 DeclContext::lookup_result Found = DC->lookup(FD.D.getIdentifier());
2222 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2223 Diag(AtLoc, diag::err_duplicate_property);
2224 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2225 return DeclPtrTy();
2226 }
2227 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
2228 FD.D.getIdentifierLoc(),
2229 FD.D.getIdentifier(),
2230 AtLoc, T);
2231 DC->addDecl(PDecl);
2232
Mike Stump1eb44332009-09-09 15:08:12 +00002233 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002234 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002235 if ((CCPrimary = CDecl->getClassInterface())) {
2236 // Find the property in continuation class's primary class only.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002237 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002238 if (ObjCPropertyDecl *PIDecl =
2239 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002240 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00002241 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002242 unsigned PIkind = PIDecl->getPropertyAttributes();
2243 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002244 unsigned retainCopyNonatomic =
Fariborz Jahaniand669be52009-11-06 22:59:12 +00002245 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002246 ObjCPropertyDecl::OBJC_PR_copy |
2247 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002248 if ((Attributes & retainCopyNonatomic) !=
2249 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002250 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002251 Diag(PIDecl->getLocation(), diag::note_property_declare);
2252 }
Fariborz Jahanianb73e2812010-01-06 21:38:30 +00002253 DeclContext *DC = dyn_cast<DeclContext>(CCPrimary);
2254 assert(DC && "ClassDecl is not a DeclContext");
2255 DeclContext::lookup_result Found =
2256 DC->lookup(PIDecl->getDeclName());
2257 bool PropertyInPrimaryClass = false;
2258 for (; Found.first != Found.second; ++Found.first)
2259 if (isa<ObjCPropertyDecl>(*Found.first)) {
2260 PropertyInPrimaryClass = true;
2261 break;
2262 }
2263 if (!PropertyInPrimaryClass) {
2264 // Protocol is not in the primary class. Must build one for it.
2265 ObjCDeclSpec ProtocolPropertyODS;
2266 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind and
2267 // ObjCPropertyDecl::PropertyAttributeKind have identical values.
2268 // Should consolidate both into one enum type.
2269 ProtocolPropertyODS.setPropertyAttributes(
2270 (ObjCDeclSpec::ObjCPropertyAttributeKind)PIkind);
2271 DeclPtrTy ProtocolPtrTy =
2272 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
2273 PIDecl->getGetterName(),
2274 PIDecl->getSetterName(),
2275 DeclPtrTy::make(CCPrimary), isOverridingProperty,
2276 MethodImplKind);
2277 PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>();
2278 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002279 PIDecl->makeitReadWriteAttribute();
2280 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2281 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2282 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2283 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2284 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002285 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002286 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002287 << CCPrimary->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002288 Diag(PIDecl->getLocation(), diag::note_property_declare);
2289 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002290 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002291 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00002292 // class's list.
2293 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002294 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002295 }
Fariborz Jahanian25760612010-02-15 21:55:26 +00002296
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002297 // No matching property found in the primary class. Just fall thru
2298 // and add property to continuation class's primary class.
2299 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002300 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00002301 Diag(CDecl->getLocation(), diag::err_continuation_class);
2302 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002303 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002304 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002305 }
Mike Stump1eb44332009-09-09 15:08:12 +00002306
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002307 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00002308 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002309 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2310 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2311 if (T->isObjCObjectPointerType()) {
2312 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00002313 if (const ObjCInterfaceType *OIT =
John McCall183700f2009-09-21 23:43:11 +00002314 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002315 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002316 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002317 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002318 LookupProtocol(&Context.Idents.get("NSCopying")))
2319 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00002320 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002321 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002322 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002323 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00002324 if (T->isObjCInterfaceType())
2325 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002326
Steve Naroff93983f82009-01-11 12:47:58 +00002327 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2328 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002329 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00002330 FD.D.getIdentifierLoc(),
Fariborz Jahaniand0502402010-01-21 17:36:00 +00002331 FD.D.getIdentifier(),
2332 AtLoc, T);
Fariborz Jahanian6dd30fc2009-12-17 00:49:09 +00002333 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
2334 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2335 Diag(PDecl->getLocation(), diag::err_duplicate_property);
2336 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2337 PDecl->setInvalidDecl();
2338 }
2339 else
2340 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002341
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002342 if (T->isArrayType() || T->isFunctionType()) {
2343 Diag(AtLoc, diag::err_property_type) << T;
2344 PDecl->setInvalidDecl();
2345 }
Mike Stump1eb44332009-09-09 15:08:12 +00002346
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002347 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00002348
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00002349 // Regardless of setter/getter attribute, we save the default getter/setter
2350 // selector names in anticipation of declaration of setter/getter methods.
2351 PDecl->setGetterName(GetterSel);
2352 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002354 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002355 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00002356
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002357 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002358 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00002359
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002360 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002361 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00002362
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002363 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002364 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00002365
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002366 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002367 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00002368
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002369 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002370 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00002371
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002372 if (isAssign)
2373 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00002374
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002375 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002376 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00002377
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00002378 if (MethodImplKind == tok::objc_required)
2379 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2380 else if (MethodImplKind == tok::objc_optional)
2381 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002382 // A case of continuation class adding a new property in the class. This
2383 // is not what it was meant for. However, gcc supports it and so should we.
2384 // Make sure setter/getters are declared here.
2385 if (CCPrimary)
2386 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002387
Chris Lattnerb28317a2009-03-28 19:18:32 +00002388 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002389}
2390
Fariborz Jahanian77e2dde2010-02-09 21:49:50 +00002391ObjCIvarDecl*
2392Sema::SynthesizeNewPropertyIvar(ObjCInterfaceDecl *IDecl,
2393 IdentifierInfo *NameII) {
2394 ObjCIvarDecl *Ivar = 0;
2395 ObjCPropertyDecl *Prop = LookupPropertyDecl(IDecl, NameII);
2396 if (Prop && !Prop->isInvalidDecl()) {
2397 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
2398 QualType PropType = Context.getCanonicalType(Prop->getType());
2399 assert(EnclosingContext &&
2400 "null DeclContext for synthesized ivar - SynthesizeNewPropertyIvar");
2401 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext,
2402 Prop->getLocation(),
2403 NameII, PropType, /*Dinfo=*/0,
2404 ObjCIvarDecl::Public,
2405 (Expr *)0);
2406 Ivar->setLexicalDeclContext(IDecl);
2407 IDecl->addDecl(Ivar);
2408 Prop->setPropertyIvarDecl(Ivar);
2409 }
2410 return Ivar;
2411}
2412
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002413/// ActOnPropertyImplDecl - This routine performs semantic checks and
2414/// builds the AST node for a property implementation declaration; declared
2415/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002416///
Mike Stump1eb44332009-09-09 15:08:12 +00002417Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002418 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002419 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002420 DeclPtrTy ClassCatImpDecl,
2421 IdentifierInfo *PropertyId,
2422 IdentifierInfo *PropertyIvar) {
2423 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002424 // Make sure we have a context for the property implementation declaration.
2425 if (!ClassImpDecl) {
2426 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002427 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002428 }
2429 ObjCPropertyDecl *property = 0;
2430 ObjCInterfaceDecl* IDecl = 0;
2431 // Find the class or category class where this property must have
2432 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002433 ObjCImplementationDecl *IC = 0;
2434 ObjCCategoryImplDecl* CatImplClass = 0;
2435 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002436 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002437 // We always synthesize an interface for an implementation
2438 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002439 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002440 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002441
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002442 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002443 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002444 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002445 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002446 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002447 }
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002448 if (const ObjCCategoryDecl *CD =
2449 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002450 if (!CD->IsClassExtension()) {
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002451 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002452 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002453 return DeclPtrTy();
2454 }
2455 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002456 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002457 if (Synthesize) {
2458 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002459 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002460 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002461 IDecl = CatImplClass->getClassInterface();
2462 if (!IDecl) {
2463 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002464 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002465 }
Mike Stump1eb44332009-09-09 15:08:12 +00002466 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002467 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002468
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002469 // If category for this implementation not found, it is an error which
2470 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002471 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002472 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002473 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002474 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002475 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002476 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002477 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002478 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002479 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002480 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002481 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002482 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002483 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002484 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002485 // Check that we have a valid, previously declared ivar for @synthesize
2486 if (Synthesize) {
2487 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002488 if (!PropertyIvar)
2489 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002490 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002491 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002492 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002493 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002494 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002495 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002496 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002497 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002498 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2499 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002500 ObjCIvarDecl::Public,
2501 (Expr *)0);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00002502 IDecl->makeDeclVisibleInContext(Ivar, false);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002503 property->setPropertyIvarDecl(Ivar);
2504 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002505 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002506 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002507 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002508 } else if (getLangOptions().ObjCNonFragileABI &&
2509 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002510 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002511 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002512 << ClassDeclared->getDeclName();
2513 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2514 << Ivar << Ivar->getNameAsCString();
2515 // Note! I deliberately want it to fall thru so more errors are caught.
2516 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002517 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002518
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002519 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002520 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002521 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002522 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002523 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002524 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002525 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002526 }
Mike Stump1eb44332009-09-09 15:08:12 +00002527
Chris Lattnerb28317a2009-03-28 19:18:32 +00002528 // FIXME! Rules for properties are somewhat different that those
2529 // for assignments. Use a new routine to consolidate all cases;
2530 // specifically for property redeclarations as well as for ivars.
2531 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2532 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002533 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002534 lhsType->isArithmeticType()) {
2535 Diag(PropertyLoc, diag::error_property_ivar_type)
2536 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002537 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002538 }
2539 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002540 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2541 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002542 Diag(PropertyLoc, diag::error_weak_property)
2543 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002544 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002545 }
Mike Stump1eb44332009-09-09 15:08:12 +00002546 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002547 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2548 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002549 Diag(PropertyLoc, diag::error_strong_property)
2550 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002551 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002552 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002553 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002554 } else if (PropertyIvar)
2555 // @dynamic
2556 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002557 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002558 ObjCPropertyImplDecl *PIDecl =
2559 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2560 property,
2561 (Synthesize ?
2562 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002563 : ObjCPropertyImplDecl::Dynamic),
2564 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002565 if (IC) {
2566 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002567 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002568 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002569 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2570 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002571 << PropertyIvar;
2572 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2573 }
Mike Stump1eb44332009-09-09 15:08:12 +00002574
2575 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002576 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002577 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2578 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002579 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002580 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002581 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002582 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002583 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002584 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002585 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002586 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2587 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002588 << PropertyIvar;
2589 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2590 }
Mike Stump1eb44332009-09-09 15:08:12 +00002591
2592 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002593 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002594 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2595 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002596 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002597 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002598 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002599 }
Mike Stump1eb44332009-09-09 15:08:12 +00002600
Chris Lattnerb28317a2009-03-28 19:18:32 +00002601 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002602}
Anders Carlsson15281452008-11-04 16:57:32 +00002603
Chris Lattnercc98eac2008-12-17 07:13:27 +00002604bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002605 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002606 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002607
Anders Carlsson15281452008-11-04 16:57:32 +00002608 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2609 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002610
Anders Carlsson15281452008-11-04 16:57:32 +00002611 return true;
2612}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002613
Chris Lattnercc98eac2008-12-17 07:13:27 +00002614/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2615/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002616void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002617 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002618 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002619 // Check that ClassName is a valid class
2620 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2621 if (!Class) {
2622 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2623 return;
2624 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002625 if (LangOpts.ObjCNonFragileABI) {
2626 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2627 return;
2628 }
Mike Stump1eb44332009-09-09 15:08:12 +00002629
Chris Lattnercc98eac2008-12-17 07:13:27 +00002630 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002631 llvm::SmallVector<FieldDecl*, 32> RecFields;
2632 Context.CollectObjCIvars(Class, RecFields);
2633 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2634 for (unsigned i = 0; i < RecFields.size(); i++) {
2635 FieldDecl* ID = RecFields[i];
2636 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2637 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2638 ID->getIdentifier(), ID->getType(),
2639 ID->getBitWidth());
2640 Decls.push_back(Sema::DeclPtrTy::make(FD));
2641 }
Mike Stump1eb44332009-09-09 15:08:12 +00002642
Chris Lattnercc98eac2008-12-17 07:13:27 +00002643 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002644 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002645 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002646 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002647 if (getLangOptions().CPlusPlus)
2648 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002649 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002650 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002651 }
2652}
2653