blob: e5526746d9d0c93ffb0cf4add8196cc5119c8e2f [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;
53
Steve Naroffa56f6162007-12-18 01:30:32 +000054 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000055 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000056 AddInstanceMethodToGlobalPool(MDecl);
57 else
58 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000059
Chris Lattner4d391482007-12-12 07:09:47 +000060 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000061 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000062
63 // Create Decl objects for each parameter, entrring them in the scope for
64 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000065
66 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000067 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +000068
Daniel Dunbar451318c2008-08-26 06:07:48 +000069 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
70 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000071
Chris Lattner8123a952008-04-10 02:22:51 +000072 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000073 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
74 E = MDecl->param_end(); PI != E; ++PI)
75 if ((*PI)->getIdentifier())
76 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000077}
78
Chris Lattnerb28317a2009-03-28 19:18:32 +000079Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000080ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
81 IdentifierInfo *ClassName, SourceLocation ClassLoc,
82 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000083 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000084 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000085 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000086
Chris Lattner4d391482007-12-12 07:09:47 +000087 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +000088 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000089 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000090 // Maybe we will complain about the shadowed template parameter.
91 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
92 // Just pretend that we didn't see the previous declaration.
93 PrevDecl = 0;
94 }
95
Ted Kremeneka526c5c2008-01-07 19:49:32 +000096 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000097 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000098 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000099 }
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000101 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000102 if (IDecl) {
103 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000104 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000105 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000106 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000107 Diag(IDecl->getLocation(), diag::note_previous_definition);
108
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000109 // Return the previous class interface.
110 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000111 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000112 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000113 IDecl->setLocation(AtInterfaceLoc);
114 IDecl->setForwardDecl(false);
Steve Naroff8b26cbd2009-09-11 00:12:01 +0000115 IDecl->setClassLoc(ClassLoc);
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000116
117 // Since this ObjCInterfaceDecl was created by a forward declaration,
118 // we now add it to the DeclContext since it wasn't added before
119 // (see ActOnForwardClassDeclaration).
120 CurContext->addDecl(IDecl);
121
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000122 if (AttrList)
123 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000124 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000125 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000126 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000127 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000128 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000129 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Steve Naroffa7503a72009-04-23 15:15:40 +0000131 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000132 }
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Chris Lattner4d391482007-12-12 07:09:47 +0000134 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000135 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000136 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000137
138 if (!PrevDecl) {
139 // Try to correct for a typo in the superclass name.
140 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
141 if (CorrectTypo(R, TUScope, 0) &&
142 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
143 Diag(SuperLoc, diag::err_undef_superclass_suggest)
144 << SuperName << ClassName << PrevDecl->getDeclName();
145 }
146 }
147
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000148 if (PrevDecl == IDecl) {
149 Diag(SuperLoc, diag::err_recursive_superclass)
150 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
151 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000152 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000153 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000154 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000155
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000156 // Diagnose classes that inherit from deprecated classes.
157 if (SuperClassDecl)
158 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000160 if (PrevDecl && SuperClassDecl == 0) {
161 // The previous declaration was not a class decl. Check if we have a
162 // typedef. If we do, get the underlying class type.
163 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
164 QualType T = TDecl->getUnderlyingType();
165 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000166 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000167 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
168 }
169 }
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000171 // This handles the following case:
172 //
173 // typedef int SuperClass;
174 // @interface MyClass : SuperClass {} @end
175 //
176 if (!SuperClassDecl) {
177 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
178 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000179 }
180 }
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000182 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
183 if (!SuperClassDecl)
184 Diag(SuperLoc, diag::err_undef_superclass)
185 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
186 else if (SuperClassDecl->isForwardDecl())
187 Diag(SuperLoc, diag::err_undef_superclass)
188 << SuperClassDecl->getDeclName() << ClassName
189 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000190 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000191 IDecl->setSuperClass(SuperClassDecl);
192 IDecl->setSuperClassLoc(SuperLoc);
193 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000194 }
Chris Lattner4d391482007-12-12 07:09:47 +0000195 } else { // we have a root class.
196 IDecl->setLocEnd(ClassLoc);
197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000199 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000200 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000201 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
202 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000203 IDecl->setLocEnd(EndProtoLoc);
204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Anders Carlsson15281452008-11-04 16:57:32 +0000206 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000207 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000208}
209
210/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000211/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000212Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000213 IdentifierInfo *AliasName,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000214 SourceLocation AliasLocation,
215 IdentifierInfo *ClassName,
216 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000217 // Look for previous declaration of alias name
John McCallf36e02d2009-10-09 21:13:30 +0000218 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000219 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000220 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000221 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000222 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000223 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000224 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000225 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000226 }
227 // Check for class declaration
John McCallf36e02d2009-10-09 21:13:30 +0000228 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000229 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
230 QualType T = TDecl->getUnderlyingType();
231 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000232 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000233 ClassName = IDecl->getIdentifier();
John McCallf36e02d2009-10-09 21:13:30 +0000234 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000235 }
236 }
237 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000238 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
239 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000240 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000241 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000242 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000243 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000244 }
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000246 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000247 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000248 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Anders Carlsson15281452008-11-04 16:57:32 +0000250 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000251 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000252
Chris Lattnerb28317a2009-03-28 19:18:32 +0000253 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000254}
255
Steve Naroff61d68522009-03-05 15:22:01 +0000256void Sema::CheckForwardProtocolDeclarationForCircularDependency(
257 IdentifierInfo *PName,
258 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000259 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000260 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
261 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Douglas Gregor6e378de2009-04-23 23:18:26 +0000263 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000264 if (PDecl->getIdentifier() == PName) {
265 Diag(Ploc, diag::err_protocol_has_circular_dependency);
266 Diag(PrevLoc, diag::note_previous_definition);
267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000269 PDecl->getLocation(), PDecl->getReferencedProtocols());
270 }
271 }
272}
273
Chris Lattnerb28317a2009-03-28 19:18:32 +0000274Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000275Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
276 IdentifierInfo *ProtocolName,
277 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000278 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000279 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000280 SourceLocation EndProtoLoc,
281 AttributeList *AttrList) {
282 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000283 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000284 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000285 if (PDecl) {
286 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000287 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000288 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000289 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000290 // Just return the protocol we already had.
291 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000292 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000293 }
Steve Naroff61d68522009-03-05 15:22:01 +0000294 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000295 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000296 CheckForwardProtocolDeclarationForCircularDependency(
297 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
298 PList.Destroy(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Steve Narofff11b5082008-08-13 16:39:22 +0000300 // Make sure the cached decl gets a valid start location.
301 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000302 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000303 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000304 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000305 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000306 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000307 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000308 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000309 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000310 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000311 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000312 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000313 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000314 PDecl->setLocEnd(EndProtoLoc);
315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
317 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000318 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000319}
320
321/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000322/// issues an error if they are not declared. It returns list of
323/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000324void
Chris Lattnere13b9592008-07-26 04:03:38 +0000325Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000326 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000327 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000328 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000329 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000330 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000331 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000332 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
333 LookupObjCProtocolName);
334 if (CorrectTypo(R, TUScope, 0) &&
335 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
336 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
337 << ProtocolId[i].first << R.getLookupName();
338 }
339 }
340
341 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000342 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000343 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000344 continue;
345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000347 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000348
349 // If this is a forward declaration and we are supposed to warn in this
350 // case, do it.
351 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000352 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000353 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000354 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000355 }
356}
357
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000358/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000359/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000360///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000361void
Mike Stump1eb44332009-09-09 15:08:12 +0000362Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000363 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000364 const IdentifierInfo *inheritedName) {
Mike Stump1eb44332009-09-09 15:08:12 +0000365 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000366 Property->getPropertyAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000367 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000368 SuperProperty->getPropertyAttributes();
369 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
370 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000371 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000372 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000373 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
374 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000375 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000376 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000377 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
378 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000379 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000380 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000382 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
383 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000384 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000385 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000386 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000387 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump1eb44332009-09-09 15:08:12 +0000388 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000389 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000390 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000391 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000392
Mike Stump1eb44332009-09-09 15:08:12 +0000393 QualType LHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000394 Context.getCanonicalType(SuperProperty->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000395 QualType RHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000396 Context.getCanonicalType(Property->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Steve Naroff15edf0d2009-03-03 15:43:24 +0000398 if (!Context.typesAreCompatible(LHSType, RHSType)) {
399 // FIXME: Incorporate this test with typesAreCompatible.
400 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff4084c302009-07-23 01:01:38 +0000401 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff15edf0d2009-03-03 15:43:24 +0000402 return;
403 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
404 << Property->getType() << SuperProperty->getType() << inheritedName;
405 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000406}
407
408/// ComparePropertiesInBaseAndSuper - This routine compares property
409/// declarations in base and its super class, if any, and issues
410/// diagnostics in a variety of inconsistant situations.
411///
Chris Lattner70f19542009-02-16 21:26:43 +0000412void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000413 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
414 if (!SDecl)
415 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000416 // FIXME: O(N^2)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000417 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
418 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000419 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000420 // Does property in super class has declaration in current class?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000421 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
422 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000423 ObjCPropertyDecl *PDecl = (*I);
424 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump1eb44332009-09-09 15:08:12 +0000425 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000426 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000427 }
428 }
429}
430
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000431/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
432/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000433/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000434void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000435Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000436 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000437 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
438 if (!IDecl) {
439 // Category
440 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
441 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000442 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
443 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000444 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000445 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000446 // Is this property already in category's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000447 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000448 if ((*CP)->getIdentifier() == Pr->getIdentifier())
449 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000450 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000451 // Property protocol already exist in class. Diagnose any mismatch.
452 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
453 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000454 return;
455 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000456 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
457 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000458 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000459 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000460 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000461 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000462 if ((*CP)->getIdentifier() == Pr->getIdentifier())
463 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000464 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000465 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000466 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000467 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000468}
469
470/// MergeProtocolPropertiesIntoClass - This routine merges properties
471/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000472/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000473///
Chris Lattner70f19542009-02-16 21:26:43 +0000474void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000475 DeclPtrTy MergeItsProtocols) {
476 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000477 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
478
479 if (!IDecl) {
480 // Category
481 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
482 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
483 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
484 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
485 E = MDecl->protocol_end(); P != E; ++P)
486 // Merge properties of category (*P) into IDECL's
487 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000489 // Go thru the list of protocols for this category and recursively merge
490 // their properties into this class as well.
491 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
492 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000493 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000494 } else {
495 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
496 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
497 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000498 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000499 }
500 return;
501 }
502
Chris Lattnerb752f282008-07-21 07:06:49 +0000503 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000504 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
505 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000506 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000507 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000509 // Go thru the list of protocols for this class and recursively merge
510 // their properties into this class as well.
511 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
512 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000513 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000514 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000515 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
516 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
517 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000518 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000519 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000520}
521
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000522/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000523/// a class method in its extension.
524///
Mike Stump1eb44332009-09-09 15:08:12 +0000525void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000526 ObjCInterfaceDecl *ID) {
527 if (!ID)
528 return; // Possibly due to previous error
529
530 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000531 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
532 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000533 ObjCMethodDecl *MD = *i;
534 MethodMap[MD->getSelector()] = MD;
535 }
536
537 if (MethodMap.empty())
538 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000539 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
540 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000541 ObjCMethodDecl *Method = *i;
542 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
543 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
544 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
545 << Method->getDeclName();
546 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
547 }
548 }
549}
550
Chris Lattner58fe03b2009-04-12 08:43:13 +0000551/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000552Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000553Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000554 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000555 unsigned NumElts,
556 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000557 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattner4d391482007-12-12 07:09:47 +0000559 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000560 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000561 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000562 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000563 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000564 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000565 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000566 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000567 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000568 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000569 Protocols.push_back(PDecl);
570 }
Mike Stump1eb44332009-09-09 15:08:12 +0000571
572 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000573 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000574 &Protocols[0], Protocols.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000575 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000576 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000577 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000578}
579
Chris Lattnerb28317a2009-03-28 19:18:32 +0000580Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000581ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
582 IdentifierInfo *ClassName, SourceLocation ClassLoc,
583 IdentifierInfo *CategoryName,
584 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000585 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000586 unsigned NumProtoRefs,
587 SourceLocation EndProtoLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000588 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000589 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
590 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000591 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000592
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000593 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000594 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000595 if (!IDecl || IDecl->isForwardDecl()) {
596 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000597 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000598 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000599 }
Chris Lattner4d391482007-12-12 07:09:47 +0000600
Chris Lattner70f19542009-02-16 21:26:43 +0000601 CDecl->setClassInterface(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Chris Lattner16b34b42009-02-16 21:30:01 +0000603 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000604 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000605
606 /// Check for duplicate interface declaration for this category
607 ObjCCategoryDecl *CDeclChain;
608 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
609 CDeclChain = CDeclChain->getNextClassCategory()) {
610 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
611 Diag(CategoryLoc, diag::warn_dup_category_def)
612 << ClassName << CategoryName;
613 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
614 break;
615 }
616 }
617 if (!CDeclChain)
618 CDecl->insertNextClassCategory();
619
Chris Lattner4d391482007-12-12 07:09:47 +0000620 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000621 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
622 Context);
623 CDecl->setLocEnd(EndProtoLoc);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000624 // Protocols in the class extension belong to the class.
625 if (!CDecl->getIdentifier())
626 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
627 NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Anders Carlsson15281452008-11-04 16:57:32 +0000630 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000631 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000632}
633
634/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000635/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000636/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000637Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000638 SourceLocation AtCatImplLoc,
639 IdentifierInfo *ClassName, SourceLocation ClassLoc,
640 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000641 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000642 ObjCCategoryDecl *CatIDecl = 0;
643 if (IDecl) {
644 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
645 if (!CatIDecl) {
646 // Category @implementation with no corresponding @interface.
647 // Create and install one.
648 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
649 CatName);
650 CatIDecl->setClassInterface(IDecl);
651 CatIDecl->insertNextClassCategory();
652 }
653 }
654
Mike Stump1eb44332009-09-09 15:08:12 +0000655 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000656 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
657 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000658 /// Check that class of this category is already completely declared.
659 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000660 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000661
Douglas Gregord0434102009-01-09 00:49:46 +0000662 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000663 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000664
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000665 /// Check that CatName, category name, is not used in another implementation.
666 if (CatIDecl) {
667 if (CatIDecl->getImplementation()) {
668 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
669 << CatName;
670 Diag(CatIDecl->getImplementation()->getLocation(),
671 diag::note_previous_definition);
672 } else
673 CatIDecl->setImplementation(CDecl);
674 }
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Anders Carlsson15281452008-11-04 16:57:32 +0000676 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000677 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000678}
679
Chris Lattnerb28317a2009-03-28 19:18:32 +0000680Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000681 SourceLocation AtClassImplLoc,
682 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000683 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000684 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000685 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000686 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000687 NamedDecl *PrevDecl
688 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000689 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000690 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000691 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000692 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
693 // If this is a forward declaration of an interface, warn.
694 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000695 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000696 IDecl = 0;
697 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000698 } else {
699 // We did not find anything with the name ClassName; try to correct for
700 // typos in the class name.
701 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
702 if (CorrectTypo(R, TUScope, 0) &&
703 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000704 // Suggest the (potentially) correct interface name. However, put the
705 // fix-it hint itself in a separate note, since changing the name in
706 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000707 // provide a code-modification hint or use the typo name for recovery,
708 // because this is just a warning. The program may actually be correct.
709 Diag(ClassLoc, diag::warn_undef_interface_suggest)
710 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000711 Diag(IDecl->getLocation(), diag::note_previous_decl)
712 << R.getLookupName()
713 << CodeModificationHint::CreateReplacement(ClassLoc,
714 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000715 IDecl = 0;
716 } else {
717 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
718 }
Chris Lattner4d391482007-12-12 07:09:47 +0000719 }
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Chris Lattner4d391482007-12-12 07:09:47 +0000721 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000722 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000723 if (SuperClassname) {
724 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000725 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000726 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000727 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
728 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000729 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000730 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000731 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000732 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000733 Diag(SuperClassLoc, diag::err_undef_superclass)
734 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000735 else if (IDecl && IDecl->getSuperClass() != SDecl) {
736 // This implementation and its interface do not have the same
737 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000738 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000739 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000740 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000741 }
742 }
743 }
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Chris Lattner4d391482007-12-12 07:09:47 +0000745 if (!IDecl) {
746 // Legacy case of @implementation with no corresponding @interface.
747 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000748
Mike Stump390b4cc2009-05-16 07:39:55 +0000749 // FIXME: Do we support attributes on the @implementation? If so we should
750 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000751 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000752 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000753 IDecl->setSuperClass(SDecl);
754 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000755
756 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000757 } else {
758 // Mark the interface as being completed, even if it was just as
759 // @class ....;
760 // declaration; the user cannot reopen it.
761 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000762 }
Mike Stump1eb44332009-09-09 15:08:12 +0000763
764 ObjCImplementationDecl* IMPDecl =
765 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000766 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Anders Carlsson15281452008-11-04 16:57:32 +0000768 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000769 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Chris Lattner4d391482007-12-12 07:09:47 +0000771 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000772 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000773 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000774 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000775 Diag(IDecl->getImplementation()->getLocation(),
776 diag::note_previous_definition);
777 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000778 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000779 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000780 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000781 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000782}
783
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000784void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
785 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000786 SourceLocation RBrace) {
787 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000788 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000789 if (!IDecl)
790 return;
791 /// Check case of non-existing @interface decl.
792 /// (legacy objective-c @implementation decl without an @interface decl).
793 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000794 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000795 IDecl->setIVarList(ivars, numIvars, Context);
796 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000797 return;
798 }
799 // If implementation has empty ivar list, just return.
800 if (numIvars == 0)
801 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Chris Lattner4d391482007-12-12 07:09:47 +0000803 assert(ivars && "missing @implementation ivars");
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Chris Lattner4d391482007-12-12 07:09:47 +0000805 // Check interface's Ivar list against those in the implementation.
806 // names and types must match.
807 //
Chris Lattner4d391482007-12-12 07:09:47 +0000808 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000809 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000810 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
811 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000812 ObjCIvarDecl* ImplIvar = ivars[j++];
813 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000814 assert (ImplIvar && "missing implementation ivar");
815 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Steve Naroffca331292009-03-03 14:49:36 +0000817 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000818 if (Context.getCanonicalType(ImplIvar->getType()) !=
819 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000820 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000821 << ImplIvar->getIdentifier()
822 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000823 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000824 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
825 Expr *ImplBitWidth = ImplIvar->getBitWidth();
826 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000827 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
828 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000829 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
830 << ImplIvar->getIdentifier();
831 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
832 }
Mike Stump1eb44332009-09-09 15:08:12 +0000833 }
Steve Naroffca331292009-03-03 14:49:36 +0000834 // Make sure the names are identical.
835 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000836 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000837 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000838 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000839 }
840 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000841 }
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Chris Lattner609e4c72007-12-12 18:11:49 +0000843 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000844 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000845 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000846 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000847}
848
Steve Naroff3c2eb662008-02-10 21:38:56 +0000849void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
850 bool &IncompleteImpl) {
851 if (!IncompleteImpl) {
852 Diag(ImpLoc, diag::warn_incomplete_impl);
853 IncompleteImpl = true;
854 }
Chris Lattner08631c52008-11-23 21:45:46 +0000855 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000856}
857
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000858void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
859 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000860 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000861 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000862 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
863 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000864 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000865 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
866 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000867 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
868 }
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Chris Lattner3aff9192009-04-11 19:58:42 +0000870 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
871 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
872 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000873 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
874 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
875 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
876 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000877 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000878
879 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000880 << ImpMethodDecl->getDeclName() << (*IF)->getType()
881 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000882 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000883 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000884}
885
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000886/// isPropertyReadonly - Return true if property is readonly, by searching
887/// for the property in the class and in its categories and implementations
888///
889bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000890 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000891 // by far the most common case.
892 if (!PDecl->isReadOnly())
893 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000894 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000895 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000896 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000897 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000899 // Main class has the property as 'readonly'. Must search
Mike Stump1eb44332009-09-09 15:08:12 +0000900 // through the category list to see if the property's
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000901 // attribute has been over-ridden to 'readwrite'.
902 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
903 Category; Category = Category->getNextClassCategory()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000904 // Even if property is ready only, if a category has a user defined setter,
905 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000906 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000907 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000908 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000909 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000910 if (P && !P->isReadOnly())
911 return false;
912 }
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000914 // Also, check for definition of a setter method in the implementation if
915 // all else failed.
916 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000917 if (ObjCImplementationDecl *IMD =
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000918 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000919 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000920 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000921 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000922 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000923 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000924 return false;
925 }
926 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000927 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000928 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000929 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000930 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000931 // If all fails, look at the super class.
932 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
933 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000934 return true;
935}
936
Mike Stump390b4cc2009-05-16 07:39:55 +0000937/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
938/// improve the efficiency of selector lookups and type checking by associating
939/// with each protocol / interface / category the flattened instance tables. If
940/// we used an immutable set to keep the table then it wouldn't add significant
941/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000942
Steve Naroffefe7f362008-02-08 22:06:17 +0000943/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000944/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000945void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
946 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000947 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000948 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000949 const llvm::DenseSet<Selector> &ClsMap,
950 ObjCInterfaceDecl *IDecl) {
951 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000952 ObjCInterfaceDecl *NSIDecl = 0;
953 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000954 // check to see if class implements forwardInvocation method and objects
955 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000956 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000957 // Under such conditions, which means that every method possible is
958 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000959 // found" warnings.
960 // FIXME: Use a general GetUnarySelector method for this.
961 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
962 Selector fISelector = Context.Selectors.getSelector(1, &II);
963 if (InsMap.count(fISelector))
964 // Is IDecl derived from 'NSProxy'? If so, no instance methods
965 // need be implemented in the implementation.
966 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
967 }
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000969 // If a method lookup fails locally we still need to look and see if
970 // the method was implemented by a base class or an inherited
971 // protocol. This lookup is slow, but occurs rarely in correct code
972 // and otherwise would terminate in a warning.
973
Chris Lattner4d391482007-12-12 07:09:47 +0000974 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000975 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000976 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000977 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000978 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000979 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000980 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000981 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000982 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000983 // Ugly, but necessary. Method declared in protcol might have
984 // have been synthesized due to a property declared in the class which
985 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000986 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000987 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000988 if (!MethodInClass || !MethodInClass->isSynthesized())
989 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
990 }
991 }
Chris Lattner4d391482007-12-12 07:09:47 +0000992 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000993 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000994 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000995 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000996 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000997 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
998 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000999 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +00001000 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001001 }
Chris Lattner780f3292008-07-21 21:32:27 +00001002 // Check on this protocols's referenced protocols, recursively.
1003 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1004 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001005 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001006}
1007
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001008/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1009/// or protocol against those declared in their implementations.
1010///
1011void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1012 const llvm::DenseSet<Selector> &ClsMap,
1013 llvm::DenseSet<Selector> &InsMapSeen,
1014 llvm::DenseSet<Selector> &ClsMapSeen,
1015 ObjCImplDecl* IMPDecl,
1016 ObjCContainerDecl* CDecl,
1017 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001018 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001019 // Check and see if instance methods in class interface have been
1020 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001021 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1022 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001023 if (InsMapSeen.count((*I)->getSelector()))
1024 continue;
1025 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001026 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001027 !InsMap.count((*I)->getSelector())) {
1028 if (ImmediateClass)
1029 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1030 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001031 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001032 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001033 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001034 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001035 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001036 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001037 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1038 // ImpMethodDecl may be null as in a @dynamic property.
1039 if (ImpMethodDecl)
1040 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1041 }
1042 }
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001044 // Check and see if class methods in class interface have been
1045 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001046 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001047 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001048 if (ClsMapSeen.count((*I)->getSelector()))
1049 continue;
1050 ClsMapSeen.insert((*I)->getSelector());
1051 if (!ClsMap.count((*I)->getSelector())) {
1052 if (ImmediateClass)
1053 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001054 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001055 ObjCMethodDecl *ImpMethodDecl =
1056 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001057 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001058 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001059 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1060 }
1061 }
1062 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1063 // Check for any implementation of a methods declared in protocol.
1064 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1065 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001066 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1067 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001068 (*PI), IncompleteImpl, false);
1069 if (I->getSuperClass())
1070 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001071 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001072 I->getSuperClass(), IncompleteImpl, false);
1073 }
1074}
1075
Mike Stump1eb44332009-09-09 15:08:12 +00001076void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1077 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001078 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001079 llvm::DenseSet<Selector> InsMap;
1080 // Check and see if instance methods in class interface have been
1081 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001082 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001083 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001084 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001086 // Check and see if properties declared in the interface have either 1)
1087 // an implementation or 2) there is a @synthesize/@dynamic implementation
1088 // of the property in the @implementation.
1089 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001090 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1091 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001092 ObjCPropertyDecl *Prop = (*P);
1093 if (Prop->isInvalidDecl())
1094 continue;
1095 ObjCPropertyImplDecl *PI = 0;
1096 // Is there a matching propery synthesize/dynamic?
Mike Stump1eb44332009-09-09 15:08:12 +00001097 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001098 I = IMPDecl->propimpl_begin(),
1099 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001100 if ((*I)->getPropertyDecl() == Prop) {
1101 PI = (*I);
1102 break;
1103 }
1104 if (PI)
1105 continue;
1106 if (!InsMap.count(Prop->getGetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001107 Diag(Prop->getLocation(),
1108 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001109 << Prop->getDeclName() << Prop->getGetterName();
1110 Diag(IMPDecl->getLocation(),
1111 diag::note_property_impl_required);
1112 }
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001114 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001115 Diag(Prop->getLocation(),
1116 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001117 << Prop->getDeclName() << Prop->getSetterName();
1118 Diag(IMPDecl->getLocation(),
1119 diag::note_property_impl_required);
1120 }
1121 }
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Chris Lattner4d391482007-12-12 07:09:47 +00001123 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001124 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001125 I = IMPDecl->classmeth_begin(),
1126 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001127 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001129 // Check for type conflict of methods declared in a class/protocol and
1130 // its implementation; if any.
1131 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001132 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1133 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001134 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Chris Lattner4d391482007-12-12 07:09:47 +00001136 // Check the protocol list for unimplemented methods in the @implementation
1137 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001138 // Check and see if class methods in class interface have been
1139 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Chris Lattnercddc8882009-03-01 00:56:52 +00001141 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001142 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001143 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001144 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001145 InsMap, ClsMap, I);
1146 // Check class extensions (unnamed categories)
1147 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1148 Categories; Categories = Categories->getNextClassCategory()) {
1149 if (!Categories->getIdentifier()) {
1150 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1151 break;
1152 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001153 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001154 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001155 // For extended class, unimplemented methods in its protocols will
1156 // be reported in the primary class.
1157 if (C->getIdentifier()) {
1158 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1159 E = C->protocol_end(); PI != E; ++PI)
1160 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1161 InsMap, ClsMap, C->getClassInterface());
1162 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001163 } else
1164 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001165}
1166
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001167void
1168Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1169 ObjCContainerDecl* IDecl) {
1170 // Rules apply in non-GC mode only
1171 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1172 return;
1173 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1174 E = IDecl->prop_end();
1175 I != E; ++I) {
1176 ObjCPropertyDecl *Property = (*I);
1177 unsigned Attributes = Property->getPropertyAttributes();
1178 // We only care about readwrite atomic property.
1179 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1180 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1181 continue;
1182 if (const ObjCPropertyImplDecl *PIDecl
1183 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1184 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1185 continue;
1186 ObjCMethodDecl *GetterMethod =
1187 IMPDecl->getInstanceMethod(Property->getGetterName());
1188 ObjCMethodDecl *SetterMethod =
1189 IMPDecl->getInstanceMethod(Property->getSetterName());
1190 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1191 SourceLocation MethodLoc =
1192 (GetterMethod ? GetterMethod->getLocation()
1193 : SetterMethod->getLocation());
1194 Diag(MethodLoc, diag::warn_atomic_property_rule)
1195 << Property->getIdentifier();
1196 Diag(Property->getLocation(), diag::note_property_declare);
1197 }
1198 }
1199 }
1200}
1201
Mike Stump1eb44332009-09-09 15:08:12 +00001202/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001203Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001204Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001205 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001206 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001207 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001208 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Chris Lattner4d391482007-12-12 07:09:47 +00001210 for (unsigned i = 0; i != NumElts; ++i) {
1211 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001212 NamedDecl *PrevDecl
1213 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001214 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001215 // Maybe we will complain about the shadowed template parameter.
1216 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1217 // Just pretend that we didn't see the previous declaration.
1218 PrevDecl = 0;
1219 }
1220
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001221 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001222 // GCC apparently allows the following idiom:
1223 //
1224 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1225 // @class XCElementToggler;
1226 //
Mike Stump1eb44332009-09-09 15:08:12 +00001227 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001228 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1229 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001230 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001231 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001232 } else if (TDD) {
1233 // a forward class declaration matching a typedef name of a class refers
1234 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001235 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001236 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1237 PrevDecl = OI->getDecl();
1238 }
Chris Lattner4d391482007-12-12 07:09:47 +00001239 }
Mike Stump1eb44332009-09-09 15:08:12 +00001240 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001241 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001242 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001243 IdentList[i], IdentLocs[i], true);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001244
1245 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1246 // the current DeclContext. This prevents clients that walk DeclContext
1247 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1248 // declared later (if at all). We also take care to explicitly make
1249 // sure this declaration is visible for name lookup.
1250 PushOnScopeChains(IDecl, TUScope, false);
1251 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001252 }
1253
1254 Interfaces.push_back(IDecl);
1255 }
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Ted Kremenek321c22f2009-11-18 00:28:11 +00001257 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001258 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001259 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001260 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001261 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001262 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001263 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001264}
1265
1266
1267/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1268/// returns true, or false, accordingly.
1269/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001270bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001271 const ObjCMethodDecl *PrevMethod,
1272 bool matchBasedOnSizeAndAlignment) {
1273 QualType T1 = Context.getCanonicalType(Method->getResultType());
1274 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001276 if (T1 != T2) {
1277 // The result types are different.
1278 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001279 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001280 // Incomplete types don't have a size and alignment.
1281 if (T1->isIncompleteType() || T2->isIncompleteType())
1282 return false;
1283 // Check is based on size and alignment.
1284 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1285 return false;
1286 }
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Chris Lattner89951a82009-02-20 18:43:26 +00001288 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1289 E = Method->param_end();
1290 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Chris Lattner89951a82009-02-20 18:43:26 +00001292 for (; ParamI != E; ++ParamI, ++PrevI) {
1293 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1294 T1 = Context.getCanonicalType((*ParamI)->getType());
1295 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001296 if (T1 != T2) {
1297 // The result types are different.
1298 if (!matchBasedOnSizeAndAlignment)
1299 return false;
1300 // Incomplete types don't have a size and alignment.
1301 if (T1->isIncompleteType() || T2->isIncompleteType())
1302 return false;
1303 // Check is based on size and alignment.
1304 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1305 return false;
1306 }
Chris Lattner4d391482007-12-12 07:09:47 +00001307 }
1308 return true;
1309}
1310
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001311/// \brief Read the contents of the instance and factory method pools
1312/// for a given selector from external storage.
1313///
1314/// This routine should only be called once, when neither the instance
1315/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001316Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001317 bool isInstance) {
1318 assert(ExternalSource && "We need an external AST source");
1319 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1320 "Selector data already loaded into the instance method pool");
1321 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1322 "Selector data already loaded into the factory method pool");
1323
1324 // Read the method list from the external source.
1325 std::pair<ObjCMethodList, ObjCMethodList> Methods
1326 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001328 if (isInstance) {
1329 if (Methods.second.Method)
1330 FactoryMethodPool[Sel] = Methods.second;
1331 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001332 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001333
1334 if (Methods.first.Method)
1335 InstanceMethodPool[Sel] = Methods.first;
1336
1337 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1338}
1339
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001340void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001341 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1342 = InstanceMethodPool.find(Method->getSelector());
1343 if (Pos == InstanceMethodPool.end()) {
1344 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1345 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1346 else
1347 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1348 ObjCMethodList())).first;
1349 }
1350
1351 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001352 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001353 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001354 Entry.Method = Method;
1355 Entry.Next = 0;
1356 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001357 }
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Chris Lattnerb25df352009-03-04 05:16:45 +00001359 // We've seen a method with this name, see if we have already seen this type
1360 // signature.
1361 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1362 if (MatchTwoMethodDeclarations(Method, List->Method))
1363 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Chris Lattnerb25df352009-03-04 05:16:45 +00001365 // We have a new signature for an existing method - add it.
1366 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1367 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001368}
1369
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001370// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001371ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001372 SourceRange R,
1373 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001374 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1375 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001376 if (Pos == InstanceMethodPool.end()) {
1377 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001378 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1379 else
1380 return 0;
1381 }
1382
1383 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001384 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Steve Naroff037cda52008-09-30 14:38:43 +00001386 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001387 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1388 // This checks if the methods differ by size & alignment.
1389 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001390 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001391 }
1392 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001393 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001394 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001395 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001396 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001397 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001398 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001399 }
1400 return MethList.Method;
1401}
1402
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001403void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001404 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1405 = FactoryMethodPool.find(Method->getSelector());
1406 if (Pos == FactoryMethodPool.end()) {
1407 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1408 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1409 else
1410 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1411 ObjCMethodList())).first;
1412 }
1413
1414 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001415 if (!FirstMethod.Method) {
1416 // Haven't seen a method with this selector name yet - add it.
1417 FirstMethod.Method = Method;
1418 FirstMethod.Next = 0;
1419 } else {
1420 // We've seen a method with this name, now check the type signature(s).
1421 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001422
1423 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001424 Next = Next->Next)
1425 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Chris Lattner4d391482007-12-12 07:09:47 +00001427 if (!match) {
1428 // We have a new signature for an existing method - add it.
1429 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001430 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001431 FirstMethod.Next = OMI;
1432 }
1433 }
1434}
1435
Mike Stump1eb44332009-09-09 15:08:12 +00001436ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001437 SourceRange R) {
1438 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1439 = FactoryMethodPool.find(Sel);
1440 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001441 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001442 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1443 else
1444 return 0;
1445 }
1446
1447 ObjCMethodList &MethList = Pos->second;
1448 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001450 if (MethList.Method && MethList.Next) {
1451 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1452 // This checks if the methods differ by size & alignment.
1453 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1454 issueWarning = true;
1455 }
1456 if (issueWarning && (MethList.Method && MethList.Next)) {
1457 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001458 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001459 << MethList.Method->getSourceRange();
1460 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001461 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001462 << Next->Method->getSourceRange();
1463 }
1464 return MethList.Method;
1465}
1466
Mike Stump1eb44332009-09-09 15:08:12 +00001467/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff0701bbb2009-01-08 17:28:14 +00001468/// have the property type and issue diagnostics if they don't.
1469/// Also synthesize a getter/setter method if none exist (and update the
1470/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1471/// methods is the "right" thing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001472void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001473 ObjCContainerDecl *CD) {
1474 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00001475
1476 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001477 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump1eb44332009-09-09 15:08:12 +00001478 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001479 property->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001481 if (SetterMethod) {
Fariborz Jahanian02deae82010-01-06 00:18:12 +00001482 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1483 property->getPropertyAttributes();
1484 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1485 Context.getCanonicalType(SetterMethod->getResultType()) !=
1486 Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001487 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001488 if (SetterMethod->param_size() != 1 ||
1489 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001490 Diag(property->getLocation(),
1491 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001492 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001493 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001494 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1495 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001496 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001497
1498 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001499 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001500 // FIXME: The synthesized property we set here is misleading. We almost always
1501 // synthesize these methods unless the user explicitly provided prototypes
1502 // (which is odd, but allowed). Sema should be typechecking that the
1503 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001504 if (!GetterMethod) {
1505 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001506 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001507 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001508 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1509 property->getLocation(), property->getGetterName(),
1510 property->getType(), CD, true, false, true,
1511 (property->getPropertyImplementation() ==
1512 ObjCPropertyDecl::Optional) ?
1513 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001514 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001515 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001516 } else
1517 // A user declared getter will be synthesize when @synthesize of
1518 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001519 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001520 property->setGetterMethodDecl(GetterMethod);
1521
1522 // Skip setter if property is read-only.
1523 if (!property->isReadOnly()) {
1524 // Find the default setter and if one not found, add one.
1525 if (!SetterMethod) {
1526 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001527 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001528 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001529 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1530 property->getLocation(),
1531 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001532 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001533 (property->getPropertyImplementation() ==
1534 ObjCPropertyDecl::Optional) ?
1535 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001536 ObjCMethodDecl::Required);
1537 // Invent the arguments for the setter. We don't bother making a
1538 // nice name for the argument.
1539 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001540 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001541 property->getIdentifier(),
1542 property->getType(),
John McCalla93c9342009-12-07 02:54:59 +00001543 /*TInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001544 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001545 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001546 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001547 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001548 } else
1549 // A user declared setter will be synthesize when @synthesize of
1550 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001551 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001552 property->setSetterMethodDecl(SetterMethod);
1553 }
Mike Stump1eb44332009-09-09 15:08:12 +00001554 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001555 // handle the following, which is supported by GCC (and part of the design).
1556 //
1557 // @interface Foo
1558 // @property double bar;
1559 // @end
1560 //
1561 // void thisIsUnfortunate() {
1562 // id foo;
1563 // double bar = [foo bar];
1564 // }
1565 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001566 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001567 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001568 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001569 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001570}
1571
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001572/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1573/// identical selector names in current and its super classes and issues
1574/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001575void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1576 ObjCMethodDecl *Method,
1577 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001578 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1579 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001581 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001582 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001583 SD->lookupMethod(Method->getSelector(), IsInstance);
1584 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001585 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001586 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001587 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001588 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1589 E = Method->param_end();
1590 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1591 for (; ParamI != E; ++ParamI, ++PrevI) {
1592 // Number of parameters are the same and is guaranteed by selector match.
1593 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1594 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1595 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1596 // If type of arguement of method in this class does not match its
1597 // respective argument type in the super class method, issue warning;
1598 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001599 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001600 << T1 << T2;
1601 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1602 return;
1603 }
1604 }
1605 ID = SD;
1606 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001607}
1608
Steve Naroffa56f6162007-12-18 01:30:32 +00001609// Note: For class/category implemenations, allMethods/allProperties is
1610// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001611void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1612 DeclPtrTy *allMethods, unsigned allNum,
1613 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001614 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001615 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001616
Steve Naroffa56f6162007-12-18 01:30:32 +00001617 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1618 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001619 // should be true.
1620 if (!ClassDecl)
1621 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001622
Mike Stump1eb44332009-09-09 15:08:12 +00001623 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001624 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1625 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001626 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001627
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001628 if (!isInterfaceDeclKind && AtEndLoc.isInvalid()) {
1629 AtEndLoc = ClassDecl->getLocation();
1630 Diag(AtEndLoc, diag::warn_missing_atend);
1631 }
1632
Steve Naroff0701bbb2009-01-08 17:28:14 +00001633 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001634
1635 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1636 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1637 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1638
Chris Lattner4d391482007-12-12 07:09:47 +00001639 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001640 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001641 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001642
1643 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001644 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001645 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001646 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001647 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001648 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001649 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001650 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001651 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001652 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001653 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001654 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001655 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001656 InsMap[Method->getSelector()] = Method;
1657 /// The following allows us to typecheck messages to "id".
1658 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001659 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001660 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001661 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001662 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001663 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001664 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001665 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001666 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001667 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001668 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001669 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001670 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001671 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001672 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001673 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001674 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001675 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001676 /// The following allows us to typecheck messages to "Class".
1677 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001678 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001679 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001680 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001681 }
1682 }
1683 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001684 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001685 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001686 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001687 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001688 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001689 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001690 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001691 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001692 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001693
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001694 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001695 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001696 if (C->getIdentifier() == 0)
1697 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001698 }
Steve Naroff09c47192009-01-09 15:36:25 +00001699 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1700 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1701 // user-defined setter/getter. It also synthesizes setter/getter methods
1702 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001703 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1704 E = CDecl->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001705 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001706 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001707 CDecl->setAtEndLoc(AtEndLoc);
1708 }
1709 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001710 IC->setAtEndLoc(AtEndLoc);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001711 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001712 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001713 AtomicPropertySetterGetterRules(IC, IDecl);
1714 }
Mike Stump1eb44332009-09-09 15:08:12 +00001715 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001716 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001717 CatImplClass->setAtEndLoc(AtEndLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Chris Lattner4d391482007-12-12 07:09:47 +00001719 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001720 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001721 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001722 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001723 Categories; Categories = Categories->getNextClassCategory()) {
1724 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001725 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001726 break;
1727 }
1728 }
1729 }
1730 }
Chris Lattner682bf922009-03-29 16:50:03 +00001731 if (isInterfaceDeclKind) {
1732 // Reject invalid vardecls.
1733 for (unsigned i = 0; i != tuvNum; i++) {
1734 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1735 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1736 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001737 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001738 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001739 }
Chris Lattner682bf922009-03-29 16:50:03 +00001740 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001741 }
Chris Lattner4d391482007-12-12 07:09:47 +00001742}
1743
1744
1745/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1746/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001747static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001748CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1749 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1750 if (PQTVal & ObjCDeclSpec::DQ_In)
1751 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1752 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1753 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1754 if (PQTVal & ObjCDeclSpec::DQ_Out)
1755 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1756 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1757 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1758 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1759 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1760 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1761 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001762
1763 return ret;
1764}
1765
Chris Lattnerb28317a2009-03-28 19:18:32 +00001766Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001767 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001768 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001769 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001770 Selector Sel,
1771 // optional arguments. The number of types/arguments is obtained
1772 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001773 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001774 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001775 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1776 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001777 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001778
1779 // Make sure we can establish a context for the method.
1780 if (!ClassDecl) {
1781 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanian32844b32009-08-28 17:52:37 +00001782 FunctionLabelMap.clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001783 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001784 }
Chris Lattner4d391482007-12-12 07:09:47 +00001785 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Steve Naroffccef3712009-02-20 22:59:16 +00001787 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001788 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Steve Naroffccef3712009-02-20 22:59:16 +00001790 // Methods cannot return interface types. All ObjC objects are
1791 // passed by reference.
1792 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001793 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1794 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001795 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001796 }
1797 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001798 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001799
1800 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001801 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00001802 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001803 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001804 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001805 MethodDeclKind == tok::objc_optional ?
1806 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001807 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Chris Lattner0ed844b2008-04-04 06:12:32 +00001809 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Chris Lattner7db638d2009-04-11 19:42:43 +00001811 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001812 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001813 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Chris Lattnere294d3f2009-04-11 18:57:04 +00001815 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001816 ArgType = Context.getObjCIdType();
1817 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001818 } else {
John McCall58e46772009-10-23 21:48:59 +00001819 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001820 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001821 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001822 }
Mike Stump1eb44332009-09-09 15:08:12 +00001823
John McCall58e46772009-10-23 21:48:59 +00001824 ParmVarDecl* Param
1825 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1826 ArgInfo[i].Name, ArgType, DI,
1827 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001829 if (ArgType->isObjCInterfaceType()) {
1830 Diag(ArgInfo[i].NameLoc,
1831 diag::err_object_cannot_be_passed_returned_by_value)
1832 << 1 << ArgType;
1833 Param->setInvalidDecl();
1834 }
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Chris Lattner0ed844b2008-04-04 06:12:32 +00001836 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001837 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001839 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001840 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001841
Chris Lattner0ed844b2008-04-04 06:12:32 +00001842 Params.push_back(Param);
1843 }
1844
Jay Foadbeaaccd2009-05-21 09:52:38 +00001845 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001846 ObjCMethod->setObjCDeclQualifier(
1847 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1848 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001849
1850 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001851 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001852
John McCall54abf7d2009-11-04 02:18:39 +00001853 const ObjCMethodDecl *InterfaceMD = 0;
1854
Mike Stump1eb44332009-09-09 15:08:12 +00001855 // For implementations (which can be very "coarse grain"), we add the
1856 // method now. This allows the AST to implement lookup methods that work
1857 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001858 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001859 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001860 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001861 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001862 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1863 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001864 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001865 PrevMethod = ImpDecl->getClassMethod(Sel);
1866 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001867 }
John McCall54abf7d2009-11-04 02:18:39 +00001868 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1869 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001870 if (AttrList)
1871 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001872 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001873 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001874 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001875 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1876 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001877 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001878 PrevMethod = CatImpDecl->getClassMethod(Sel);
1879 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001880 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001881 if (AttrList)
1882 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001883 }
1884 if (PrevMethod) {
1885 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001886 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001887 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001888 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001889 }
John McCall54abf7d2009-11-04 02:18:39 +00001890
1891 // If the interface declared this method, and it was deprecated there,
1892 // mark it deprecated here.
1893 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1894 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1895
Chris Lattnerb28317a2009-03-28 19:18:32 +00001896 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001897}
1898
Mike Stump1eb44332009-09-09 15:08:12 +00001899void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001900 SourceLocation Loc,
1901 unsigned &Attributes) {
1902 // FIXME: Improve the reported location.
1903
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001904 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001905 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001906 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1907 ObjCDeclSpec::DQ_PR_assign |
1908 ObjCDeclSpec::DQ_PR_copy |
1909 ObjCDeclSpec::DQ_PR_retain))) {
1910 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1911 "readwrite" :
1912 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1913 "assign" :
1914 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1915 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00001916
1917 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001918 diag::err_objc_property_attr_mutually_exclusive :
1919 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001920 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001921 }
1922
1923 // Check for copy or retain on non-object types.
1924 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001925 !PropertyTy->isObjCObjectPointerType() &&
1926 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00001927 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001928 Diag(Loc, diag::err_objc_property_requires_object)
1929 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001930 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1931 }
1932
1933 // Check for more than one of { assign, copy, retain }.
1934 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1935 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001936 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1937 << "assign" << "copy";
1938 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00001939 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001940 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001941 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1942 << "assign" << "retain";
1943 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001944 }
1945 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1946 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001947 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1948 << "copy" << "retain";
1949 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001950 }
1951 }
1952
1953 // Warn if user supplied no assignment attribute, property is
1954 // readwrite, and this is an object type.
1955 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1956 ObjCDeclSpec::DQ_PR_retain)) &&
1957 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00001958 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001959 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00001960 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001961 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1962
1963 // If non-gc code warn that this is likely inappropriate.
1964 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1965 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001967 // FIXME: Implement warning dependent on NSCopying being
1968 // implemented. See also:
1969 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1970 // (please trim this list while you are at it).
1971 }
Mike Stump046efd92009-05-07 23:06:50 +00001972
1973 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1974 && getLangOptions().getGCMode() == LangOptions::GCOnly
1975 && PropertyTy->isBlockPointerType())
1976 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001977}
1978
Mike Stump1eb44332009-09-09 15:08:12 +00001979Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001980 FieldDeclarator &FD,
1981 ObjCDeclSpec &ODS,
1982 Selector GetterSel,
1983 Selector SetterSel,
1984 DeclPtrTy ClassCategory,
1985 bool *isOverridingProperty,
1986 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001987 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001988 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1989 // default is readwrite!
1990 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00001991 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001992 // not retain or copy
1993 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001994 (isReadWrite &&
1995 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001996 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1997 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahaniandd69aae2009-12-16 18:03:30 +00001998 if (T->isReferenceType()) {
1999 Diag(AtLoc, diag::error_reference_property);
2000 return DeclPtrTy();
2001 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002002 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002003 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002004 // May modify Attributes.
2005 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002006 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2007 if (!CDecl->getIdentifier()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002008 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002009 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002010 if ((CCPrimary = CDecl->getClassInterface())) {
2011 // Find the property in continuation class's primary class only.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002012 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002013 if (ObjCPropertyDecl *PIDecl =
2014 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002015 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00002016 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002017 unsigned PIkind = PIDecl->getPropertyAttributes();
2018 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002019 unsigned retainCopyNonatomic =
Fariborz Jahaniand669be52009-11-06 22:59:12 +00002020 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002021 ObjCPropertyDecl::OBJC_PR_copy |
2022 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002023 if ((Attributes & retainCopyNonatomic) !=
2024 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002025 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002026 Diag(PIDecl->getLocation(), diag::note_property_declare);
2027 }
Fariborz Jahanianb73e2812010-01-06 21:38:30 +00002028 DeclContext *DC = dyn_cast<DeclContext>(CCPrimary);
2029 assert(DC && "ClassDecl is not a DeclContext");
2030 DeclContext::lookup_result Found =
2031 DC->lookup(PIDecl->getDeclName());
2032 bool PropertyInPrimaryClass = false;
2033 for (; Found.first != Found.second; ++Found.first)
2034 if (isa<ObjCPropertyDecl>(*Found.first)) {
2035 PropertyInPrimaryClass = true;
2036 break;
2037 }
2038 if (!PropertyInPrimaryClass) {
2039 // Protocol is not in the primary class. Must build one for it.
2040 ObjCDeclSpec ProtocolPropertyODS;
2041 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind and
2042 // ObjCPropertyDecl::PropertyAttributeKind have identical values.
2043 // Should consolidate both into one enum type.
2044 ProtocolPropertyODS.setPropertyAttributes(
2045 (ObjCDeclSpec::ObjCPropertyAttributeKind)PIkind);
2046 DeclPtrTy ProtocolPtrTy =
2047 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
2048 PIDecl->getGetterName(),
2049 PIDecl->getSetterName(),
2050 DeclPtrTy::make(CCPrimary), isOverridingProperty,
2051 MethodImplKind);
2052 PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>();
2053 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002054 PIDecl->makeitReadWriteAttribute();
2055 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2056 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2057 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2058 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2059 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002060 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002061 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002062 << CCPrimary->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002063 Diag(PIDecl->getLocation(), diag::note_property_declare);
2064 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002065 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002066 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00002067 // class's list.
2068 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002069 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002070 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002071 // No matching property found in the primary class. Just fall thru
2072 // and add property to continuation class's primary class.
2073 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002074 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00002075 Diag(CDecl->getLocation(), diag::err_continuation_class);
2076 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002077 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002078 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002079 }
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002081 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00002082 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002083 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2084 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2085 if (T->isObjCObjectPointerType()) {
2086 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00002087 if (const ObjCInterfaceType *OIT =
John McCall183700f2009-09-21 23:43:11 +00002088 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002089 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002090 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002091 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002092 LookupProtocol(&Context.Idents.get("NSCopying")))
2093 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00002094 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002095 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002096 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002097 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00002098 if (T->isObjCInterfaceType())
2099 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002100
Steve Naroff93983f82009-01-11 12:47:58 +00002101 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2102 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002103 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00002104 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00002105 FD.D.getIdentifier(), T);
Fariborz Jahanian6dd30fc2009-12-17 00:49:09 +00002106 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
2107 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2108 Diag(PDecl->getLocation(), diag::err_duplicate_property);
2109 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2110 PDecl->setInvalidDecl();
2111 }
2112 else
2113 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002114
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002115 if (T->isArrayType() || T->isFunctionType()) {
2116 Diag(AtLoc, diag::err_property_type) << T;
2117 PDecl->setInvalidDecl();
2118 }
Mike Stump1eb44332009-09-09 15:08:12 +00002119
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002120 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00002121
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00002122 // Regardless of setter/getter attribute, we save the default getter/setter
2123 // selector names in anticipation of declaration of setter/getter methods.
2124 PDecl->setGetterName(GetterSel);
2125 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00002126
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002127 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002128 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00002129
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002130 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002131 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00002132
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002133 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002134 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00002135
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002136 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002137 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00002138
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002139 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002140 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00002141
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002142 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002143 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00002144
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002145 if (isAssign)
2146 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00002147
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002148 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002149 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00002150
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00002151 if (MethodImplKind == tok::objc_required)
2152 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2153 else if (MethodImplKind == tok::objc_optional)
2154 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002155 // A case of continuation class adding a new property in the class. This
2156 // is not what it was meant for. However, gcc supports it and so should we.
2157 // Make sure setter/getters are declared here.
2158 if (CCPrimary)
2159 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002160
Chris Lattnerb28317a2009-03-28 19:18:32 +00002161 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002162}
2163
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002164/// ActOnPropertyImplDecl - This routine performs semantic checks and
2165/// builds the AST node for a property implementation declaration; declared
2166/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002167///
Mike Stump1eb44332009-09-09 15:08:12 +00002168Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002169 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002170 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002171 DeclPtrTy ClassCatImpDecl,
2172 IdentifierInfo *PropertyId,
2173 IdentifierInfo *PropertyIvar) {
2174 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002175 // Make sure we have a context for the property implementation declaration.
2176 if (!ClassImpDecl) {
2177 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002178 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002179 }
2180 ObjCPropertyDecl *property = 0;
2181 ObjCInterfaceDecl* IDecl = 0;
2182 // Find the class or category class where this property must have
2183 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002184 ObjCImplementationDecl *IC = 0;
2185 ObjCCategoryImplDecl* CatImplClass = 0;
2186 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002187 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002188 // We always synthesize an interface for an implementation
2189 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002190 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002191 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002192
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002193 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002194 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002195 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002196 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002197 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002198 }
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002199 if (const ObjCCategoryDecl *CD =
2200 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
2201 if (CD->getIdentifier()) {
2202 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002203 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002204 return DeclPtrTy();
2205 }
2206 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002207 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002208 if (Synthesize) {
2209 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002210 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002211 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002212 IDecl = CatImplClass->getClassInterface();
2213 if (!IDecl) {
2214 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002215 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002216 }
Mike Stump1eb44332009-09-09 15:08:12 +00002217 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002218 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002219
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002220 // If category for this implementation not found, it is an error which
2221 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002222 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002223 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002224 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002225 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002226 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002227 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002228 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002229 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002230 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002231 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002232 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002233 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002234 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002235 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002236 // Check that we have a valid, previously declared ivar for @synthesize
2237 if (Synthesize) {
2238 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002239 if (!PropertyIvar)
2240 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002241 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002242 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002243 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002244 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002245 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002246 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002247 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002248 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002249 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2250 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002251 ObjCIvarDecl::Public,
2252 (Expr *)0);
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002253 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002254 IDecl->addDecl(Ivar);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002255 property->setPropertyIvarDecl(Ivar);
2256 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002257 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002258 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002259 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002260 } else if (getLangOptions().ObjCNonFragileABI &&
2261 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002262 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002263 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002264 << ClassDeclared->getDeclName();
2265 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2266 << Ivar << Ivar->getNameAsCString();
2267 // Note! I deliberately want it to fall thru so more errors are caught.
2268 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002269 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002270
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002271 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002272 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002273 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002274 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002275 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002276 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002277 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002278 }
Mike Stump1eb44332009-09-09 15:08:12 +00002279
Chris Lattnerb28317a2009-03-28 19:18:32 +00002280 // FIXME! Rules for properties are somewhat different that those
2281 // for assignments. Use a new routine to consolidate all cases;
2282 // specifically for property redeclarations as well as for ivars.
2283 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2284 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002285 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002286 lhsType->isArithmeticType()) {
2287 Diag(PropertyLoc, diag::error_property_ivar_type)
2288 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002289 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002290 }
2291 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002292 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2293 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002294 Diag(PropertyLoc, diag::error_weak_property)
2295 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002296 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002297 }
Mike Stump1eb44332009-09-09 15:08:12 +00002298 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002299 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2300 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002301 Diag(PropertyLoc, diag::error_strong_property)
2302 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002303 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002304 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002305 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002306 } else if (PropertyIvar)
2307 // @dynamic
2308 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002309 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002310 ObjCPropertyImplDecl *PIDecl =
2311 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2312 property,
2313 (Synthesize ?
2314 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002315 : ObjCPropertyImplDecl::Dynamic),
2316 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002317 if (IC) {
2318 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002319 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002320 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002321 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2322 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002323 << PropertyIvar;
2324 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2325 }
Mike Stump1eb44332009-09-09 15:08:12 +00002326
2327 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002328 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002329 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2330 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002331 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002332 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002333 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002334 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002335 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002336 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002337 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002338 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2339 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002340 << PropertyIvar;
2341 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2342 }
Mike Stump1eb44332009-09-09 15:08:12 +00002343
2344 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002345 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002346 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2347 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002348 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002349 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002350 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002351 }
Mike Stump1eb44332009-09-09 15:08:12 +00002352
Chris Lattnerb28317a2009-03-28 19:18:32 +00002353 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002354}
Anders Carlsson15281452008-11-04 16:57:32 +00002355
Chris Lattnercc98eac2008-12-17 07:13:27 +00002356bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002357 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002358 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002359
Anders Carlsson15281452008-11-04 16:57:32 +00002360 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2361 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002362
Anders Carlsson15281452008-11-04 16:57:32 +00002363 return true;
2364}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002365
Chris Lattnercc98eac2008-12-17 07:13:27 +00002366/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2367/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002368void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002369 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002370 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002371 // Check that ClassName is a valid class
2372 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2373 if (!Class) {
2374 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2375 return;
2376 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002377 if (LangOpts.ObjCNonFragileABI) {
2378 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2379 return;
2380 }
Mike Stump1eb44332009-09-09 15:08:12 +00002381
Chris Lattnercc98eac2008-12-17 07:13:27 +00002382 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002383 llvm::SmallVector<FieldDecl*, 32> RecFields;
2384 Context.CollectObjCIvars(Class, RecFields);
2385 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2386 for (unsigned i = 0; i < RecFields.size(); i++) {
2387 FieldDecl* ID = RecFields[i];
2388 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2389 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2390 ID->getIdentifier(), ID->getType(),
2391 ID->getBitWidth());
2392 Decls.push_back(Sema::DeclPtrTy::make(FD));
2393 }
Mike Stump1eb44332009-09-09 15:08:12 +00002394
Chris Lattnercc98eac2008-12-17 07:13:27 +00002395 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002396 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002397 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002398 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002399 if (getLangOptions().CPlusPlus)
2400 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002401 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002402 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002403 }
2404}
2405