blob: f627c23d76932318295bbadaacd2d2dcb360c952 [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>())) {
704 // Suggest the (potentially) correct interface name. However, don't
705 // provide a code-modification hint or use the typo name for recovery,
706 // because this is just a warning. The program may actually be correct.
707 Diag(ClassLoc, diag::warn_undef_interface_suggest)
708 << ClassName << R.getLookupName();
709 IDecl = 0;
710 } else {
711 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
712 }
Chris Lattner4d391482007-12-12 07:09:47 +0000713 }
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattner4d391482007-12-12 07:09:47 +0000715 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000716 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000717 if (SuperClassname) {
718 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000719 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000720 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000721 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
722 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000723 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000724 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000725 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000726 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000727 Diag(SuperClassLoc, diag::err_undef_superclass)
728 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000729 else if (IDecl && IDecl->getSuperClass() != SDecl) {
730 // This implementation and its interface do not have the same
731 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000732 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000733 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000734 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000735 }
736 }
737 }
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Chris Lattner4d391482007-12-12 07:09:47 +0000739 if (!IDecl) {
740 // Legacy case of @implementation with no corresponding @interface.
741 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000742
Mike Stump390b4cc2009-05-16 07:39:55 +0000743 // FIXME: Do we support attributes on the @implementation? If so we should
744 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000745 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000746 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000747 IDecl->setSuperClass(SDecl);
748 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000749
750 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000751 } else {
752 // Mark the interface as being completed, even if it was just as
753 // @class ....;
754 // declaration; the user cannot reopen it.
755 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
758 ObjCImplementationDecl* IMPDecl =
759 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000760 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Anders Carlsson15281452008-11-04 16:57:32 +0000762 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000763 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Chris Lattner4d391482007-12-12 07:09:47 +0000765 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000766 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000767 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000768 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000769 Diag(IDecl->getImplementation()->getLocation(),
770 diag::note_previous_definition);
771 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000772 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000773 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000774 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000775 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000776}
777
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000778void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
779 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000780 SourceLocation RBrace) {
781 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000782 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000783 if (!IDecl)
784 return;
785 /// Check case of non-existing @interface decl.
786 /// (legacy objective-c @implementation decl without an @interface decl).
787 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000788 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000789 IDecl->setIVarList(ivars, numIvars, Context);
790 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000791 return;
792 }
793 // If implementation has empty ivar list, just return.
794 if (numIvars == 0)
795 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Chris Lattner4d391482007-12-12 07:09:47 +0000797 assert(ivars && "missing @implementation ivars");
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Chris Lattner4d391482007-12-12 07:09:47 +0000799 // Check interface's Ivar list against those in the implementation.
800 // names and types must match.
801 //
Chris Lattner4d391482007-12-12 07:09:47 +0000802 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000803 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000804 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
805 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000806 ObjCIvarDecl* ImplIvar = ivars[j++];
807 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000808 assert (ImplIvar && "missing implementation ivar");
809 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Steve Naroffca331292009-03-03 14:49:36 +0000811 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000812 if (Context.getCanonicalType(ImplIvar->getType()) !=
813 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000814 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000815 << ImplIvar->getIdentifier()
816 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000817 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000818 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
819 Expr *ImplBitWidth = ImplIvar->getBitWidth();
820 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000821 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
822 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000823 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
824 << ImplIvar->getIdentifier();
825 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827 }
Steve Naroffca331292009-03-03 14:49:36 +0000828 // Make sure the names are identical.
829 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000830 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000831 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000832 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000833 }
834 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000835 }
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Chris Lattner609e4c72007-12-12 18:11:49 +0000837 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000838 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000839 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000840 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000841}
842
Steve Naroff3c2eb662008-02-10 21:38:56 +0000843void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
844 bool &IncompleteImpl) {
845 if (!IncompleteImpl) {
846 Diag(ImpLoc, diag::warn_incomplete_impl);
847 IncompleteImpl = true;
848 }
Chris Lattner08631c52008-11-23 21:45:46 +0000849 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000850}
851
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000852void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
853 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000854 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000855 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000856 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
857 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000858 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000859 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
860 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000861 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
862 }
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Chris Lattner3aff9192009-04-11 19:58:42 +0000864 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
865 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
866 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000867 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
868 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
869 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
870 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000871 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000872
873 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000874 << ImpMethodDecl->getDeclName() << (*IF)->getType()
875 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000876 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000877 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000878}
879
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000880/// isPropertyReadonly - Return true if property is readonly, by searching
881/// for the property in the class and in its categories and implementations
882///
883bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000884 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000885 // by far the most common case.
886 if (!PDecl->isReadOnly())
887 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000888 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000889 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000890 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000891 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000893 // Main class has the property as 'readonly'. Must search
Mike Stump1eb44332009-09-09 15:08:12 +0000894 // through the category list to see if the property's
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000895 // attribute has been over-ridden to 'readwrite'.
896 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
897 Category; Category = Category->getNextClassCategory()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000898 // Even if property is ready only, if a category has a user defined setter,
899 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000900 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000901 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000902 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000903 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000904 if (P && !P->isReadOnly())
905 return false;
906 }
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000908 // Also, check for definition of a setter method in the implementation if
909 // all else failed.
910 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000911 if (ObjCImplementationDecl *IMD =
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000912 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000913 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000914 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000915 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000916 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000917 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000918 return false;
919 }
920 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000921 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000922 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000923 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000924 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000925 // If all fails, look at the super class.
926 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
927 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000928 return true;
929}
930
Mike Stump390b4cc2009-05-16 07:39:55 +0000931/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
932/// improve the efficiency of selector lookups and type checking by associating
933/// with each protocol / interface / category the flattened instance tables. If
934/// we used an immutable set to keep the table then it wouldn't add significant
935/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000936
Steve Naroffefe7f362008-02-08 22:06:17 +0000937/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000938/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000939void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
940 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000941 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000942 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000943 const llvm::DenseSet<Selector> &ClsMap,
944 ObjCInterfaceDecl *IDecl) {
945 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000946 ObjCInterfaceDecl *NSIDecl = 0;
947 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000948 // check to see if class implements forwardInvocation method and objects
949 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000950 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000951 // Under such conditions, which means that every method possible is
952 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000953 // found" warnings.
954 // FIXME: Use a general GetUnarySelector method for this.
955 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
956 Selector fISelector = Context.Selectors.getSelector(1, &II);
957 if (InsMap.count(fISelector))
958 // Is IDecl derived from 'NSProxy'? If so, no instance methods
959 // need be implemented in the implementation.
960 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
961 }
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000963 // If a method lookup fails locally we still need to look and see if
964 // the method was implemented by a base class or an inherited
965 // protocol. This lookup is slow, but occurs rarely in correct code
966 // and otherwise would terminate in a warning.
967
Chris Lattner4d391482007-12-12 07:09:47 +0000968 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000969 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000970 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000971 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000972 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000973 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000974 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000975 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000976 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000977 // Ugly, but necessary. Method declared in protcol might have
978 // have been synthesized due to a property declared in the class which
979 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000980 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000981 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000982 if (!MethodInClass || !MethodInClass->isSynthesized())
983 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
984 }
985 }
Chris Lattner4d391482007-12-12 07:09:47 +0000986 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000987 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000988 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000989 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000990 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000991 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
992 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000993 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000994 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000995 }
Chris Lattner780f3292008-07-21 21:32:27 +0000996 // Check on this protocols's referenced protocols, recursively.
997 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
998 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000999 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001000}
1001
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001002/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1003/// or protocol against those declared in their implementations.
1004///
1005void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1006 const llvm::DenseSet<Selector> &ClsMap,
1007 llvm::DenseSet<Selector> &InsMapSeen,
1008 llvm::DenseSet<Selector> &ClsMapSeen,
1009 ObjCImplDecl* IMPDecl,
1010 ObjCContainerDecl* CDecl,
1011 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001012 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001013 // Check and see if instance methods in class interface have been
1014 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001015 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1016 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001017 if (InsMapSeen.count((*I)->getSelector()))
1018 continue;
1019 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001020 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001021 !InsMap.count((*I)->getSelector())) {
1022 if (ImmediateClass)
1023 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1024 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001025 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001026 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001027 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001028 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001029 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001030 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001031 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1032 // ImpMethodDecl may be null as in a @dynamic property.
1033 if (ImpMethodDecl)
1034 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1035 }
1036 }
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001038 // Check and see if class methods in class interface have been
1039 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001040 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001041 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001042 if (ClsMapSeen.count((*I)->getSelector()))
1043 continue;
1044 ClsMapSeen.insert((*I)->getSelector());
1045 if (!ClsMap.count((*I)->getSelector())) {
1046 if (ImmediateClass)
1047 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001048 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001049 ObjCMethodDecl *ImpMethodDecl =
1050 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001051 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001052 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001053 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1054 }
1055 }
1056 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1057 // Check for any implementation of a methods declared in protocol.
1058 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1059 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001060 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1061 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001062 (*PI), IncompleteImpl, false);
1063 if (I->getSuperClass())
1064 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001065 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001066 I->getSuperClass(), IncompleteImpl, false);
1067 }
1068}
1069
Mike Stump1eb44332009-09-09 15:08:12 +00001070void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1071 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001072 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001073 llvm::DenseSet<Selector> InsMap;
1074 // Check and see if instance methods in class interface have been
1075 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001076 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001077 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001078 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001080 // Check and see if properties declared in the interface have either 1)
1081 // an implementation or 2) there is a @synthesize/@dynamic implementation
1082 // of the property in the @implementation.
1083 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001084 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1085 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001086 ObjCPropertyDecl *Prop = (*P);
1087 if (Prop->isInvalidDecl())
1088 continue;
1089 ObjCPropertyImplDecl *PI = 0;
1090 // Is there a matching propery synthesize/dynamic?
Mike Stump1eb44332009-09-09 15:08:12 +00001091 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001092 I = IMPDecl->propimpl_begin(),
1093 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001094 if ((*I)->getPropertyDecl() == Prop) {
1095 PI = (*I);
1096 break;
1097 }
1098 if (PI)
1099 continue;
1100 if (!InsMap.count(Prop->getGetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001101 Diag(Prop->getLocation(),
1102 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001103 << Prop->getDeclName() << Prop->getGetterName();
1104 Diag(IMPDecl->getLocation(),
1105 diag::note_property_impl_required);
1106 }
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001108 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001109 Diag(Prop->getLocation(),
1110 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001111 << Prop->getDeclName() << Prop->getSetterName();
1112 Diag(IMPDecl->getLocation(),
1113 diag::note_property_impl_required);
1114 }
1115 }
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Chris Lattner4d391482007-12-12 07:09:47 +00001117 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001118 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001119 I = IMPDecl->classmeth_begin(),
1120 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001121 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001123 // Check for type conflict of methods declared in a class/protocol and
1124 // its implementation; if any.
1125 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001126 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1127 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001128 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Chris Lattner4d391482007-12-12 07:09:47 +00001130 // Check the protocol list for unimplemented methods in the @implementation
1131 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001132 // Check and see if class methods in class interface have been
1133 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Chris Lattnercddc8882009-03-01 00:56:52 +00001135 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001136 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001137 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001138 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001139 InsMap, ClsMap, I);
1140 // Check class extensions (unnamed categories)
1141 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1142 Categories; Categories = Categories->getNextClassCategory()) {
1143 if (!Categories->getIdentifier()) {
1144 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1145 break;
1146 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001147 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001148 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001149 // For extended class, unimplemented methods in its protocols will
1150 // be reported in the primary class.
1151 if (C->getIdentifier()) {
1152 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1153 E = C->protocol_end(); PI != E; ++PI)
1154 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1155 InsMap, ClsMap, C->getClassInterface());
1156 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001157 } else
1158 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001159}
1160
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001161void
1162Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1163 ObjCContainerDecl* IDecl) {
1164 // Rules apply in non-GC mode only
1165 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1166 return;
1167 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1168 E = IDecl->prop_end();
1169 I != E; ++I) {
1170 ObjCPropertyDecl *Property = (*I);
1171 unsigned Attributes = Property->getPropertyAttributes();
1172 // We only care about readwrite atomic property.
1173 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1174 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1175 continue;
1176 if (const ObjCPropertyImplDecl *PIDecl
1177 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1178 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1179 continue;
1180 ObjCMethodDecl *GetterMethod =
1181 IMPDecl->getInstanceMethod(Property->getGetterName());
1182 ObjCMethodDecl *SetterMethod =
1183 IMPDecl->getInstanceMethod(Property->getSetterName());
1184 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1185 SourceLocation MethodLoc =
1186 (GetterMethod ? GetterMethod->getLocation()
1187 : SetterMethod->getLocation());
1188 Diag(MethodLoc, diag::warn_atomic_property_rule)
1189 << Property->getIdentifier();
1190 Diag(Property->getLocation(), diag::note_property_declare);
1191 }
1192 }
1193 }
1194}
1195
Mike Stump1eb44332009-09-09 15:08:12 +00001196/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001197Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001198Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001199 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001200 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001201 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001202 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Chris Lattner4d391482007-12-12 07:09:47 +00001204 for (unsigned i = 0; i != NumElts; ++i) {
1205 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001206 NamedDecl *PrevDecl
1207 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001208 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001209 // Maybe we will complain about the shadowed template parameter.
1210 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1211 // Just pretend that we didn't see the previous declaration.
1212 PrevDecl = 0;
1213 }
1214
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001215 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001216 // GCC apparently allows the following idiom:
1217 //
1218 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1219 // @class XCElementToggler;
1220 //
Mike Stump1eb44332009-09-09 15:08:12 +00001221 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001222 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1223 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001224 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001225 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001226 } else if (TDD) {
1227 // a forward class declaration matching a typedef name of a class refers
1228 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001229 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001230 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1231 PrevDecl = OI->getDecl();
1232 }
Chris Lattner4d391482007-12-12 07:09:47 +00001233 }
Mike Stump1eb44332009-09-09 15:08:12 +00001234 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001235 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001236 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001237 IdentList[i], IdentLocs[i], true);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001238
1239 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1240 // the current DeclContext. This prevents clients that walk DeclContext
1241 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1242 // declared later (if at all). We also take care to explicitly make
1243 // sure this declaration is visible for name lookup.
1244 PushOnScopeChains(IDecl, TUScope, false);
1245 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001246 }
1247
1248 Interfaces.push_back(IDecl);
1249 }
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Ted Kremenek321c22f2009-11-18 00:28:11 +00001251 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001252 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001253 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001254 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001255 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001256 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001257 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001258}
1259
1260
1261/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1262/// returns true, or false, accordingly.
1263/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001264bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001265 const ObjCMethodDecl *PrevMethod,
1266 bool matchBasedOnSizeAndAlignment) {
1267 QualType T1 = Context.getCanonicalType(Method->getResultType());
1268 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001270 if (T1 != T2) {
1271 // The result types are different.
1272 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001273 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001274 // Incomplete types don't have a size and alignment.
1275 if (T1->isIncompleteType() || T2->isIncompleteType())
1276 return false;
1277 // Check is based on size and alignment.
1278 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1279 return false;
1280 }
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Chris Lattner89951a82009-02-20 18:43:26 +00001282 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1283 E = Method->param_end();
1284 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Chris Lattner89951a82009-02-20 18:43:26 +00001286 for (; ParamI != E; ++ParamI, ++PrevI) {
1287 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1288 T1 = Context.getCanonicalType((*ParamI)->getType());
1289 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001290 if (T1 != T2) {
1291 // The result types are different.
1292 if (!matchBasedOnSizeAndAlignment)
1293 return false;
1294 // Incomplete types don't have a size and alignment.
1295 if (T1->isIncompleteType() || T2->isIncompleteType())
1296 return false;
1297 // Check is based on size and alignment.
1298 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1299 return false;
1300 }
Chris Lattner4d391482007-12-12 07:09:47 +00001301 }
1302 return true;
1303}
1304
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001305/// \brief Read the contents of the instance and factory method pools
1306/// for a given selector from external storage.
1307///
1308/// This routine should only be called once, when neither the instance
1309/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001310Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001311 bool isInstance) {
1312 assert(ExternalSource && "We need an external AST source");
1313 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1314 "Selector data already loaded into the instance method pool");
1315 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1316 "Selector data already loaded into the factory method pool");
1317
1318 // Read the method list from the external source.
1319 std::pair<ObjCMethodList, ObjCMethodList> Methods
1320 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001322 if (isInstance) {
1323 if (Methods.second.Method)
1324 FactoryMethodPool[Sel] = Methods.second;
1325 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001326 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001327
1328 if (Methods.first.Method)
1329 InstanceMethodPool[Sel] = Methods.first;
1330
1331 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1332}
1333
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001334void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001335 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1336 = InstanceMethodPool.find(Method->getSelector());
1337 if (Pos == InstanceMethodPool.end()) {
1338 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1339 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1340 else
1341 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1342 ObjCMethodList())).first;
1343 }
1344
1345 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001346 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001347 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001348 Entry.Method = Method;
1349 Entry.Next = 0;
1350 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001351 }
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Chris Lattnerb25df352009-03-04 05:16:45 +00001353 // We've seen a method with this name, see if we have already seen this type
1354 // signature.
1355 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1356 if (MatchTwoMethodDeclarations(Method, List->Method))
1357 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Chris Lattnerb25df352009-03-04 05:16:45 +00001359 // We have a new signature for an existing method - add it.
1360 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1361 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001362}
1363
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001364// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001365ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001366 SourceRange R,
1367 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001368 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1369 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001370 if (Pos == InstanceMethodPool.end()) {
1371 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001372 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1373 else
1374 return 0;
1375 }
1376
1377 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001378 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Steve Naroff037cda52008-09-30 14:38:43 +00001380 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001381 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1382 // This checks if the methods differ by size & alignment.
1383 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001384 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001385 }
1386 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001387 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001388 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001389 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001390 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001391 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001392 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001393 }
1394 return MethList.Method;
1395}
1396
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001397void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001398 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1399 = FactoryMethodPool.find(Method->getSelector());
1400 if (Pos == FactoryMethodPool.end()) {
1401 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1402 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1403 else
1404 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1405 ObjCMethodList())).first;
1406 }
1407
1408 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001409 if (!FirstMethod.Method) {
1410 // Haven't seen a method with this selector name yet - add it.
1411 FirstMethod.Method = Method;
1412 FirstMethod.Next = 0;
1413 } else {
1414 // We've seen a method with this name, now check the type signature(s).
1415 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001416
1417 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001418 Next = Next->Next)
1419 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Chris Lattner4d391482007-12-12 07:09:47 +00001421 if (!match) {
1422 // We have a new signature for an existing method - add it.
1423 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001424 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001425 FirstMethod.Next = OMI;
1426 }
1427 }
1428}
1429
Mike Stump1eb44332009-09-09 15:08:12 +00001430ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001431 SourceRange R) {
1432 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1433 = FactoryMethodPool.find(Sel);
1434 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001435 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001436 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1437 else
1438 return 0;
1439 }
1440
1441 ObjCMethodList &MethList = Pos->second;
1442 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001444 if (MethList.Method && MethList.Next) {
1445 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1446 // This checks if the methods differ by size & alignment.
1447 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1448 issueWarning = true;
1449 }
1450 if (issueWarning && (MethList.Method && MethList.Next)) {
1451 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001452 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001453 << MethList.Method->getSourceRange();
1454 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001455 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001456 << Next->Method->getSourceRange();
1457 }
1458 return MethList.Method;
1459}
1460
Mike Stump1eb44332009-09-09 15:08:12 +00001461/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff0701bbb2009-01-08 17:28:14 +00001462/// have the property type and issue diagnostics if they don't.
1463/// Also synthesize a getter/setter method if none exist (and update the
1464/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1465/// methods is the "right" thing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001466void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001467 ObjCContainerDecl *CD) {
1468 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00001469
1470 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001471 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump1eb44332009-09-09 15:08:12 +00001472 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001473 property->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001475 if (SetterMethod) {
Fariborz Jahanian02deae82010-01-06 00:18:12 +00001476 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1477 property->getPropertyAttributes();
1478 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1479 Context.getCanonicalType(SetterMethod->getResultType()) !=
1480 Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001481 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001482 if (SetterMethod->param_size() != 1 ||
1483 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001484 Diag(property->getLocation(),
1485 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001486 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001487 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001488 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1489 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001490 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001491
1492 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001493 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001494 // FIXME: The synthesized property we set here is misleading. We almost always
1495 // synthesize these methods unless the user explicitly provided prototypes
1496 // (which is odd, but allowed). Sema should be typechecking that the
1497 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001498 if (!GetterMethod) {
1499 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001500 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001501 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001502 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1503 property->getLocation(), property->getGetterName(),
1504 property->getType(), CD, true, false, true,
1505 (property->getPropertyImplementation() ==
1506 ObjCPropertyDecl::Optional) ?
1507 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001508 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001509 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001510 } else
1511 // A user declared getter will be synthesize when @synthesize of
1512 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001513 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001514 property->setGetterMethodDecl(GetterMethod);
1515
1516 // Skip setter if property is read-only.
1517 if (!property->isReadOnly()) {
1518 // Find the default setter and if one not found, add one.
1519 if (!SetterMethod) {
1520 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001521 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001522 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001523 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1524 property->getLocation(),
1525 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001526 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001527 (property->getPropertyImplementation() ==
1528 ObjCPropertyDecl::Optional) ?
1529 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001530 ObjCMethodDecl::Required);
1531 // Invent the arguments for the setter. We don't bother making a
1532 // nice name for the argument.
1533 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001534 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001535 property->getIdentifier(),
1536 property->getType(),
John McCalla93c9342009-12-07 02:54:59 +00001537 /*TInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001538 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001539 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001540 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001541 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001542 } else
1543 // A user declared setter will be synthesize when @synthesize of
1544 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001545 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001546 property->setSetterMethodDecl(SetterMethod);
1547 }
Mike Stump1eb44332009-09-09 15:08:12 +00001548 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001549 // handle the following, which is supported by GCC (and part of the design).
1550 //
1551 // @interface Foo
1552 // @property double bar;
1553 // @end
1554 //
1555 // void thisIsUnfortunate() {
1556 // id foo;
1557 // double bar = [foo bar];
1558 // }
1559 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001560 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001561 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001562 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001563 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001564}
1565
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001566/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1567/// identical selector names in current and its super classes and issues
1568/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001569void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1570 ObjCMethodDecl *Method,
1571 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001572 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1573 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001574
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001575 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001576 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001577 SD->lookupMethod(Method->getSelector(), IsInstance);
1578 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001579 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001580 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001581 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001582 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1583 E = Method->param_end();
1584 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1585 for (; ParamI != E; ++ParamI, ++PrevI) {
1586 // Number of parameters are the same and is guaranteed by selector match.
1587 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1588 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1589 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1590 // If type of arguement of method in this class does not match its
1591 // respective argument type in the super class method, issue warning;
1592 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001593 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001594 << T1 << T2;
1595 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1596 return;
1597 }
1598 }
1599 ID = SD;
1600 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001601}
1602
Steve Naroffa56f6162007-12-18 01:30:32 +00001603// Note: For class/category implemenations, allMethods/allProperties is
1604// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001605void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1606 DeclPtrTy *allMethods, unsigned allNum,
1607 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001608 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001609 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001610
Steve Naroffa56f6162007-12-18 01:30:32 +00001611 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1612 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001613 // should be true.
1614 if (!ClassDecl)
1615 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001616
Mike Stump1eb44332009-09-09 15:08:12 +00001617 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001618 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1619 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001620 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001621
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001622 if (!isInterfaceDeclKind && AtEndLoc.isInvalid()) {
1623 AtEndLoc = ClassDecl->getLocation();
1624 Diag(AtEndLoc, diag::warn_missing_atend);
1625 }
1626
Steve Naroff0701bbb2009-01-08 17:28:14 +00001627 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001628
1629 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1630 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1631 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1632
Chris Lattner4d391482007-12-12 07:09:47 +00001633 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001634 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001635 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001636
1637 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001638 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001639 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001640 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001641 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001642 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001643 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001644 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001645 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001646 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001647 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001648 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001649 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001650 InsMap[Method->getSelector()] = Method;
1651 /// The following allows us to typecheck messages to "id".
1652 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001653 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001654 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001655 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001656 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001657 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001658 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001659 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001660 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001661 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001662 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001663 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001664 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001665 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001666 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001667 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001668 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001669 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001670 /// The following allows us to typecheck messages to "Class".
1671 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001672 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001673 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001674 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001675 }
1676 }
1677 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001678 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001679 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001680 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001681 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001682 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001683 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001684 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001685 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001686 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001687
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001688 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001689 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001690 if (C->getIdentifier() == 0)
1691 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001692 }
Steve Naroff09c47192009-01-09 15:36:25 +00001693 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1694 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1695 // user-defined setter/getter. It also synthesizes setter/getter methods
1696 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001697 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1698 E = CDecl->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001699 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001700 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001701 CDecl->setAtEndLoc(AtEndLoc);
1702 }
1703 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001704 IC->setAtEndLoc(AtEndLoc);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001705 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001706 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001707 AtomicPropertySetterGetterRules(IC, IDecl);
1708 }
Mike Stump1eb44332009-09-09 15:08:12 +00001709 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001710 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001711 CatImplClass->setAtEndLoc(AtEndLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Chris Lattner4d391482007-12-12 07:09:47 +00001713 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001714 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001715 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001716 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001717 Categories; Categories = Categories->getNextClassCategory()) {
1718 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001719 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001720 break;
1721 }
1722 }
1723 }
1724 }
Chris Lattner682bf922009-03-29 16:50:03 +00001725 if (isInterfaceDeclKind) {
1726 // Reject invalid vardecls.
1727 for (unsigned i = 0; i != tuvNum; i++) {
1728 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1729 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1730 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001731 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001732 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001733 }
Chris Lattner682bf922009-03-29 16:50:03 +00001734 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001735 }
Chris Lattner4d391482007-12-12 07:09:47 +00001736}
1737
1738
1739/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1740/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001741static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001742CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1743 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1744 if (PQTVal & ObjCDeclSpec::DQ_In)
1745 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1746 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1747 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1748 if (PQTVal & ObjCDeclSpec::DQ_Out)
1749 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1750 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1751 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1752 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1753 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1754 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1755 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001756
1757 return ret;
1758}
1759
Chris Lattnerb28317a2009-03-28 19:18:32 +00001760Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001761 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001762 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001763 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001764 Selector Sel,
1765 // optional arguments. The number of types/arguments is obtained
1766 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001767 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001768 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001769 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1770 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001771 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001772
1773 // Make sure we can establish a context for the method.
1774 if (!ClassDecl) {
1775 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanian32844b32009-08-28 17:52:37 +00001776 FunctionLabelMap.clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001777 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001778 }
Chris Lattner4d391482007-12-12 07:09:47 +00001779 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Steve Naroffccef3712009-02-20 22:59:16 +00001781 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001782 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Steve Naroffccef3712009-02-20 22:59:16 +00001784 // Methods cannot return interface types. All ObjC objects are
1785 // passed by reference.
1786 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001787 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1788 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001789 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001790 }
1791 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001792 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001793
1794 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001795 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00001796 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001797 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001798 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001799 MethodDeclKind == tok::objc_optional ?
1800 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001801 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Chris Lattner0ed844b2008-04-04 06:12:32 +00001803 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Chris Lattner7db638d2009-04-11 19:42:43 +00001805 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001806 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001807 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Chris Lattnere294d3f2009-04-11 18:57:04 +00001809 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001810 ArgType = Context.getObjCIdType();
1811 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001812 } else {
John McCall58e46772009-10-23 21:48:59 +00001813 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001814 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001815 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001816 }
Mike Stump1eb44332009-09-09 15:08:12 +00001817
John McCall58e46772009-10-23 21:48:59 +00001818 ParmVarDecl* Param
1819 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1820 ArgInfo[i].Name, ArgType, DI,
1821 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001823 if (ArgType->isObjCInterfaceType()) {
1824 Diag(ArgInfo[i].NameLoc,
1825 diag::err_object_cannot_be_passed_returned_by_value)
1826 << 1 << ArgType;
1827 Param->setInvalidDecl();
1828 }
Mike Stump1eb44332009-09-09 15:08:12 +00001829
Chris Lattner0ed844b2008-04-04 06:12:32 +00001830 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001831 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001833 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001834 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Chris Lattner0ed844b2008-04-04 06:12:32 +00001836 Params.push_back(Param);
1837 }
1838
Jay Foadbeaaccd2009-05-21 09:52:38 +00001839 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001840 ObjCMethod->setObjCDeclQualifier(
1841 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1842 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001843
1844 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001845 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001846
John McCall54abf7d2009-11-04 02:18:39 +00001847 const ObjCMethodDecl *InterfaceMD = 0;
1848
Mike Stump1eb44332009-09-09 15:08:12 +00001849 // For implementations (which can be very "coarse grain"), we add the
1850 // method now. This allows the AST to implement lookup methods that work
1851 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001852 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001853 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001854 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001855 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001856 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1857 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001858 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001859 PrevMethod = ImpDecl->getClassMethod(Sel);
1860 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001861 }
John McCall54abf7d2009-11-04 02:18:39 +00001862 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1863 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001864 if (AttrList)
1865 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001866 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001867 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001868 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001869 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1870 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001871 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001872 PrevMethod = CatImpDecl->getClassMethod(Sel);
1873 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001874 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001875 if (AttrList)
1876 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001877 }
1878 if (PrevMethod) {
1879 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001880 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001881 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001882 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001883 }
John McCall54abf7d2009-11-04 02:18:39 +00001884
1885 // If the interface declared this method, and it was deprecated there,
1886 // mark it deprecated here.
1887 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1888 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1889
Chris Lattnerb28317a2009-03-28 19:18:32 +00001890 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001891}
1892
Mike Stump1eb44332009-09-09 15:08:12 +00001893void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001894 SourceLocation Loc,
1895 unsigned &Attributes) {
1896 // FIXME: Improve the reported location.
1897
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001898 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001899 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001900 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1901 ObjCDeclSpec::DQ_PR_assign |
1902 ObjCDeclSpec::DQ_PR_copy |
1903 ObjCDeclSpec::DQ_PR_retain))) {
1904 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1905 "readwrite" :
1906 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1907 "assign" :
1908 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1909 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00001910
1911 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001912 diag::err_objc_property_attr_mutually_exclusive :
1913 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001914 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001915 }
1916
1917 // Check for copy or retain on non-object types.
1918 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001919 !PropertyTy->isObjCObjectPointerType() &&
1920 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00001921 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001922 Diag(Loc, diag::err_objc_property_requires_object)
1923 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001924 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1925 }
1926
1927 // Check for more than one of { assign, copy, retain }.
1928 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1929 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001930 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1931 << "assign" << "copy";
1932 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00001933 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001934 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001935 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1936 << "assign" << "retain";
1937 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001938 }
1939 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1940 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001941 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1942 << "copy" << "retain";
1943 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001944 }
1945 }
1946
1947 // Warn if user supplied no assignment attribute, property is
1948 // readwrite, and this is an object type.
1949 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1950 ObjCDeclSpec::DQ_PR_retain)) &&
1951 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00001952 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001953 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00001954 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001955 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1956
1957 // If non-gc code warn that this is likely inappropriate.
1958 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1959 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00001960
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001961 // FIXME: Implement warning dependent on NSCopying being
1962 // implemented. See also:
1963 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1964 // (please trim this list while you are at it).
1965 }
Mike Stump046efd92009-05-07 23:06:50 +00001966
1967 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1968 && getLangOptions().getGCMode() == LangOptions::GCOnly
1969 && PropertyTy->isBlockPointerType())
1970 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001971}
1972
Mike Stump1eb44332009-09-09 15:08:12 +00001973Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001974 FieldDeclarator &FD,
1975 ObjCDeclSpec &ODS,
1976 Selector GetterSel,
1977 Selector SetterSel,
1978 DeclPtrTy ClassCategory,
1979 bool *isOverridingProperty,
1980 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001981 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001982 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1983 // default is readwrite!
1984 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00001985 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001986 // not retain or copy
1987 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001988 (isReadWrite &&
1989 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001990 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1991 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahaniandd69aae2009-12-16 18:03:30 +00001992 if (T->isReferenceType()) {
1993 Diag(AtLoc, diag::error_reference_property);
1994 return DeclPtrTy();
1995 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001996 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001997 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001998 // May modify Attributes.
1999 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002000 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2001 if (!CDecl->getIdentifier()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002002 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002003 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002004 if ((CCPrimary = CDecl->getClassInterface())) {
2005 // Find the property in continuation class's primary class only.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002006 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002007 if (ObjCPropertyDecl *PIDecl =
2008 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002009 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00002010 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002011 unsigned PIkind = PIDecl->getPropertyAttributes();
2012 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002013 unsigned retainCopyNonatomic =
Fariborz Jahaniand669be52009-11-06 22:59:12 +00002014 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002015 ObjCPropertyDecl::OBJC_PR_copy |
2016 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002017 if ((Attributes & retainCopyNonatomic) !=
2018 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002019 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002020 Diag(PIDecl->getLocation(), diag::note_property_declare);
2021 }
Fariborz Jahanianb73e2812010-01-06 21:38:30 +00002022 DeclContext *DC = dyn_cast<DeclContext>(CCPrimary);
2023 assert(DC && "ClassDecl is not a DeclContext");
2024 DeclContext::lookup_result Found =
2025 DC->lookup(PIDecl->getDeclName());
2026 bool PropertyInPrimaryClass = false;
2027 for (; Found.first != Found.second; ++Found.first)
2028 if (isa<ObjCPropertyDecl>(*Found.first)) {
2029 PropertyInPrimaryClass = true;
2030 break;
2031 }
2032 if (!PropertyInPrimaryClass) {
2033 // Protocol is not in the primary class. Must build one for it.
2034 ObjCDeclSpec ProtocolPropertyODS;
2035 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind and
2036 // ObjCPropertyDecl::PropertyAttributeKind have identical values.
2037 // Should consolidate both into one enum type.
2038 ProtocolPropertyODS.setPropertyAttributes(
2039 (ObjCDeclSpec::ObjCPropertyAttributeKind)PIkind);
2040 DeclPtrTy ProtocolPtrTy =
2041 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
2042 PIDecl->getGetterName(),
2043 PIDecl->getSetterName(),
2044 DeclPtrTy::make(CCPrimary), isOverridingProperty,
2045 MethodImplKind);
2046 PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>();
2047 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002048 PIDecl->makeitReadWriteAttribute();
2049 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2050 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2051 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2052 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2053 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002054 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002055 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002056 << CCPrimary->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002057 Diag(PIDecl->getLocation(), diag::note_property_declare);
2058 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002059 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002060 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00002061 // class's list.
2062 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002063 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002064 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002065 // No matching property found in the primary class. Just fall thru
2066 // and add property to continuation class's primary class.
2067 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002068 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00002069 Diag(CDecl->getLocation(), diag::err_continuation_class);
2070 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002071 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002072 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002073 }
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002075 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00002076 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002077 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2078 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2079 if (T->isObjCObjectPointerType()) {
2080 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00002081 if (const ObjCInterfaceType *OIT =
John McCall183700f2009-09-21 23:43:11 +00002082 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002083 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002084 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002085 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002086 LookupProtocol(&Context.Idents.get("NSCopying")))
2087 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00002088 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002089 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002090 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002091 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00002092 if (T->isObjCInterfaceType())
2093 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002094
Steve Naroff93983f82009-01-11 12:47:58 +00002095 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2096 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002097 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00002098 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00002099 FD.D.getIdentifier(), T);
Fariborz Jahanian6dd30fc2009-12-17 00:49:09 +00002100 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
2101 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2102 Diag(PDecl->getLocation(), diag::err_duplicate_property);
2103 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2104 PDecl->setInvalidDecl();
2105 }
2106 else
2107 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002108
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002109 if (T->isArrayType() || T->isFunctionType()) {
2110 Diag(AtLoc, diag::err_property_type) << T;
2111 PDecl->setInvalidDecl();
2112 }
Mike Stump1eb44332009-09-09 15:08:12 +00002113
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002114 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00002115
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00002116 // Regardless of setter/getter attribute, we save the default getter/setter
2117 // selector names in anticipation of declaration of setter/getter methods.
2118 PDecl->setGetterName(GetterSel);
2119 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00002120
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002121 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002122 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00002123
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002124 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002125 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00002126
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002127 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002128 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00002129
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002130 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002131 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00002132
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002133 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002134 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00002135
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002136 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002137 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00002138
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002139 if (isAssign)
2140 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00002141
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002142 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002143 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00002144
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00002145 if (MethodImplKind == tok::objc_required)
2146 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2147 else if (MethodImplKind == tok::objc_optional)
2148 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002149 // A case of continuation class adding a new property in the class. This
2150 // is not what it was meant for. However, gcc supports it and so should we.
2151 // Make sure setter/getters are declared here.
2152 if (CCPrimary)
2153 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002154
Chris Lattnerb28317a2009-03-28 19:18:32 +00002155 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002156}
2157
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002158/// ActOnPropertyImplDecl - This routine performs semantic checks and
2159/// builds the AST node for a property implementation declaration; declared
2160/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002161///
Mike Stump1eb44332009-09-09 15:08:12 +00002162Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002163 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002164 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002165 DeclPtrTy ClassCatImpDecl,
2166 IdentifierInfo *PropertyId,
2167 IdentifierInfo *PropertyIvar) {
2168 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002169 // Make sure we have a context for the property implementation declaration.
2170 if (!ClassImpDecl) {
2171 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002172 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002173 }
2174 ObjCPropertyDecl *property = 0;
2175 ObjCInterfaceDecl* IDecl = 0;
2176 // Find the class or category class where this property must have
2177 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002178 ObjCImplementationDecl *IC = 0;
2179 ObjCCategoryImplDecl* CatImplClass = 0;
2180 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002181 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002182 // We always synthesize an interface for an implementation
2183 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002184 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002185 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002186
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002187 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002188 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002189 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002190 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002191 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002192 }
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002193 if (const ObjCCategoryDecl *CD =
2194 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
2195 if (CD->getIdentifier()) {
2196 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002197 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002198 return DeclPtrTy();
2199 }
2200 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002201 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002202 if (Synthesize) {
2203 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002204 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002205 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002206 IDecl = CatImplClass->getClassInterface();
2207 if (!IDecl) {
2208 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002209 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002210 }
Mike Stump1eb44332009-09-09 15:08:12 +00002211 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002212 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002213
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002214 // If category for this implementation not found, it is an error which
2215 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002216 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002217 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002218 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002219 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002220 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002221 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002222 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002223 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002224 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002225 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002226 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002227 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002228 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002229 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002230 // Check that we have a valid, previously declared ivar for @synthesize
2231 if (Synthesize) {
2232 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002233 if (!PropertyIvar)
2234 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002235 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002236 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002237 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002238 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002239 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002240 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002241 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002242 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002243 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2244 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002245 ObjCIvarDecl::Public,
2246 (Expr *)0);
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002247 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002248 IDecl->addDecl(Ivar);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002249 property->setPropertyIvarDecl(Ivar);
2250 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002251 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002252 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002253 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002254 } else if (getLangOptions().ObjCNonFragileABI &&
2255 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002256 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002257 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002258 << ClassDeclared->getDeclName();
2259 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2260 << Ivar << Ivar->getNameAsCString();
2261 // Note! I deliberately want it to fall thru so more errors are caught.
2262 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002263 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002264
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002265 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002266 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002267 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002268 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002269 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002270 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002271 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002272 }
Mike Stump1eb44332009-09-09 15:08:12 +00002273
Chris Lattnerb28317a2009-03-28 19:18:32 +00002274 // FIXME! Rules for properties are somewhat different that those
2275 // for assignments. Use a new routine to consolidate all cases;
2276 // specifically for property redeclarations as well as for ivars.
2277 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2278 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002279 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002280 lhsType->isArithmeticType()) {
2281 Diag(PropertyLoc, diag::error_property_ivar_type)
2282 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002283 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002284 }
2285 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002286 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2287 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002288 Diag(PropertyLoc, diag::error_weak_property)
2289 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002290 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002291 }
Mike Stump1eb44332009-09-09 15:08:12 +00002292 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002293 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2294 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002295 Diag(PropertyLoc, diag::error_strong_property)
2296 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002297 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002298 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002299 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002300 } else if (PropertyIvar)
2301 // @dynamic
2302 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002303 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002304 ObjCPropertyImplDecl *PIDecl =
2305 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2306 property,
2307 (Synthesize ?
2308 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002309 : ObjCPropertyImplDecl::Dynamic),
2310 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002311 if (IC) {
2312 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002313 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002314 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002315 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2316 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002317 << PropertyIvar;
2318 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2319 }
Mike Stump1eb44332009-09-09 15:08:12 +00002320
2321 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002322 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002323 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2324 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002325 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002326 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002327 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002328 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002329 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002330 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002331 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002332 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2333 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002334 << PropertyIvar;
2335 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2336 }
Mike Stump1eb44332009-09-09 15:08:12 +00002337
2338 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002339 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002340 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2341 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002342 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002343 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002344 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002345 }
Mike Stump1eb44332009-09-09 15:08:12 +00002346
Chris Lattnerb28317a2009-03-28 19:18:32 +00002347 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002348}
Anders Carlsson15281452008-11-04 16:57:32 +00002349
Chris Lattnercc98eac2008-12-17 07:13:27 +00002350bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002351 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002352 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Anders Carlsson15281452008-11-04 16:57:32 +00002354 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2355 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002356
Anders Carlsson15281452008-11-04 16:57:32 +00002357 return true;
2358}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002359
Chris Lattnercc98eac2008-12-17 07:13:27 +00002360/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2361/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002362void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002363 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002364 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002365 // Check that ClassName is a valid class
2366 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2367 if (!Class) {
2368 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2369 return;
2370 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002371 if (LangOpts.ObjCNonFragileABI) {
2372 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2373 return;
2374 }
Mike Stump1eb44332009-09-09 15:08:12 +00002375
Chris Lattnercc98eac2008-12-17 07:13:27 +00002376 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002377 llvm::SmallVector<FieldDecl*, 32> RecFields;
2378 Context.CollectObjCIvars(Class, RecFields);
2379 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2380 for (unsigned i = 0; i < RecFields.size(); i++) {
2381 FieldDecl* ID = RecFields[i];
2382 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2383 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2384 ID->getIdentifier(), ID->getType(),
2385 ID->getBitWidth());
2386 Decls.push_back(Sema::DeclPtrTy::make(FD));
2387 }
Mike Stump1eb44332009-09-09 15:08:12 +00002388
Chris Lattnercc98eac2008-12-17 07:13:27 +00002389 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002390 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002391 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002392 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002393 if (getLangOptions().CPlusPlus)
2394 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002395 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002396 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002397 }
2398}
2399