blob: 939ccab35db7cb585fe8b2c0199e46da493334f9 [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);
Ted Kremenek09b68972010-02-23 19:39:46 +0000602
603 /// Check that class of this category is already completely declared.
604 if (!IDecl || IDecl->isForwardDecl()) {
605 // Create an invalid ObjCCategoryDecl to serve as context for
606 // the enclosing method declarations. We mark the decl invalid
607 // to make it clear that this isn't a valid AST.
608 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
609 ClassLoc, CategoryLoc, CategoryName);
610 CDecl->setInvalidDecl();
611 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
612 return DeclPtrTy::make(CDecl);
613 }
614
615 if (!CategoryName) {
Fariborz Jahanian25760612010-02-15 21:55:26 +0000616 // Class extensions require a special treatment. Use an existing one.
Ted Kremenek09b68972010-02-23 19:39:46 +0000617 // Note that 'getClassExtension()' can return NULL.
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000618 CDecl = IDecl->getClassExtension();
Ted Kremenek09b68972010-02-23 19:39:46 +0000619 }
620
Fariborz Jahanian25760612010-02-15 21:55:26 +0000621 if (!CDecl) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000622 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
623 ClassLoc, CategoryLoc, CategoryName);
Fariborz Jahanian25760612010-02-15 21:55:26 +0000624 // FIXME: PushOnScopeChains?
625 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000626
Fariborz Jahanian25760612010-02-15 21:55:26 +0000627 CDecl->setClassInterface(IDecl);
628 // Insert first use of class extension to the list of class's categories.
629 if (!CategoryName)
630 CDecl->insertNextClassCategory();
631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Chris Lattner16b34b42009-02-16 21:30:01 +0000633 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000634 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000635
Fariborz Jahanian25760612010-02-15 21:55:26 +0000636 if (CategoryName) {
637 /// Check for duplicate interface declaration for this category
638 ObjCCategoryDecl *CDeclChain;
639 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
640 CDeclChain = CDeclChain->getNextClassCategory()) {
641 if (CDeclChain->getIdentifier() == CategoryName) {
642 // Class extensions can be declared multiple times.
643 Diag(CategoryLoc, diag::warn_dup_category_def)
644 << ClassName << CategoryName;
645 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
646 break;
647 }
Chris Lattner70f19542009-02-16 21:26:43 +0000648 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000649 if (!CDeclChain)
650 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000651 }
Chris Lattner70f19542009-02-16 21:26:43 +0000652
Chris Lattner4d391482007-12-12 07:09:47 +0000653 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000654 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000655 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000656 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000657 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000658 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000659 NumProtoRefs, ProtoLocs,
660 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000661 }
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Anders Carlsson15281452008-11-04 16:57:32 +0000663 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000664 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000665}
666
667/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000668/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000669/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000670Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000671 SourceLocation AtCatImplLoc,
672 IdentifierInfo *ClassName, SourceLocation ClassLoc,
673 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000674 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000675 ObjCCategoryDecl *CatIDecl = 0;
676 if (IDecl) {
677 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
678 if (!CatIDecl) {
679 // Category @implementation with no corresponding @interface.
680 // Create and install one.
681 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000682 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000683 CatName);
684 CatIDecl->setClassInterface(IDecl);
685 CatIDecl->insertNextClassCategory();
686 }
687 }
688
Mike Stump1eb44332009-09-09 15:08:12 +0000689 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000690 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
691 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000692 /// Check that class of this category is already completely declared.
693 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000694 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000695
Douglas Gregord0434102009-01-09 00:49:46 +0000696 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000697 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000698
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000699 /// Check that CatName, category name, is not used in another implementation.
700 if (CatIDecl) {
701 if (CatIDecl->getImplementation()) {
702 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
703 << CatName;
704 Diag(CatIDecl->getImplementation()->getLocation(),
705 diag::note_previous_definition);
706 } else
707 CatIDecl->setImplementation(CDecl);
708 }
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Anders Carlsson15281452008-11-04 16:57:32 +0000710 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000711 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000712}
713
Chris Lattnerb28317a2009-03-28 19:18:32 +0000714Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000715 SourceLocation AtClassImplLoc,
716 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000717 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000718 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000719 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000720 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000721 NamedDecl *PrevDecl
722 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000723 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000724 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000725 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000726 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
727 // If this is a forward declaration of an interface, warn.
728 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000729 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000730 IDecl = 0;
731 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000732 } else {
733 // We did not find anything with the name ClassName; try to correct for
734 // typos in the class name.
735 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
736 if (CorrectTypo(R, TUScope, 0) &&
737 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000738 // Suggest the (potentially) correct interface name. However, put the
739 // fix-it hint itself in a separate note, since changing the name in
740 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000741 // provide a code-modification hint or use the typo name for recovery,
742 // because this is just a warning. The program may actually be correct.
743 Diag(ClassLoc, diag::warn_undef_interface_suggest)
744 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000745 Diag(IDecl->getLocation(), diag::note_previous_decl)
746 << R.getLookupName()
747 << CodeModificationHint::CreateReplacement(ClassLoc,
748 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000749 IDecl = 0;
750 } else {
751 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
752 }
Chris Lattner4d391482007-12-12 07:09:47 +0000753 }
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Chris Lattner4d391482007-12-12 07:09:47 +0000755 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000756 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000757 if (SuperClassname) {
758 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000759 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000760 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000761 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
762 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000763 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000764 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000765 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000766 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000767 Diag(SuperClassLoc, diag::err_undef_superclass)
768 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000769 else if (IDecl && IDecl->getSuperClass() != SDecl) {
770 // This implementation and its interface do not have the same
771 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000772 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000773 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000774 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000775 }
776 }
777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Chris Lattner4d391482007-12-12 07:09:47 +0000779 if (!IDecl) {
780 // Legacy case of @implementation with no corresponding @interface.
781 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000782
Mike Stump390b4cc2009-05-16 07:39:55 +0000783 // FIXME: Do we support attributes on the @implementation? If so we should
784 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000785 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000786 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000787 IDecl->setSuperClass(SDecl);
788 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000789
790 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000791 } else {
792 // Mark the interface as being completed, even if it was just as
793 // @class ....;
794 // declaration; the user cannot reopen it.
795 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000796 }
Mike Stump1eb44332009-09-09 15:08:12 +0000797
798 ObjCImplementationDecl* IMPDecl =
799 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000800 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Anders Carlsson15281452008-11-04 16:57:32 +0000802 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000803 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Chris Lattner4d391482007-12-12 07:09:47 +0000805 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000806 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000807 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000808 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000809 Diag(IDecl->getImplementation()->getLocation(),
810 diag::note_previous_definition);
811 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000812 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000813 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000814 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000815 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000816}
817
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000818void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
819 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000820 SourceLocation RBrace) {
821 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000822 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000823 if (!IDecl)
824 return;
825 /// Check case of non-existing @interface decl.
826 /// (legacy objective-c @implementation decl without an @interface decl).
827 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000828 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000829 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000830 // Add ivar's to class's DeclContext.
831 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000832 ivars[i]->setLexicalDeclContext(ImpDecl);
833 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000834 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000835 }
836
Chris Lattner4d391482007-12-12 07:09:47 +0000837 return;
838 }
839 // If implementation has empty ivar list, just return.
840 if (numIvars == 0)
841 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Chris Lattner4d391482007-12-12 07:09:47 +0000843 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000844 if (LangOpts.ObjCNonFragileABI2) {
845 if (ImpDecl->getSuperClass())
846 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
847 for (unsigned i = 0; i < numIvars; i++) {
848 ObjCIvarDecl* ImplIvar = ivars[i];
849 if (const ObjCIvarDecl *ClsIvar =
850 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
851 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
852 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
853 continue;
854 }
855 if (ImplIvar->getAccessControl() != ObjCIvarDecl::Private)
856 Diag(ImplIvar->getLocation(), diag::err_non_private_ivar_declaration);
857 // Instance ivar to Implementation's DeclContext.
858 ImplIvar->setLexicalDeclContext(ImpDecl);
859 IDecl->makeDeclVisibleInContext(ImplIvar, false);
860 ImpDecl->addDecl(ImplIvar);
861 }
862 return;
863 }
Chris Lattner4d391482007-12-12 07:09:47 +0000864 // Check interface's Ivar list against those in the implementation.
865 // names and types must match.
866 //
Chris Lattner4d391482007-12-12 07:09:47 +0000867 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000868 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000869 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
870 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000871 ObjCIvarDecl* ImplIvar = ivars[j++];
872 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000873 assert (ImplIvar && "missing implementation ivar");
874 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Steve Naroffca331292009-03-03 14:49:36 +0000876 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000877 if (Context.getCanonicalType(ImplIvar->getType()) !=
878 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000879 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000880 << ImplIvar->getIdentifier()
881 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000882 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000883 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
884 Expr *ImplBitWidth = ImplIvar->getBitWidth();
885 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000886 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
887 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000888 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
889 << ImplIvar->getIdentifier();
890 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
891 }
Mike Stump1eb44332009-09-09 15:08:12 +0000892 }
Steve Naroffca331292009-03-03 14:49:36 +0000893 // Make sure the names are identical.
894 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000895 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000896 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000897 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000898 }
899 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000900 }
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Chris Lattner609e4c72007-12-12 18:11:49 +0000902 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000903 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000904 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000905 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000906}
907
Steve Naroff3c2eb662008-02-10 21:38:56 +0000908void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
909 bool &IncompleteImpl) {
910 if (!IncompleteImpl) {
911 Diag(ImpLoc, diag::warn_incomplete_impl);
912 IncompleteImpl = true;
913 }
Chris Lattner08631c52008-11-23 21:45:46 +0000914 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000915}
916
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000917void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
918 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000919 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000920 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000921 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
922 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000923 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000924 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
925 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000926 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Chris Lattner3aff9192009-04-11 19:58:42 +0000929 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
930 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
931 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000932 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
933 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
934 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
935 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000936 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000937
938 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000939 << ImpMethodDecl->getDeclName() << (*IF)->getType()
940 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000941 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000942 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000943}
944
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000945/// isPropertyReadonly - Return true if property is readonly, by searching
946/// for the property in the class and in its categories and implementations
947///
948bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000949 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000950 // by far the most common case.
951 if (!PDecl->isReadOnly())
952 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000953 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000954 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000955 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000956 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000958 // Main class has the property as 'readonly'. Must search
Mike Stump1eb44332009-09-09 15:08:12 +0000959 // through the category list to see if the property's
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000960 // attribute has been over-ridden to 'readwrite'.
961 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
962 Category; Category = Category->getNextClassCategory()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000963 // Even if property is ready only, if a category has a user defined setter,
964 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000965 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000966 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000967 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000968 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000969 if (P && !P->isReadOnly())
970 return false;
971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000973 // Also, check for definition of a setter method in the implementation if
974 // all else failed.
975 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000976 if (ObjCImplementationDecl *IMD =
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000977 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000978 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000979 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000980 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000981 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000982 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000983 return false;
984 }
985 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000986 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000987 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000988 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000989 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000990 // If all fails, look at the super class.
991 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
992 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000993 return true;
994}
995
Mike Stump390b4cc2009-05-16 07:39:55 +0000996/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
997/// improve the efficiency of selector lookups and type checking by associating
998/// with each protocol / interface / category the flattened instance tables. If
999/// we used an immutable set to keep the table then it wouldn't add significant
1000/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001001
Steve Naroffefe7f362008-02-08 22:06:17 +00001002/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001003/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001004void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1005 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001006 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001007 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001008 const llvm::DenseSet<Selector> &ClsMap,
1009 ObjCInterfaceDecl *IDecl) {
1010 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001011 ObjCInterfaceDecl *NSIDecl = 0;
1012 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001013 // check to see if class implements forwardInvocation method and objects
1014 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001015 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001016 // Under such conditions, which means that every method possible is
1017 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001018 // found" warnings.
1019 // FIXME: Use a general GetUnarySelector method for this.
1020 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1021 Selector fISelector = Context.Selectors.getSelector(1, &II);
1022 if (InsMap.count(fISelector))
1023 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1024 // need be implemented in the implementation.
1025 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1026 }
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001028 // If a method lookup fails locally we still need to look and see if
1029 // the method was implemented by a base class or an inherited
1030 // protocol. This lookup is slow, but occurs rarely in correct code
1031 // and otherwise would terminate in a warning.
1032
Chris Lattner4d391482007-12-12 07:09:47 +00001033 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001034 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001035 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001036 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001037 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001038 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001039 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001040 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001041 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001042 // Ugly, but necessary. Method declared in protcol might have
1043 // have been synthesized due to a property declared in the class which
1044 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001045 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001046 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001047 if (!MethodInClass || !MethodInClass->isSynthesized())
1048 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
1049 }
1050 }
Chris Lattner4d391482007-12-12 07:09:47 +00001051 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001052 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001053 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001054 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001055 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001056 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1057 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001058 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +00001059 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001060 }
Chris Lattner780f3292008-07-21 21:32:27 +00001061 // Check on this protocols's referenced protocols, recursively.
1062 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1063 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001064 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001065}
1066
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001067/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1068/// or protocol against those declared in their implementations.
1069///
1070void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1071 const llvm::DenseSet<Selector> &ClsMap,
1072 llvm::DenseSet<Selector> &InsMapSeen,
1073 llvm::DenseSet<Selector> &ClsMapSeen,
1074 ObjCImplDecl* IMPDecl,
1075 ObjCContainerDecl* CDecl,
1076 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001077 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001078 // Check and see if instance methods in class interface have been
1079 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001080 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1081 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001082 if (InsMapSeen.count((*I)->getSelector()))
1083 continue;
1084 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001085 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001086 !InsMap.count((*I)->getSelector())) {
1087 if (ImmediateClass)
1088 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1089 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001090 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001091 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001092 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001093 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001094 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001095 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001096 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1097 // ImpMethodDecl may be null as in a @dynamic property.
1098 if (ImpMethodDecl)
1099 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1100 }
1101 }
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001103 // Check and see if class methods in class interface have been
1104 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001105 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001106 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001107 if (ClsMapSeen.count((*I)->getSelector()))
1108 continue;
1109 ClsMapSeen.insert((*I)->getSelector());
1110 if (!ClsMap.count((*I)->getSelector())) {
1111 if (ImmediateClass)
1112 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001113 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001114 ObjCMethodDecl *ImpMethodDecl =
1115 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001116 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001117 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001118 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1119 }
1120 }
1121 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1122 // Check for any implementation of a methods declared in protocol.
1123 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1124 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001125 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1126 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001127 (*PI), IncompleteImpl, false);
1128 if (I->getSuperClass())
1129 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001130 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001131 I->getSuperClass(), IncompleteImpl, false);
1132 }
1133}
1134
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001135/// CollectImmediateProperties - This routine collects all properties in
1136/// the class and its conforming protocols; but not those it its super class.
1137void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
1138 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1139 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1140 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1141 E = IDecl->prop_end(); P != E; ++P) {
1142 ObjCPropertyDecl *Prop = (*P);
1143 PropMap[Prop->getIdentifier()] = Prop;
1144 }
1145 // scan through class's protocols.
1146 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
1147 E = IDecl->protocol_end(); PI != E; ++PI)
1148 CollectImmediateProperties((*PI), PropMap);
1149 }
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001150 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001151 if (!CATDecl->IsClassExtension())
1152 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1153 E = CATDecl->prop_end(); P != E; ++P) {
1154 ObjCPropertyDecl *Prop = (*P);
1155 PropMap[Prop->getIdentifier()] = Prop;
1156 }
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001157 // scan through class's protocols.
1158 for (ObjCInterfaceDecl::protocol_iterator PI = CATDecl->protocol_begin(),
1159 E = CATDecl->protocol_end(); PI != E; ++PI)
1160 CollectImmediateProperties((*PI), PropMap);
1161 }
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001162 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1163 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1164 E = PDecl->prop_end(); P != E; ++P) {
1165 ObjCPropertyDecl *Prop = (*P);
1166 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1167 if (!PropEntry)
1168 PropEntry = Prop;
1169 }
1170 // scan through protocol's protocols.
1171 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1172 E = PDecl->protocol_end(); PI != E; ++PI)
1173 CollectImmediateProperties((*PI), PropMap);
1174 }
1175}
1176
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001177/// LookupPropertyDecl - Looks up a property in the current class and all
1178/// its protocols.
1179ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1180 IdentifierInfo *II) {
1181 if (const ObjCInterfaceDecl *IDecl =
1182 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1183 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1184 E = IDecl->prop_end(); P != E; ++P) {
1185 ObjCPropertyDecl *Prop = (*P);
1186 if (Prop->getIdentifier() == II)
1187 return Prop;
1188 }
1189 // scan through class's protocols.
1190 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
1191 E = IDecl->protocol_end(); PI != E; ++PI) {
1192 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1193 if (Prop)
1194 return Prop;
1195 }
1196 }
1197 else if (const ObjCProtocolDecl *PDecl =
1198 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1199 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1200 E = PDecl->prop_end(); P != E; ++P) {
1201 ObjCPropertyDecl *Prop = (*P);
1202 if (Prop->getIdentifier() == II)
1203 return Prop;
1204 }
1205 // scan through protocol's protocols.
1206 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1207 E = PDecl->protocol_end(); PI != E; ++PI) {
1208 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1209 if (Prop)
1210 return Prop;
1211 }
1212 }
1213 return 0;
1214}
1215
1216
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001217void Sema::DiagnoseUnimplementedProperties(ObjCImplDecl* IMPDecl,
1218 ObjCContainerDecl *CDecl,
1219 const llvm::DenseSet<Selector>& InsMap) {
1220 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1221 CollectImmediateProperties(CDecl, PropMap);
Fariborz Jahaniana84f2e42010-01-20 17:27:59 +00001222 if (PropMap.empty())
1223 return;
1224
1225 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1226 for (ObjCImplDecl::propimpl_iterator
1227 I = IMPDecl->propimpl_begin(),
1228 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1229 PropImplMap.insert((*I)->getPropertyDecl());
1230
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001231 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1232 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1233 ObjCPropertyDecl *Prop = P->second;
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001234 // Is there a matching propery synthesize/dynamic?
Fariborz Jahaniana84f2e42010-01-20 17:27:59 +00001235 if (Prop->isInvalidDecl() ||
1236 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1237 PropImplMap.count(Prop))
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001238 continue;
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001239 if (LangOpts.ObjCNonFragileABI2) {
1240 ActOnPropertyImplDecl(IMPDecl->getLocation(),
1241 SourceLocation(),
1242 true, DeclPtrTy::make(IMPDecl),
1243 Prop->getIdentifier(),
1244 Prop->getIdentifier());
1245 continue;
1246 }
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001247 if (!InsMap.count(Prop->getGetterName())) {
1248 Diag(Prop->getLocation(),
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001249 isa<ObjCCategoryDecl>(CDecl) ?
1250 diag::warn_setter_getter_impl_required_in_category :
1251 diag::warn_setter_getter_impl_required)
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001252 << Prop->getDeclName() << Prop->getGetterName();
1253 Diag(IMPDecl->getLocation(),
1254 diag::note_property_impl_required);
1255 }
1256
1257 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1258 Diag(Prop->getLocation(),
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001259 isa<ObjCCategoryDecl>(CDecl) ?
1260 diag::warn_setter_getter_impl_required_in_category :
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001261 diag::warn_setter_getter_impl_required)
1262 << Prop->getDeclName() << Prop->getSetterName();
1263 Diag(IMPDecl->getLocation(),
1264 diag::note_property_impl_required);
1265 }
1266 }
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001267}
1268
Mike Stump1eb44332009-09-09 15:08:12 +00001269void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1270 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001271 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001272 llvm::DenseSet<Selector> InsMap;
1273 // Check and see if instance methods in class interface have been
1274 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001275 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001276 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001277 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001279 // Check and see if properties declared in the interface have either 1)
1280 // an implementation or 2) there is a @synthesize/@dynamic implementation
1281 // of the property in the @implementation.
1282 if (isa<ObjCInterfaceDecl>(CDecl))
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001283 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
1284
Chris Lattner4d391482007-12-12 07:09:47 +00001285 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001286 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001287 I = IMPDecl->classmeth_begin(),
1288 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001289 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001291 // Check for type conflict of methods declared in a class/protocol and
1292 // its implementation; if any.
1293 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001294 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1295 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001296 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Chris Lattner4d391482007-12-12 07:09:47 +00001298 // Check the protocol list for unimplemented methods in the @implementation
1299 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001300 // Check and see if class methods in class interface have been
1301 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Chris Lattnercddc8882009-03-01 00:56:52 +00001303 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001304 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001305 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001306 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001307 InsMap, ClsMap, I);
1308 // Check class extensions (unnamed categories)
1309 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1310 Categories; Categories = Categories->getNextClassCategory()) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001311 if (Categories->IsClassExtension()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001312 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1313 break;
1314 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001315 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001316 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001317 // For extended class, unimplemented methods in its protocols will
1318 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001319 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001320 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1321 E = C->protocol_end(); PI != E; ++PI)
1322 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1323 InsMap, ClsMap, C->getClassInterface());
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001324 // Report unimplemented properties in the category as well.
1325 // When reporting on missing setter/getters, do not report when
1326 // setter/getter is implemented in category's primary class
1327 // implementation.
1328 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1329 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1330 for (ObjCImplementationDecl::instmeth_iterator
1331 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1332 InsMap.insert((*I)->getSelector());
1333 }
1334 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
1335 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001336 } else
1337 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001338}
1339
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001340void
1341Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1342 ObjCContainerDecl* IDecl) {
1343 // Rules apply in non-GC mode only
1344 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1345 return;
1346 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1347 E = IDecl->prop_end();
1348 I != E; ++I) {
1349 ObjCPropertyDecl *Property = (*I);
1350 unsigned Attributes = Property->getPropertyAttributes();
1351 // We only care about readwrite atomic property.
1352 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1353 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1354 continue;
1355 if (const ObjCPropertyImplDecl *PIDecl
1356 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1357 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1358 continue;
1359 ObjCMethodDecl *GetterMethod =
1360 IMPDecl->getInstanceMethod(Property->getGetterName());
1361 ObjCMethodDecl *SetterMethod =
1362 IMPDecl->getInstanceMethod(Property->getSetterName());
1363 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1364 SourceLocation MethodLoc =
1365 (GetterMethod ? GetterMethod->getLocation()
1366 : SetterMethod->getLocation());
1367 Diag(MethodLoc, diag::warn_atomic_property_rule)
1368 << Property->getIdentifier();
1369 Diag(Property->getLocation(), diag::note_property_declare);
1370 }
1371 }
1372 }
1373}
1374
Mike Stump1eb44332009-09-09 15:08:12 +00001375/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001376Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001377Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001378 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001379 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001380 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001381 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Chris Lattner4d391482007-12-12 07:09:47 +00001383 for (unsigned i = 0; i != NumElts; ++i) {
1384 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001385 NamedDecl *PrevDecl
1386 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001387 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001388 // Maybe we will complain about the shadowed template parameter.
1389 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1390 // Just pretend that we didn't see the previous declaration.
1391 PrevDecl = 0;
1392 }
1393
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001394 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001395 // GCC apparently allows the following idiom:
1396 //
1397 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1398 // @class XCElementToggler;
1399 //
Mike Stump1eb44332009-09-09 15:08:12 +00001400 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001401 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1402 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001403 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001404 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001405 } else if (TDD) {
1406 // a forward class declaration matching a typedef name of a class refers
1407 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001408 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001409 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1410 PrevDecl = OI->getDecl();
1411 }
Chris Lattner4d391482007-12-12 07:09:47 +00001412 }
Mike Stump1eb44332009-09-09 15:08:12 +00001413 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001414 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001415 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001416 IdentList[i], IdentLocs[i], true);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001417
1418 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1419 // the current DeclContext. This prevents clients that walk DeclContext
1420 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1421 // declared later (if at all). We also take care to explicitly make
1422 // sure this declaration is visible for name lookup.
1423 PushOnScopeChains(IDecl, TUScope, false);
1424 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001425 }
1426
1427 Interfaces.push_back(IDecl);
1428 }
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Ted Kremenek321c22f2009-11-18 00:28:11 +00001430 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001431 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001432 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001433 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001434 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001435 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001436 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001437}
1438
1439
1440/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1441/// returns true, or false, accordingly.
1442/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001443bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001444 const ObjCMethodDecl *PrevMethod,
1445 bool matchBasedOnSizeAndAlignment) {
1446 QualType T1 = Context.getCanonicalType(Method->getResultType());
1447 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001449 if (T1 != T2) {
1450 // The result types are different.
1451 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001452 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001453 // Incomplete types don't have a size and alignment.
1454 if (T1->isIncompleteType() || T2->isIncompleteType())
1455 return false;
1456 // Check is based on size and alignment.
1457 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1458 return false;
1459 }
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Chris Lattner89951a82009-02-20 18:43:26 +00001461 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1462 E = Method->param_end();
1463 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Chris Lattner89951a82009-02-20 18:43:26 +00001465 for (; ParamI != E; ++ParamI, ++PrevI) {
1466 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1467 T1 = Context.getCanonicalType((*ParamI)->getType());
1468 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001469 if (T1 != T2) {
1470 // The result types are different.
1471 if (!matchBasedOnSizeAndAlignment)
1472 return false;
1473 // Incomplete types don't have a size and alignment.
1474 if (T1->isIncompleteType() || T2->isIncompleteType())
1475 return false;
1476 // Check is based on size and alignment.
1477 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1478 return false;
1479 }
Chris Lattner4d391482007-12-12 07:09:47 +00001480 }
1481 return true;
1482}
1483
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001484/// \brief Read the contents of the instance and factory method pools
1485/// for a given selector from external storage.
1486///
1487/// This routine should only be called once, when neither the instance
1488/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001489Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001490 bool isInstance) {
1491 assert(ExternalSource && "We need an external AST source");
1492 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1493 "Selector data already loaded into the instance method pool");
1494 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1495 "Selector data already loaded into the factory method pool");
1496
1497 // Read the method list from the external source.
1498 std::pair<ObjCMethodList, ObjCMethodList> Methods
1499 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001501 if (isInstance) {
1502 if (Methods.second.Method)
1503 FactoryMethodPool[Sel] = Methods.second;
1504 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001505 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001506
1507 if (Methods.first.Method)
1508 InstanceMethodPool[Sel] = Methods.first;
1509
1510 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1511}
1512
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001513void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001514 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1515 = InstanceMethodPool.find(Method->getSelector());
1516 if (Pos == InstanceMethodPool.end()) {
1517 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1518 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1519 else
1520 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1521 ObjCMethodList())).first;
1522 }
1523
1524 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001525 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001526 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001527 Entry.Method = Method;
1528 Entry.Next = 0;
1529 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001530 }
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Chris Lattnerb25df352009-03-04 05:16:45 +00001532 // We've seen a method with this name, see if we have already seen this type
1533 // signature.
1534 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1535 if (MatchTwoMethodDeclarations(Method, List->Method))
1536 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Chris Lattnerb25df352009-03-04 05:16:45 +00001538 // We have a new signature for an existing method - add it.
1539 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001540 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1541 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001542}
1543
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001544// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001545ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001546 SourceRange R,
1547 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001548 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1549 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001550 if (Pos == InstanceMethodPool.end()) {
1551 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001552 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1553 else
1554 return 0;
1555 }
1556
1557 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001558 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Steve Naroff037cda52008-09-30 14:38:43 +00001560 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001561 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1562 // This checks if the methods differ by size & alignment.
1563 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001564 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001565 }
1566 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001567 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001568 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001569 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001570 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001571 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001572 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001573 }
1574 return MethList.Method;
1575}
1576
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001577void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001578 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1579 = FactoryMethodPool.find(Method->getSelector());
1580 if (Pos == FactoryMethodPool.end()) {
1581 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1582 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1583 else
1584 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1585 ObjCMethodList())).first;
1586 }
1587
1588 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001589 if (!FirstMethod.Method) {
1590 // Haven't seen a method with this selector name yet - add it.
1591 FirstMethod.Method = Method;
1592 FirstMethod.Next = 0;
1593 } else {
1594 // We've seen a method with this name, now check the type signature(s).
1595 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001596
1597 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001598 Next = Next->Next)
1599 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Chris Lattner4d391482007-12-12 07:09:47 +00001601 if (!match) {
1602 // We have a new signature for an existing method - add it.
1603 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001604 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1605 ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001606 FirstMethod.Next = OMI;
1607 }
1608 }
1609}
1610
Mike Stump1eb44332009-09-09 15:08:12 +00001611ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001612 SourceRange R) {
1613 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1614 = FactoryMethodPool.find(Sel);
1615 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001616 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001617 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1618 else
1619 return 0;
1620 }
1621
1622 ObjCMethodList &MethList = Pos->second;
1623 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001625 if (MethList.Method && MethList.Next) {
1626 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1627 // This checks if the methods differ by size & alignment.
1628 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1629 issueWarning = true;
1630 }
1631 if (issueWarning && (MethList.Method && MethList.Next)) {
1632 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001633 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001634 << MethList.Method->getSourceRange();
1635 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001636 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001637 << Next->Method->getSourceRange();
1638 }
1639 return MethList.Method;
1640}
1641
Mike Stump1eb44332009-09-09 15:08:12 +00001642/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff0701bbb2009-01-08 17:28:14 +00001643/// have the property type and issue diagnostics if they don't.
1644/// Also synthesize a getter/setter method if none exist (and update the
1645/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1646/// methods is the "right" thing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001647void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001648 ObjCContainerDecl *CD) {
1649 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00001650
1651 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001652 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump1eb44332009-09-09 15:08:12 +00001653 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001654 property->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001656 if (SetterMethod) {
Fariborz Jahanian02deae82010-01-06 00:18:12 +00001657 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1658 property->getPropertyAttributes();
1659 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1660 Context.getCanonicalType(SetterMethod->getResultType()) !=
1661 Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001662 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001663 if (SetterMethod->param_size() != 1 ||
1664 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001665 Diag(property->getLocation(),
1666 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001667 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001668 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001669 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1670 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001671 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001672
1673 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001674 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001675 // FIXME: The synthesized property we set here is misleading. We almost always
1676 // synthesize these methods unless the user explicitly provided prototypes
1677 // (which is odd, but allowed). Sema should be typechecking that the
1678 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001679 if (!GetterMethod) {
1680 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001681 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001682 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001683 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1684 property->getLocation(), property->getGetterName(),
1685 property->getType(), CD, true, false, true,
1686 (property->getPropertyImplementation() ==
1687 ObjCPropertyDecl::Optional) ?
1688 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001689 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001690 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001691 } else
1692 // A user declared getter will be synthesize when @synthesize of
1693 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001694 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001695 property->setGetterMethodDecl(GetterMethod);
1696
1697 // Skip setter if property is read-only.
1698 if (!property->isReadOnly()) {
1699 // Find the default setter and if one not found, add one.
1700 if (!SetterMethod) {
1701 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001702 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001703 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001704 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1705 property->getLocation(),
1706 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001707 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001708 (property->getPropertyImplementation() ==
1709 ObjCPropertyDecl::Optional) ?
1710 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001711 ObjCMethodDecl::Required);
1712 // Invent the arguments for the setter. We don't bother making a
1713 // nice name for the argument.
1714 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001715 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001716 property->getIdentifier(),
1717 property->getType(),
John McCalla93c9342009-12-07 02:54:59 +00001718 /*TInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001719 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001720 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001721 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001722 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001723 } else
1724 // A user declared setter will be synthesize when @synthesize of
1725 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001726 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001727 property->setSetterMethodDecl(SetterMethod);
1728 }
Mike Stump1eb44332009-09-09 15:08:12 +00001729 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001730 // handle the following, which is supported by GCC (and part of the design).
1731 //
1732 // @interface Foo
1733 // @property double bar;
1734 // @end
1735 //
1736 // void thisIsUnfortunate() {
1737 // id foo;
1738 // double bar = [foo bar];
1739 // }
1740 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001741 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001742 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001743 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001744 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001745}
1746
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001747/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1748/// identical selector names in current and its super classes and issues
1749/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001750void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1751 ObjCMethodDecl *Method,
1752 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001753 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1754 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001756 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001757 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001758 SD->lookupMethod(Method->getSelector(), IsInstance);
1759 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001760 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001761 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001762 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001763 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1764 E = Method->param_end();
1765 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1766 for (; ParamI != E; ++ParamI, ++PrevI) {
1767 // Number of parameters are the same and is guaranteed by selector match.
1768 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1769 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1770 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1771 // If type of arguement of method in this class does not match its
1772 // respective argument type in the super class method, issue warning;
1773 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001774 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001775 << T1 << T2;
1776 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1777 return;
1778 }
1779 }
1780 ID = SD;
1781 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001782}
1783
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001784/// DiagnoseDuplicateIvars -
1785/// Check for duplicate ivars in the entire class at the start of
1786/// @implementation. This becomes necesssary because class extension can
1787/// add ivars to a class in random order which will not be known until
1788/// class's @implementation is seen.
1789void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1790 ObjCInterfaceDecl *SID) {
1791 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1792 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1793 ObjCIvarDecl* Ivar = (*IVI);
1794 if (Ivar->isInvalidDecl())
1795 continue;
1796 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1797 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1798 if (prevIvar) {
1799 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1800 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1801 Ivar->setInvalidDecl();
1802 }
1803 }
1804 }
1805}
1806
Steve Naroffa56f6162007-12-18 01:30:32 +00001807// Note: For class/category implemenations, allMethods/allProperties is
1808// always null.
Ted Kremenek782f2f52010-01-07 01:20:12 +00001809void Sema::ActOnAtEnd(SourceRange AtEnd,
1810 DeclPtrTy classDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001811 DeclPtrTy *allMethods, unsigned allNum,
1812 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001813 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001814 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001815
Steve Naroffa56f6162007-12-18 01:30:32 +00001816 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1817 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001818 // should be true.
1819 if (!ClassDecl)
1820 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001821
Mike Stump1eb44332009-09-09 15:08:12 +00001822 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001823 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1824 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001825 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001826
Ted Kremenek782f2f52010-01-07 01:20:12 +00001827 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1828 // FIXME: This is wrong. We shouldn't be pretending that there is
1829 // an '@end' in the declaration.
1830 SourceLocation L = ClassDecl->getLocation();
1831 AtEnd.setBegin(L);
1832 AtEnd.setEnd(L);
1833 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001834 }
1835
Steve Naroff0701bbb2009-01-08 17:28:14 +00001836 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001837
1838 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1839 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1840 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1841
Chris Lattner4d391482007-12-12 07:09:47 +00001842 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001843 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001844 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001845
1846 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001847 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001848 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001849 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001850 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001851 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001852 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001853 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001854 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001855 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001856 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001857 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001858 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001859 InsMap[Method->getSelector()] = Method;
1860 /// The following allows us to typecheck messages to "id".
1861 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001862 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001863 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001864 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001865 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001866 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001867 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001868 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001869 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001870 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001871 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001872 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001873 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001874 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001875 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001876 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001877 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001878 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001879 /// The following allows us to typecheck messages to "Class".
1880 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001881 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001882 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001883 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001884 }
1885 }
1886 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001887 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001888 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001889 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001890 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001891 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001892 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001893 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001894 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001895 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001896
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001897 // Compare protocol properties with those in category
1898 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian25760612010-02-15 21:55:26 +00001899 if (C->IsClassExtension())
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001900 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001901 }
Steve Naroff09c47192009-01-09 15:36:25 +00001902 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001903 if (CDecl->getIdentifier())
1904 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1905 // user-defined setter/getter. It also synthesizes setter/getter methods
1906 // and adds them to the DeclContext and global method pools.
1907 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1908 E = CDecl->prop_end();
1909 I != E; ++I)
1910 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001911 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001912 }
1913 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001914 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001915 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001916 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001917 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001918 if (LangOpts.ObjCNonFragileABI2)
1919 while (IDecl->getSuperClass()) {
1920 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1921 IDecl = IDecl->getSuperClass();
1922 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001923 }
Mike Stump1eb44332009-09-09 15:08:12 +00001924 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001925 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001926 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Chris Lattner4d391482007-12-12 07:09:47 +00001928 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001929 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001930 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001931 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001932 Categories; Categories = Categories->getNextClassCategory()) {
1933 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001934 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001935 break;
1936 }
1937 }
1938 }
1939 }
Chris Lattner682bf922009-03-29 16:50:03 +00001940 if (isInterfaceDeclKind) {
1941 // Reject invalid vardecls.
1942 for (unsigned i = 0; i != tuvNum; i++) {
1943 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1944 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1945 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001946 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001947 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001948 }
Chris Lattner682bf922009-03-29 16:50:03 +00001949 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001950 }
Chris Lattner4d391482007-12-12 07:09:47 +00001951}
1952
1953
1954/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1955/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001956static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001957CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1958 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1959 if (PQTVal & ObjCDeclSpec::DQ_In)
1960 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1961 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1962 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1963 if (PQTVal & ObjCDeclSpec::DQ_Out)
1964 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1965 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1966 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1967 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1968 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1969 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1970 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001971
1972 return ret;
1973}
1974
Chris Lattnerb28317a2009-03-28 19:18:32 +00001975Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001976 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001977 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001978 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001979 Selector Sel,
1980 // optional arguments. The number of types/arguments is obtained
1981 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001982 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001983 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001984 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1985 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001986 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001987
1988 // Make sure we can establish a context for the method.
1989 if (!ClassDecl) {
1990 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanian32844b32009-08-28 17:52:37 +00001991 FunctionLabelMap.clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001992 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001993 }
Chris Lattner4d391482007-12-12 07:09:47 +00001994 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001995
Steve Naroffccef3712009-02-20 22:59:16 +00001996 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001997 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001998
Steve Naroffccef3712009-02-20 22:59:16 +00001999 // Methods cannot return interface types. All ObjC objects are
2000 // passed by reference.
2001 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002002 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2003 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002004 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00002005 }
2006 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002007 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00002008
2009 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002010 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00002011 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002012 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00002013 false,
Mike Stump1eb44332009-09-09 15:08:12 +00002014 MethodDeclKind == tok::objc_optional ?
2015 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002016 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Chris Lattner0ed844b2008-04-04 06:12:32 +00002018 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002019
Chris Lattner7db638d2009-04-11 19:42:43 +00002020 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002021 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002022 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Chris Lattnere294d3f2009-04-11 18:57:04 +00002024 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002025 ArgType = Context.getObjCIdType();
2026 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002027 } else {
John McCall58e46772009-10-23 21:48:59 +00002028 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002029 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002030 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002031 }
Mike Stump1eb44332009-09-09 15:08:12 +00002032
John McCall58e46772009-10-23 21:48:59 +00002033 ParmVarDecl* Param
2034 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
2035 ArgInfo[i].Name, ArgType, DI,
2036 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002037
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002038 if (ArgType->isObjCInterfaceType()) {
2039 Diag(ArgInfo[i].NameLoc,
2040 diag::err_object_cannot_be_passed_returned_by_value)
2041 << 1 << ArgType;
2042 Param->setInvalidDecl();
2043 }
Mike Stump1eb44332009-09-09 15:08:12 +00002044
Chris Lattner0ed844b2008-04-04 06:12:32 +00002045 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002046 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002047
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002048 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002049 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002050
Chris Lattner0ed844b2008-04-04 06:12:32 +00002051 Params.push_back(Param);
2052 }
2053
Jay Foadbeaaccd2009-05-21 09:52:38 +00002054 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002055 ObjCMethod->setObjCDeclQualifier(
2056 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
2057 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00002058
2059 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002060 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002061
John McCall54abf7d2009-11-04 02:18:39 +00002062 const ObjCMethodDecl *InterfaceMD = 0;
2063
Mike Stump1eb44332009-09-09 15:08:12 +00002064 // For implementations (which can be very "coarse grain"), we add the
2065 // method now. This allows the AST to implement lookup methods that work
2066 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00002067 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002068 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002069 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002070 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002071 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2072 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002073 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002074 PrevMethod = ImpDecl->getClassMethod(Sel);
2075 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002076 }
John McCall54abf7d2009-11-04 02:18:39 +00002077 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
2078 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002079 if (AttrList)
2080 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00002081 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002082 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002083 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002084 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
2085 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002086 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002087 PrevMethod = CatImpDecl->getClassMethod(Sel);
2088 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002089 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002090 if (AttrList)
2091 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00002092 }
2093 if (PrevMethod) {
2094 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002095 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002096 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002097 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002098 }
John McCall54abf7d2009-11-04 02:18:39 +00002099
2100 // If the interface declared this method, and it was deprecated there,
2101 // mark it deprecated here.
2102 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
2103 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
2104
Chris Lattnerb28317a2009-03-28 19:18:32 +00002105 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002106}
2107
Mike Stump1eb44332009-09-09 15:08:12 +00002108void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002109 SourceLocation Loc,
2110 unsigned &Attributes) {
2111 // FIXME: Improve the reported location.
2112
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002113 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002114 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002115 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
2116 ObjCDeclSpec::DQ_PR_assign |
2117 ObjCDeclSpec::DQ_PR_copy |
2118 ObjCDeclSpec::DQ_PR_retain))) {
2119 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
2120 "readwrite" :
2121 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
2122 "assign" :
2123 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
2124 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00002125
2126 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00002127 diag::err_objc_property_attr_mutually_exclusive :
2128 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002129 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002130 }
2131
2132 // Check for copy or retain on non-object types.
2133 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00002134 !PropertyTy->isObjCObjectPointerType() &&
2135 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00002136 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002137 Diag(Loc, diag::err_objc_property_requires_object)
2138 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002139 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
2140 }
2141
2142 // Check for more than one of { assign, copy, retain }.
2143 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2144 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002145 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2146 << "assign" << "copy";
2147 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00002148 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002149 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002150 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2151 << "assign" << "retain";
2152 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002153 }
2154 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2155 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002156 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2157 << "copy" << "retain";
2158 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002159 }
2160 }
2161
2162 // Warn if user supplied no assignment attribute, property is
2163 // readwrite, and this is an object type.
2164 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
2165 ObjCDeclSpec::DQ_PR_retain)) &&
2166 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00002167 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002168 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00002169 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002170 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
2171
2172 // If non-gc code warn that this is likely inappropriate.
2173 if (getLangOptions().getGCMode() == LangOptions::NonGC)
2174 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002175
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002176 // FIXME: Implement warning dependent on NSCopying being
2177 // implemented. See also:
2178 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2179 // (please trim this list while you are at it).
2180 }
Mike Stump046efd92009-05-07 23:06:50 +00002181
2182 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2183 && getLangOptions().getGCMode() == LangOptions::GCOnly
2184 && PropertyTy->isBlockPointerType())
2185 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002186}
2187
Mike Stump1eb44332009-09-09 15:08:12 +00002188Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002189 FieldDeclarator &FD,
2190 ObjCDeclSpec &ODS,
2191 Selector GetterSel,
2192 Selector SetterSel,
2193 DeclPtrTy ClassCategory,
2194 bool *isOverridingProperty,
2195 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002196 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002197 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
2198 // default is readwrite!
2199 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00002200 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002201 // not retain or copy
2202 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00002203 (isReadWrite &&
2204 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002205 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
2206 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahaniandd69aae2009-12-16 18:03:30 +00002207 if (T->isReferenceType()) {
2208 Diag(AtLoc, diag::error_reference_property);
2209 return DeclPtrTy();
2210 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002211 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002212 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002213 // May modify Attributes.
2214 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002215 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanian25760612010-02-15 21:55:26 +00002216 if (CDecl->IsClassExtension()) {
2217 // Diagnose if this property is already in continuation class.
2218 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2219 assert(DC && "ClassDecl is not a DeclContext");
2220 DeclContext::lookup_result Found = DC->lookup(FD.D.getIdentifier());
2221 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2222 Diag(AtLoc, diag::err_duplicate_property);
2223 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2224 return DeclPtrTy();
2225 }
2226 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
2227 FD.D.getIdentifierLoc(),
2228 FD.D.getIdentifier(),
2229 AtLoc, T);
2230 DC->addDecl(PDecl);
2231
Mike Stump1eb44332009-09-09 15:08:12 +00002232 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002233 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002234 if ((CCPrimary = CDecl->getClassInterface())) {
2235 // Find the property in continuation class's primary class only.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002236 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002237 if (ObjCPropertyDecl *PIDecl =
2238 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002239 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00002240 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002241 unsigned PIkind = PIDecl->getPropertyAttributes();
2242 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002243 unsigned retainCopyNonatomic =
Fariborz Jahaniand669be52009-11-06 22:59:12 +00002244 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002245 ObjCPropertyDecl::OBJC_PR_copy |
2246 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002247 if ((Attributes & retainCopyNonatomic) !=
2248 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002249 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002250 Diag(PIDecl->getLocation(), diag::note_property_declare);
2251 }
Fariborz Jahanianb73e2812010-01-06 21:38:30 +00002252 DeclContext *DC = dyn_cast<DeclContext>(CCPrimary);
2253 assert(DC && "ClassDecl is not a DeclContext");
2254 DeclContext::lookup_result Found =
2255 DC->lookup(PIDecl->getDeclName());
2256 bool PropertyInPrimaryClass = false;
2257 for (; Found.first != Found.second; ++Found.first)
2258 if (isa<ObjCPropertyDecl>(*Found.first)) {
2259 PropertyInPrimaryClass = true;
2260 break;
2261 }
2262 if (!PropertyInPrimaryClass) {
2263 // Protocol is not in the primary class. Must build one for it.
2264 ObjCDeclSpec ProtocolPropertyODS;
2265 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind and
2266 // ObjCPropertyDecl::PropertyAttributeKind have identical values.
2267 // Should consolidate both into one enum type.
2268 ProtocolPropertyODS.setPropertyAttributes(
2269 (ObjCDeclSpec::ObjCPropertyAttributeKind)PIkind);
2270 DeclPtrTy ProtocolPtrTy =
2271 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
2272 PIDecl->getGetterName(),
2273 PIDecl->getSetterName(),
2274 DeclPtrTy::make(CCPrimary), isOverridingProperty,
2275 MethodImplKind);
2276 PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>();
2277 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002278 PIDecl->makeitReadWriteAttribute();
2279 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2280 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2281 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2282 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2283 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002284 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002285 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002286 << CCPrimary->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002287 Diag(PIDecl->getLocation(), diag::note_property_declare);
2288 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002289 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002290 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00002291 // class's list.
2292 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002293 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002294 }
Fariborz Jahanian25760612010-02-15 21:55:26 +00002295
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002296 // No matching property found in the primary class. Just fall thru
2297 // and add property to continuation class's primary class.
2298 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002299 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00002300 Diag(CDecl->getLocation(), diag::err_continuation_class);
2301 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002302 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002303 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002304 }
Mike Stump1eb44332009-09-09 15:08:12 +00002305
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002306 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00002307 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002308 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2309 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2310 if (T->isObjCObjectPointerType()) {
2311 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00002312 if (const ObjCInterfaceType *OIT =
John McCall183700f2009-09-21 23:43:11 +00002313 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002314 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002315 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002316 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002317 LookupProtocol(&Context.Idents.get("NSCopying")))
2318 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00002319 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002320 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002321 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002322 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00002323 if (T->isObjCInterfaceType())
2324 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002325
Steve Naroff93983f82009-01-11 12:47:58 +00002326 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2327 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002328 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00002329 FD.D.getIdentifierLoc(),
Fariborz Jahaniand0502402010-01-21 17:36:00 +00002330 FD.D.getIdentifier(),
2331 AtLoc, T);
Fariborz Jahanian6dd30fc2009-12-17 00:49:09 +00002332 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
2333 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2334 Diag(PDecl->getLocation(), diag::err_duplicate_property);
2335 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2336 PDecl->setInvalidDecl();
2337 }
2338 else
2339 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002340
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002341 if (T->isArrayType() || T->isFunctionType()) {
2342 Diag(AtLoc, diag::err_property_type) << T;
2343 PDecl->setInvalidDecl();
2344 }
Mike Stump1eb44332009-09-09 15:08:12 +00002345
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002346 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00002347
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00002348 // Regardless of setter/getter attribute, we save the default getter/setter
2349 // selector names in anticipation of declaration of setter/getter methods.
2350 PDecl->setGetterName(GetterSel);
2351 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00002352
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002353 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002354 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00002355
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002356 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002357 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00002358
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002359 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002360 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00002361
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002362 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002363 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00002364
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002365 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002366 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00002367
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002368 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002369 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00002370
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002371 if (isAssign)
2372 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00002373
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002374 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002375 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00002376
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00002377 if (MethodImplKind == tok::objc_required)
2378 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2379 else if (MethodImplKind == tok::objc_optional)
2380 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002381 // A case of continuation class adding a new property in the class. This
2382 // is not what it was meant for. However, gcc supports it and so should we.
2383 // Make sure setter/getters are declared here.
2384 if (CCPrimary)
2385 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002386
Chris Lattnerb28317a2009-03-28 19:18:32 +00002387 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002388}
2389
Fariborz Jahanian77e2dde2010-02-09 21:49:50 +00002390ObjCIvarDecl*
2391Sema::SynthesizeNewPropertyIvar(ObjCInterfaceDecl *IDecl,
2392 IdentifierInfo *NameII) {
2393 ObjCIvarDecl *Ivar = 0;
2394 ObjCPropertyDecl *Prop = LookupPropertyDecl(IDecl, NameII);
2395 if (Prop && !Prop->isInvalidDecl()) {
2396 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
2397 QualType PropType = Context.getCanonicalType(Prop->getType());
2398 assert(EnclosingContext &&
2399 "null DeclContext for synthesized ivar - SynthesizeNewPropertyIvar");
2400 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext,
2401 Prop->getLocation(),
2402 NameII, PropType, /*Dinfo=*/0,
2403 ObjCIvarDecl::Public,
2404 (Expr *)0);
2405 Ivar->setLexicalDeclContext(IDecl);
2406 IDecl->addDecl(Ivar);
2407 Prop->setPropertyIvarDecl(Ivar);
2408 }
2409 return Ivar;
2410}
2411
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002412/// ActOnPropertyImplDecl - This routine performs semantic checks and
2413/// builds the AST node for a property implementation declaration; declared
2414/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002415///
Mike Stump1eb44332009-09-09 15:08:12 +00002416Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002417 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002418 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002419 DeclPtrTy ClassCatImpDecl,
2420 IdentifierInfo *PropertyId,
2421 IdentifierInfo *PropertyIvar) {
2422 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002423 // Make sure we have a context for the property implementation declaration.
2424 if (!ClassImpDecl) {
2425 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002426 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002427 }
2428 ObjCPropertyDecl *property = 0;
2429 ObjCInterfaceDecl* IDecl = 0;
2430 // Find the class or category class where this property must have
2431 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002432 ObjCImplementationDecl *IC = 0;
2433 ObjCCategoryImplDecl* CatImplClass = 0;
2434 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002435 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002436 // We always synthesize an interface for an implementation
2437 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002438 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002439 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002440
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002441 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002442 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002443 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002444 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002445 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002446 }
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002447 if (const ObjCCategoryDecl *CD =
2448 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002449 if (!CD->IsClassExtension()) {
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002450 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002451 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002452 return DeclPtrTy();
2453 }
2454 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002455 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002456 if (Synthesize) {
2457 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002458 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002459 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002460 IDecl = CatImplClass->getClassInterface();
2461 if (!IDecl) {
2462 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002463 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002464 }
Mike Stump1eb44332009-09-09 15:08:12 +00002465 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002466 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002467
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002468 // If category for this implementation not found, it is an error which
2469 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002470 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002471 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002472 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002473 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002474 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002475 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002476 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002477 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002478 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002479 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002480 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002481 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002482 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002483 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002484 // Check that we have a valid, previously declared ivar for @synthesize
2485 if (Synthesize) {
2486 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002487 if (!PropertyIvar)
2488 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002489 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002490 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002491 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002492 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002493 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002494 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002495 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002496 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002497 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2498 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002499 ObjCIvarDecl::Public,
2500 (Expr *)0);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00002501 IDecl->makeDeclVisibleInContext(Ivar, false);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002502 property->setPropertyIvarDecl(Ivar);
2503 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002504 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002505 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002506 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002507 } else if (getLangOptions().ObjCNonFragileABI &&
2508 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002509 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002510 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002511 << ClassDeclared->getDeclName();
2512 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2513 << Ivar << Ivar->getNameAsCString();
2514 // Note! I deliberately want it to fall thru so more errors are caught.
2515 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002516 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002517
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002518 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002519 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002520 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002521 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002522 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002523 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002524 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002525 }
Mike Stump1eb44332009-09-09 15:08:12 +00002526
Chris Lattnerb28317a2009-03-28 19:18:32 +00002527 // FIXME! Rules for properties are somewhat different that those
2528 // for assignments. Use a new routine to consolidate all cases;
2529 // specifically for property redeclarations as well as for ivars.
2530 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2531 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002532 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002533 lhsType->isArithmeticType()) {
2534 Diag(PropertyLoc, diag::error_property_ivar_type)
2535 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002536 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002537 }
2538 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002539 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2540 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002541 Diag(PropertyLoc, diag::error_weak_property)
2542 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002543 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002544 }
Mike Stump1eb44332009-09-09 15:08:12 +00002545 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002546 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2547 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002548 Diag(PropertyLoc, diag::error_strong_property)
2549 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002550 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002551 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002552 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002553 } else if (PropertyIvar)
2554 // @dynamic
2555 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002556 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002557 ObjCPropertyImplDecl *PIDecl =
2558 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2559 property,
2560 (Synthesize ?
2561 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002562 : ObjCPropertyImplDecl::Dynamic),
2563 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002564 if (IC) {
2565 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002566 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002567 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002568 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2569 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002570 << PropertyIvar;
2571 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2572 }
Mike Stump1eb44332009-09-09 15:08:12 +00002573
2574 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002575 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002576 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2577 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002578 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002579 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002580 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002581 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002582 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002583 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002584 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002585 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2586 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002587 << PropertyIvar;
2588 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2589 }
Mike Stump1eb44332009-09-09 15:08:12 +00002590
2591 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002592 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002593 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2594 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002595 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002596 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002597 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002598 }
Mike Stump1eb44332009-09-09 15:08:12 +00002599
Chris Lattnerb28317a2009-03-28 19:18:32 +00002600 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002601}
Anders Carlsson15281452008-11-04 16:57:32 +00002602
Chris Lattnercc98eac2008-12-17 07:13:27 +00002603bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002604 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002605 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002606
Anders Carlsson15281452008-11-04 16:57:32 +00002607 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2608 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Anders Carlsson15281452008-11-04 16:57:32 +00002610 return true;
2611}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002612
Chris Lattnercc98eac2008-12-17 07:13:27 +00002613/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2614/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002615void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002616 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002617 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002618 // Check that ClassName is a valid class
2619 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2620 if (!Class) {
2621 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2622 return;
2623 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002624 if (LangOpts.ObjCNonFragileABI) {
2625 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2626 return;
2627 }
Mike Stump1eb44332009-09-09 15:08:12 +00002628
Chris Lattnercc98eac2008-12-17 07:13:27 +00002629 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002630 llvm::SmallVector<FieldDecl*, 32> RecFields;
2631 Context.CollectObjCIvars(Class, RecFields);
2632 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2633 for (unsigned i = 0; i < RecFields.size(); i++) {
2634 FieldDecl* ID = RecFields[i];
2635 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2636 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2637 ID->getIdentifier(), ID->getType(),
2638 ID->getBitWidth());
2639 Decls.push_back(Sema::DeclPtrTy::make(FD));
2640 }
Mike Stump1eb44332009-09-09 15:08:12 +00002641
Chris Lattnercc98eac2008-12-17 07:13:27 +00002642 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002643 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002644 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002645 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002646 if (getLangOptions().CPlusPlus)
2647 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002648 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002649 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002650 }
2651}
2652