blob: c0e8219f67fcf677bdb34d132a982e8f27a6c480 [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");
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000449 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
450 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000451 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000452 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000453 // Is this property already in category's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000454 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000455 if ((*CP)->getIdentifier() == Pr->getIdentifier())
456 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000457 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000458 // Property protocol already exist in class. Diagnose any mismatch.
459 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
460 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000461 return;
462 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000463 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
464 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000465 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000466 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000467 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000468 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000469 if ((*CP)->getIdentifier() == Pr->getIdentifier())
470 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000471 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000472 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000473 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000474 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000475}
476
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000477/// CompareProperties - This routine compares properties
478/// declared in 'ClassOrProtocol' objects (which can be a class or an
479/// inherited protocol with the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000480///
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000481void Sema::CompareProperties(Decl *CDecl,
482 DeclPtrTy ClassOrProtocol) {
483 Decl *ClassDecl = ClassOrProtocol.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000484 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
485
486 if (!IDecl) {
487 // Category
488 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000489 assert (CatDecl && "CompareProperties");
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000490 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
491 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
492 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000493 // Match properties of category with those of protocol (*P)
494 MatchOneProtocolPropertiesInClass(CatDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000496 // Go thru the list of protocols for this category and recursively match
497 // their properties with those in the category.
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000498 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
499 E = CatDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000500 CompareProperties(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000501 } else {
502 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
503 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
504 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000505 MatchOneProtocolPropertiesInClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000506 }
507 return;
508 }
509
Chris Lattnerb752f282008-07-21 07:06:49 +0000510 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000511 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
512 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000513 // Match properties of class IDecl with those of protocol (*P).
514 MatchOneProtocolPropertiesInClass(IDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000516 // Go thru the list of protocols for this class and recursively match
517 // their properties with those declared in the class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000518 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
519 E = IDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000520 CompareProperties(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000521 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000522 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
523 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
524 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000525 MatchOneProtocolPropertiesInClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000526 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000527}
528
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000529/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000530/// a class method in its extension.
531///
Mike Stump1eb44332009-09-09 15:08:12 +0000532void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000533 ObjCInterfaceDecl *ID) {
534 if (!ID)
535 return; // Possibly due to previous error
536
537 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000538 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
539 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000540 ObjCMethodDecl *MD = *i;
541 MethodMap[MD->getSelector()] = MD;
542 }
543
544 if (MethodMap.empty())
545 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000546 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
547 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000548 ObjCMethodDecl *Method = *i;
549 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
550 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
551 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
552 << Method->getDeclName();
553 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
554 }
555 }
556}
557
Chris Lattner58fe03b2009-04-12 08:43:13 +0000558/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000559Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000560Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000561 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000562 unsigned NumElts,
563 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000564 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000565 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Chris Lattner4d391482007-12-12 07:09:47 +0000567 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000568 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000569 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000570 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000571 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000572 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000573 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000574 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000575 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000576 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000577 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000578 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000579 }
Mike Stump1eb44332009-09-09 15:08:12 +0000580
581 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000582 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000583 Protocols.data(), Protocols.size(),
584 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000585 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000586 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000587 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000588}
589
Chris Lattnerb28317a2009-03-28 19:18:32 +0000590Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000591ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
592 IdentifierInfo *ClassName, SourceLocation ClassLoc,
593 IdentifierInfo *CategoryName,
594 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000595 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000596 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000597 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000598 SourceLocation EndProtoLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000599 ObjCCategoryDecl *CDecl =
Douglas Gregor3db211b2010-01-16 16:38:58 +0000600 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, ClassLoc,
601 CategoryLoc, CategoryName);
Douglas Gregord0434102009-01-09 00:49:46 +0000602 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000603 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000604
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000605 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000606 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000607 if (!IDecl || IDecl->isForwardDecl()) {
608 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000609 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000610 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000611 }
Chris Lattner4d391482007-12-12 07:09:47 +0000612
Chris Lattner70f19542009-02-16 21:26:43 +0000613 CDecl->setClassInterface(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Chris Lattner16b34b42009-02-16 21:30:01 +0000615 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000616 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000617
618 /// Check for duplicate interface declaration for this category
619 ObjCCategoryDecl *CDeclChain;
620 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
621 CDeclChain = CDeclChain->getNextClassCategory()) {
622 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
623 Diag(CategoryLoc, diag::warn_dup_category_def)
624 << ClassName << CategoryName;
625 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
626 break;
627 }
628 }
629 if (!CDeclChain)
630 CDecl->insertNextClassCategory();
631
Chris Lattner4d391482007-12-12 07:09:47 +0000632 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000633 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000634 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000635 // Protocols in the class extension belong to the class.
636 if (!CDecl->getIdentifier())
637 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000638 NumProtoRefs, ProtoLocs,
639 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000640 }
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Anders Carlsson15281452008-11-04 16:57:32 +0000642 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000643 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000644}
645
646/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000647/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000648/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000649Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000650 SourceLocation AtCatImplLoc,
651 IdentifierInfo *ClassName, SourceLocation ClassLoc,
652 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000653 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000654 ObjCCategoryDecl *CatIDecl = 0;
655 if (IDecl) {
656 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
657 if (!CatIDecl) {
658 // Category @implementation with no corresponding @interface.
659 // Create and install one.
660 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000661 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000662 CatName);
663 CatIDecl->setClassInterface(IDecl);
664 CatIDecl->insertNextClassCategory();
665 }
666 }
667
Mike Stump1eb44332009-09-09 15:08:12 +0000668 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000669 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
670 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000671 /// Check that class of this category is already completely declared.
672 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000673 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000674
Douglas Gregord0434102009-01-09 00:49:46 +0000675 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000676 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000677
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000678 /// Check that CatName, category name, is not used in another implementation.
679 if (CatIDecl) {
680 if (CatIDecl->getImplementation()) {
681 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
682 << CatName;
683 Diag(CatIDecl->getImplementation()->getLocation(),
684 diag::note_previous_definition);
685 } else
686 CatIDecl->setImplementation(CDecl);
687 }
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Anders Carlsson15281452008-11-04 16:57:32 +0000689 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000690 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000691}
692
Chris Lattnerb28317a2009-03-28 19:18:32 +0000693Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000694 SourceLocation AtClassImplLoc,
695 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000696 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000697 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000698 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000699 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000700 NamedDecl *PrevDecl
701 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000702 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000703 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000704 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000705 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
706 // If this is a forward declaration of an interface, warn.
707 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000708 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000709 IDecl = 0;
710 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000711 } else {
712 // We did not find anything with the name ClassName; try to correct for
713 // typos in the class name.
714 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
715 if (CorrectTypo(R, TUScope, 0) &&
716 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000717 // Suggest the (potentially) correct interface name. However, put the
718 // fix-it hint itself in a separate note, since changing the name in
719 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000720 // provide a code-modification hint or use the typo name for recovery,
721 // because this is just a warning. The program may actually be correct.
722 Diag(ClassLoc, diag::warn_undef_interface_suggest)
723 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000724 Diag(IDecl->getLocation(), diag::note_previous_decl)
725 << R.getLookupName()
726 << CodeModificationHint::CreateReplacement(ClassLoc,
727 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000728 IDecl = 0;
729 } else {
730 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
731 }
Chris Lattner4d391482007-12-12 07:09:47 +0000732 }
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Chris Lattner4d391482007-12-12 07:09:47 +0000734 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000735 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000736 if (SuperClassname) {
737 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000738 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000739 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000740 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
741 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000742 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000743 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000744 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000745 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000746 Diag(SuperClassLoc, diag::err_undef_superclass)
747 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000748 else if (IDecl && IDecl->getSuperClass() != SDecl) {
749 // This implementation and its interface do not have the same
750 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000751 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000752 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000753 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000754 }
755 }
756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Chris Lattner4d391482007-12-12 07:09:47 +0000758 if (!IDecl) {
759 // Legacy case of @implementation with no corresponding @interface.
760 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000761
Mike Stump390b4cc2009-05-16 07:39:55 +0000762 // FIXME: Do we support attributes on the @implementation? If so we should
763 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000764 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000765 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000766 IDecl->setSuperClass(SDecl);
767 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000768
769 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000770 } else {
771 // Mark the interface as being completed, even if it was just as
772 // @class ....;
773 // declaration; the user cannot reopen it.
774 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
777 ObjCImplementationDecl* IMPDecl =
778 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000779 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Anders Carlsson15281452008-11-04 16:57:32 +0000781 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000782 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Chris Lattner4d391482007-12-12 07:09:47 +0000784 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000785 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000786 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000787 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000788 Diag(IDecl->getImplementation()->getLocation(),
789 diag::note_previous_definition);
790 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000791 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000792 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000793 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000794 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000795}
796
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000797void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
798 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000799 SourceLocation RBrace) {
800 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000801 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000802 if (!IDecl)
803 return;
804 /// Check case of non-existing @interface decl.
805 /// (legacy objective-c @implementation decl without an @interface decl).
806 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000807 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000808 IDecl->setIVarList(ivars, numIvars, Context);
809 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000810 return;
811 }
812 // If implementation has empty ivar list, just return.
813 if (numIvars == 0)
814 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Chris Lattner4d391482007-12-12 07:09:47 +0000816 assert(ivars && "missing @implementation ivars");
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Chris Lattner4d391482007-12-12 07:09:47 +0000818 // Check interface's Ivar list against those in the implementation.
819 // names and types must match.
820 //
Chris Lattner4d391482007-12-12 07:09:47 +0000821 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000822 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000823 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
824 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000825 ObjCIvarDecl* ImplIvar = ivars[j++];
826 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000827 assert (ImplIvar && "missing implementation ivar");
828 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Steve Naroffca331292009-03-03 14:49:36 +0000830 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000831 if (Context.getCanonicalType(ImplIvar->getType()) !=
832 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000833 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000834 << ImplIvar->getIdentifier()
835 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000836 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000837 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
838 Expr *ImplBitWidth = ImplIvar->getBitWidth();
839 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000840 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
841 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000842 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
843 << ImplIvar->getIdentifier();
844 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
845 }
Mike Stump1eb44332009-09-09 15:08:12 +0000846 }
Steve Naroffca331292009-03-03 14:49:36 +0000847 // Make sure the names are identical.
848 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000849 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000850 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000851 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000852 }
853 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000854 }
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Chris Lattner609e4c72007-12-12 18:11:49 +0000856 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000857 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000858 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000859 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000860}
861
Steve Naroff3c2eb662008-02-10 21:38:56 +0000862void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
863 bool &IncompleteImpl) {
864 if (!IncompleteImpl) {
865 Diag(ImpLoc, diag::warn_incomplete_impl);
866 IncompleteImpl = true;
867 }
Chris Lattner08631c52008-11-23 21:45:46 +0000868 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000869}
870
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000871void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
872 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000873 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000874 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000875 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
876 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000877 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000878 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
879 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000880 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Chris Lattner3aff9192009-04-11 19:58:42 +0000883 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
884 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
885 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000886 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
887 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
888 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
889 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000890 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000891
892 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000893 << ImpMethodDecl->getDeclName() << (*IF)->getType()
894 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000895 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000896 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000897}
898
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000899/// isPropertyReadonly - Return true if property is readonly, by searching
900/// for the property in the class and in its categories and implementations
901///
902bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000903 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000904 // by far the most common case.
905 if (!PDecl->isReadOnly())
906 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000907 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000908 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000909 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000910 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000912 // Main class has the property as 'readonly'. Must search
Mike Stump1eb44332009-09-09 15:08:12 +0000913 // through the category list to see if the property's
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000914 // attribute has been over-ridden to 'readwrite'.
915 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
916 Category; Category = Category->getNextClassCategory()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000917 // Even if property is ready only, if a category has a user defined setter,
918 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000919 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000920 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000921 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000922 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000923 if (P && !P->isReadOnly())
924 return false;
925 }
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000927 // Also, check for definition of a setter method in the implementation if
928 // all else failed.
929 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000930 if (ObjCImplementationDecl *IMD =
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000931 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000932 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000933 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000934 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000935 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000936 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000937 return false;
938 }
939 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000940 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000941 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000942 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000943 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000944 // If all fails, look at the super class.
945 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
946 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000947 return true;
948}
949
Mike Stump390b4cc2009-05-16 07:39:55 +0000950/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
951/// improve the efficiency of selector lookups and type checking by associating
952/// with each protocol / interface / category the flattened instance tables. If
953/// we used an immutable set to keep the table then it wouldn't add significant
954/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000955
Steve Naroffefe7f362008-02-08 22:06:17 +0000956/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000957/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000958void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
959 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000960 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000961 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000962 const llvm::DenseSet<Selector> &ClsMap,
963 ObjCInterfaceDecl *IDecl) {
964 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000965 ObjCInterfaceDecl *NSIDecl = 0;
966 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000967 // check to see if class implements forwardInvocation method and objects
968 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000969 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000970 // Under such conditions, which means that every method possible is
971 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000972 // found" warnings.
973 // FIXME: Use a general GetUnarySelector method for this.
974 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
975 Selector fISelector = Context.Selectors.getSelector(1, &II);
976 if (InsMap.count(fISelector))
977 // Is IDecl derived from 'NSProxy'? If so, no instance methods
978 // need be implemented in the implementation.
979 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
980 }
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000982 // If a method lookup fails locally we still need to look and see if
983 // the method was implemented by a base class or an inherited
984 // protocol. This lookup is slow, but occurs rarely in correct code
985 // and otherwise would terminate in a warning.
986
Chris Lattner4d391482007-12-12 07:09:47 +0000987 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000988 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000989 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000990 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000991 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000992 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000993 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000994 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000995 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000996 // Ugly, but necessary. Method declared in protcol might have
997 // have been synthesized due to a property declared in the class which
998 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000999 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001000 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001001 if (!MethodInClass || !MethodInClass->isSynthesized())
1002 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
1003 }
1004 }
Chris Lattner4d391482007-12-12 07:09:47 +00001005 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001006 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001007 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001008 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001009 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001010 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1011 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001012 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +00001013 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001014 }
Chris Lattner780f3292008-07-21 21:32:27 +00001015 // Check on this protocols's referenced protocols, recursively.
1016 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1017 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001018 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001019}
1020
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001021/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1022/// or protocol against those declared in their implementations.
1023///
1024void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1025 const llvm::DenseSet<Selector> &ClsMap,
1026 llvm::DenseSet<Selector> &InsMapSeen,
1027 llvm::DenseSet<Selector> &ClsMapSeen,
1028 ObjCImplDecl* IMPDecl,
1029 ObjCContainerDecl* CDecl,
1030 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001031 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001032 // Check and see if instance methods in class interface have been
1033 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001034 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1035 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001036 if (InsMapSeen.count((*I)->getSelector()))
1037 continue;
1038 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001039 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001040 !InsMap.count((*I)->getSelector())) {
1041 if (ImmediateClass)
1042 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1043 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001044 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001045 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001046 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001047 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001048 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001049 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001050 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1051 // ImpMethodDecl may be null as in a @dynamic property.
1052 if (ImpMethodDecl)
1053 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1054 }
1055 }
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001057 // Check and see if class methods in class interface have been
1058 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001059 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001060 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001061 if (ClsMapSeen.count((*I)->getSelector()))
1062 continue;
1063 ClsMapSeen.insert((*I)->getSelector());
1064 if (!ClsMap.count((*I)->getSelector())) {
1065 if (ImmediateClass)
1066 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001067 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001068 ObjCMethodDecl *ImpMethodDecl =
1069 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001070 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001071 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001072 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1073 }
1074 }
1075 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1076 // Check for any implementation of a methods declared in protocol.
1077 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1078 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001079 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1080 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001081 (*PI), IncompleteImpl, false);
1082 if (I->getSuperClass())
1083 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001084 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001085 I->getSuperClass(), IncompleteImpl, false);
1086 }
1087}
1088
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001089/// CollectImmediateProperties - This routine collects all properties in
1090/// the class and its conforming protocols; but not those it its super class.
1091void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
1092 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1093 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1094 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1095 E = IDecl->prop_end(); P != E; ++P) {
1096 ObjCPropertyDecl *Prop = (*P);
1097 PropMap[Prop->getIdentifier()] = Prop;
1098 }
1099 // scan through class's protocols.
1100 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
1101 E = IDecl->protocol_end(); PI != E; ++PI)
1102 CollectImmediateProperties((*PI), PropMap);
1103 }
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001104 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1105 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1106 E = CATDecl->prop_end(); P != E; ++P) {
1107 ObjCPropertyDecl *Prop = (*P);
1108 PropMap[Prop->getIdentifier()] = Prop;
1109 }
1110 // scan through class's protocols.
1111 for (ObjCInterfaceDecl::protocol_iterator PI = CATDecl->protocol_begin(),
1112 E = CATDecl->protocol_end(); PI != E; ++PI)
1113 CollectImmediateProperties((*PI), PropMap);
1114 }
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001115 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1116 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1117 E = PDecl->prop_end(); P != E; ++P) {
1118 ObjCPropertyDecl *Prop = (*P);
1119 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1120 if (!PropEntry)
1121 PropEntry = Prop;
1122 }
1123 // scan through protocol's protocols.
1124 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1125 E = PDecl->protocol_end(); PI != E; ++PI)
1126 CollectImmediateProperties((*PI), PropMap);
1127 }
1128}
1129
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001130/// LookupPropertyDecl - Looks up a property in the current class and all
1131/// its protocols.
1132ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1133 IdentifierInfo *II) {
1134 if (const ObjCInterfaceDecl *IDecl =
1135 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1136 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1137 E = IDecl->prop_end(); P != E; ++P) {
1138 ObjCPropertyDecl *Prop = (*P);
1139 if (Prop->getIdentifier() == II)
1140 return Prop;
1141 }
1142 // scan through class's protocols.
1143 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
1144 E = IDecl->protocol_end(); PI != E; ++PI) {
1145 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1146 if (Prop)
1147 return Prop;
1148 }
1149 }
1150 else if (const ObjCProtocolDecl *PDecl =
1151 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1152 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1153 E = PDecl->prop_end(); P != E; ++P) {
1154 ObjCPropertyDecl *Prop = (*P);
1155 if (Prop->getIdentifier() == II)
1156 return Prop;
1157 }
1158 // scan through protocol's protocols.
1159 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1160 E = PDecl->protocol_end(); PI != E; ++PI) {
1161 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1162 if (Prop)
1163 return Prop;
1164 }
1165 }
1166 return 0;
1167}
1168
1169
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001170void Sema::DiagnoseUnimplementedProperties(ObjCImplDecl* IMPDecl,
1171 ObjCContainerDecl *CDecl,
1172 const llvm::DenseSet<Selector>& InsMap) {
1173 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1174 CollectImmediateProperties(CDecl, PropMap);
Fariborz Jahaniana84f2e42010-01-20 17:27:59 +00001175 if (PropMap.empty())
1176 return;
1177
1178 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1179 for (ObjCImplDecl::propimpl_iterator
1180 I = IMPDecl->propimpl_begin(),
1181 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1182 PropImplMap.insert((*I)->getPropertyDecl());
1183
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001184 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1185 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1186 ObjCPropertyDecl *Prop = P->second;
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001187 // Is there a matching propery synthesize/dynamic?
Fariborz Jahaniana84f2e42010-01-20 17:27:59 +00001188 if (Prop->isInvalidDecl() ||
1189 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1190 PropImplMap.count(Prop))
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001191 continue;
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001192 if (LangOpts.ObjCNonFragileABI2) {
1193 ActOnPropertyImplDecl(IMPDecl->getLocation(),
1194 SourceLocation(),
1195 true, DeclPtrTy::make(IMPDecl),
1196 Prop->getIdentifier(),
1197 Prop->getIdentifier());
1198 continue;
1199 }
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001200 if (!InsMap.count(Prop->getGetterName())) {
1201 Diag(Prop->getLocation(),
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001202 isa<ObjCCategoryDecl>(CDecl) ?
1203 diag::warn_setter_getter_impl_required_in_category :
1204 diag::warn_setter_getter_impl_required)
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001205 << Prop->getDeclName() << Prop->getGetterName();
1206 Diag(IMPDecl->getLocation(),
1207 diag::note_property_impl_required);
1208 }
1209
1210 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1211 Diag(Prop->getLocation(),
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001212 isa<ObjCCategoryDecl>(CDecl) ?
1213 diag::warn_setter_getter_impl_required_in_category :
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001214 diag::warn_setter_getter_impl_required)
1215 << Prop->getDeclName() << Prop->getSetterName();
1216 Diag(IMPDecl->getLocation(),
1217 diag::note_property_impl_required);
1218 }
1219 }
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001220}
1221
Mike Stump1eb44332009-09-09 15:08:12 +00001222void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1223 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001224 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001225 llvm::DenseSet<Selector> InsMap;
1226 // Check and see if instance methods in class interface have been
1227 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001228 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001229 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001230 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001232 // Check and see if properties declared in the interface have either 1)
1233 // an implementation or 2) there is a @synthesize/@dynamic implementation
1234 // of the property in the @implementation.
1235 if (isa<ObjCInterfaceDecl>(CDecl))
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001236 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
1237
Chris Lattner4d391482007-12-12 07:09:47 +00001238 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001239 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001240 I = IMPDecl->classmeth_begin(),
1241 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001242 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001244 // Check for type conflict of methods declared in a class/protocol and
1245 // its implementation; if any.
1246 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001247 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1248 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001249 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Chris Lattner4d391482007-12-12 07:09:47 +00001251 // Check the protocol list for unimplemented methods in the @implementation
1252 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001253 // Check and see if class methods in class interface have been
1254 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Chris Lattnercddc8882009-03-01 00:56:52 +00001256 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001257 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001258 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001259 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001260 InsMap, ClsMap, I);
1261 // Check class extensions (unnamed categories)
1262 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1263 Categories; Categories = Categories->getNextClassCategory()) {
1264 if (!Categories->getIdentifier()) {
1265 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1266 break;
1267 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001268 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001269 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001270 // For extended class, unimplemented methods in its protocols will
1271 // be reported in the primary class.
1272 if (C->getIdentifier()) {
1273 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1274 E = C->protocol_end(); PI != E; ++PI)
1275 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1276 InsMap, ClsMap, C->getClassInterface());
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001277 // Report unimplemented properties in the category as well.
1278 // When reporting on missing setter/getters, do not report when
1279 // setter/getter is implemented in category's primary class
1280 // implementation.
1281 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1282 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1283 for (ObjCImplementationDecl::instmeth_iterator
1284 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1285 InsMap.insert((*I)->getSelector());
1286 }
1287 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
1288 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001289 } else
1290 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001291}
1292
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001293void
1294Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1295 ObjCContainerDecl* IDecl) {
1296 // Rules apply in non-GC mode only
1297 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1298 return;
1299 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1300 E = IDecl->prop_end();
1301 I != E; ++I) {
1302 ObjCPropertyDecl *Property = (*I);
1303 unsigned Attributes = Property->getPropertyAttributes();
1304 // We only care about readwrite atomic property.
1305 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1306 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1307 continue;
1308 if (const ObjCPropertyImplDecl *PIDecl
1309 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1310 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1311 continue;
1312 ObjCMethodDecl *GetterMethod =
1313 IMPDecl->getInstanceMethod(Property->getGetterName());
1314 ObjCMethodDecl *SetterMethod =
1315 IMPDecl->getInstanceMethod(Property->getSetterName());
1316 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1317 SourceLocation MethodLoc =
1318 (GetterMethod ? GetterMethod->getLocation()
1319 : SetterMethod->getLocation());
1320 Diag(MethodLoc, diag::warn_atomic_property_rule)
1321 << Property->getIdentifier();
1322 Diag(Property->getLocation(), diag::note_property_declare);
1323 }
1324 }
1325 }
1326}
1327
Mike Stump1eb44332009-09-09 15:08:12 +00001328/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001329Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001330Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001331 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001332 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001333 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001334 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Chris Lattner4d391482007-12-12 07:09:47 +00001336 for (unsigned i = 0; i != NumElts; ++i) {
1337 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001338 NamedDecl *PrevDecl
1339 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001340 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001341 // Maybe we will complain about the shadowed template parameter.
1342 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1343 // Just pretend that we didn't see the previous declaration.
1344 PrevDecl = 0;
1345 }
1346
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001347 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001348 // GCC apparently allows the following idiom:
1349 //
1350 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1351 // @class XCElementToggler;
1352 //
Mike Stump1eb44332009-09-09 15:08:12 +00001353 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001354 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1355 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001356 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001357 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001358 } else if (TDD) {
1359 // a forward class declaration matching a typedef name of a class refers
1360 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001361 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001362 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1363 PrevDecl = OI->getDecl();
1364 }
Chris Lattner4d391482007-12-12 07:09:47 +00001365 }
Mike Stump1eb44332009-09-09 15:08:12 +00001366 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001367 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001368 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001369 IdentList[i], IdentLocs[i], true);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001370
1371 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1372 // the current DeclContext. This prevents clients that walk DeclContext
1373 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1374 // declared later (if at all). We also take care to explicitly make
1375 // sure this declaration is visible for name lookup.
1376 PushOnScopeChains(IDecl, TUScope, false);
1377 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001378 }
1379
1380 Interfaces.push_back(IDecl);
1381 }
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Ted Kremenek321c22f2009-11-18 00:28:11 +00001383 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001384 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001385 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001386 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001387 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001388 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001389 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001390}
1391
1392
1393/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1394/// returns true, or false, accordingly.
1395/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001396bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001397 const ObjCMethodDecl *PrevMethod,
1398 bool matchBasedOnSizeAndAlignment) {
1399 QualType T1 = Context.getCanonicalType(Method->getResultType());
1400 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001402 if (T1 != T2) {
1403 // The result types are different.
1404 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001405 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001406 // Incomplete types don't have a size and alignment.
1407 if (T1->isIncompleteType() || T2->isIncompleteType())
1408 return false;
1409 // Check is based on size and alignment.
1410 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1411 return false;
1412 }
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Chris Lattner89951a82009-02-20 18:43:26 +00001414 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1415 E = Method->param_end();
1416 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Chris Lattner89951a82009-02-20 18:43:26 +00001418 for (; ParamI != E; ++ParamI, ++PrevI) {
1419 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1420 T1 = Context.getCanonicalType((*ParamI)->getType());
1421 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001422 if (T1 != T2) {
1423 // The result types are different.
1424 if (!matchBasedOnSizeAndAlignment)
1425 return false;
1426 // Incomplete types don't have a size and alignment.
1427 if (T1->isIncompleteType() || T2->isIncompleteType())
1428 return false;
1429 // Check is based on size and alignment.
1430 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1431 return false;
1432 }
Chris Lattner4d391482007-12-12 07:09:47 +00001433 }
1434 return true;
1435}
1436
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001437/// \brief Read the contents of the instance and factory method pools
1438/// for a given selector from external storage.
1439///
1440/// This routine should only be called once, when neither the instance
1441/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001442Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001443 bool isInstance) {
1444 assert(ExternalSource && "We need an external AST source");
1445 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1446 "Selector data already loaded into the instance method pool");
1447 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1448 "Selector data already loaded into the factory method pool");
1449
1450 // Read the method list from the external source.
1451 std::pair<ObjCMethodList, ObjCMethodList> Methods
1452 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001454 if (isInstance) {
1455 if (Methods.second.Method)
1456 FactoryMethodPool[Sel] = Methods.second;
1457 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001458 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001459
1460 if (Methods.first.Method)
1461 InstanceMethodPool[Sel] = Methods.first;
1462
1463 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1464}
1465
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001466void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001467 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1468 = InstanceMethodPool.find(Method->getSelector());
1469 if (Pos == InstanceMethodPool.end()) {
1470 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1471 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1472 else
1473 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1474 ObjCMethodList())).first;
1475 }
1476
1477 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001478 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001479 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001480 Entry.Method = Method;
1481 Entry.Next = 0;
1482 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001483 }
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Chris Lattnerb25df352009-03-04 05:16:45 +00001485 // We've seen a method with this name, see if we have already seen this type
1486 // signature.
1487 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1488 if (MatchTwoMethodDeclarations(Method, List->Method))
1489 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Chris Lattnerb25df352009-03-04 05:16:45 +00001491 // We have a new signature for an existing method - add it.
1492 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1493 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001494}
1495
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001496// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001497ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001498 SourceRange R,
1499 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001500 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1501 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001502 if (Pos == InstanceMethodPool.end()) {
1503 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001504 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1505 else
1506 return 0;
1507 }
1508
1509 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001510 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Steve Naroff037cda52008-09-30 14:38:43 +00001512 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001513 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1514 // This checks if the methods differ by size & alignment.
1515 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001516 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001517 }
1518 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001519 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001520 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001521 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001522 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001523 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001524 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001525 }
1526 return MethList.Method;
1527}
1528
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001529void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001530 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1531 = FactoryMethodPool.find(Method->getSelector());
1532 if (Pos == FactoryMethodPool.end()) {
1533 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1534 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1535 else
1536 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1537 ObjCMethodList())).first;
1538 }
1539
1540 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001541 if (!FirstMethod.Method) {
1542 // Haven't seen a method with this selector name yet - add it.
1543 FirstMethod.Method = Method;
1544 FirstMethod.Next = 0;
1545 } else {
1546 // We've seen a method with this name, now check the type signature(s).
1547 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001548
1549 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001550 Next = Next->Next)
1551 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Chris Lattner4d391482007-12-12 07:09:47 +00001553 if (!match) {
1554 // We have a new signature for an existing method - add it.
1555 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001556 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001557 FirstMethod.Next = OMI;
1558 }
1559 }
1560}
1561
Mike Stump1eb44332009-09-09 15:08:12 +00001562ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001563 SourceRange R) {
1564 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1565 = FactoryMethodPool.find(Sel);
1566 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001567 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001568 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1569 else
1570 return 0;
1571 }
1572
1573 ObjCMethodList &MethList = Pos->second;
1574 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001576 if (MethList.Method && MethList.Next) {
1577 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1578 // This checks if the methods differ by size & alignment.
1579 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1580 issueWarning = true;
1581 }
1582 if (issueWarning && (MethList.Method && MethList.Next)) {
1583 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001584 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001585 << MethList.Method->getSourceRange();
1586 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001587 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001588 << Next->Method->getSourceRange();
1589 }
1590 return MethList.Method;
1591}
1592
Mike Stump1eb44332009-09-09 15:08:12 +00001593/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff0701bbb2009-01-08 17:28:14 +00001594/// have the property type and issue diagnostics if they don't.
1595/// Also synthesize a getter/setter method if none exist (and update the
1596/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1597/// methods is the "right" thing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001598void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001599 ObjCContainerDecl *CD) {
1600 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00001601
1602 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001603 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump1eb44332009-09-09 15:08:12 +00001604 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001605 property->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001607 if (SetterMethod) {
Fariborz Jahanian02deae82010-01-06 00:18:12 +00001608 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1609 property->getPropertyAttributes();
1610 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1611 Context.getCanonicalType(SetterMethod->getResultType()) !=
1612 Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001613 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001614 if (SetterMethod->param_size() != 1 ||
1615 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001616 Diag(property->getLocation(),
1617 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001618 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001619 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001620 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1621 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001622 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001623
1624 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001625 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001626 // FIXME: The synthesized property we set here is misleading. We almost always
1627 // synthesize these methods unless the user explicitly provided prototypes
1628 // (which is odd, but allowed). Sema should be typechecking that the
1629 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001630 if (!GetterMethod) {
1631 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001632 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001633 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001634 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1635 property->getLocation(), property->getGetterName(),
1636 property->getType(), CD, true, false, true,
1637 (property->getPropertyImplementation() ==
1638 ObjCPropertyDecl::Optional) ?
1639 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001640 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001641 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001642 } else
1643 // A user declared getter will be synthesize when @synthesize of
1644 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001645 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001646 property->setGetterMethodDecl(GetterMethod);
1647
1648 // Skip setter if property is read-only.
1649 if (!property->isReadOnly()) {
1650 // Find the default setter and if one not found, add one.
1651 if (!SetterMethod) {
1652 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001653 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001654 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001655 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1656 property->getLocation(),
1657 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001658 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001659 (property->getPropertyImplementation() ==
1660 ObjCPropertyDecl::Optional) ?
1661 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001662 ObjCMethodDecl::Required);
1663 // Invent the arguments for the setter. We don't bother making a
1664 // nice name for the argument.
1665 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001666 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001667 property->getIdentifier(),
1668 property->getType(),
John McCalla93c9342009-12-07 02:54:59 +00001669 /*TInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001670 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001671 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001672 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001673 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001674 } else
1675 // A user declared setter will be synthesize when @synthesize of
1676 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001677 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001678 property->setSetterMethodDecl(SetterMethod);
1679 }
Mike Stump1eb44332009-09-09 15:08:12 +00001680 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001681 // handle the following, which is supported by GCC (and part of the design).
1682 //
1683 // @interface Foo
1684 // @property double bar;
1685 // @end
1686 //
1687 // void thisIsUnfortunate() {
1688 // id foo;
1689 // double bar = [foo bar];
1690 // }
1691 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001692 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001693 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001694 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001695 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001696}
1697
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001698/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1699/// identical selector names in current and its super classes and issues
1700/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001701void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1702 ObjCMethodDecl *Method,
1703 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001704 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1705 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001707 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001708 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001709 SD->lookupMethod(Method->getSelector(), IsInstance);
1710 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001711 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001712 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001713 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001714 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1715 E = Method->param_end();
1716 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1717 for (; ParamI != E; ++ParamI, ++PrevI) {
1718 // Number of parameters are the same and is guaranteed by selector match.
1719 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1720 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1721 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1722 // If type of arguement of method in this class does not match its
1723 // respective argument type in the super class method, issue warning;
1724 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001725 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001726 << T1 << T2;
1727 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1728 return;
1729 }
1730 }
1731 ID = SD;
1732 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001733}
1734
Steve Naroffa56f6162007-12-18 01:30:32 +00001735// Note: For class/category implemenations, allMethods/allProperties is
1736// always null.
Ted Kremenek782f2f52010-01-07 01:20:12 +00001737void Sema::ActOnAtEnd(SourceRange AtEnd,
1738 DeclPtrTy classDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001739 DeclPtrTy *allMethods, unsigned allNum,
1740 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001741 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001742 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001743
Steve Naroffa56f6162007-12-18 01:30:32 +00001744 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1745 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001746 // should be true.
1747 if (!ClassDecl)
1748 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001749
Mike Stump1eb44332009-09-09 15:08:12 +00001750 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001751 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1752 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001753 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001754
Ted Kremenek782f2f52010-01-07 01:20:12 +00001755 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1756 // FIXME: This is wrong. We shouldn't be pretending that there is
1757 // an '@end' in the declaration.
1758 SourceLocation L = ClassDecl->getLocation();
1759 AtEnd.setBegin(L);
1760 AtEnd.setEnd(L);
1761 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001762 }
1763
Steve Naroff0701bbb2009-01-08 17:28:14 +00001764 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001765
1766 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1767 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1768 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1769
Chris Lattner4d391482007-12-12 07:09:47 +00001770 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001771 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001772 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001773
1774 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001775 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001776 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001777 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001778 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001779 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001780 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001781 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001782 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001783 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001784 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001785 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001786 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001787 InsMap[Method->getSelector()] = Method;
1788 /// The following allows us to typecheck messages to "id".
1789 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001790 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001791 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001792 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001793 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001794 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001795 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001796 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001797 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001798 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001799 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001800 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001801 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001802 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001803 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001804 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001805 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001806 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001807 /// The following allows us to typecheck messages to "Class".
1808 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001809 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001810 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001811 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001812 }
1813 }
1814 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001815 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001816 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001817 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001818 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001819 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001820 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001821 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001822 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001823 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001824
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001825 // Compare protocol properties with those in category
1826 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001827 if (C->getIdentifier() == 0)
1828 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001829 }
Steve Naroff09c47192009-01-09 15:36:25 +00001830 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1831 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1832 // user-defined setter/getter. It also synthesizes setter/getter methods
1833 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001834 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1835 E = CDecl->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001836 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001837 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001838 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001839 }
1840 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001841 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001842 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001843 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001844 AtomicPropertySetterGetterRules(IC, IDecl);
1845 }
Mike Stump1eb44332009-09-09 15:08:12 +00001846 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001847 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001848 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Chris Lattner4d391482007-12-12 07:09:47 +00001850 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001851 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001852 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001853 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001854 Categories; Categories = Categories->getNextClassCategory()) {
1855 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001856 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001857 break;
1858 }
1859 }
1860 }
1861 }
Chris Lattner682bf922009-03-29 16:50:03 +00001862 if (isInterfaceDeclKind) {
1863 // Reject invalid vardecls.
1864 for (unsigned i = 0; i != tuvNum; i++) {
1865 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1866 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1867 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001868 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001869 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001870 }
Chris Lattner682bf922009-03-29 16:50:03 +00001871 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001872 }
Chris Lattner4d391482007-12-12 07:09:47 +00001873}
1874
1875
1876/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1877/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001878static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001879CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1880 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1881 if (PQTVal & ObjCDeclSpec::DQ_In)
1882 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1883 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1884 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1885 if (PQTVal & ObjCDeclSpec::DQ_Out)
1886 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1887 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1888 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1889 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1890 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1891 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1892 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001893
1894 return ret;
1895}
1896
Chris Lattnerb28317a2009-03-28 19:18:32 +00001897Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001898 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001899 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001900 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001901 Selector Sel,
1902 // optional arguments. The number of types/arguments is obtained
1903 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001904 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001905 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001906 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1907 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001908 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001909
1910 // Make sure we can establish a context for the method.
1911 if (!ClassDecl) {
1912 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanian32844b32009-08-28 17:52:37 +00001913 FunctionLabelMap.clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001914 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001915 }
Chris Lattner4d391482007-12-12 07:09:47 +00001916 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001917
Steve Naroffccef3712009-02-20 22:59:16 +00001918 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001919 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Steve Naroffccef3712009-02-20 22:59:16 +00001921 // Methods cannot return interface types. All ObjC objects are
1922 // passed by reference.
1923 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001924 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1925 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001926 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001927 }
1928 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001929 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001930
1931 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001932 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00001933 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001934 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001935 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001936 MethodDeclKind == tok::objc_optional ?
1937 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001938 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001939
Chris Lattner0ed844b2008-04-04 06:12:32 +00001940 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Chris Lattner7db638d2009-04-11 19:42:43 +00001942 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001943 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001944 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001945
Chris Lattnere294d3f2009-04-11 18:57:04 +00001946 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001947 ArgType = Context.getObjCIdType();
1948 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001949 } else {
John McCall58e46772009-10-23 21:48:59 +00001950 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001951 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001952 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001953 }
Mike Stump1eb44332009-09-09 15:08:12 +00001954
John McCall58e46772009-10-23 21:48:59 +00001955 ParmVarDecl* Param
1956 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1957 ArgInfo[i].Name, ArgType, DI,
1958 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001960 if (ArgType->isObjCInterfaceType()) {
1961 Diag(ArgInfo[i].NameLoc,
1962 diag::err_object_cannot_be_passed_returned_by_value)
1963 << 1 << ArgType;
1964 Param->setInvalidDecl();
1965 }
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Chris Lattner0ed844b2008-04-04 06:12:32 +00001967 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001968 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001970 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001971 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Chris Lattner0ed844b2008-04-04 06:12:32 +00001973 Params.push_back(Param);
1974 }
1975
Jay Foadbeaaccd2009-05-21 09:52:38 +00001976 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001977 ObjCMethod->setObjCDeclQualifier(
1978 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1979 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001980
1981 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001982 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001983
John McCall54abf7d2009-11-04 02:18:39 +00001984 const ObjCMethodDecl *InterfaceMD = 0;
1985
Mike Stump1eb44332009-09-09 15:08:12 +00001986 // For implementations (which can be very "coarse grain"), we add the
1987 // method now. This allows the AST to implement lookup methods that work
1988 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001989 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001990 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001991 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001992 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001993 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1994 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001995 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001996 PrevMethod = ImpDecl->getClassMethod(Sel);
1997 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001998 }
John McCall54abf7d2009-11-04 02:18:39 +00001999 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
2000 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002001 if (AttrList)
2002 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00002003 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002004 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002005 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002006 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
2007 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002008 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002009 PrevMethod = CatImpDecl->getClassMethod(Sel);
2010 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002011 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002012 if (AttrList)
2013 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00002014 }
2015 if (PrevMethod) {
2016 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002017 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002018 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002019 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002020 }
John McCall54abf7d2009-11-04 02:18:39 +00002021
2022 // If the interface declared this method, and it was deprecated there,
2023 // mark it deprecated here.
2024 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
2025 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
2026
Chris Lattnerb28317a2009-03-28 19:18:32 +00002027 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002028}
2029
Mike Stump1eb44332009-09-09 15:08:12 +00002030void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002031 SourceLocation Loc,
2032 unsigned &Attributes) {
2033 // FIXME: Improve the reported location.
2034
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002035 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002036 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002037 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
2038 ObjCDeclSpec::DQ_PR_assign |
2039 ObjCDeclSpec::DQ_PR_copy |
2040 ObjCDeclSpec::DQ_PR_retain))) {
2041 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
2042 "readwrite" :
2043 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
2044 "assign" :
2045 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
2046 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00002047
2048 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00002049 diag::err_objc_property_attr_mutually_exclusive :
2050 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002051 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002052 }
2053
2054 // Check for copy or retain on non-object types.
2055 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00002056 !PropertyTy->isObjCObjectPointerType() &&
2057 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00002058 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002059 Diag(Loc, diag::err_objc_property_requires_object)
2060 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002061 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
2062 }
2063
2064 // Check for more than one of { assign, copy, retain }.
2065 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2066 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002067 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2068 << "assign" << "copy";
2069 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00002070 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002071 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002072 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2073 << "assign" << "retain";
2074 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002075 }
2076 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2077 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002078 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2079 << "copy" << "retain";
2080 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002081 }
2082 }
2083
2084 // Warn if user supplied no assignment attribute, property is
2085 // readwrite, and this is an object type.
2086 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
2087 ObjCDeclSpec::DQ_PR_retain)) &&
2088 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00002089 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002090 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00002091 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002092 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
2093
2094 // If non-gc code warn that this is likely inappropriate.
2095 if (getLangOptions().getGCMode() == LangOptions::NonGC)
2096 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002097
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002098 // FIXME: Implement warning dependent on NSCopying being
2099 // implemented. See also:
2100 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2101 // (please trim this list while you are at it).
2102 }
Mike Stump046efd92009-05-07 23:06:50 +00002103
2104 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2105 && getLangOptions().getGCMode() == LangOptions::GCOnly
2106 && PropertyTy->isBlockPointerType())
2107 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002108}
2109
Mike Stump1eb44332009-09-09 15:08:12 +00002110Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002111 FieldDeclarator &FD,
2112 ObjCDeclSpec &ODS,
2113 Selector GetterSel,
2114 Selector SetterSel,
2115 DeclPtrTy ClassCategory,
2116 bool *isOverridingProperty,
2117 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002118 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002119 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
2120 // default is readwrite!
2121 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00002122 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002123 // not retain or copy
2124 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00002125 (isReadWrite &&
2126 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002127 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
2128 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahaniandd69aae2009-12-16 18:03:30 +00002129 if (T->isReferenceType()) {
2130 Diag(AtLoc, diag::error_reference_property);
2131 return DeclPtrTy();
2132 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002133 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002134 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002135 // May modify Attributes.
2136 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002137 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2138 if (!CDecl->getIdentifier()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002139 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002140 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002141 if ((CCPrimary = CDecl->getClassInterface())) {
2142 // Find the property in continuation class's primary class only.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002143 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002144 if (ObjCPropertyDecl *PIDecl =
2145 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002146 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00002147 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002148 unsigned PIkind = PIDecl->getPropertyAttributes();
2149 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002150 unsigned retainCopyNonatomic =
Fariborz Jahaniand669be52009-11-06 22:59:12 +00002151 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002152 ObjCPropertyDecl::OBJC_PR_copy |
2153 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002154 if ((Attributes & retainCopyNonatomic) !=
2155 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002156 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002157 Diag(PIDecl->getLocation(), diag::note_property_declare);
2158 }
Fariborz Jahanianb73e2812010-01-06 21:38:30 +00002159 DeclContext *DC = dyn_cast<DeclContext>(CCPrimary);
2160 assert(DC && "ClassDecl is not a DeclContext");
2161 DeclContext::lookup_result Found =
2162 DC->lookup(PIDecl->getDeclName());
2163 bool PropertyInPrimaryClass = false;
2164 for (; Found.first != Found.second; ++Found.first)
2165 if (isa<ObjCPropertyDecl>(*Found.first)) {
2166 PropertyInPrimaryClass = true;
2167 break;
2168 }
2169 if (!PropertyInPrimaryClass) {
2170 // Protocol is not in the primary class. Must build one for it.
2171 ObjCDeclSpec ProtocolPropertyODS;
2172 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind and
2173 // ObjCPropertyDecl::PropertyAttributeKind have identical values.
2174 // Should consolidate both into one enum type.
2175 ProtocolPropertyODS.setPropertyAttributes(
2176 (ObjCDeclSpec::ObjCPropertyAttributeKind)PIkind);
2177 DeclPtrTy ProtocolPtrTy =
2178 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
2179 PIDecl->getGetterName(),
2180 PIDecl->getSetterName(),
2181 DeclPtrTy::make(CCPrimary), isOverridingProperty,
2182 MethodImplKind);
2183 PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>();
2184 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002185 PIDecl->makeitReadWriteAttribute();
2186 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2187 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2188 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2189 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2190 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002191 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002192 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002193 << CCPrimary->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002194 Diag(PIDecl->getLocation(), diag::note_property_declare);
2195 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002196 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002197 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00002198 // class's list.
2199 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002200 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002201 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002202 // No matching property found in the primary class. Just fall thru
2203 // and add property to continuation class's primary class.
2204 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002205 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00002206 Diag(CDecl->getLocation(), diag::err_continuation_class);
2207 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002208 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002209 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002210 }
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002212 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00002213 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002214 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2215 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2216 if (T->isObjCObjectPointerType()) {
2217 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00002218 if (const ObjCInterfaceType *OIT =
John McCall183700f2009-09-21 23:43:11 +00002219 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002220 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002221 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002222 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002223 LookupProtocol(&Context.Idents.get("NSCopying")))
2224 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00002225 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002226 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002227 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002228 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00002229 if (T->isObjCInterfaceType())
2230 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002231
Steve Naroff93983f82009-01-11 12:47:58 +00002232 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2233 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002234 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00002235 FD.D.getIdentifierLoc(),
Fariborz Jahaniand0502402010-01-21 17:36:00 +00002236 FD.D.getIdentifier(),
2237 AtLoc, T);
Fariborz Jahanian6dd30fc2009-12-17 00:49:09 +00002238 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
2239 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2240 Diag(PDecl->getLocation(), diag::err_duplicate_property);
2241 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2242 PDecl->setInvalidDecl();
2243 }
2244 else
2245 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002246
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002247 if (T->isArrayType() || T->isFunctionType()) {
2248 Diag(AtLoc, diag::err_property_type) << T;
2249 PDecl->setInvalidDecl();
2250 }
Mike Stump1eb44332009-09-09 15:08:12 +00002251
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002252 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00002253
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00002254 // Regardless of setter/getter attribute, we save the default getter/setter
2255 // selector names in anticipation of declaration of setter/getter methods.
2256 PDecl->setGetterName(GetterSel);
2257 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00002258
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002259 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002260 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00002261
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002262 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002263 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00002264
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002265 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002266 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002268 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002269 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00002270
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002271 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002272 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00002273
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002274 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002275 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00002276
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002277 if (isAssign)
2278 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00002279
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002280 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002281 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00002282
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00002283 if (MethodImplKind == tok::objc_required)
2284 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2285 else if (MethodImplKind == tok::objc_optional)
2286 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002287 // A case of continuation class adding a new property in the class. This
2288 // is not what it was meant for. However, gcc supports it and so should we.
2289 // Make sure setter/getters are declared here.
2290 if (CCPrimary)
2291 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002292
Chris Lattnerb28317a2009-03-28 19:18:32 +00002293 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002294}
2295
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002296/// ActOnPropertyImplDecl - This routine performs semantic checks and
2297/// builds the AST node for a property implementation declaration; declared
2298/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002299///
Mike Stump1eb44332009-09-09 15:08:12 +00002300Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002301 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002302 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002303 DeclPtrTy ClassCatImpDecl,
2304 IdentifierInfo *PropertyId,
2305 IdentifierInfo *PropertyIvar) {
2306 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002307 // Make sure we have a context for the property implementation declaration.
2308 if (!ClassImpDecl) {
2309 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002310 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002311 }
2312 ObjCPropertyDecl *property = 0;
2313 ObjCInterfaceDecl* IDecl = 0;
2314 // Find the class or category class where this property must have
2315 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002316 ObjCImplementationDecl *IC = 0;
2317 ObjCCategoryImplDecl* CatImplClass = 0;
2318 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002319 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002320 // We always synthesize an interface for an implementation
2321 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002322 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002323 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002324
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002325 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002326 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002327 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002328 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002329 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002330 }
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002331 if (const ObjCCategoryDecl *CD =
2332 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
2333 if (CD->getIdentifier()) {
2334 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002335 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002336 return DeclPtrTy();
2337 }
2338 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002339 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002340 if (Synthesize) {
2341 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002342 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002343 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002344 IDecl = CatImplClass->getClassInterface();
2345 if (!IDecl) {
2346 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002347 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002348 }
Mike Stump1eb44332009-09-09 15:08:12 +00002349 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002350 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002351
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002352 // If category for this implementation not found, it is an error which
2353 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002354 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002355 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002356 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002357 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002358 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002359 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002360 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002361 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002362 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002363 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002364 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002365 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002366 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002367 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002368 // Check that we have a valid, previously declared ivar for @synthesize
2369 if (Synthesize) {
2370 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002371 if (!PropertyIvar)
2372 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002373 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002374 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002375 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002376 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002377 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002378 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002379 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002380 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002381 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2382 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002383 ObjCIvarDecl::Public,
2384 (Expr *)0);
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002385 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002386 IDecl->addDecl(Ivar);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002387 property->setPropertyIvarDecl(Ivar);
2388 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002389 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002390 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002391 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002392 } else if (getLangOptions().ObjCNonFragileABI &&
2393 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002394 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002395 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002396 << ClassDeclared->getDeclName();
2397 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2398 << Ivar << Ivar->getNameAsCString();
2399 // Note! I deliberately want it to fall thru so more errors are caught.
2400 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002401 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002402
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002403 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002404 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002405 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002406 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002407 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002408 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002409 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002410 }
Mike Stump1eb44332009-09-09 15:08:12 +00002411
Chris Lattnerb28317a2009-03-28 19:18:32 +00002412 // FIXME! Rules for properties are somewhat different that those
2413 // for assignments. Use a new routine to consolidate all cases;
2414 // specifically for property redeclarations as well as for ivars.
2415 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2416 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002417 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002418 lhsType->isArithmeticType()) {
2419 Diag(PropertyLoc, diag::error_property_ivar_type)
2420 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002421 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002422 }
2423 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002424 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2425 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002426 Diag(PropertyLoc, diag::error_weak_property)
2427 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002428 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002429 }
Mike Stump1eb44332009-09-09 15:08:12 +00002430 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002431 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2432 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002433 Diag(PropertyLoc, diag::error_strong_property)
2434 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002435 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002436 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002437 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002438 } else if (PropertyIvar)
2439 // @dynamic
2440 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002441 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002442 ObjCPropertyImplDecl *PIDecl =
2443 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2444 property,
2445 (Synthesize ?
2446 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002447 : ObjCPropertyImplDecl::Dynamic),
2448 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002449 if (IC) {
2450 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002451 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002452 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002453 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2454 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002455 << PropertyIvar;
2456 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2457 }
Mike Stump1eb44332009-09-09 15:08:12 +00002458
2459 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002460 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002461 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2462 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002463 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002464 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002465 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002466 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002467 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002468 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002469 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002470 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2471 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002472 << PropertyIvar;
2473 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2474 }
Mike Stump1eb44332009-09-09 15:08:12 +00002475
2476 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002477 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002478 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2479 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002480 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002481 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002482 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002483 }
Mike Stump1eb44332009-09-09 15:08:12 +00002484
Chris Lattnerb28317a2009-03-28 19:18:32 +00002485 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002486}
Anders Carlsson15281452008-11-04 16:57:32 +00002487
Chris Lattnercc98eac2008-12-17 07:13:27 +00002488bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002489 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002490 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002491
Anders Carlsson15281452008-11-04 16:57:32 +00002492 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2493 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002494
Anders Carlsson15281452008-11-04 16:57:32 +00002495 return true;
2496}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002497
Chris Lattnercc98eac2008-12-17 07:13:27 +00002498/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2499/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002500void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002501 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002502 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002503 // Check that ClassName is a valid class
2504 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2505 if (!Class) {
2506 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2507 return;
2508 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002509 if (LangOpts.ObjCNonFragileABI) {
2510 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2511 return;
2512 }
Mike Stump1eb44332009-09-09 15:08:12 +00002513
Chris Lattnercc98eac2008-12-17 07:13:27 +00002514 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002515 llvm::SmallVector<FieldDecl*, 32> RecFields;
2516 Context.CollectObjCIvars(Class, RecFields);
2517 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2518 for (unsigned i = 0; i < RecFields.size(); i++) {
2519 FieldDecl* ID = RecFields[i];
2520 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2521 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2522 ID->getIdentifier(), ID->getType(),
2523 ID->getBitWidth());
2524 Decls.push_back(Sema::DeclPtrTy::make(FD));
2525 }
Mike Stump1eb44332009-09-09 15:08:12 +00002526
Chris Lattnercc98eac2008-12-17 07:13:27 +00002527 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002528 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002529 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002530 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002531 if (getLangOptions().CPlusPlus)
2532 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002533 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002534 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002535 }
2536}
2537