blob: 5f69dc4462e3df1add5836df1c81d35a22868ae1 [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,
Douglas Gregor18df52b2010-01-16 15:02:53 +000084 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000085 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000086 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner4d391482007-12-12 07:09:47 +000088 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +000089 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000090 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000091 // Maybe we will complain about the shadowed template parameter.
92 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
93 // Just pretend that we didn't see the previous declaration.
94 PrevDecl = 0;
95 }
96
Ted Kremeneka526c5c2008-01-07 19:49:32 +000097 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000098 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000099 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000100 }
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000102 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000103 if (IDecl) {
104 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000105 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000106 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000107 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000108 Diag(IDecl->getLocation(), diag::note_previous_definition);
109
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000110 // Return the previous class interface.
111 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000112 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000113 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000114 IDecl->setLocation(AtInterfaceLoc);
115 IDecl->setForwardDecl(false);
Steve Naroff8b26cbd2009-09-11 00:12:01 +0000116 IDecl->setClassLoc(ClassLoc);
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000117
118 // Since this ObjCInterfaceDecl was created by a forward declaration,
119 // we now add it to the DeclContext since it wasn't added before
120 // (see ActOnForwardClassDeclaration).
121 CurContext->addDecl(IDecl);
122
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000123 if (AttrList)
124 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000125 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000126 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000127 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000128 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000129 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000130 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Steve Naroffa7503a72009-04-23 15:15:40 +0000132 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000133 }
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Chris Lattner4d391482007-12-12 07:09:47 +0000135 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000136 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000137 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000138
139 if (!PrevDecl) {
140 // Try to correct for a typo in the superclass name.
141 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
142 if (CorrectTypo(R, TUScope, 0) &&
143 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
144 Diag(SuperLoc, diag::err_undef_superclass_suggest)
145 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000146 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
147 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000148 }
149 }
150
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000151 if (PrevDecl == IDecl) {
152 Diag(SuperLoc, diag::err_recursive_superclass)
153 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
154 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000155 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000156 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000157 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000158
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000159 // Diagnose classes that inherit from deprecated classes.
160 if (SuperClassDecl)
161 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000163 if (PrevDecl && SuperClassDecl == 0) {
164 // The previous declaration was not a class decl. Check if we have a
165 // typedef. If we do, get the underlying class type.
166 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
167 QualType T = TDecl->getUnderlyingType();
168 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000169 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000170 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
171 }
172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000174 // This handles the following case:
175 //
176 // typedef int SuperClass;
177 // @interface MyClass : SuperClass {} @end
178 //
179 if (!SuperClassDecl) {
180 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
181 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000182 }
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000185 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
186 if (!SuperClassDecl)
187 Diag(SuperLoc, diag::err_undef_superclass)
188 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
189 else if (SuperClassDecl->isForwardDecl())
190 Diag(SuperLoc, diag::err_undef_superclass)
191 << SuperClassDecl->getDeclName() << ClassName
192 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000193 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000194 IDecl->setSuperClass(SuperClassDecl);
195 IDecl->setSuperClassLoc(SuperLoc);
196 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000197 }
Chris Lattner4d391482007-12-12 07:09:47 +0000198 } else { // we have a root class.
199 IDecl->setLocEnd(ClassLoc);
200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000202 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000203 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000204 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000205 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000206 IDecl->setLocEnd(EndProtoLoc);
207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Anders Carlsson15281452008-11-04 16:57:32 +0000209 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000210 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000211}
212
213/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000214/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000215Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000216 IdentifierInfo *AliasName,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000217 SourceLocation AliasLocation,
218 IdentifierInfo *ClassName,
219 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000220 // Look for previous declaration of alias name
John McCallf36e02d2009-10-09 21:13:30 +0000221 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000222 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000223 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000224 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000225 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000226 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000227 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000228 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000229 }
230 // Check for class declaration
John McCallf36e02d2009-10-09 21:13:30 +0000231 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000232 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
233 QualType T = TDecl->getUnderlyingType();
234 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000235 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000236 ClassName = IDecl->getIdentifier();
John McCallf36e02d2009-10-09 21:13:30 +0000237 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000238 }
239 }
240 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000241 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
242 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000243 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000244 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000245 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000246 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000247 }
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000249 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000250 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000251 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Anders Carlsson15281452008-11-04 16:57:32 +0000253 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000254 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000255
Chris Lattnerb28317a2009-03-28 19:18:32 +0000256 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000257}
258
Steve Naroff61d68522009-03-05 15:22:01 +0000259void Sema::CheckForwardProtocolDeclarationForCircularDependency(
260 IdentifierInfo *PName,
261 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000262 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000263 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
264 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Douglas Gregor6e378de2009-04-23 23:18:26 +0000266 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000267 if (PDecl->getIdentifier() == PName) {
268 Diag(Ploc, diag::err_protocol_has_circular_dependency);
269 Diag(PrevLoc, diag::note_previous_definition);
270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000272 PDecl->getLocation(), PDecl->getReferencedProtocols());
273 }
274 }
275}
276
Chris Lattnerb28317a2009-03-28 19:18:32 +0000277Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000278Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
279 IdentifierInfo *ProtocolName,
280 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000281 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000282 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000283 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000284 SourceLocation EndProtoLoc,
285 AttributeList *AttrList) {
286 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000287 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000288 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000289 if (PDecl) {
290 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000291 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000292 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000293 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000294 // Just return the protocol we already had.
295 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000296 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000297 }
Steve Naroff61d68522009-03-05 15:22:01 +0000298 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000299 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000300 CheckForwardProtocolDeclarationForCircularDependency(
301 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
302 PList.Destroy(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Steve Narofff11b5082008-08-13 16:39:22 +0000304 // Make sure the cached decl gets a valid start location.
305 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000306 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000307 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000308 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000309 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000310 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000311 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000312 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000313 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000314 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000315 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000316 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000317 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
318 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000319 PDecl->setLocEnd(EndProtoLoc);
320 }
Mike Stump1eb44332009-09-09 15:08:12 +0000321
322 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000323 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000324}
325
326/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000327/// issues an error if they are not declared. It returns list of
328/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000329void
Chris Lattnere13b9592008-07-26 04:03:38 +0000330Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000331 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000332 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000333 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000334 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000335 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000336 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000337 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
338 LookupObjCProtocolName);
339 if (CorrectTypo(R, TUScope, 0) &&
340 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
341 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
342 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000343 Diag(PDecl->getLocation(), diag::note_previous_decl)
344 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000345 }
346 }
347
348 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000349 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000350 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000351 continue;
352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000354 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000355
356 // If this is a forward declaration and we are supposed to warn in this
357 // case, do it.
358 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000359 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000360 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000361 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000362 }
363}
364
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000365/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000366/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000367///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000368void
Mike Stump1eb44332009-09-09 15:08:12 +0000369Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000370 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000371 const IdentifierInfo *inheritedName) {
Mike Stump1eb44332009-09-09 15:08:12 +0000372 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000373 Property->getPropertyAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000374 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000375 SuperProperty->getPropertyAttributes();
376 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
377 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000378 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000379 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000380 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
381 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000382 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000383 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000384 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
385 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000386 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000387 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000389 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
390 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000391 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000392 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000393 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000394 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump1eb44332009-09-09 15:08:12 +0000395 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000396 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000397 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000398 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000399
Mike Stump1eb44332009-09-09 15:08:12 +0000400 QualType LHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000401 Context.getCanonicalType(SuperProperty->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000402 QualType RHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000403 Context.getCanonicalType(Property->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Steve Naroff15edf0d2009-03-03 15:43:24 +0000405 if (!Context.typesAreCompatible(LHSType, RHSType)) {
406 // FIXME: Incorporate this test with typesAreCompatible.
407 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff4084c302009-07-23 01:01:38 +0000408 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff15edf0d2009-03-03 15:43:24 +0000409 return;
410 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
411 << Property->getType() << SuperProperty->getType() << inheritedName;
412 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000413}
414
415/// ComparePropertiesInBaseAndSuper - This routine compares property
416/// declarations in base and its super class, if any, and issues
417/// diagnostics in a variety of inconsistant situations.
418///
Chris Lattner70f19542009-02-16 21:26:43 +0000419void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000420 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
421 if (!SDecl)
422 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000423 // FIXME: O(N^2)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000424 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
425 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000426 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000427 // Does property in super class has declaration in current class?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000428 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
429 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000430 ObjCPropertyDecl *PDecl = (*I);
431 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump1eb44332009-09-09 15:08:12 +0000432 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000433 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000434 }
435 }
436}
437
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000438/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
439/// of properties declared in a protocol and compares their attribute against
440/// the same property declared in the class or category.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000441void
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000442Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000443 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000444 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
445 if (!IDecl) {
446 // Category
447 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000448 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
Fariborz Jahanian25760612010-02-15 21:55:26 +0000449 if (!CatDecl->IsClassExtension())
450 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
451 E = PDecl->prop_end(); P != E; ++P) {
452 ObjCPropertyDecl *Pr = (*P);
453 ObjCCategoryDecl::prop_iterator CP, CE;
454 // Is this property already in category's list of properties?
455 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
456 if ((*CP)->getIdentifier() == Pr->getIdentifier())
457 break;
458 if (CP != CE)
459 // Property protocol already exist in class. Diagnose any mismatch.
460 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
461 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000462 return;
463 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000464 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
465 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000466 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000467 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000468 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000469 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000470 if ((*CP)->getIdentifier() == Pr->getIdentifier())
471 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000472 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000473 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000474 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000475 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000476}
477
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000478/// CompareProperties - This routine compares properties
479/// declared in 'ClassOrProtocol' objects (which can be a class or an
480/// inherited protocol with the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000481///
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000482void Sema::CompareProperties(Decl *CDecl,
483 DeclPtrTy ClassOrProtocol) {
484 Decl *ClassDecl = ClassOrProtocol.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000485 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
486
487 if (!IDecl) {
488 // Category
489 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000490 assert (CatDecl && "CompareProperties");
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000491 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
492 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
493 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000494 // Match properties of category with those of protocol (*P)
495 MatchOneProtocolPropertiesInClass(CatDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000497 // Go thru the list of protocols for this category and recursively match
498 // their properties with those in the category.
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000499 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
500 E = CatDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000501 CompareProperties(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000502 } else {
503 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
504 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
505 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000506 MatchOneProtocolPropertiesInClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000507 }
508 return;
509 }
510
Chris Lattnerb752f282008-07-21 07:06:49 +0000511 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000512 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
513 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000514 // Match properties of class IDecl with those of protocol (*P).
515 MatchOneProtocolPropertiesInClass(IDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000517 // Go thru the list of protocols for this class and recursively match
518 // their properties with those declared in the class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000519 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
520 E = IDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000521 CompareProperties(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000522 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000523 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
524 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
525 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000526 MatchOneProtocolPropertiesInClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000527 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000528}
529
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000530/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000531/// a class method in its extension.
532///
Mike Stump1eb44332009-09-09 15:08:12 +0000533void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000534 ObjCInterfaceDecl *ID) {
535 if (!ID)
536 return; // Possibly due to previous error
537
538 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000539 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
540 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000541 ObjCMethodDecl *MD = *i;
542 MethodMap[MD->getSelector()] = MD;
543 }
544
545 if (MethodMap.empty())
546 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000547 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
548 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000549 ObjCMethodDecl *Method = *i;
550 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
551 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
552 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
553 << Method->getDeclName();
554 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
555 }
556 }
557}
558
Chris Lattner58fe03b2009-04-12 08:43:13 +0000559/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000560Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000561Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000562 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000563 unsigned NumElts,
564 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000565 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000566 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Chris Lattner4d391482007-12-12 07:09:47 +0000568 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000569 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000570 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000571 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000572 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000573 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000574 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000575 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000576 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000577 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000578 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000579 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000580 }
Mike Stump1eb44332009-09-09 15:08:12 +0000581
582 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000583 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000584 Protocols.data(), Protocols.size(),
585 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000586 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000587 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000588 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000589}
590
Chris Lattnerb28317a2009-03-28 19:18:32 +0000591Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000592ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
593 IdentifierInfo *ClassName, SourceLocation ClassLoc,
594 IdentifierInfo *CategoryName,
595 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000596 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000597 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000598 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000599 SourceLocation EndProtoLoc) {
Fariborz Jahanian25760612010-02-15 21:55:26 +0000600 ObjCCategoryDecl *CDecl = 0;
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000601 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Fariborz Jahanian25760612010-02-15 21:55:26 +0000602 if (!CategoryName) {
603 // Class extensions require a special treatment. Use an existing one.
604 for (CDecl = IDecl->getCategoryList(); CDecl;
605 CDecl = CDecl->getNextClassCategory())
606 if (CDecl->IsClassExtension())
607 break;
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000608 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000609 if (!CDecl) {
610 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, ClassLoc,
611 CategoryLoc, CategoryName);
612 // FIXME: PushOnScopeChains?
613 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000614
Fariborz Jahanian25760612010-02-15 21:55:26 +0000615 /// Check that class of this category is already completely declared.
616 if (!IDecl || IDecl->isForwardDecl()) {
617 CDecl->setInvalidDecl();
618 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
619 return DeclPtrTy::make(CDecl);
620 }
621
622 CDecl->setClassInterface(IDecl);
623 // Insert first use of class extension to the list of class's categories.
624 if (!CategoryName)
625 CDecl->insertNextClassCategory();
626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Chris Lattner16b34b42009-02-16 21:30:01 +0000628 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000629 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000630
Fariborz Jahanian25760612010-02-15 21:55:26 +0000631 if (CategoryName) {
632 /// Check for duplicate interface declaration for this category
633 ObjCCategoryDecl *CDeclChain;
634 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
635 CDeclChain = CDeclChain->getNextClassCategory()) {
636 if (CDeclChain->getIdentifier() == CategoryName) {
637 // Class extensions can be declared multiple times.
638 Diag(CategoryLoc, diag::warn_dup_category_def)
639 << ClassName << CategoryName;
640 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
641 break;
642 }
Chris Lattner70f19542009-02-16 21:26:43 +0000643 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000644 if (!CDeclChain)
645 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000646 }
Chris Lattner70f19542009-02-16 21:26:43 +0000647
Chris Lattner4d391482007-12-12 07:09:47 +0000648 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000649 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000650 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000651 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000652 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000653 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000654 NumProtoRefs, ProtoLocs,
655 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000656 }
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Anders Carlsson15281452008-11-04 16:57:32 +0000658 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000659 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000660}
661
662/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000663/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000664/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000665Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000666 SourceLocation AtCatImplLoc,
667 IdentifierInfo *ClassName, SourceLocation ClassLoc,
668 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000669 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000670 ObjCCategoryDecl *CatIDecl = 0;
671 if (IDecl) {
672 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
673 if (!CatIDecl) {
674 // Category @implementation with no corresponding @interface.
675 // Create and install one.
676 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000677 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000678 CatName);
679 CatIDecl->setClassInterface(IDecl);
680 CatIDecl->insertNextClassCategory();
681 }
682 }
683
Mike Stump1eb44332009-09-09 15:08:12 +0000684 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000685 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
686 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000687 /// Check that class of this category is already completely declared.
688 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000689 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000690
Douglas Gregord0434102009-01-09 00:49:46 +0000691 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000692 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000693
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000694 /// Check that CatName, category name, is not used in another implementation.
695 if (CatIDecl) {
696 if (CatIDecl->getImplementation()) {
697 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
698 << CatName;
699 Diag(CatIDecl->getImplementation()->getLocation(),
700 diag::note_previous_definition);
701 } else
702 CatIDecl->setImplementation(CDecl);
703 }
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Anders Carlsson15281452008-11-04 16:57:32 +0000705 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000706 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000707}
708
Chris Lattnerb28317a2009-03-28 19:18:32 +0000709Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000710 SourceLocation AtClassImplLoc,
711 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000712 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000713 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000714 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000715 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000716 NamedDecl *PrevDecl
717 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000718 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000719 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000720 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000721 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
722 // If this is a forward declaration of an interface, warn.
723 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000724 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000725 IDecl = 0;
726 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000727 } else {
728 // We did not find anything with the name ClassName; try to correct for
729 // typos in the class name.
730 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
731 if (CorrectTypo(R, TUScope, 0) &&
732 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000733 // Suggest the (potentially) correct interface name. However, put the
734 // fix-it hint itself in a separate note, since changing the name in
735 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000736 // provide a code-modification hint or use the typo name for recovery,
737 // because this is just a warning. The program may actually be correct.
738 Diag(ClassLoc, diag::warn_undef_interface_suggest)
739 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000740 Diag(IDecl->getLocation(), diag::note_previous_decl)
741 << R.getLookupName()
742 << CodeModificationHint::CreateReplacement(ClassLoc,
743 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000744 IDecl = 0;
745 } else {
746 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
747 }
Chris Lattner4d391482007-12-12 07:09:47 +0000748 }
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Chris Lattner4d391482007-12-12 07:09:47 +0000750 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000751 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000752 if (SuperClassname) {
753 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000754 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000755 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000756 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
757 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000758 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000759 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000760 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000761 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000762 Diag(SuperClassLoc, diag::err_undef_superclass)
763 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000764 else if (IDecl && IDecl->getSuperClass() != SDecl) {
765 // This implementation and its interface do not have the same
766 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000767 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000768 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000769 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000770 }
771 }
772 }
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Chris Lattner4d391482007-12-12 07:09:47 +0000774 if (!IDecl) {
775 // Legacy case of @implementation with no corresponding @interface.
776 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000777
Mike Stump390b4cc2009-05-16 07:39:55 +0000778 // FIXME: Do we support attributes on the @implementation? If so we should
779 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000780 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000781 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000782 IDecl->setSuperClass(SDecl);
783 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000784
785 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000786 } else {
787 // Mark the interface as being completed, even if it was just as
788 // @class ....;
789 // declaration; the user cannot reopen it.
790 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000791 }
Mike Stump1eb44332009-09-09 15:08:12 +0000792
793 ObjCImplementationDecl* IMPDecl =
794 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000795 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Anders Carlsson15281452008-11-04 16:57:32 +0000797 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000798 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Chris Lattner4d391482007-12-12 07:09:47 +0000800 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000801 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000802 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000803 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000804 Diag(IDecl->getImplementation()->getLocation(),
805 diag::note_previous_definition);
806 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000807 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000808 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000809 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000810 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000811}
812
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000813void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
814 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000815 SourceLocation RBrace) {
816 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000817 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000818 if (!IDecl)
819 return;
820 /// Check case of non-existing @interface decl.
821 /// (legacy objective-c @implementation decl without an @interface decl).
822 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000823 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000824 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000825 // Add ivar's to class's DeclContext.
826 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000827 ivars[i]->setLexicalDeclContext(ImpDecl);
828 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000829 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000830 }
831
Chris Lattner4d391482007-12-12 07:09:47 +0000832 return;
833 }
834 // If implementation has empty ivar list, just return.
835 if (numIvars == 0)
836 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Chris Lattner4d391482007-12-12 07:09:47 +0000838 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000839 if (LangOpts.ObjCNonFragileABI2) {
840 if (ImpDecl->getSuperClass())
841 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
842 for (unsigned i = 0; i < numIvars; i++) {
843 ObjCIvarDecl* ImplIvar = ivars[i];
844 if (const ObjCIvarDecl *ClsIvar =
845 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
846 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
847 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
848 continue;
849 }
850 if (ImplIvar->getAccessControl() != ObjCIvarDecl::Private)
851 Diag(ImplIvar->getLocation(), diag::err_non_private_ivar_declaration);
852 // Instance ivar to Implementation's DeclContext.
853 ImplIvar->setLexicalDeclContext(ImpDecl);
854 IDecl->makeDeclVisibleInContext(ImplIvar, false);
855 ImpDecl->addDecl(ImplIvar);
856 }
857 return;
858 }
Chris Lattner4d391482007-12-12 07:09:47 +0000859 // Check interface's Ivar list against those in the implementation.
860 // names and types must match.
861 //
Chris Lattner4d391482007-12-12 07:09:47 +0000862 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000863 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000864 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
865 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000866 ObjCIvarDecl* ImplIvar = ivars[j++];
867 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000868 assert (ImplIvar && "missing implementation ivar");
869 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Steve Naroffca331292009-03-03 14:49:36 +0000871 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000872 if (Context.getCanonicalType(ImplIvar->getType()) !=
873 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000874 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000875 << ImplIvar->getIdentifier()
876 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000877 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000878 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
879 Expr *ImplBitWidth = ImplIvar->getBitWidth();
880 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000881 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
882 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000883 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
884 << ImplIvar->getIdentifier();
885 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
886 }
Mike Stump1eb44332009-09-09 15:08:12 +0000887 }
Steve Naroffca331292009-03-03 14:49:36 +0000888 // Make sure the names are identical.
889 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000890 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000891 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000892 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000893 }
894 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000895 }
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Chris Lattner609e4c72007-12-12 18:11:49 +0000897 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000898 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000899 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000900 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000901}
902
Steve Naroff3c2eb662008-02-10 21:38:56 +0000903void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
904 bool &IncompleteImpl) {
905 if (!IncompleteImpl) {
906 Diag(ImpLoc, diag::warn_incomplete_impl);
907 IncompleteImpl = true;
908 }
Chris Lattner08631c52008-11-23 21:45:46 +0000909 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000910}
911
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000912void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
913 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000914 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000915 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000916 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
917 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000918 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000919 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
920 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000921 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
922 }
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Chris Lattner3aff9192009-04-11 19:58:42 +0000924 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
925 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
926 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000927 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
928 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
929 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
930 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000931 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000932
933 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000934 << ImpMethodDecl->getDeclName() << (*IF)->getType()
935 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000936 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000937 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000938}
939
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000940/// isPropertyReadonly - Return true if property is readonly, by searching
941/// for the property in the class and in its categories and implementations
942///
943bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000944 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000945 // by far the most common case.
946 if (!PDecl->isReadOnly())
947 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000948 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000949 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000950 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000951 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000953 // Main class has the property as 'readonly'. Must search
Mike Stump1eb44332009-09-09 15:08:12 +0000954 // through the category list to see if the property's
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000955 // attribute has been over-ridden to 'readwrite'.
956 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
957 Category; Category = Category->getNextClassCategory()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000958 // Even if property is ready only, if a category has a user defined setter,
959 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000960 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000961 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000962 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000963 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000964 if (P && !P->isReadOnly())
965 return false;
966 }
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000968 // Also, check for definition of a setter method in the implementation if
969 // all else failed.
970 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000971 if (ObjCImplementationDecl *IMD =
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000972 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000973 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000974 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000975 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000976 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000977 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000978 return false;
979 }
980 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000981 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000982 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000983 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000984 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000985 // If all fails, look at the super class.
986 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
987 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000988 return true;
989}
990
Mike Stump390b4cc2009-05-16 07:39:55 +0000991/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
992/// improve the efficiency of selector lookups and type checking by associating
993/// with each protocol / interface / category the flattened instance tables. If
994/// we used an immutable set to keep the table then it wouldn't add significant
995/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000996
Steve Naroffefe7f362008-02-08 22:06:17 +0000997/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000998/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000999void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1000 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001001 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001002 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001003 const llvm::DenseSet<Selector> &ClsMap,
1004 ObjCInterfaceDecl *IDecl) {
1005 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001006 ObjCInterfaceDecl *NSIDecl = 0;
1007 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001008 // check to see if class implements forwardInvocation method and objects
1009 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001010 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001011 // Under such conditions, which means that every method possible is
1012 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001013 // found" warnings.
1014 // FIXME: Use a general GetUnarySelector method for this.
1015 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1016 Selector fISelector = Context.Selectors.getSelector(1, &II);
1017 if (InsMap.count(fISelector))
1018 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1019 // need be implemented in the implementation.
1020 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1021 }
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001023 // If a method lookup fails locally we still need to look and see if
1024 // the method was implemented by a base class or an inherited
1025 // protocol. This lookup is slow, but occurs rarely in correct code
1026 // and otherwise would terminate in a warning.
1027
Chris Lattner4d391482007-12-12 07:09:47 +00001028 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001029 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001030 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001031 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001032 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001033 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001034 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001035 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001036 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001037 // Ugly, but necessary. Method declared in protcol might have
1038 // have been synthesized due to a property declared in the class which
1039 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001040 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001041 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001042 if (!MethodInClass || !MethodInClass->isSynthesized())
1043 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
1044 }
1045 }
Chris Lattner4d391482007-12-12 07:09:47 +00001046 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001047 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001048 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001049 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001050 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001051 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1052 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001053 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +00001054 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001055 }
Chris Lattner780f3292008-07-21 21:32:27 +00001056 // Check on this protocols's referenced protocols, recursively.
1057 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1058 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001059 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001060}
1061
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001062/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1063/// or protocol against those declared in their implementations.
1064///
1065void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1066 const llvm::DenseSet<Selector> &ClsMap,
1067 llvm::DenseSet<Selector> &InsMapSeen,
1068 llvm::DenseSet<Selector> &ClsMapSeen,
1069 ObjCImplDecl* IMPDecl,
1070 ObjCContainerDecl* CDecl,
1071 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001072 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001073 // Check and see if instance methods in class interface have been
1074 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001075 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1076 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001077 if (InsMapSeen.count((*I)->getSelector()))
1078 continue;
1079 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001080 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001081 !InsMap.count((*I)->getSelector())) {
1082 if (ImmediateClass)
1083 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1084 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001085 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001086 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001087 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001088 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001089 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001090 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001091 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1092 // ImpMethodDecl may be null as in a @dynamic property.
1093 if (ImpMethodDecl)
1094 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1095 }
1096 }
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001098 // Check and see if class methods in class interface have been
1099 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001100 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001101 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001102 if (ClsMapSeen.count((*I)->getSelector()))
1103 continue;
1104 ClsMapSeen.insert((*I)->getSelector());
1105 if (!ClsMap.count((*I)->getSelector())) {
1106 if (ImmediateClass)
1107 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001108 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001109 ObjCMethodDecl *ImpMethodDecl =
1110 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001111 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001112 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001113 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1114 }
1115 }
1116 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1117 // Check for any implementation of a methods declared in protocol.
1118 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1119 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001120 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1121 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001122 (*PI), IncompleteImpl, false);
1123 if (I->getSuperClass())
1124 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001125 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001126 I->getSuperClass(), IncompleteImpl, false);
1127 }
1128}
1129
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001130/// CollectImmediateProperties - This routine collects all properties in
1131/// the class and its conforming protocols; but not those it its super class.
1132void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
1133 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1134 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1135 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1136 E = IDecl->prop_end(); P != E; ++P) {
1137 ObjCPropertyDecl *Prop = (*P);
1138 PropMap[Prop->getIdentifier()] = Prop;
1139 }
1140 // scan through class's protocols.
1141 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
1142 E = IDecl->protocol_end(); PI != E; ++PI)
1143 CollectImmediateProperties((*PI), PropMap);
1144 }
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001145 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001146 if (!CATDecl->IsClassExtension())
1147 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1148 E = CATDecl->prop_end(); P != E; ++P) {
1149 ObjCPropertyDecl *Prop = (*P);
1150 PropMap[Prop->getIdentifier()] = Prop;
1151 }
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001152 // scan through class's protocols.
1153 for (ObjCInterfaceDecl::protocol_iterator PI = CATDecl->protocol_begin(),
1154 E = CATDecl->protocol_end(); PI != E; ++PI)
1155 CollectImmediateProperties((*PI), PropMap);
1156 }
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001157 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1158 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1159 E = PDecl->prop_end(); P != E; ++P) {
1160 ObjCPropertyDecl *Prop = (*P);
1161 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1162 if (!PropEntry)
1163 PropEntry = Prop;
1164 }
1165 // scan through protocol's protocols.
1166 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1167 E = PDecl->protocol_end(); PI != E; ++PI)
1168 CollectImmediateProperties((*PI), PropMap);
1169 }
1170}
1171
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001172/// LookupPropertyDecl - Looks up a property in the current class and all
1173/// its protocols.
1174ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1175 IdentifierInfo *II) {
1176 if (const ObjCInterfaceDecl *IDecl =
1177 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1178 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1179 E = IDecl->prop_end(); P != E; ++P) {
1180 ObjCPropertyDecl *Prop = (*P);
1181 if (Prop->getIdentifier() == II)
1182 return Prop;
1183 }
1184 // scan through class's protocols.
1185 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
1186 E = IDecl->protocol_end(); PI != E; ++PI) {
1187 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1188 if (Prop)
1189 return Prop;
1190 }
1191 }
1192 else if (const ObjCProtocolDecl *PDecl =
1193 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1194 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1195 E = PDecl->prop_end(); P != E; ++P) {
1196 ObjCPropertyDecl *Prop = (*P);
1197 if (Prop->getIdentifier() == II)
1198 return Prop;
1199 }
1200 // scan through protocol's protocols.
1201 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1202 E = PDecl->protocol_end(); PI != E; ++PI) {
1203 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1204 if (Prop)
1205 return Prop;
1206 }
1207 }
1208 return 0;
1209}
1210
1211
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001212void Sema::DiagnoseUnimplementedProperties(ObjCImplDecl* IMPDecl,
1213 ObjCContainerDecl *CDecl,
1214 const llvm::DenseSet<Selector>& InsMap) {
1215 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1216 CollectImmediateProperties(CDecl, PropMap);
Fariborz Jahaniana84f2e42010-01-20 17:27:59 +00001217 if (PropMap.empty())
1218 return;
1219
1220 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1221 for (ObjCImplDecl::propimpl_iterator
1222 I = IMPDecl->propimpl_begin(),
1223 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1224 PropImplMap.insert((*I)->getPropertyDecl());
1225
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001226 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1227 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1228 ObjCPropertyDecl *Prop = P->second;
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001229 // Is there a matching propery synthesize/dynamic?
Fariborz Jahaniana84f2e42010-01-20 17:27:59 +00001230 if (Prop->isInvalidDecl() ||
1231 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1232 PropImplMap.count(Prop))
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001233 continue;
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001234 if (LangOpts.ObjCNonFragileABI2) {
1235 ActOnPropertyImplDecl(IMPDecl->getLocation(),
1236 SourceLocation(),
1237 true, DeclPtrTy::make(IMPDecl),
1238 Prop->getIdentifier(),
1239 Prop->getIdentifier());
1240 continue;
1241 }
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001242 if (!InsMap.count(Prop->getGetterName())) {
1243 Diag(Prop->getLocation(),
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001244 isa<ObjCCategoryDecl>(CDecl) ?
1245 diag::warn_setter_getter_impl_required_in_category :
1246 diag::warn_setter_getter_impl_required)
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001247 << Prop->getDeclName() << Prop->getGetterName();
1248 Diag(IMPDecl->getLocation(),
1249 diag::note_property_impl_required);
1250 }
1251
1252 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1253 Diag(Prop->getLocation(),
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001254 isa<ObjCCategoryDecl>(CDecl) ?
1255 diag::warn_setter_getter_impl_required_in_category :
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001256 diag::warn_setter_getter_impl_required)
1257 << Prop->getDeclName() << Prop->getSetterName();
1258 Diag(IMPDecl->getLocation(),
1259 diag::note_property_impl_required);
1260 }
1261 }
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001262}
1263
Mike Stump1eb44332009-09-09 15:08:12 +00001264void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1265 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001266 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001267 llvm::DenseSet<Selector> InsMap;
1268 // Check and see if instance methods in class interface have been
1269 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001270 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001271 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001272 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001274 // Check and see if properties declared in the interface have either 1)
1275 // an implementation or 2) there is a @synthesize/@dynamic implementation
1276 // of the property in the @implementation.
1277 if (isa<ObjCInterfaceDecl>(CDecl))
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001278 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
1279
Chris Lattner4d391482007-12-12 07:09:47 +00001280 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001281 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001282 I = IMPDecl->classmeth_begin(),
1283 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001284 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001286 // Check for type conflict of methods declared in a class/protocol and
1287 // its implementation; if any.
1288 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001289 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1290 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001291 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Chris Lattner4d391482007-12-12 07:09:47 +00001293 // Check the protocol list for unimplemented methods in the @implementation
1294 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001295 // Check and see if class methods in class interface have been
1296 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Chris Lattnercddc8882009-03-01 00:56:52 +00001298 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001299 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001300 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001301 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001302 InsMap, ClsMap, I);
1303 // Check class extensions (unnamed categories)
1304 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1305 Categories; Categories = Categories->getNextClassCategory()) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001306 if (Categories->IsClassExtension()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001307 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1308 break;
1309 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001310 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001311 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001312 // For extended class, unimplemented methods in its protocols will
1313 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001314 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001315 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1316 E = C->protocol_end(); PI != E; ++PI)
1317 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1318 InsMap, ClsMap, C->getClassInterface());
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001319 // Report unimplemented properties in the category as well.
1320 // When reporting on missing setter/getters, do not report when
1321 // setter/getter is implemented in category's primary class
1322 // implementation.
1323 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1324 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1325 for (ObjCImplementationDecl::instmeth_iterator
1326 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1327 InsMap.insert((*I)->getSelector());
1328 }
1329 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
1330 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001331 } else
1332 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001333}
1334
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001335void
1336Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1337 ObjCContainerDecl* IDecl) {
1338 // Rules apply in non-GC mode only
1339 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1340 return;
1341 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1342 E = IDecl->prop_end();
1343 I != E; ++I) {
1344 ObjCPropertyDecl *Property = (*I);
1345 unsigned Attributes = Property->getPropertyAttributes();
1346 // We only care about readwrite atomic property.
1347 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1348 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1349 continue;
1350 if (const ObjCPropertyImplDecl *PIDecl
1351 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1352 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1353 continue;
1354 ObjCMethodDecl *GetterMethod =
1355 IMPDecl->getInstanceMethod(Property->getGetterName());
1356 ObjCMethodDecl *SetterMethod =
1357 IMPDecl->getInstanceMethod(Property->getSetterName());
1358 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1359 SourceLocation MethodLoc =
1360 (GetterMethod ? GetterMethod->getLocation()
1361 : SetterMethod->getLocation());
1362 Diag(MethodLoc, diag::warn_atomic_property_rule)
1363 << Property->getIdentifier();
1364 Diag(Property->getLocation(), diag::note_property_declare);
1365 }
1366 }
1367 }
1368}
1369
Mike Stump1eb44332009-09-09 15:08:12 +00001370/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001371Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001372Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001373 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001374 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001375 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001376 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Chris Lattner4d391482007-12-12 07:09:47 +00001378 for (unsigned i = 0; i != NumElts; ++i) {
1379 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001380 NamedDecl *PrevDecl
1381 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001382 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001383 // Maybe we will complain about the shadowed template parameter.
1384 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1385 // Just pretend that we didn't see the previous declaration.
1386 PrevDecl = 0;
1387 }
1388
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001389 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001390 // GCC apparently allows the following idiom:
1391 //
1392 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1393 // @class XCElementToggler;
1394 //
Mike Stump1eb44332009-09-09 15:08:12 +00001395 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001396 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1397 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001398 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001399 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001400 } else if (TDD) {
1401 // a forward class declaration matching a typedef name of a class refers
1402 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001403 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001404 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1405 PrevDecl = OI->getDecl();
1406 }
Chris Lattner4d391482007-12-12 07:09:47 +00001407 }
Mike Stump1eb44332009-09-09 15:08:12 +00001408 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001409 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001410 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001411 IdentList[i], IdentLocs[i], true);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001412
1413 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1414 // the current DeclContext. This prevents clients that walk DeclContext
1415 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1416 // declared later (if at all). We also take care to explicitly make
1417 // sure this declaration is visible for name lookup.
1418 PushOnScopeChains(IDecl, TUScope, false);
1419 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001420 }
1421
1422 Interfaces.push_back(IDecl);
1423 }
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Ted Kremenek321c22f2009-11-18 00:28:11 +00001425 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001426 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001427 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001428 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001429 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001430 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001431 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001432}
1433
1434
1435/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1436/// returns true, or false, accordingly.
1437/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001438bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001439 const ObjCMethodDecl *PrevMethod,
1440 bool matchBasedOnSizeAndAlignment) {
1441 QualType T1 = Context.getCanonicalType(Method->getResultType());
1442 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001444 if (T1 != T2) {
1445 // The result types are different.
1446 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001447 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001448 // Incomplete types don't have a size and alignment.
1449 if (T1->isIncompleteType() || T2->isIncompleteType())
1450 return false;
1451 // Check is based on size and alignment.
1452 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1453 return false;
1454 }
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Chris Lattner89951a82009-02-20 18:43:26 +00001456 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1457 E = Method->param_end();
1458 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Chris Lattner89951a82009-02-20 18:43:26 +00001460 for (; ParamI != E; ++ParamI, ++PrevI) {
1461 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1462 T1 = Context.getCanonicalType((*ParamI)->getType());
1463 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001464 if (T1 != T2) {
1465 // The result types are different.
1466 if (!matchBasedOnSizeAndAlignment)
1467 return false;
1468 // Incomplete types don't have a size and alignment.
1469 if (T1->isIncompleteType() || T2->isIncompleteType())
1470 return false;
1471 // Check is based on size and alignment.
1472 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1473 return false;
1474 }
Chris Lattner4d391482007-12-12 07:09:47 +00001475 }
1476 return true;
1477}
1478
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001479/// \brief Read the contents of the instance and factory method pools
1480/// for a given selector from external storage.
1481///
1482/// This routine should only be called once, when neither the instance
1483/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001484Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001485 bool isInstance) {
1486 assert(ExternalSource && "We need an external AST source");
1487 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1488 "Selector data already loaded into the instance method pool");
1489 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1490 "Selector data already loaded into the factory method pool");
1491
1492 // Read the method list from the external source.
1493 std::pair<ObjCMethodList, ObjCMethodList> Methods
1494 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001496 if (isInstance) {
1497 if (Methods.second.Method)
1498 FactoryMethodPool[Sel] = Methods.second;
1499 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001500 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001501
1502 if (Methods.first.Method)
1503 InstanceMethodPool[Sel] = Methods.first;
1504
1505 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1506}
1507
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001508void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001509 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1510 = InstanceMethodPool.find(Method->getSelector());
1511 if (Pos == InstanceMethodPool.end()) {
1512 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1513 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1514 else
1515 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1516 ObjCMethodList())).first;
1517 }
1518
1519 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001520 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001521 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001522 Entry.Method = Method;
1523 Entry.Next = 0;
1524 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001525 }
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Chris Lattnerb25df352009-03-04 05:16:45 +00001527 // We've seen a method with this name, see if we have already seen this type
1528 // signature.
1529 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1530 if (MatchTwoMethodDeclarations(Method, List->Method))
1531 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Chris Lattnerb25df352009-03-04 05:16:45 +00001533 // We have a new signature for an existing method - add it.
1534 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001535 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1536 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001537}
1538
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001539// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001540ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001541 SourceRange R,
1542 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001543 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1544 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001545 if (Pos == InstanceMethodPool.end()) {
1546 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001547 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1548 else
1549 return 0;
1550 }
1551
1552 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001553 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Steve Naroff037cda52008-09-30 14:38:43 +00001555 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001556 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1557 // This checks if the methods differ by size & alignment.
1558 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001559 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001560 }
1561 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001562 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001563 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001564 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001565 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001566 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001567 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001568 }
1569 return MethList.Method;
1570}
1571
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001572void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001573 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1574 = FactoryMethodPool.find(Method->getSelector());
1575 if (Pos == FactoryMethodPool.end()) {
1576 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1577 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1578 else
1579 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1580 ObjCMethodList())).first;
1581 }
1582
1583 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001584 if (!FirstMethod.Method) {
1585 // Haven't seen a method with this selector name yet - add it.
1586 FirstMethod.Method = Method;
1587 FirstMethod.Next = 0;
1588 } else {
1589 // We've seen a method with this name, now check the type signature(s).
1590 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001591
1592 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001593 Next = Next->Next)
1594 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Chris Lattner4d391482007-12-12 07:09:47 +00001596 if (!match) {
1597 // We have a new signature for an existing method - add it.
1598 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001599 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1600 ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001601 FirstMethod.Next = OMI;
1602 }
1603 }
1604}
1605
Mike Stump1eb44332009-09-09 15:08:12 +00001606ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001607 SourceRange R) {
1608 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1609 = FactoryMethodPool.find(Sel);
1610 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001611 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001612 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1613 else
1614 return 0;
1615 }
1616
1617 ObjCMethodList &MethList = Pos->second;
1618 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001620 if (MethList.Method && MethList.Next) {
1621 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1622 // This checks if the methods differ by size & alignment.
1623 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1624 issueWarning = true;
1625 }
1626 if (issueWarning && (MethList.Method && MethList.Next)) {
1627 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001628 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001629 << MethList.Method->getSourceRange();
1630 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001631 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001632 << Next->Method->getSourceRange();
1633 }
1634 return MethList.Method;
1635}
1636
Mike Stump1eb44332009-09-09 15:08:12 +00001637/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff0701bbb2009-01-08 17:28:14 +00001638/// have the property type and issue diagnostics if they don't.
1639/// Also synthesize a getter/setter method if none exist (and update the
1640/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1641/// methods is the "right" thing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001642void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001643 ObjCContainerDecl *CD) {
1644 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00001645
1646 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001647 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump1eb44332009-09-09 15:08:12 +00001648 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001649 property->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001651 if (SetterMethod) {
Fariborz Jahanian02deae82010-01-06 00:18:12 +00001652 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1653 property->getPropertyAttributes();
1654 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1655 Context.getCanonicalType(SetterMethod->getResultType()) !=
1656 Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001657 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001658 if (SetterMethod->param_size() != 1 ||
1659 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001660 Diag(property->getLocation(),
1661 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001662 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001663 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001664 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1665 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001666 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001667
1668 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001669 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001670 // FIXME: The synthesized property we set here is misleading. We almost always
1671 // synthesize these methods unless the user explicitly provided prototypes
1672 // (which is odd, but allowed). Sema should be typechecking that the
1673 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001674 if (!GetterMethod) {
1675 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001676 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001677 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001678 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1679 property->getLocation(), property->getGetterName(),
1680 property->getType(), CD, true, false, true,
1681 (property->getPropertyImplementation() ==
1682 ObjCPropertyDecl::Optional) ?
1683 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001684 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001685 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001686 } else
1687 // A user declared getter will be synthesize when @synthesize of
1688 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001689 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001690 property->setGetterMethodDecl(GetterMethod);
1691
1692 // Skip setter if property is read-only.
1693 if (!property->isReadOnly()) {
1694 // Find the default setter and if one not found, add one.
1695 if (!SetterMethod) {
1696 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001697 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001698 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001699 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1700 property->getLocation(),
1701 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001702 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001703 (property->getPropertyImplementation() ==
1704 ObjCPropertyDecl::Optional) ?
1705 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001706 ObjCMethodDecl::Required);
1707 // Invent the arguments for the setter. We don't bother making a
1708 // nice name for the argument.
1709 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001710 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001711 property->getIdentifier(),
1712 property->getType(),
John McCalla93c9342009-12-07 02:54:59 +00001713 /*TInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001714 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001715 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001716 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001717 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001718 } else
1719 // A user declared setter will be synthesize when @synthesize of
1720 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001721 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001722 property->setSetterMethodDecl(SetterMethod);
1723 }
Mike Stump1eb44332009-09-09 15:08:12 +00001724 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001725 // handle the following, which is supported by GCC (and part of the design).
1726 //
1727 // @interface Foo
1728 // @property double bar;
1729 // @end
1730 //
1731 // void thisIsUnfortunate() {
1732 // id foo;
1733 // double bar = [foo bar];
1734 // }
1735 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001736 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001737 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001738 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001739 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001740}
1741
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001742/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1743/// identical selector names in current and its super classes and issues
1744/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001745void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1746 ObjCMethodDecl *Method,
1747 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001748 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1749 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001751 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001752 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001753 SD->lookupMethod(Method->getSelector(), IsInstance);
1754 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001755 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001756 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001757 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001758 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1759 E = Method->param_end();
1760 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1761 for (; ParamI != E; ++ParamI, ++PrevI) {
1762 // Number of parameters are the same and is guaranteed by selector match.
1763 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1764 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1765 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1766 // If type of arguement of method in this class does not match its
1767 // respective argument type in the super class method, issue warning;
1768 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001769 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001770 << T1 << T2;
1771 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1772 return;
1773 }
1774 }
1775 ID = SD;
1776 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001777}
1778
Steve Naroffa56f6162007-12-18 01:30:32 +00001779// Note: For class/category implemenations, allMethods/allProperties is
1780// always null.
Ted Kremenek782f2f52010-01-07 01:20:12 +00001781void Sema::ActOnAtEnd(SourceRange AtEnd,
1782 DeclPtrTy classDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001783 DeclPtrTy *allMethods, unsigned allNum,
1784 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001785 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001786 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001787
Steve Naroffa56f6162007-12-18 01:30:32 +00001788 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1789 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001790 // should be true.
1791 if (!ClassDecl)
1792 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001793
Mike Stump1eb44332009-09-09 15:08:12 +00001794 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001795 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1796 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001797 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001798
Ted Kremenek782f2f52010-01-07 01:20:12 +00001799 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1800 // FIXME: This is wrong. We shouldn't be pretending that there is
1801 // an '@end' in the declaration.
1802 SourceLocation L = ClassDecl->getLocation();
1803 AtEnd.setBegin(L);
1804 AtEnd.setEnd(L);
1805 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001806 }
1807
Steve Naroff0701bbb2009-01-08 17:28:14 +00001808 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001809
1810 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1811 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1812 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1813
Chris Lattner4d391482007-12-12 07:09:47 +00001814 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001815 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001816 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001817
1818 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001819 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001820 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001821 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001822 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001823 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001824 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001825 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001826 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001827 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001828 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001829 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001830 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001831 InsMap[Method->getSelector()] = Method;
1832 /// The following allows us to typecheck messages to "id".
1833 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001834 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001835 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001836 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001837 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001838 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001839 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001840 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001841 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001842 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001843 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001844 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001845 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001846 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001847 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001848 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001849 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001850 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001851 /// The following allows us to typecheck messages to "Class".
1852 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001853 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001854 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001855 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001856 }
1857 }
1858 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001859 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001860 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001861 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001862 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001863 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001864 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001865 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001866 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001867 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001868
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001869 // Compare protocol properties with those in category
1870 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian25760612010-02-15 21:55:26 +00001871 if (C->IsClassExtension())
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001872 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001873 }
Steve Naroff09c47192009-01-09 15:36:25 +00001874 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001875 if (CDecl->getIdentifier())
1876 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1877 // user-defined setter/getter. It also synthesizes setter/getter methods
1878 // and adds them to the DeclContext and global method pools.
1879 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1880 E = CDecl->prop_end();
1881 I != E; ++I)
1882 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001883 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001884 }
1885 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001886 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001887 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001888 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001889 AtomicPropertySetterGetterRules(IC, IDecl);
1890 }
Mike Stump1eb44332009-09-09 15:08:12 +00001891 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001892 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001893 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Chris Lattner4d391482007-12-12 07:09:47 +00001895 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001896 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001897 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001898 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001899 Categories; Categories = Categories->getNextClassCategory()) {
1900 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001901 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001902 break;
1903 }
1904 }
1905 }
1906 }
Chris Lattner682bf922009-03-29 16:50:03 +00001907 if (isInterfaceDeclKind) {
1908 // Reject invalid vardecls.
1909 for (unsigned i = 0; i != tuvNum; i++) {
1910 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1911 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1912 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001913 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001914 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001915 }
Chris Lattner682bf922009-03-29 16:50:03 +00001916 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001917 }
Chris Lattner4d391482007-12-12 07:09:47 +00001918}
1919
1920
1921/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1922/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001923static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001924CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1925 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1926 if (PQTVal & ObjCDeclSpec::DQ_In)
1927 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1928 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1929 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1930 if (PQTVal & ObjCDeclSpec::DQ_Out)
1931 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1932 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1933 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1934 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1935 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1936 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1937 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001938
1939 return ret;
1940}
1941
Chris Lattnerb28317a2009-03-28 19:18:32 +00001942Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001943 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001944 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001945 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001946 Selector Sel,
1947 // optional arguments. The number of types/arguments is obtained
1948 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001949 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001950 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001951 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1952 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001953 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001954
1955 // Make sure we can establish a context for the method.
1956 if (!ClassDecl) {
1957 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanian32844b32009-08-28 17:52:37 +00001958 FunctionLabelMap.clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001959 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001960 }
Chris Lattner4d391482007-12-12 07:09:47 +00001961 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001962
Steve Naroffccef3712009-02-20 22:59:16 +00001963 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001964 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Steve Naroffccef3712009-02-20 22:59:16 +00001966 // Methods cannot return interface types. All ObjC objects are
1967 // passed by reference.
1968 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001969 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1970 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001971 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001972 }
1973 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001974 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001975
1976 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001977 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00001978 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001979 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001980 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001981 MethodDeclKind == tok::objc_optional ?
1982 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001983 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Chris Lattner0ed844b2008-04-04 06:12:32 +00001985 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001986
Chris Lattner7db638d2009-04-11 19:42:43 +00001987 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001988 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001989 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Chris Lattnere294d3f2009-04-11 18:57:04 +00001991 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001992 ArgType = Context.getObjCIdType();
1993 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001994 } else {
John McCall58e46772009-10-23 21:48:59 +00001995 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001996 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001997 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001998 }
Mike Stump1eb44332009-09-09 15:08:12 +00001999
John McCall58e46772009-10-23 21:48:59 +00002000 ParmVarDecl* Param
2001 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
2002 ArgInfo[i].Name, ArgType, DI,
2003 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002005 if (ArgType->isObjCInterfaceType()) {
2006 Diag(ArgInfo[i].NameLoc,
2007 diag::err_object_cannot_be_passed_returned_by_value)
2008 << 1 << ArgType;
2009 Param->setInvalidDecl();
2010 }
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Chris Lattner0ed844b2008-04-04 06:12:32 +00002012 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002013 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002015 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002016 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Chris Lattner0ed844b2008-04-04 06:12:32 +00002018 Params.push_back(Param);
2019 }
2020
Jay Foadbeaaccd2009-05-21 09:52:38 +00002021 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002022 ObjCMethod->setObjCDeclQualifier(
2023 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
2024 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00002025
2026 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002027 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002028
John McCall54abf7d2009-11-04 02:18:39 +00002029 const ObjCMethodDecl *InterfaceMD = 0;
2030
Mike Stump1eb44332009-09-09 15:08:12 +00002031 // For implementations (which can be very "coarse grain"), we add the
2032 // method now. This allows the AST to implement lookup methods that work
2033 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00002034 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002035 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002036 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002037 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002038 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2039 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002040 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002041 PrevMethod = ImpDecl->getClassMethod(Sel);
2042 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002043 }
John McCall54abf7d2009-11-04 02:18:39 +00002044 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
2045 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002046 if (AttrList)
2047 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00002048 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002049 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002050 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002051 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
2052 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002053 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002054 PrevMethod = CatImpDecl->getClassMethod(Sel);
2055 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002056 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002057 if (AttrList)
2058 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00002059 }
2060 if (PrevMethod) {
2061 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002062 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002063 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002064 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002065 }
John McCall54abf7d2009-11-04 02:18:39 +00002066
2067 // If the interface declared this method, and it was deprecated there,
2068 // mark it deprecated here.
2069 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
2070 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
2071
Chris Lattnerb28317a2009-03-28 19:18:32 +00002072 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002073}
2074
Mike Stump1eb44332009-09-09 15:08:12 +00002075void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002076 SourceLocation Loc,
2077 unsigned &Attributes) {
2078 // FIXME: Improve the reported location.
2079
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002080 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002081 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002082 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
2083 ObjCDeclSpec::DQ_PR_assign |
2084 ObjCDeclSpec::DQ_PR_copy |
2085 ObjCDeclSpec::DQ_PR_retain))) {
2086 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
2087 "readwrite" :
2088 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
2089 "assign" :
2090 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
2091 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00002092
2093 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00002094 diag::err_objc_property_attr_mutually_exclusive :
2095 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002096 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002097 }
2098
2099 // Check for copy or retain on non-object types.
2100 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00002101 !PropertyTy->isObjCObjectPointerType() &&
2102 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00002103 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002104 Diag(Loc, diag::err_objc_property_requires_object)
2105 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002106 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
2107 }
2108
2109 // Check for more than one of { assign, copy, retain }.
2110 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2111 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002112 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2113 << "assign" << "copy";
2114 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00002115 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002116 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002117 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2118 << "assign" << "retain";
2119 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002120 }
2121 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2122 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002123 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2124 << "copy" << "retain";
2125 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002126 }
2127 }
2128
2129 // Warn if user supplied no assignment attribute, property is
2130 // readwrite, and this is an object type.
2131 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
2132 ObjCDeclSpec::DQ_PR_retain)) &&
2133 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00002134 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002135 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00002136 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002137 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
2138
2139 // If non-gc code warn that this is likely inappropriate.
2140 if (getLangOptions().getGCMode() == LangOptions::NonGC)
2141 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002142
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002143 // FIXME: Implement warning dependent on NSCopying being
2144 // implemented. See also:
2145 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2146 // (please trim this list while you are at it).
2147 }
Mike Stump046efd92009-05-07 23:06:50 +00002148
2149 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2150 && getLangOptions().getGCMode() == LangOptions::GCOnly
2151 && PropertyTy->isBlockPointerType())
2152 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002153}
2154
Mike Stump1eb44332009-09-09 15:08:12 +00002155Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002156 FieldDeclarator &FD,
2157 ObjCDeclSpec &ODS,
2158 Selector GetterSel,
2159 Selector SetterSel,
2160 DeclPtrTy ClassCategory,
2161 bool *isOverridingProperty,
2162 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002163 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002164 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
2165 // default is readwrite!
2166 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00002167 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002168 // not retain or copy
2169 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00002170 (isReadWrite &&
2171 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002172 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
2173 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahaniandd69aae2009-12-16 18:03:30 +00002174 if (T->isReferenceType()) {
2175 Diag(AtLoc, diag::error_reference_property);
2176 return DeclPtrTy();
2177 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002178 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002179 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002180 // May modify Attributes.
2181 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002182 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanian25760612010-02-15 21:55:26 +00002183 if (CDecl->IsClassExtension()) {
2184 // Diagnose if this property is already in continuation class.
2185 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2186 assert(DC && "ClassDecl is not a DeclContext");
2187 DeclContext::lookup_result Found = DC->lookup(FD.D.getIdentifier());
2188 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2189 Diag(AtLoc, diag::err_duplicate_property);
2190 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2191 return DeclPtrTy();
2192 }
2193 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
2194 FD.D.getIdentifierLoc(),
2195 FD.D.getIdentifier(),
2196 AtLoc, T);
2197 DC->addDecl(PDecl);
2198
Mike Stump1eb44332009-09-09 15:08:12 +00002199 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002200 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002201 if ((CCPrimary = CDecl->getClassInterface())) {
2202 // Find the property in continuation class's primary class only.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002203 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002204 if (ObjCPropertyDecl *PIDecl =
2205 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002206 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00002207 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002208 unsigned PIkind = PIDecl->getPropertyAttributes();
2209 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002210 unsigned retainCopyNonatomic =
Fariborz Jahaniand669be52009-11-06 22:59:12 +00002211 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002212 ObjCPropertyDecl::OBJC_PR_copy |
2213 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002214 if ((Attributes & retainCopyNonatomic) !=
2215 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002216 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002217 Diag(PIDecl->getLocation(), diag::note_property_declare);
2218 }
Fariborz Jahanianb73e2812010-01-06 21:38:30 +00002219 DeclContext *DC = dyn_cast<DeclContext>(CCPrimary);
2220 assert(DC && "ClassDecl is not a DeclContext");
2221 DeclContext::lookup_result Found =
2222 DC->lookup(PIDecl->getDeclName());
2223 bool PropertyInPrimaryClass = false;
2224 for (; Found.first != Found.second; ++Found.first)
2225 if (isa<ObjCPropertyDecl>(*Found.first)) {
2226 PropertyInPrimaryClass = true;
2227 break;
2228 }
2229 if (!PropertyInPrimaryClass) {
2230 // Protocol is not in the primary class. Must build one for it.
2231 ObjCDeclSpec ProtocolPropertyODS;
2232 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind and
2233 // ObjCPropertyDecl::PropertyAttributeKind have identical values.
2234 // Should consolidate both into one enum type.
2235 ProtocolPropertyODS.setPropertyAttributes(
2236 (ObjCDeclSpec::ObjCPropertyAttributeKind)PIkind);
2237 DeclPtrTy ProtocolPtrTy =
2238 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
2239 PIDecl->getGetterName(),
2240 PIDecl->getSetterName(),
2241 DeclPtrTy::make(CCPrimary), isOverridingProperty,
2242 MethodImplKind);
2243 PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>();
2244 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002245 PIDecl->makeitReadWriteAttribute();
2246 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2247 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2248 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2249 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2250 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002251 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002252 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002253 << CCPrimary->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002254 Diag(PIDecl->getLocation(), diag::note_property_declare);
2255 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002256 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002257 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00002258 // class's list.
2259 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002260 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002261 }
Fariborz Jahanian25760612010-02-15 21:55:26 +00002262
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002263 // No matching property found in the primary class. Just fall thru
2264 // and add property to continuation class's primary class.
2265 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002266 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00002267 Diag(CDecl->getLocation(), diag::err_continuation_class);
2268 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002269 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002270 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002271 }
Mike Stump1eb44332009-09-09 15:08:12 +00002272
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002273 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00002274 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002275 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2276 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2277 if (T->isObjCObjectPointerType()) {
2278 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00002279 if (const ObjCInterfaceType *OIT =
John McCall183700f2009-09-21 23:43:11 +00002280 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002281 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002282 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002283 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002284 LookupProtocol(&Context.Idents.get("NSCopying")))
2285 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00002286 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002287 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002288 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002289 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00002290 if (T->isObjCInterfaceType())
2291 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002292
Steve Naroff93983f82009-01-11 12:47:58 +00002293 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2294 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002295 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00002296 FD.D.getIdentifierLoc(),
Fariborz Jahaniand0502402010-01-21 17:36:00 +00002297 FD.D.getIdentifier(),
2298 AtLoc, T);
Fariborz Jahanian6dd30fc2009-12-17 00:49:09 +00002299 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
2300 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2301 Diag(PDecl->getLocation(), diag::err_duplicate_property);
2302 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2303 PDecl->setInvalidDecl();
2304 }
2305 else
2306 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002307
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002308 if (T->isArrayType() || T->isFunctionType()) {
2309 Diag(AtLoc, diag::err_property_type) << T;
2310 PDecl->setInvalidDecl();
2311 }
Mike Stump1eb44332009-09-09 15:08:12 +00002312
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002313 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00002314
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00002315 // Regardless of setter/getter attribute, we save the default getter/setter
2316 // selector names in anticipation of declaration of setter/getter methods.
2317 PDecl->setGetterName(GetterSel);
2318 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00002319
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002320 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002321 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00002322
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002323 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002324 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00002325
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002326 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002327 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00002328
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002329 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002330 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002332 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002333 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00002334
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002335 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002336 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00002337
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002338 if (isAssign)
2339 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00002340
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002341 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002342 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00002343
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00002344 if (MethodImplKind == tok::objc_required)
2345 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2346 else if (MethodImplKind == tok::objc_optional)
2347 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002348 // A case of continuation class adding a new property in the class. This
2349 // is not what it was meant for. However, gcc supports it and so should we.
2350 // Make sure setter/getters are declared here.
2351 if (CCPrimary)
2352 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Chris Lattnerb28317a2009-03-28 19:18:32 +00002354 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002355}
2356
Fariborz Jahanian77e2dde2010-02-09 21:49:50 +00002357ObjCIvarDecl*
2358Sema::SynthesizeNewPropertyIvar(ObjCInterfaceDecl *IDecl,
2359 IdentifierInfo *NameII) {
2360 ObjCIvarDecl *Ivar = 0;
2361 ObjCPropertyDecl *Prop = LookupPropertyDecl(IDecl, NameII);
2362 if (Prop && !Prop->isInvalidDecl()) {
2363 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
2364 QualType PropType = Context.getCanonicalType(Prop->getType());
2365 assert(EnclosingContext &&
2366 "null DeclContext for synthesized ivar - SynthesizeNewPropertyIvar");
2367 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext,
2368 Prop->getLocation(),
2369 NameII, PropType, /*Dinfo=*/0,
2370 ObjCIvarDecl::Public,
2371 (Expr *)0);
2372 Ivar->setLexicalDeclContext(IDecl);
2373 IDecl->addDecl(Ivar);
2374 Prop->setPropertyIvarDecl(Ivar);
2375 }
2376 return Ivar;
2377}
2378
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002379/// ActOnPropertyImplDecl - This routine performs semantic checks and
2380/// builds the AST node for a property implementation declaration; declared
2381/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002382///
Mike Stump1eb44332009-09-09 15:08:12 +00002383Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002384 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002385 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002386 DeclPtrTy ClassCatImpDecl,
2387 IdentifierInfo *PropertyId,
2388 IdentifierInfo *PropertyIvar) {
2389 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002390 // Make sure we have a context for the property implementation declaration.
2391 if (!ClassImpDecl) {
2392 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002393 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002394 }
2395 ObjCPropertyDecl *property = 0;
2396 ObjCInterfaceDecl* IDecl = 0;
2397 // Find the class or category class where this property must have
2398 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002399 ObjCImplementationDecl *IC = 0;
2400 ObjCCategoryImplDecl* CatImplClass = 0;
2401 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002402 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002403 // We always synthesize an interface for an implementation
2404 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002405 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002406 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002407
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002408 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002409 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002410 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002411 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002412 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002413 }
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002414 if (const ObjCCategoryDecl *CD =
2415 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002416 if (!CD->IsClassExtension()) {
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002417 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002418 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002419 return DeclPtrTy();
2420 }
2421 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002422 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002423 if (Synthesize) {
2424 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002425 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002426 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002427 IDecl = CatImplClass->getClassInterface();
2428 if (!IDecl) {
2429 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002430 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002431 }
Mike Stump1eb44332009-09-09 15:08:12 +00002432 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002433 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002434
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002435 // If category for this implementation not found, it is an error which
2436 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002437 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002438 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002439 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002440 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002441 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002442 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002443 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002444 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002445 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002446 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002447 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002448 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002449 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002450 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002451 // Check that we have a valid, previously declared ivar for @synthesize
2452 if (Synthesize) {
2453 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002454 if (!PropertyIvar)
2455 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002456 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002457 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002458 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002459 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002460 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002461 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002462 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002463 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002464 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2465 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002466 ObjCIvarDecl::Public,
2467 (Expr *)0);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00002468 IDecl->makeDeclVisibleInContext(Ivar, false);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002469 property->setPropertyIvarDecl(Ivar);
2470 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002471 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002472 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002473 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002474 } else if (getLangOptions().ObjCNonFragileABI &&
2475 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002476 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002477 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002478 << ClassDeclared->getDeclName();
2479 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2480 << Ivar << Ivar->getNameAsCString();
2481 // Note! I deliberately want it to fall thru so more errors are caught.
2482 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002483 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002484
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002485 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002486 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002487 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002488 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002489 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002490 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002491 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002492 }
Mike Stump1eb44332009-09-09 15:08:12 +00002493
Chris Lattnerb28317a2009-03-28 19:18:32 +00002494 // FIXME! Rules for properties are somewhat different that those
2495 // for assignments. Use a new routine to consolidate all cases;
2496 // specifically for property redeclarations as well as for ivars.
2497 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2498 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002499 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002500 lhsType->isArithmeticType()) {
2501 Diag(PropertyLoc, diag::error_property_ivar_type)
2502 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002503 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002504 }
2505 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002506 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2507 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002508 Diag(PropertyLoc, diag::error_weak_property)
2509 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002510 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002511 }
Mike Stump1eb44332009-09-09 15:08:12 +00002512 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002513 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2514 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002515 Diag(PropertyLoc, diag::error_strong_property)
2516 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002517 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002518 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002519 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002520 } else if (PropertyIvar)
2521 // @dynamic
2522 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002523 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002524 ObjCPropertyImplDecl *PIDecl =
2525 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2526 property,
2527 (Synthesize ?
2528 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002529 : ObjCPropertyImplDecl::Dynamic),
2530 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002531 if (IC) {
2532 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002533 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002534 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002535 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2536 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002537 << PropertyIvar;
2538 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2539 }
Mike Stump1eb44332009-09-09 15:08:12 +00002540
2541 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002542 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002543 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2544 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002545 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002546 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002547 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002548 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002549 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002550 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002551 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002552 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2553 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002554 << PropertyIvar;
2555 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2556 }
Mike Stump1eb44332009-09-09 15:08:12 +00002557
2558 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002559 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002560 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2561 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002562 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002563 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002564 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002565 }
Mike Stump1eb44332009-09-09 15:08:12 +00002566
Chris Lattnerb28317a2009-03-28 19:18:32 +00002567 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002568}
Anders Carlsson15281452008-11-04 16:57:32 +00002569
Chris Lattnercc98eac2008-12-17 07:13:27 +00002570bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002571 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002572 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002573
Anders Carlsson15281452008-11-04 16:57:32 +00002574 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2575 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002576
Anders Carlsson15281452008-11-04 16:57:32 +00002577 return true;
2578}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002579
Chris Lattnercc98eac2008-12-17 07:13:27 +00002580/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2581/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002582void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002583 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002584 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002585 // Check that ClassName is a valid class
2586 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2587 if (!Class) {
2588 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2589 return;
2590 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002591 if (LangOpts.ObjCNonFragileABI) {
2592 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2593 return;
2594 }
Mike Stump1eb44332009-09-09 15:08:12 +00002595
Chris Lattnercc98eac2008-12-17 07:13:27 +00002596 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002597 llvm::SmallVector<FieldDecl*, 32> RecFields;
2598 Context.CollectObjCIvars(Class, RecFields);
2599 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2600 for (unsigned i = 0; i < RecFields.size(); i++) {
2601 FieldDecl* ID = RecFields[i];
2602 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2603 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2604 ID->getIdentifier(), ID->getType(),
2605 ID->getBitWidth());
2606 Decls.push_back(Sema::DeclPtrTy::make(FD));
2607 }
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Chris Lattnercc98eac2008-12-17 07:13:27 +00002609 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002610 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002611 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002612 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002613 if (getLangOptions().CPlusPlus)
2614 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002615 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002616 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002617 }
2618}
2619