blob: fb2cc712dce9473508304e85c7684a400a0f0849 [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) {
Mike Stump1eb44332009-09-09 15:08:12 +00001476 if (Context.getCanonicalType(SetterMethod->getResultType())
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001477 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001478 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001479 if (SetterMethod->param_size() != 1 ||
1480 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001481 Diag(property->getLocation(),
1482 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001483 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001484 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001485 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1486 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001487 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001488
1489 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001490 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001491 // FIXME: The synthesized property we set here is misleading. We almost always
1492 // synthesize these methods unless the user explicitly provided prototypes
1493 // (which is odd, but allowed). Sema should be typechecking that the
1494 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001495 if (!GetterMethod) {
1496 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001497 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001498 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001499 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1500 property->getLocation(), property->getGetterName(),
1501 property->getType(), CD, true, false, true,
1502 (property->getPropertyImplementation() ==
1503 ObjCPropertyDecl::Optional) ?
1504 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001505 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001506 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001507 } else
1508 // A user declared getter will be synthesize when @synthesize of
1509 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001510 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001511 property->setGetterMethodDecl(GetterMethod);
1512
1513 // Skip setter if property is read-only.
1514 if (!property->isReadOnly()) {
1515 // Find the default setter and if one not found, add one.
1516 if (!SetterMethod) {
1517 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001518 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001519 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001520 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1521 property->getLocation(),
1522 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001523 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001524 (property->getPropertyImplementation() ==
1525 ObjCPropertyDecl::Optional) ?
1526 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001527 ObjCMethodDecl::Required);
1528 // Invent the arguments for the setter. We don't bother making a
1529 // nice name for the argument.
1530 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001531 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001532 property->getIdentifier(),
1533 property->getType(),
John McCalla93c9342009-12-07 02:54:59 +00001534 /*TInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001535 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001536 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001537 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001538 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001539 } else
1540 // A user declared setter will be synthesize when @synthesize of
1541 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001542 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001543 property->setSetterMethodDecl(SetterMethod);
1544 }
Mike Stump1eb44332009-09-09 15:08:12 +00001545 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001546 // handle the following, which is supported by GCC (and part of the design).
1547 //
1548 // @interface Foo
1549 // @property double bar;
1550 // @end
1551 //
1552 // void thisIsUnfortunate() {
1553 // id foo;
1554 // double bar = [foo bar];
1555 // }
1556 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001557 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001558 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001559 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001560 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001561}
1562
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001563/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1564/// identical selector names in current and its super classes and issues
1565/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001566void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1567 ObjCMethodDecl *Method,
1568 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001569 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1570 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001572 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001573 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001574 SD->lookupMethod(Method->getSelector(), IsInstance);
1575 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001576 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001577 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001578 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001579 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1580 E = Method->param_end();
1581 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1582 for (; ParamI != E; ++ParamI, ++PrevI) {
1583 // Number of parameters are the same and is guaranteed by selector match.
1584 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1585 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1586 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1587 // If type of arguement of method in this class does not match its
1588 // respective argument type in the super class method, issue warning;
1589 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001590 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001591 << T1 << T2;
1592 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1593 return;
1594 }
1595 }
1596 ID = SD;
1597 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001598}
1599
Steve Naroffa56f6162007-12-18 01:30:32 +00001600// Note: For class/category implemenations, allMethods/allProperties is
1601// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001602void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1603 DeclPtrTy *allMethods, unsigned allNum,
1604 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001605 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001606 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001607
Steve Naroffa56f6162007-12-18 01:30:32 +00001608 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1609 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001610 // should be true.
1611 if (!ClassDecl)
1612 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001613
Mike Stump1eb44332009-09-09 15:08:12 +00001614 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001615 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1616 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001617 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001618
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001619 if (!isInterfaceDeclKind && AtEndLoc.isInvalid()) {
1620 AtEndLoc = ClassDecl->getLocation();
1621 Diag(AtEndLoc, diag::warn_missing_atend);
1622 }
1623
Steve Naroff0701bbb2009-01-08 17:28:14 +00001624 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001625
1626 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1627 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1628 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1629
Chris Lattner4d391482007-12-12 07:09:47 +00001630 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001631 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001632 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001633
1634 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001635 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001636 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001637 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001638 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001639 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001640 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001641 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001642 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001643 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001644 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001645 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001646 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001647 InsMap[Method->getSelector()] = Method;
1648 /// The following allows us to typecheck messages to "id".
1649 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001650 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001651 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001652 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001653 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001654 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001655 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001656 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001657 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001658 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001659 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001660 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001661 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001662 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001663 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001664 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001665 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001666 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001667 /// The following allows us to typecheck messages to "Class".
1668 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001669 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001670 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001671 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001672 }
1673 }
1674 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001675 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001676 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001677 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001678 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001679 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001680 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001681 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001682 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001683 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001684
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001685 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001686 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001687 if (C->getIdentifier() == 0)
1688 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001689 }
Steve Naroff09c47192009-01-09 15:36:25 +00001690 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1691 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1692 // user-defined setter/getter. It also synthesizes setter/getter methods
1693 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001694 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1695 E = CDecl->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001696 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001697 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001698 CDecl->setAtEndLoc(AtEndLoc);
1699 }
1700 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001701 IC->setAtEndLoc(AtEndLoc);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001702 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001703 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001704 AtomicPropertySetterGetterRules(IC, IDecl);
1705 }
Mike Stump1eb44332009-09-09 15:08:12 +00001706 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001707 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001708 CatImplClass->setAtEndLoc(AtEndLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Chris Lattner4d391482007-12-12 07:09:47 +00001710 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001711 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001712 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001713 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001714 Categories; Categories = Categories->getNextClassCategory()) {
1715 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001716 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001717 break;
1718 }
1719 }
1720 }
1721 }
Chris Lattner682bf922009-03-29 16:50:03 +00001722 if (isInterfaceDeclKind) {
1723 // Reject invalid vardecls.
1724 for (unsigned i = 0; i != tuvNum; i++) {
1725 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1726 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1727 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001728 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001729 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001730 }
Chris Lattner682bf922009-03-29 16:50:03 +00001731 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001732 }
Chris Lattner4d391482007-12-12 07:09:47 +00001733}
1734
1735
1736/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1737/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001738static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001739CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1740 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1741 if (PQTVal & ObjCDeclSpec::DQ_In)
1742 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1743 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1744 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1745 if (PQTVal & ObjCDeclSpec::DQ_Out)
1746 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1747 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1748 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1749 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1750 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1751 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1752 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001753
1754 return ret;
1755}
1756
Chris Lattnerb28317a2009-03-28 19:18:32 +00001757Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001758 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001759 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001760 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001761 Selector Sel,
1762 // optional arguments. The number of types/arguments is obtained
1763 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001764 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001765 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001766 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1767 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001768 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001769
1770 // Make sure we can establish a context for the method.
1771 if (!ClassDecl) {
1772 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanian32844b32009-08-28 17:52:37 +00001773 FunctionLabelMap.clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001774 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001775 }
Chris Lattner4d391482007-12-12 07:09:47 +00001776 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Steve Naroffccef3712009-02-20 22:59:16 +00001778 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001779 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Steve Naroffccef3712009-02-20 22:59:16 +00001781 // Methods cannot return interface types. All ObjC objects are
1782 // passed by reference.
1783 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001784 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1785 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001786 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001787 }
1788 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001789 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001790
1791 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001792 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00001793 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001794 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001795 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001796 MethodDeclKind == tok::objc_optional ?
1797 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001798 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Chris Lattner0ed844b2008-04-04 06:12:32 +00001800 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Chris Lattner7db638d2009-04-11 19:42:43 +00001802 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001803 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001804 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Chris Lattnere294d3f2009-04-11 18:57:04 +00001806 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001807 ArgType = Context.getObjCIdType();
1808 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001809 } else {
John McCall58e46772009-10-23 21:48:59 +00001810 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001811 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001812 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001813 }
Mike Stump1eb44332009-09-09 15:08:12 +00001814
John McCall58e46772009-10-23 21:48:59 +00001815 ParmVarDecl* Param
1816 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1817 ArgInfo[i].Name, ArgType, DI,
1818 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001820 if (ArgType->isObjCInterfaceType()) {
1821 Diag(ArgInfo[i].NameLoc,
1822 diag::err_object_cannot_be_passed_returned_by_value)
1823 << 1 << ArgType;
1824 Param->setInvalidDecl();
1825 }
Mike Stump1eb44332009-09-09 15:08:12 +00001826
Chris Lattner0ed844b2008-04-04 06:12:32 +00001827 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001828 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001829
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001830 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001831 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Chris Lattner0ed844b2008-04-04 06:12:32 +00001833 Params.push_back(Param);
1834 }
1835
Jay Foadbeaaccd2009-05-21 09:52:38 +00001836 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001837 ObjCMethod->setObjCDeclQualifier(
1838 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1839 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001840
1841 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001842 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001843
John McCall54abf7d2009-11-04 02:18:39 +00001844 const ObjCMethodDecl *InterfaceMD = 0;
1845
Mike Stump1eb44332009-09-09 15:08:12 +00001846 // For implementations (which can be very "coarse grain"), we add the
1847 // method now. This allows the AST to implement lookup methods that work
1848 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001849 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001850 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001851 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001852 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001853 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1854 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001855 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001856 PrevMethod = ImpDecl->getClassMethod(Sel);
1857 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001858 }
John McCall54abf7d2009-11-04 02:18:39 +00001859 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1860 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001861 if (AttrList)
1862 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001863 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001864 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001865 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001866 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1867 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001868 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001869 PrevMethod = CatImpDecl->getClassMethod(Sel);
1870 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001871 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001872 if (AttrList)
1873 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001874 }
1875 if (PrevMethod) {
1876 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001877 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001878 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001879 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001880 }
John McCall54abf7d2009-11-04 02:18:39 +00001881
1882 // If the interface declared this method, and it was deprecated there,
1883 // mark it deprecated here.
1884 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1885 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1886
Chris Lattnerb28317a2009-03-28 19:18:32 +00001887 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001888}
1889
Mike Stump1eb44332009-09-09 15:08:12 +00001890void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001891 SourceLocation Loc,
1892 unsigned &Attributes) {
1893 // FIXME: Improve the reported location.
1894
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001895 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001896 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001897 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1898 ObjCDeclSpec::DQ_PR_assign |
1899 ObjCDeclSpec::DQ_PR_copy |
1900 ObjCDeclSpec::DQ_PR_retain))) {
1901 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1902 "readwrite" :
1903 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1904 "assign" :
1905 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1906 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00001907
1908 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001909 diag::err_objc_property_attr_mutually_exclusive :
1910 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001911 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001912 }
1913
1914 // Check for copy or retain on non-object types.
1915 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001916 !PropertyTy->isObjCObjectPointerType() &&
1917 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00001918 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001919 Diag(Loc, diag::err_objc_property_requires_object)
1920 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001921 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1922 }
1923
1924 // Check for more than one of { assign, copy, retain }.
1925 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1926 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001927 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1928 << "assign" << "copy";
1929 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00001930 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001931 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001932 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1933 << "assign" << "retain";
1934 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001935 }
1936 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1937 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001938 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1939 << "copy" << "retain";
1940 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001941 }
1942 }
1943
1944 // Warn if user supplied no assignment attribute, property is
1945 // readwrite, and this is an object type.
1946 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1947 ObjCDeclSpec::DQ_PR_retain)) &&
1948 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00001949 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001950 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00001951 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001952 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1953
1954 // If non-gc code warn that this is likely inappropriate.
1955 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1956 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00001957
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001958 // FIXME: Implement warning dependent on NSCopying being
1959 // implemented. See also:
1960 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1961 // (please trim this list while you are at it).
1962 }
Mike Stump046efd92009-05-07 23:06:50 +00001963
1964 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1965 && getLangOptions().getGCMode() == LangOptions::GCOnly
1966 && PropertyTy->isBlockPointerType())
1967 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001968}
1969
Mike Stump1eb44332009-09-09 15:08:12 +00001970Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001971 FieldDeclarator &FD,
1972 ObjCDeclSpec &ODS,
1973 Selector GetterSel,
1974 Selector SetterSel,
1975 DeclPtrTy ClassCategory,
1976 bool *isOverridingProperty,
1977 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001978 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001979 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1980 // default is readwrite!
1981 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00001982 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001983 // not retain or copy
1984 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001985 (isReadWrite &&
1986 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001987 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1988 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahaniandd69aae2009-12-16 18:03:30 +00001989 if (T->isReferenceType()) {
1990 Diag(AtLoc, diag::error_reference_property);
1991 return DeclPtrTy();
1992 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001993 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001994 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001995 // May modify Attributes.
1996 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001997 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1998 if (!CDecl->getIdentifier()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001999 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002000 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002001 if ((CCPrimary = CDecl->getClassInterface())) {
2002 // Find the property in continuation class's primary class only.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002003 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002004 if (ObjCPropertyDecl *PIDecl =
2005 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002006 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00002007 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002008 unsigned PIkind = PIDecl->getPropertyAttributes();
2009 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002010 unsigned retainCopyNonatomic =
Fariborz Jahaniand669be52009-11-06 22:59:12 +00002011 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002012 ObjCPropertyDecl::OBJC_PR_copy |
2013 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002014 if ((Attributes & retainCopyNonatomic) !=
2015 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002016 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002017 Diag(PIDecl->getLocation(), diag::note_property_declare);
2018 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002019 PIDecl->makeitReadWriteAttribute();
2020 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2021 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2022 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2023 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2024 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002025 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002026 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002027 << CCPrimary->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002028 Diag(PIDecl->getLocation(), diag::note_property_declare);
2029 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002030 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002031 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00002032 // class's list.
2033 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002034 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002035 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002036 // No matching property found in the primary class. Just fall thru
2037 // and add property to continuation class's primary class.
2038 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002039 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00002040 Diag(CDecl->getLocation(), diag::err_continuation_class);
2041 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002042 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002043 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002044 }
Mike Stump1eb44332009-09-09 15:08:12 +00002045
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002046 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00002047 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002048 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2049 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2050 if (T->isObjCObjectPointerType()) {
2051 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00002052 if (const ObjCInterfaceType *OIT =
John McCall183700f2009-09-21 23:43:11 +00002053 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002054 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002055 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002056 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002057 LookupProtocol(&Context.Idents.get("NSCopying")))
2058 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00002059 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002060 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002061 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002062 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00002063 if (T->isObjCInterfaceType())
2064 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Steve Naroff93983f82009-01-11 12:47:58 +00002066 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2067 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002068 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00002069 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00002070 FD.D.getIdentifier(), T);
Fariborz Jahanian6dd30fc2009-12-17 00:49:09 +00002071 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
2072 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2073 Diag(PDecl->getLocation(), diag::err_duplicate_property);
2074 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2075 PDecl->setInvalidDecl();
2076 }
2077 else
2078 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002079
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002080 if (T->isArrayType() || T->isFunctionType()) {
2081 Diag(AtLoc, diag::err_property_type) << T;
2082 PDecl->setInvalidDecl();
2083 }
Mike Stump1eb44332009-09-09 15:08:12 +00002084
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002085 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00002086
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00002087 // Regardless of setter/getter attribute, we save the default getter/setter
2088 // selector names in anticipation of declaration of setter/getter methods.
2089 PDecl->setGetterName(GetterSel);
2090 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00002091
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002092 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002093 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00002094
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002095 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002096 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00002097
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002098 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002099 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00002100
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002101 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002102 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00002103
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002104 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002105 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00002106
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002107 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002108 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00002109
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002110 if (isAssign)
2111 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00002112
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002113 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002114 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00002115
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00002116 if (MethodImplKind == tok::objc_required)
2117 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2118 else if (MethodImplKind == tok::objc_optional)
2119 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002120 // A case of continuation class adding a new property in the class. This
2121 // is not what it was meant for. However, gcc supports it and so should we.
2122 // Make sure setter/getters are declared here.
2123 if (CCPrimary)
2124 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002125
Chris Lattnerb28317a2009-03-28 19:18:32 +00002126 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002127}
2128
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002129/// ActOnPropertyImplDecl - This routine performs semantic checks and
2130/// builds the AST node for a property implementation declaration; declared
2131/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002132///
Mike Stump1eb44332009-09-09 15:08:12 +00002133Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002134 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002135 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002136 DeclPtrTy ClassCatImpDecl,
2137 IdentifierInfo *PropertyId,
2138 IdentifierInfo *PropertyIvar) {
2139 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002140 // Make sure we have a context for the property implementation declaration.
2141 if (!ClassImpDecl) {
2142 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002143 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002144 }
2145 ObjCPropertyDecl *property = 0;
2146 ObjCInterfaceDecl* IDecl = 0;
2147 // Find the class or category class where this property must have
2148 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002149 ObjCImplementationDecl *IC = 0;
2150 ObjCCategoryImplDecl* CatImplClass = 0;
2151 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002152 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002153 // We always synthesize an interface for an implementation
2154 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002155 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002156 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002157
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002158 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002159 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002160 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002161 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002162 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002163 }
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002164 if (const ObjCCategoryDecl *CD =
2165 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
2166 if (CD->getIdentifier()) {
2167 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002168 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002169 return DeclPtrTy();
2170 }
2171 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002172 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002173 if (Synthesize) {
2174 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002175 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002176 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002177 IDecl = CatImplClass->getClassInterface();
2178 if (!IDecl) {
2179 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002180 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002181 }
Mike Stump1eb44332009-09-09 15:08:12 +00002182 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002183 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002184
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002185 // If category for this implementation not found, it is an error which
2186 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002187 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002188 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002189 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002190 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002191 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002192 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002193 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002194 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002195 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002196 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002197 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002198 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002199 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002200 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002201 // Check that we have a valid, previously declared ivar for @synthesize
2202 if (Synthesize) {
2203 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002204 if (!PropertyIvar)
2205 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002206 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002207 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002208 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002209 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002210 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002211 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002212 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002213 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002214 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2215 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002216 ObjCIvarDecl::Public,
2217 (Expr *)0);
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002218 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002219 IDecl->addDecl(Ivar);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002220 property->setPropertyIvarDecl(Ivar);
2221 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002222 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002223 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002224 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002225 } else if (getLangOptions().ObjCNonFragileABI &&
2226 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002227 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002228 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002229 << ClassDeclared->getDeclName();
2230 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2231 << Ivar << Ivar->getNameAsCString();
2232 // Note! I deliberately want it to fall thru so more errors are caught.
2233 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002234 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002235
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002236 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002237 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002238 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002239 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002240 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002241 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002242 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002243 }
Mike Stump1eb44332009-09-09 15:08:12 +00002244
Chris Lattnerb28317a2009-03-28 19:18:32 +00002245 // FIXME! Rules for properties are somewhat different that those
2246 // for assignments. Use a new routine to consolidate all cases;
2247 // specifically for property redeclarations as well as for ivars.
2248 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2249 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002250 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002251 lhsType->isArithmeticType()) {
2252 Diag(PropertyLoc, diag::error_property_ivar_type)
2253 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002254 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002255 }
2256 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002257 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2258 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002259 Diag(PropertyLoc, diag::error_weak_property)
2260 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002261 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002262 }
Mike Stump1eb44332009-09-09 15:08:12 +00002263 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002264 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2265 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002266 Diag(PropertyLoc, diag::error_strong_property)
2267 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002268 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002269 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002270 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002271 } else if (PropertyIvar)
2272 // @dynamic
2273 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002274 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002275 ObjCPropertyImplDecl *PIDecl =
2276 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2277 property,
2278 (Synthesize ?
2279 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002280 : ObjCPropertyImplDecl::Dynamic),
2281 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002282 if (IC) {
2283 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002284 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002285 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002286 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2287 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002288 << PropertyIvar;
2289 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2290 }
Mike Stump1eb44332009-09-09 15:08:12 +00002291
2292 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002293 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002294 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2295 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002296 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002297 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002298 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002299 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002300 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002301 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002302 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002303 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2304 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002305 << PropertyIvar;
2306 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2307 }
Mike Stump1eb44332009-09-09 15:08:12 +00002308
2309 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002310 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002311 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2312 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002313 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002314 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002315 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002316 }
Mike Stump1eb44332009-09-09 15:08:12 +00002317
Chris Lattnerb28317a2009-03-28 19:18:32 +00002318 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002319}
Anders Carlsson15281452008-11-04 16:57:32 +00002320
Chris Lattnercc98eac2008-12-17 07:13:27 +00002321bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002322 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002323 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002324
Anders Carlsson15281452008-11-04 16:57:32 +00002325 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2326 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002327
Anders Carlsson15281452008-11-04 16:57:32 +00002328 return true;
2329}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002330
Chris Lattnercc98eac2008-12-17 07:13:27 +00002331/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2332/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002333void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002334 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002335 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002336 // Check that ClassName is a valid class
2337 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2338 if (!Class) {
2339 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2340 return;
2341 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002342 if (LangOpts.ObjCNonFragileABI) {
2343 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2344 return;
2345 }
Mike Stump1eb44332009-09-09 15:08:12 +00002346
Chris Lattnercc98eac2008-12-17 07:13:27 +00002347 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002348 llvm::SmallVector<FieldDecl*, 32> RecFields;
2349 Context.CollectObjCIvars(Class, RecFields);
2350 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2351 for (unsigned i = 0; i < RecFields.size(); i++) {
2352 FieldDecl* ID = RecFields[i];
2353 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2354 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2355 ID->getIdentifier(), ID->getType(),
2356 ID->getBitWidth());
2357 Decls.push_back(Sema::DeclPtrTy::make(FD));
2358 }
Mike Stump1eb44332009-09-09 15:08:12 +00002359
Chris Lattnercc98eac2008-12-17 07:13:27 +00002360 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002361 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002362 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002363 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002364 if (getLangOptions().CPlusPlus)
2365 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002366 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002367 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002368 }
2369}
2370