blob: 179255aedf925247ef5103ccd2b4f8a1207a66b7 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Douglas Gregorf06cdae2010-01-03 18:01:57 +000015#include "Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
Steve Naroffca331292009-03-03 14:49:36 +000017#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000021using namespace clang;
22
Fariborz Jahanianc001e892009-05-08 20:20:55 +000023bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
24 ObjCMethodDecl *GetterMethod,
25 SourceLocation Loc) {
26 if (GetterMethod &&
27 GetterMethod->getResultType() != property->getType()) {
28 AssignConvertType result = Incompatible;
Steve Narofff4954562009-07-16 15:41:00 +000029 if (property->getType()->isObjCObjectPointerType())
Fariborz Jahanian7aaa4092009-05-08 21:10:00 +000030 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
Fariborz Jahanianc001e892009-05-08 20:20:55 +000031 if (result != Compatible) {
Mike Stump1eb44332009-09-09 15:08:12 +000032 Diag(Loc, diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianc001e892009-05-08 20:20:55 +000033 << property->getDeclName()
34 << GetterMethod->getSelector();
35 Diag(GetterMethod->getLocation(), diag::note_declared_at);
36 return true;
37 }
38 }
39 return false;
40}
41
Steve Naroffebf64432009-02-28 16:59:13 +000042/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000043/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000044void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000045 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000046 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump1eb44332009-09-09 15:08:12 +000047
Steve Naroff394f3f42008-07-25 17:57:26 +000048 // If we don't have a valid method decl, simply return.
49 if (!MDecl)
50 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000051
Chris Lattner38c5ebd2009-04-19 05:21:20 +000052 CurFunctionNeedsScopeChecking = false;
53
Steve Naroffa56f6162007-12-18 01:30:32 +000054 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000055 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000056 AddInstanceMethodToGlobalPool(MDecl);
57 else
58 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000059
Chris Lattner4d391482007-12-12 07:09:47 +000060 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000061 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000062
63 // Create Decl objects for each parameter, entrring them in the scope for
64 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000065
66 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000067 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +000068
Daniel Dunbar451318c2008-08-26 06:07:48 +000069 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
70 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000071
Chris Lattner8123a952008-04-10 02:22:51 +000072 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000073 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
74 E = MDecl->param_end(); PI != E; ++PI)
75 if ((*PI)->getIdentifier())
76 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000077}
78
Chris Lattnerb28317a2009-03-28 19:18:32 +000079Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000080ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
81 IdentifierInfo *ClassName, SourceLocation ClassLoc,
82 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000083 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000084 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000085 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000086
Chris Lattner4d391482007-12-12 07:09:47 +000087 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +000088 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000089 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000090 // Maybe we will complain about the shadowed template parameter.
91 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
92 // Just pretend that we didn't see the previous declaration.
93 PrevDecl = 0;
94 }
95
Ted Kremeneka526c5c2008-01-07 19:49:32 +000096 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000097 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000098 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000099 }
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000101 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000102 if (IDecl) {
103 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000104 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000105 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000106 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000107 Diag(IDecl->getLocation(), diag::note_previous_definition);
108
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000109 // Return the previous class interface.
110 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000111 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000112 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000113 IDecl->setLocation(AtInterfaceLoc);
114 IDecl->setForwardDecl(false);
Steve Naroff8b26cbd2009-09-11 00:12:01 +0000115 IDecl->setClassLoc(ClassLoc);
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000116
117 // Since this ObjCInterfaceDecl was created by a forward declaration,
118 // we now add it to the DeclContext since it wasn't added before
119 // (see ActOnForwardClassDeclaration).
120 CurContext->addDecl(IDecl);
121
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000122 if (AttrList)
123 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000124 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000125 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000126 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000127 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000128 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000129 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Steve Naroffa7503a72009-04-23 15:15:40 +0000131 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000132 }
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Chris Lattner4d391482007-12-12 07:09:47 +0000134 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000135 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000136 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000137
138 if (!PrevDecl) {
139 // Try to correct for a typo in the superclass name.
140 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
141 if (CorrectTypo(R, TUScope, 0) &&
142 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
143 Diag(SuperLoc, diag::err_undef_superclass_suggest)
144 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000145 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
146 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000147 }
148 }
149
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000150 if (PrevDecl == IDecl) {
151 Diag(SuperLoc, diag::err_recursive_superclass)
152 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
153 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000154 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000155 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000156 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000157
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000158 // Diagnose classes that inherit from deprecated classes.
159 if (SuperClassDecl)
160 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000162 if (PrevDecl && SuperClassDecl == 0) {
163 // The previous declaration was not a class decl. Check if we have a
164 // typedef. If we do, get the underlying class type.
165 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
166 QualType T = TDecl->getUnderlyingType();
167 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000168 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000169 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
170 }
171 }
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000173 // This handles the following case:
174 //
175 // typedef int SuperClass;
176 // @interface MyClass : SuperClass {} @end
177 //
178 if (!SuperClassDecl) {
179 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
180 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000181 }
182 }
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000184 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
185 if (!SuperClassDecl)
186 Diag(SuperLoc, diag::err_undef_superclass)
187 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
188 else if (SuperClassDecl->isForwardDecl())
189 Diag(SuperLoc, diag::err_undef_superclass)
190 << SuperClassDecl->getDeclName() << ClassName
191 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000192 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000193 IDecl->setSuperClass(SuperClassDecl);
194 IDecl->setSuperClassLoc(SuperLoc);
195 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000196 }
Chris Lattner4d391482007-12-12 07:09:47 +0000197 } else { // we have a root class.
198 IDecl->setLocEnd(ClassLoc);
199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000201 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000202 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000203 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
204 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000205 IDecl->setLocEnd(EndProtoLoc);
206 }
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Anders Carlsson15281452008-11-04 16:57:32 +0000208 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000209 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000210}
211
212/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000213/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000214Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000215 IdentifierInfo *AliasName,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000216 SourceLocation AliasLocation,
217 IdentifierInfo *ClassName,
218 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000219 // Look for previous declaration of alias name
John McCallf36e02d2009-10-09 21:13:30 +0000220 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000221 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000222 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000223 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000224 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000225 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000226 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000227 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000228 }
229 // Check for class declaration
John McCallf36e02d2009-10-09 21:13:30 +0000230 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000231 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
232 QualType T = TDecl->getUnderlyingType();
233 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000234 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000235 ClassName = IDecl->getIdentifier();
John McCallf36e02d2009-10-09 21:13:30 +0000236 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000237 }
238 }
239 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000240 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
241 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000242 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000243 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000244 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000245 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000246 }
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000248 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000249 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000250 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Anders Carlsson15281452008-11-04 16:57:32 +0000252 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000253 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000254
Chris Lattnerb28317a2009-03-28 19:18:32 +0000255 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000256}
257
Steve Naroff61d68522009-03-05 15:22:01 +0000258void Sema::CheckForwardProtocolDeclarationForCircularDependency(
259 IdentifierInfo *PName,
260 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000261 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000262 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
263 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Douglas Gregor6e378de2009-04-23 23:18:26 +0000265 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000266 if (PDecl->getIdentifier() == PName) {
267 Diag(Ploc, diag::err_protocol_has_circular_dependency);
268 Diag(PrevLoc, diag::note_previous_definition);
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000271 PDecl->getLocation(), PDecl->getReferencedProtocols());
272 }
273 }
274}
275
Chris Lattnerb28317a2009-03-28 19:18:32 +0000276Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000277Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
278 IdentifierInfo *ProtocolName,
279 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000280 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000281 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000282 SourceLocation EndProtoLoc,
283 AttributeList *AttrList) {
284 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000285 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000286 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000287 if (PDecl) {
288 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000289 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000290 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000291 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000292 // Just return the protocol we already had.
293 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000294 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000295 }
Steve Naroff61d68522009-03-05 15:22:01 +0000296 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000297 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000298 CheckForwardProtocolDeclarationForCircularDependency(
299 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
300 PList.Destroy(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Steve Narofff11b5082008-08-13 16:39:22 +0000302 // Make sure the cached decl gets a valid start location.
303 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000304 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000305 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000306 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000307 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000308 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000309 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000310 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000311 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000312 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000313 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000314 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000315 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000316 PDecl->setLocEnd(EndProtoLoc);
317 }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
319 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000320 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000321}
322
323/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000324/// issues an error if they are not declared. It returns list of
325/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000326void
Chris Lattnere13b9592008-07-26 04:03:38 +0000327Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000328 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000329 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000330 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000331 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000332 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000333 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000334 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
335 LookupObjCProtocolName);
336 if (CorrectTypo(R, TUScope, 0) &&
337 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
338 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
339 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000340 Diag(PDecl->getLocation(), diag::note_previous_decl)
341 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000342 }
343 }
344
345 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000346 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000347 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000348 continue;
349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000351 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000352
353 // If this is a forward declaration and we are supposed to warn in this
354 // case, do it.
355 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000356 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000357 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000358 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000359 }
360}
361
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000362/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000363/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000364///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000365void
Mike Stump1eb44332009-09-09 15:08:12 +0000366Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000367 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000368 const IdentifierInfo *inheritedName) {
Mike Stump1eb44332009-09-09 15:08:12 +0000369 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000370 Property->getPropertyAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000371 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000372 SuperProperty->getPropertyAttributes();
373 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
374 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000375 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000376 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000377 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
378 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000379 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000380 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000381 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
382 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000383 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000384 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000386 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
387 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000388 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000389 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000390 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000391 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump1eb44332009-09-09 15:08:12 +0000392 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000393 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000394 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000395 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000396
Mike Stump1eb44332009-09-09 15:08:12 +0000397 QualType LHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000398 Context.getCanonicalType(SuperProperty->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000399 QualType RHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000400 Context.getCanonicalType(Property->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Steve Naroff15edf0d2009-03-03 15:43:24 +0000402 if (!Context.typesAreCompatible(LHSType, RHSType)) {
403 // FIXME: Incorporate this test with typesAreCompatible.
404 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff4084c302009-07-23 01:01:38 +0000405 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff15edf0d2009-03-03 15:43:24 +0000406 return;
407 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
408 << Property->getType() << SuperProperty->getType() << inheritedName;
409 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000410}
411
412/// ComparePropertiesInBaseAndSuper - This routine compares property
413/// declarations in base and its super class, if any, and issues
414/// diagnostics in a variety of inconsistant situations.
415///
Chris Lattner70f19542009-02-16 21:26:43 +0000416void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000417 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
418 if (!SDecl)
419 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000420 // FIXME: O(N^2)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000421 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
422 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000423 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000424 // Does property in super class has declaration in current class?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000425 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
426 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000427 ObjCPropertyDecl *PDecl = (*I);
428 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump1eb44332009-09-09 15:08:12 +0000429 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000430 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000431 }
432 }
433}
434
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000435/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
436/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000437/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000438void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000439Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000440 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000441 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
442 if (!IDecl) {
443 // Category
444 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
445 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000446 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
447 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000448 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000449 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000450 // Is this property already in category's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000451 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000452 if ((*CP)->getIdentifier() == Pr->getIdentifier())
453 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000454 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000455 // Property protocol already exist in class. Diagnose any mismatch.
456 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
457 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000458 return;
459 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000460 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
461 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000462 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000463 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000464 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000465 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000466 if ((*CP)->getIdentifier() == Pr->getIdentifier())
467 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000468 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000469 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000470 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000471 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000472}
473
474/// MergeProtocolPropertiesIntoClass - This routine merges properties
475/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000476/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000477///
Chris Lattner70f19542009-02-16 21:26:43 +0000478void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000479 DeclPtrTy MergeItsProtocols) {
480 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000481 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
482
483 if (!IDecl) {
484 // Category
485 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
486 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
487 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
488 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
489 E = MDecl->protocol_end(); P != E; ++P)
490 // Merge properties of category (*P) into IDECL's
491 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000493 // Go thru the list of protocols for this category and recursively merge
494 // their properties into this class as well.
495 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
496 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000497 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000498 } else {
499 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
500 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
501 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000502 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000503 }
504 return;
505 }
506
Chris Lattnerb752f282008-07-21 07:06:49 +0000507 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000508 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
509 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000510 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000511 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000513 // Go thru the list of protocols for this class and recursively merge
514 // their properties into this class as well.
515 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
516 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000517 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000518 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000519 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
520 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
521 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000522 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000523 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000524}
525
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000526/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000527/// a class method in its extension.
528///
Mike Stump1eb44332009-09-09 15:08:12 +0000529void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000530 ObjCInterfaceDecl *ID) {
531 if (!ID)
532 return; // Possibly due to previous error
533
534 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000535 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
536 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000537 ObjCMethodDecl *MD = *i;
538 MethodMap[MD->getSelector()] = MD;
539 }
540
541 if (MethodMap.empty())
542 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000543 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
544 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000545 ObjCMethodDecl *Method = *i;
546 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
547 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
548 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
549 << Method->getDeclName();
550 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
551 }
552 }
553}
554
Chris Lattner58fe03b2009-04-12 08:43:13 +0000555/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000556Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000557Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000558 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000559 unsigned NumElts,
560 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000561 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Chris Lattner4d391482007-12-12 07:09:47 +0000563 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000564 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000565 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000566 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000567 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000568 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000569 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000570 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000571 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000572 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000573 Protocols.push_back(PDecl);
574 }
Mike Stump1eb44332009-09-09 15:08:12 +0000575
576 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000577 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000578 &Protocols[0], Protocols.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000579 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000580 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000581 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000582}
583
Chris Lattnerb28317a2009-03-28 19:18:32 +0000584Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000585ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
586 IdentifierInfo *ClassName, SourceLocation ClassLoc,
587 IdentifierInfo *CategoryName,
588 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000589 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000590 unsigned NumProtoRefs,
591 SourceLocation EndProtoLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000592 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000593 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
594 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000595 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000596
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000597 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000598 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000599 if (!IDecl || IDecl->isForwardDecl()) {
600 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000601 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000602 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000603 }
Chris Lattner4d391482007-12-12 07:09:47 +0000604
Chris Lattner70f19542009-02-16 21:26:43 +0000605 CDecl->setClassInterface(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Chris Lattner16b34b42009-02-16 21:30:01 +0000607 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000608 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000609
610 /// Check for duplicate interface declaration for this category
611 ObjCCategoryDecl *CDeclChain;
612 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
613 CDeclChain = CDeclChain->getNextClassCategory()) {
614 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
615 Diag(CategoryLoc, diag::warn_dup_category_def)
616 << ClassName << CategoryName;
617 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
618 break;
619 }
620 }
621 if (!CDeclChain)
622 CDecl->insertNextClassCategory();
623
Chris Lattner4d391482007-12-12 07:09:47 +0000624 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000625 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
626 Context);
627 CDecl->setLocEnd(EndProtoLoc);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000628 // Protocols in the class extension belong to the class.
629 if (!CDecl->getIdentifier())
630 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
631 NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Anders Carlsson15281452008-11-04 16:57:32 +0000634 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000635 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000636}
637
638/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000639/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000640/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000641Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000642 SourceLocation AtCatImplLoc,
643 IdentifierInfo *ClassName, SourceLocation ClassLoc,
644 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000645 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000646 ObjCCategoryDecl *CatIDecl = 0;
647 if (IDecl) {
648 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
649 if (!CatIDecl) {
650 // Category @implementation with no corresponding @interface.
651 // Create and install one.
652 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
653 CatName);
654 CatIDecl->setClassInterface(IDecl);
655 CatIDecl->insertNextClassCategory();
656 }
657 }
658
Mike Stump1eb44332009-09-09 15:08:12 +0000659 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000660 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
661 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000662 /// Check that class of this category is already completely declared.
663 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000664 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000665
Douglas Gregord0434102009-01-09 00:49:46 +0000666 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000667 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000668
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000669 /// Check that CatName, category name, is not used in another implementation.
670 if (CatIDecl) {
671 if (CatIDecl->getImplementation()) {
672 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
673 << CatName;
674 Diag(CatIDecl->getImplementation()->getLocation(),
675 diag::note_previous_definition);
676 } else
677 CatIDecl->setImplementation(CDecl);
678 }
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Anders Carlsson15281452008-11-04 16:57:32 +0000680 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000681 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000682}
683
Chris Lattnerb28317a2009-03-28 19:18:32 +0000684Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000685 SourceLocation AtClassImplLoc,
686 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000687 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000688 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000689 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000690 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000691 NamedDecl *PrevDecl
692 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000693 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000694 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000695 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000696 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
697 // If this is a forward declaration of an interface, warn.
698 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000699 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000700 IDecl = 0;
701 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000702 } else {
703 // We did not find anything with the name ClassName; try to correct for
704 // typos in the class name.
705 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
706 if (CorrectTypo(R, TUScope, 0) &&
707 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000708 // Suggest the (potentially) correct interface name. However, put the
709 // fix-it hint itself in a separate note, since changing the name in
710 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000711 // provide a code-modification hint or use the typo name for recovery,
712 // because this is just a warning. The program may actually be correct.
713 Diag(ClassLoc, diag::warn_undef_interface_suggest)
714 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000715 Diag(IDecl->getLocation(), diag::note_previous_decl)
716 << R.getLookupName()
717 << CodeModificationHint::CreateReplacement(ClassLoc,
718 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000719 IDecl = 0;
720 } else {
721 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
722 }
Chris Lattner4d391482007-12-12 07:09:47 +0000723 }
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Chris Lattner4d391482007-12-12 07:09:47 +0000725 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000726 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000727 if (SuperClassname) {
728 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000729 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000730 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000731 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
732 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000733 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000734 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000735 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000736 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000737 Diag(SuperClassLoc, diag::err_undef_superclass)
738 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000739 else if (IDecl && IDecl->getSuperClass() != SDecl) {
740 // This implementation and its interface do not have the same
741 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000742 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000743 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000744 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000745 }
746 }
747 }
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Chris Lattner4d391482007-12-12 07:09:47 +0000749 if (!IDecl) {
750 // Legacy case of @implementation with no corresponding @interface.
751 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000752
Mike Stump390b4cc2009-05-16 07:39:55 +0000753 // FIXME: Do we support attributes on the @implementation? If so we should
754 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000755 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000756 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000757 IDecl->setSuperClass(SDecl);
758 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000759
760 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000761 } else {
762 // Mark the interface as being completed, even if it was just as
763 // @class ....;
764 // declaration; the user cannot reopen it.
765 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000766 }
Mike Stump1eb44332009-09-09 15:08:12 +0000767
768 ObjCImplementationDecl* IMPDecl =
769 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000770 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Anders Carlsson15281452008-11-04 16:57:32 +0000772 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000773 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Chris Lattner4d391482007-12-12 07:09:47 +0000775 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000776 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000777 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000778 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000779 Diag(IDecl->getImplementation()->getLocation(),
780 diag::note_previous_definition);
781 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000782 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000783 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000784 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000785 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000786}
787
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000788void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
789 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000790 SourceLocation RBrace) {
791 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000792 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000793 if (!IDecl)
794 return;
795 /// Check case of non-existing @interface decl.
796 /// (legacy objective-c @implementation decl without an @interface decl).
797 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000798 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000799 IDecl->setIVarList(ivars, numIvars, Context);
800 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000801 return;
802 }
803 // If implementation has empty ivar list, just return.
804 if (numIvars == 0)
805 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Chris Lattner4d391482007-12-12 07:09:47 +0000807 assert(ivars && "missing @implementation ivars");
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Chris Lattner4d391482007-12-12 07:09:47 +0000809 // Check interface's Ivar list against those in the implementation.
810 // names and types must match.
811 //
Chris Lattner4d391482007-12-12 07:09:47 +0000812 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000813 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000814 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
815 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000816 ObjCIvarDecl* ImplIvar = ivars[j++];
817 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000818 assert (ImplIvar && "missing implementation ivar");
819 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Steve Naroffca331292009-03-03 14:49:36 +0000821 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000822 if (Context.getCanonicalType(ImplIvar->getType()) !=
823 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000824 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000825 << ImplIvar->getIdentifier()
826 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000827 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000828 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
829 Expr *ImplBitWidth = ImplIvar->getBitWidth();
830 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000831 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
832 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000833 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
834 << ImplIvar->getIdentifier();
835 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
836 }
Mike Stump1eb44332009-09-09 15:08:12 +0000837 }
Steve Naroffca331292009-03-03 14:49:36 +0000838 // Make sure the names are identical.
839 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000840 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000841 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000842 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000843 }
844 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000845 }
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Chris Lattner609e4c72007-12-12 18:11:49 +0000847 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000848 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000849 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000850 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000851}
852
Steve Naroff3c2eb662008-02-10 21:38:56 +0000853void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
854 bool &IncompleteImpl) {
855 if (!IncompleteImpl) {
856 Diag(ImpLoc, diag::warn_incomplete_impl);
857 IncompleteImpl = true;
858 }
Chris Lattner08631c52008-11-23 21:45:46 +0000859 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000860}
861
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000862void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
863 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000864 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000865 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000866 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
867 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000868 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000869 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
870 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000871 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
872 }
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Chris Lattner3aff9192009-04-11 19:58:42 +0000874 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
875 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
876 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000877 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
878 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
879 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
880 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000881 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000882
883 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000884 << ImpMethodDecl->getDeclName() << (*IF)->getType()
885 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000886 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000887 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000888}
889
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000890/// isPropertyReadonly - Return true if property is readonly, by searching
891/// for the property in the class and in its categories and implementations
892///
893bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000894 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000895 // by far the most common case.
896 if (!PDecl->isReadOnly())
897 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000898 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000899 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000900 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000901 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000903 // Main class has the property as 'readonly'. Must search
Mike Stump1eb44332009-09-09 15:08:12 +0000904 // through the category list to see if the property's
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000905 // attribute has been over-ridden to 'readwrite'.
906 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
907 Category; Category = Category->getNextClassCategory()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000908 // Even if property is ready only, if a category has a user defined setter,
909 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000910 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000911 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000912 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000913 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000914 if (P && !P->isReadOnly())
915 return false;
916 }
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000918 // Also, check for definition of a setter method in the implementation if
919 // all else failed.
920 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000921 if (ObjCImplementationDecl *IMD =
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000922 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000923 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000924 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000925 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000926 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000927 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000928 return false;
929 }
930 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000931 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000932 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000933 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000934 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000935 // If all fails, look at the super class.
936 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
937 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000938 return true;
939}
940
Mike Stump390b4cc2009-05-16 07:39:55 +0000941/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
942/// improve the efficiency of selector lookups and type checking by associating
943/// with each protocol / interface / category the flattened instance tables. If
944/// we used an immutable set to keep the table then it wouldn't add significant
945/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000946
Steve Naroffefe7f362008-02-08 22:06:17 +0000947/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000948/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000949void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
950 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000951 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000952 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000953 const llvm::DenseSet<Selector> &ClsMap,
954 ObjCInterfaceDecl *IDecl) {
955 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000956 ObjCInterfaceDecl *NSIDecl = 0;
957 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000958 // check to see if class implements forwardInvocation method and objects
959 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000960 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000961 // Under such conditions, which means that every method possible is
962 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000963 // found" warnings.
964 // FIXME: Use a general GetUnarySelector method for this.
965 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
966 Selector fISelector = Context.Selectors.getSelector(1, &II);
967 if (InsMap.count(fISelector))
968 // Is IDecl derived from 'NSProxy'? If so, no instance methods
969 // need be implemented in the implementation.
970 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000973 // If a method lookup fails locally we still need to look and see if
974 // the method was implemented by a base class or an inherited
975 // protocol. This lookup is slow, but occurs rarely in correct code
976 // and otherwise would terminate in a warning.
977
Chris Lattner4d391482007-12-12 07:09:47 +0000978 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000979 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000980 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000981 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000982 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000983 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000984 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000985 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000986 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000987 // Ugly, but necessary. Method declared in protcol might have
988 // have been synthesized due to a property declared in the class which
989 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000990 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000991 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000992 if (!MethodInClass || !MethodInClass->isSynthesized())
993 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
994 }
995 }
Chris Lattner4d391482007-12-12 07:09:47 +0000996 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000997 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000998 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000999 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001000 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001001 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1002 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001003 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +00001004 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001005 }
Chris Lattner780f3292008-07-21 21:32:27 +00001006 // Check on this protocols's referenced protocols, recursively.
1007 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1008 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001009 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001010}
1011
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001012/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1013/// or protocol against those declared in their implementations.
1014///
1015void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1016 const llvm::DenseSet<Selector> &ClsMap,
1017 llvm::DenseSet<Selector> &InsMapSeen,
1018 llvm::DenseSet<Selector> &ClsMapSeen,
1019 ObjCImplDecl* IMPDecl,
1020 ObjCContainerDecl* CDecl,
1021 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001022 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001023 // Check and see if instance methods in class interface have been
1024 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001025 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1026 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001027 if (InsMapSeen.count((*I)->getSelector()))
1028 continue;
1029 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001030 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001031 !InsMap.count((*I)->getSelector())) {
1032 if (ImmediateClass)
1033 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1034 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001035 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001036 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001037 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001038 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001039 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001040 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001041 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1042 // ImpMethodDecl may be null as in a @dynamic property.
1043 if (ImpMethodDecl)
1044 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1045 }
1046 }
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001048 // Check and see if class methods in class interface have been
1049 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001050 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001051 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001052 if (ClsMapSeen.count((*I)->getSelector()))
1053 continue;
1054 ClsMapSeen.insert((*I)->getSelector());
1055 if (!ClsMap.count((*I)->getSelector())) {
1056 if (ImmediateClass)
1057 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001058 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001059 ObjCMethodDecl *ImpMethodDecl =
1060 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001061 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001062 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001063 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1064 }
1065 }
1066 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1067 // Check for any implementation of a methods declared in protocol.
1068 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1069 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001070 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1071 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001072 (*PI), IncompleteImpl, false);
1073 if (I->getSuperClass())
1074 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001075 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001076 I->getSuperClass(), IncompleteImpl, false);
1077 }
1078}
1079
Mike Stump1eb44332009-09-09 15:08:12 +00001080void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1081 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001082 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001083 llvm::DenseSet<Selector> InsMap;
1084 // Check and see if instance methods in class interface have been
1085 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001086 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001087 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001088 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001090 // Check and see if properties declared in the interface have either 1)
1091 // an implementation or 2) there is a @synthesize/@dynamic implementation
1092 // of the property in the @implementation.
1093 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001094 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1095 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001096 ObjCPropertyDecl *Prop = (*P);
1097 if (Prop->isInvalidDecl())
1098 continue;
1099 ObjCPropertyImplDecl *PI = 0;
1100 // Is there a matching propery synthesize/dynamic?
Mike Stump1eb44332009-09-09 15:08:12 +00001101 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001102 I = IMPDecl->propimpl_begin(),
1103 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001104 if ((*I)->getPropertyDecl() == Prop) {
1105 PI = (*I);
1106 break;
1107 }
1108 if (PI)
1109 continue;
1110 if (!InsMap.count(Prop->getGetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001111 Diag(Prop->getLocation(),
1112 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001113 << Prop->getDeclName() << Prop->getGetterName();
1114 Diag(IMPDecl->getLocation(),
1115 diag::note_property_impl_required);
1116 }
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001118 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001119 Diag(Prop->getLocation(),
1120 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001121 << Prop->getDeclName() << Prop->getSetterName();
1122 Diag(IMPDecl->getLocation(),
1123 diag::note_property_impl_required);
1124 }
1125 }
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Chris Lattner4d391482007-12-12 07:09:47 +00001127 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001128 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001129 I = IMPDecl->classmeth_begin(),
1130 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001131 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001133 // Check for type conflict of methods declared in a class/protocol and
1134 // its implementation; if any.
1135 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001136 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1137 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001138 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Chris Lattner4d391482007-12-12 07:09:47 +00001140 // Check the protocol list for unimplemented methods in the @implementation
1141 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001142 // Check and see if class methods in class interface have been
1143 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Chris Lattnercddc8882009-03-01 00:56:52 +00001145 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001146 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001147 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001148 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001149 InsMap, ClsMap, I);
1150 // Check class extensions (unnamed categories)
1151 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1152 Categories; Categories = Categories->getNextClassCategory()) {
1153 if (!Categories->getIdentifier()) {
1154 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1155 break;
1156 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001157 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001158 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001159 // For extended class, unimplemented methods in its protocols will
1160 // be reported in the primary class.
1161 if (C->getIdentifier()) {
1162 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1163 E = C->protocol_end(); PI != E; ++PI)
1164 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1165 InsMap, ClsMap, C->getClassInterface());
1166 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001167 } else
1168 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001169}
1170
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001171void
1172Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1173 ObjCContainerDecl* IDecl) {
1174 // Rules apply in non-GC mode only
1175 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1176 return;
1177 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1178 E = IDecl->prop_end();
1179 I != E; ++I) {
1180 ObjCPropertyDecl *Property = (*I);
1181 unsigned Attributes = Property->getPropertyAttributes();
1182 // We only care about readwrite atomic property.
1183 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1184 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1185 continue;
1186 if (const ObjCPropertyImplDecl *PIDecl
1187 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1188 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1189 continue;
1190 ObjCMethodDecl *GetterMethod =
1191 IMPDecl->getInstanceMethod(Property->getGetterName());
1192 ObjCMethodDecl *SetterMethod =
1193 IMPDecl->getInstanceMethod(Property->getSetterName());
1194 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1195 SourceLocation MethodLoc =
1196 (GetterMethod ? GetterMethod->getLocation()
1197 : SetterMethod->getLocation());
1198 Diag(MethodLoc, diag::warn_atomic_property_rule)
1199 << Property->getIdentifier();
1200 Diag(Property->getLocation(), diag::note_property_declare);
1201 }
1202 }
1203 }
1204}
1205
Mike Stump1eb44332009-09-09 15:08:12 +00001206/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001207Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001208Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001209 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001210 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001211 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001212 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Chris Lattner4d391482007-12-12 07:09:47 +00001214 for (unsigned i = 0; i != NumElts; ++i) {
1215 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001216 NamedDecl *PrevDecl
1217 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001218 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001219 // Maybe we will complain about the shadowed template parameter.
1220 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1221 // Just pretend that we didn't see the previous declaration.
1222 PrevDecl = 0;
1223 }
1224
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001225 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001226 // GCC apparently allows the following idiom:
1227 //
1228 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1229 // @class XCElementToggler;
1230 //
Mike Stump1eb44332009-09-09 15:08:12 +00001231 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001232 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1233 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001234 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001235 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001236 } else if (TDD) {
1237 // a forward class declaration matching a typedef name of a class refers
1238 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001239 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001240 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1241 PrevDecl = OI->getDecl();
1242 }
Chris Lattner4d391482007-12-12 07:09:47 +00001243 }
Mike Stump1eb44332009-09-09 15:08:12 +00001244 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001245 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001246 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001247 IdentList[i], IdentLocs[i], true);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001248
1249 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1250 // the current DeclContext. This prevents clients that walk DeclContext
1251 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1252 // declared later (if at all). We also take care to explicitly make
1253 // sure this declaration is visible for name lookup.
1254 PushOnScopeChains(IDecl, TUScope, false);
1255 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001256 }
1257
1258 Interfaces.push_back(IDecl);
1259 }
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Ted Kremenek321c22f2009-11-18 00:28:11 +00001261 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001262 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001263 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001264 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001265 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001266 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001267 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001268}
1269
1270
1271/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1272/// returns true, or false, accordingly.
1273/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001274bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001275 const ObjCMethodDecl *PrevMethod,
1276 bool matchBasedOnSizeAndAlignment) {
1277 QualType T1 = Context.getCanonicalType(Method->getResultType());
1278 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001280 if (T1 != T2) {
1281 // The result types are different.
1282 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001283 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001284 // Incomplete types don't have a size and alignment.
1285 if (T1->isIncompleteType() || T2->isIncompleteType())
1286 return false;
1287 // Check is based on size and alignment.
1288 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1289 return false;
1290 }
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Chris Lattner89951a82009-02-20 18:43:26 +00001292 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1293 E = Method->param_end();
1294 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Chris Lattner89951a82009-02-20 18:43:26 +00001296 for (; ParamI != E; ++ParamI, ++PrevI) {
1297 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1298 T1 = Context.getCanonicalType((*ParamI)->getType());
1299 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001300 if (T1 != T2) {
1301 // The result types are different.
1302 if (!matchBasedOnSizeAndAlignment)
1303 return false;
1304 // Incomplete types don't have a size and alignment.
1305 if (T1->isIncompleteType() || T2->isIncompleteType())
1306 return false;
1307 // Check is based on size and alignment.
1308 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1309 return false;
1310 }
Chris Lattner4d391482007-12-12 07:09:47 +00001311 }
1312 return true;
1313}
1314
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001315/// \brief Read the contents of the instance and factory method pools
1316/// for a given selector from external storage.
1317///
1318/// This routine should only be called once, when neither the instance
1319/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001320Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001321 bool isInstance) {
1322 assert(ExternalSource && "We need an external AST source");
1323 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1324 "Selector data already loaded into the instance method pool");
1325 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1326 "Selector data already loaded into the factory method pool");
1327
1328 // Read the method list from the external source.
1329 std::pair<ObjCMethodList, ObjCMethodList> Methods
1330 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001332 if (isInstance) {
1333 if (Methods.second.Method)
1334 FactoryMethodPool[Sel] = Methods.second;
1335 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001336 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001337
1338 if (Methods.first.Method)
1339 InstanceMethodPool[Sel] = Methods.first;
1340
1341 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1342}
1343
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001344void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001345 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1346 = InstanceMethodPool.find(Method->getSelector());
1347 if (Pos == InstanceMethodPool.end()) {
1348 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1349 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1350 else
1351 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1352 ObjCMethodList())).first;
1353 }
1354
1355 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001356 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001357 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001358 Entry.Method = Method;
1359 Entry.Next = 0;
1360 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001361 }
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Chris Lattnerb25df352009-03-04 05:16:45 +00001363 // We've seen a method with this name, see if we have already seen this type
1364 // signature.
1365 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1366 if (MatchTwoMethodDeclarations(Method, List->Method))
1367 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Chris Lattnerb25df352009-03-04 05:16:45 +00001369 // We have a new signature for an existing method - add it.
1370 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1371 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001372}
1373
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001374// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001375ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001376 SourceRange R,
1377 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001378 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1379 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001380 if (Pos == InstanceMethodPool.end()) {
1381 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001382 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1383 else
1384 return 0;
1385 }
1386
1387 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001388 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Steve Naroff037cda52008-09-30 14:38:43 +00001390 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001391 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1392 // This checks if the methods differ by size & alignment.
1393 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001394 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001395 }
1396 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001397 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001398 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001399 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001400 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001401 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001402 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001403 }
1404 return MethList.Method;
1405}
1406
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001407void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001408 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1409 = FactoryMethodPool.find(Method->getSelector());
1410 if (Pos == FactoryMethodPool.end()) {
1411 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1412 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1413 else
1414 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1415 ObjCMethodList())).first;
1416 }
1417
1418 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001419 if (!FirstMethod.Method) {
1420 // Haven't seen a method with this selector name yet - add it.
1421 FirstMethod.Method = Method;
1422 FirstMethod.Next = 0;
1423 } else {
1424 // We've seen a method with this name, now check the type signature(s).
1425 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001426
1427 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001428 Next = Next->Next)
1429 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Chris Lattner4d391482007-12-12 07:09:47 +00001431 if (!match) {
1432 // We have a new signature for an existing method - add it.
1433 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001434 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001435 FirstMethod.Next = OMI;
1436 }
1437 }
1438}
1439
Mike Stump1eb44332009-09-09 15:08:12 +00001440ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001441 SourceRange R) {
1442 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1443 = FactoryMethodPool.find(Sel);
1444 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001445 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001446 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1447 else
1448 return 0;
1449 }
1450
1451 ObjCMethodList &MethList = Pos->second;
1452 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001454 if (MethList.Method && MethList.Next) {
1455 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1456 // This checks if the methods differ by size & alignment.
1457 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1458 issueWarning = true;
1459 }
1460 if (issueWarning && (MethList.Method && MethList.Next)) {
1461 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001462 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001463 << MethList.Method->getSourceRange();
1464 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001465 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001466 << Next->Method->getSourceRange();
1467 }
1468 return MethList.Method;
1469}
1470
Mike Stump1eb44332009-09-09 15:08:12 +00001471/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff0701bbb2009-01-08 17:28:14 +00001472/// have the property type and issue diagnostics if they don't.
1473/// Also synthesize a getter/setter method if none exist (and update the
1474/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1475/// methods is the "right" thing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001476void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001477 ObjCContainerDecl *CD) {
1478 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00001479
1480 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001481 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump1eb44332009-09-09 15:08:12 +00001482 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001483 property->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001485 if (SetterMethod) {
Fariborz Jahanian02deae82010-01-06 00:18:12 +00001486 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1487 property->getPropertyAttributes();
1488 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1489 Context.getCanonicalType(SetterMethod->getResultType()) !=
1490 Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001491 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001492 if (SetterMethod->param_size() != 1 ||
1493 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001494 Diag(property->getLocation(),
1495 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001496 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001497 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001498 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1499 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001500 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001501
1502 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001503 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001504 // FIXME: The synthesized property we set here is misleading. We almost always
1505 // synthesize these methods unless the user explicitly provided prototypes
1506 // (which is odd, but allowed). Sema should be typechecking that the
1507 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001508 if (!GetterMethod) {
1509 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001510 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001511 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001512 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1513 property->getLocation(), property->getGetterName(),
1514 property->getType(), CD, true, false, true,
1515 (property->getPropertyImplementation() ==
1516 ObjCPropertyDecl::Optional) ?
1517 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001518 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001519 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001520 } else
1521 // A user declared getter will be synthesize when @synthesize of
1522 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001523 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001524 property->setGetterMethodDecl(GetterMethod);
1525
1526 // Skip setter if property is read-only.
1527 if (!property->isReadOnly()) {
1528 // Find the default setter and if one not found, add one.
1529 if (!SetterMethod) {
1530 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001531 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001532 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001533 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1534 property->getLocation(),
1535 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001536 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001537 (property->getPropertyImplementation() ==
1538 ObjCPropertyDecl::Optional) ?
1539 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001540 ObjCMethodDecl::Required);
1541 // Invent the arguments for the setter. We don't bother making a
1542 // nice name for the argument.
1543 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001544 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001545 property->getIdentifier(),
1546 property->getType(),
John McCalla93c9342009-12-07 02:54:59 +00001547 /*TInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001548 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001549 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001550 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001551 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001552 } else
1553 // A user declared setter will be synthesize when @synthesize of
1554 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001555 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001556 property->setSetterMethodDecl(SetterMethod);
1557 }
Mike Stump1eb44332009-09-09 15:08:12 +00001558 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001559 // handle the following, which is supported by GCC (and part of the design).
1560 //
1561 // @interface Foo
1562 // @property double bar;
1563 // @end
1564 //
1565 // void thisIsUnfortunate() {
1566 // id foo;
1567 // double bar = [foo bar];
1568 // }
1569 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001570 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001571 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001572 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001573 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001574}
1575
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001576/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1577/// identical selector names in current and its super classes and issues
1578/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001579void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1580 ObjCMethodDecl *Method,
1581 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001582 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1583 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001585 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001586 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001587 SD->lookupMethod(Method->getSelector(), IsInstance);
1588 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001589 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001590 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001591 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001592 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1593 E = Method->param_end();
1594 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1595 for (; ParamI != E; ++ParamI, ++PrevI) {
1596 // Number of parameters are the same and is guaranteed by selector match.
1597 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1598 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1599 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1600 // If type of arguement of method in this class does not match its
1601 // respective argument type in the super class method, issue warning;
1602 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001603 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001604 << T1 << T2;
1605 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1606 return;
1607 }
1608 }
1609 ID = SD;
1610 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001611}
1612
Steve Naroffa56f6162007-12-18 01:30:32 +00001613// Note: For class/category implemenations, allMethods/allProperties is
1614// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001615void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1616 DeclPtrTy *allMethods, unsigned allNum,
1617 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001618 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001619 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001620
Steve Naroffa56f6162007-12-18 01:30:32 +00001621 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1622 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001623 // should be true.
1624 if (!ClassDecl)
1625 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001626
Mike Stump1eb44332009-09-09 15:08:12 +00001627 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001628 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1629 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001630 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001631
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001632 if (!isInterfaceDeclKind && AtEndLoc.isInvalid()) {
1633 AtEndLoc = ClassDecl->getLocation();
1634 Diag(AtEndLoc, diag::warn_missing_atend);
1635 }
1636
Steve Naroff0701bbb2009-01-08 17:28:14 +00001637 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001638
1639 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1640 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1641 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1642
Chris Lattner4d391482007-12-12 07:09:47 +00001643 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001644 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001645 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001646
1647 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001648 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001649 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001650 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001651 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001652 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001653 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001654 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001655 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001656 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001657 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001658 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001659 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001660 InsMap[Method->getSelector()] = Method;
1661 /// The following allows us to typecheck messages to "id".
1662 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001663 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001664 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001665 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001666 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001667 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001668 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001669 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001670 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001671 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001672 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001673 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001674 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001675 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001676 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001677 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001678 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001679 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001680 /// The following allows us to typecheck messages to "Class".
1681 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001682 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001683 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001684 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001685 }
1686 }
1687 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001688 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001689 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001690 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001691 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001692 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001693 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001694 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001695 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001696 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001697
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001698 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001699 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001700 if (C->getIdentifier() == 0)
1701 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001702 }
Steve Naroff09c47192009-01-09 15:36:25 +00001703 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1704 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1705 // user-defined setter/getter. It also synthesizes setter/getter methods
1706 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001707 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1708 E = CDecl->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001709 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001710 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001711 CDecl->setAtEndLoc(AtEndLoc);
1712 }
1713 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001714 IC->setAtEndLoc(AtEndLoc);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001715 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001716 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001717 AtomicPropertySetterGetterRules(IC, IDecl);
1718 }
Mike Stump1eb44332009-09-09 15:08:12 +00001719 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001720 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001721 CatImplClass->setAtEndLoc(AtEndLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001722
Chris Lattner4d391482007-12-12 07:09:47 +00001723 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001724 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001725 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001726 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001727 Categories; Categories = Categories->getNextClassCategory()) {
1728 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001729 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001730 break;
1731 }
1732 }
1733 }
1734 }
Chris Lattner682bf922009-03-29 16:50:03 +00001735 if (isInterfaceDeclKind) {
1736 // Reject invalid vardecls.
1737 for (unsigned i = 0; i != tuvNum; i++) {
1738 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1739 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1740 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001741 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001742 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001743 }
Chris Lattner682bf922009-03-29 16:50:03 +00001744 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001745 }
Chris Lattner4d391482007-12-12 07:09:47 +00001746}
1747
1748
1749/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1750/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001751static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001752CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1753 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1754 if (PQTVal & ObjCDeclSpec::DQ_In)
1755 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1756 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1757 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1758 if (PQTVal & ObjCDeclSpec::DQ_Out)
1759 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1760 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1761 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1762 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1763 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1764 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1765 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001766
1767 return ret;
1768}
1769
Chris Lattnerb28317a2009-03-28 19:18:32 +00001770Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001771 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001772 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001773 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001774 Selector Sel,
1775 // optional arguments. The number of types/arguments is obtained
1776 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001777 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001778 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001779 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1780 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001781 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001782
1783 // Make sure we can establish a context for the method.
1784 if (!ClassDecl) {
1785 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanian32844b32009-08-28 17:52:37 +00001786 FunctionLabelMap.clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001787 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001788 }
Chris Lattner4d391482007-12-12 07:09:47 +00001789 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Steve Naroffccef3712009-02-20 22:59:16 +00001791 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001792 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Steve Naroffccef3712009-02-20 22:59:16 +00001794 // Methods cannot return interface types. All ObjC objects are
1795 // passed by reference.
1796 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001797 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1798 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001799 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001800 }
1801 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001802 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001803
1804 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001805 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00001806 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001807 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001808 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001809 MethodDeclKind == tok::objc_optional ?
1810 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001811 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Chris Lattner0ed844b2008-04-04 06:12:32 +00001813 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Chris Lattner7db638d2009-04-11 19:42:43 +00001815 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001816 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001817 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Chris Lattnere294d3f2009-04-11 18:57:04 +00001819 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001820 ArgType = Context.getObjCIdType();
1821 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001822 } else {
John McCall58e46772009-10-23 21:48:59 +00001823 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001824 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001825 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001826 }
Mike Stump1eb44332009-09-09 15:08:12 +00001827
John McCall58e46772009-10-23 21:48:59 +00001828 ParmVarDecl* Param
1829 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1830 ArgInfo[i].Name, ArgType, DI,
1831 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001833 if (ArgType->isObjCInterfaceType()) {
1834 Diag(ArgInfo[i].NameLoc,
1835 diag::err_object_cannot_be_passed_returned_by_value)
1836 << 1 << ArgType;
1837 Param->setInvalidDecl();
1838 }
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Chris Lattner0ed844b2008-04-04 06:12:32 +00001840 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001841 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001843 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001844 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Chris Lattner0ed844b2008-04-04 06:12:32 +00001846 Params.push_back(Param);
1847 }
1848
Jay Foadbeaaccd2009-05-21 09:52:38 +00001849 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001850 ObjCMethod->setObjCDeclQualifier(
1851 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1852 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001853
1854 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001855 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001856
John McCall54abf7d2009-11-04 02:18:39 +00001857 const ObjCMethodDecl *InterfaceMD = 0;
1858
Mike Stump1eb44332009-09-09 15:08:12 +00001859 // For implementations (which can be very "coarse grain"), we add the
1860 // method now. This allows the AST to implement lookup methods that work
1861 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001862 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001863 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001864 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001865 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001866 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1867 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001868 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001869 PrevMethod = ImpDecl->getClassMethod(Sel);
1870 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001871 }
John McCall54abf7d2009-11-04 02:18:39 +00001872 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1873 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001874 if (AttrList)
1875 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001876 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001877 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001878 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001879 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1880 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001881 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001882 PrevMethod = CatImpDecl->getClassMethod(Sel);
1883 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001884 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001885 if (AttrList)
1886 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001887 }
1888 if (PrevMethod) {
1889 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001890 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001891 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001892 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001893 }
John McCall54abf7d2009-11-04 02:18:39 +00001894
1895 // If the interface declared this method, and it was deprecated there,
1896 // mark it deprecated here.
1897 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1898 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1899
Chris Lattnerb28317a2009-03-28 19:18:32 +00001900 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001901}
1902
Mike Stump1eb44332009-09-09 15:08:12 +00001903void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001904 SourceLocation Loc,
1905 unsigned &Attributes) {
1906 // FIXME: Improve the reported location.
1907
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001908 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001909 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001910 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1911 ObjCDeclSpec::DQ_PR_assign |
1912 ObjCDeclSpec::DQ_PR_copy |
1913 ObjCDeclSpec::DQ_PR_retain))) {
1914 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1915 "readwrite" :
1916 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1917 "assign" :
1918 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1919 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00001920
1921 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001922 diag::err_objc_property_attr_mutually_exclusive :
1923 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001924 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001925 }
1926
1927 // Check for copy or retain on non-object types.
1928 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001929 !PropertyTy->isObjCObjectPointerType() &&
1930 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00001931 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001932 Diag(Loc, diag::err_objc_property_requires_object)
1933 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001934 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1935 }
1936
1937 // Check for more than one of { assign, copy, retain }.
1938 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1939 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001940 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1941 << "assign" << "copy";
1942 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00001943 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001944 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001945 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1946 << "assign" << "retain";
1947 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001948 }
1949 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1950 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001951 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1952 << "copy" << "retain";
1953 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001954 }
1955 }
1956
1957 // Warn if user supplied no assignment attribute, property is
1958 // readwrite, and this is an object type.
1959 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1960 ObjCDeclSpec::DQ_PR_retain)) &&
1961 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00001962 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001963 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00001964 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001965 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1966
1967 // If non-gc code warn that this is likely inappropriate.
1968 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1969 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00001970
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001971 // FIXME: Implement warning dependent on NSCopying being
1972 // implemented. See also:
1973 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1974 // (please trim this list while you are at it).
1975 }
Mike Stump046efd92009-05-07 23:06:50 +00001976
1977 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1978 && getLangOptions().getGCMode() == LangOptions::GCOnly
1979 && PropertyTy->isBlockPointerType())
1980 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001981}
1982
Mike Stump1eb44332009-09-09 15:08:12 +00001983Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001984 FieldDeclarator &FD,
1985 ObjCDeclSpec &ODS,
1986 Selector GetterSel,
1987 Selector SetterSel,
1988 DeclPtrTy ClassCategory,
1989 bool *isOverridingProperty,
1990 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001991 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001992 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1993 // default is readwrite!
1994 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00001995 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001996 // not retain or copy
1997 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001998 (isReadWrite &&
1999 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002000 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
2001 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahaniandd69aae2009-12-16 18:03:30 +00002002 if (T->isReferenceType()) {
2003 Diag(AtLoc, diag::error_reference_property);
2004 return DeclPtrTy();
2005 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002006 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002007 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002008 // May modify Attributes.
2009 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002010 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2011 if (!CDecl->getIdentifier()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002012 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002013 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002014 if ((CCPrimary = CDecl->getClassInterface())) {
2015 // Find the property in continuation class's primary class only.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002016 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002017 if (ObjCPropertyDecl *PIDecl =
2018 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002019 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00002020 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002021 unsigned PIkind = PIDecl->getPropertyAttributes();
2022 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002023 unsigned retainCopyNonatomic =
Fariborz Jahaniand669be52009-11-06 22:59:12 +00002024 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002025 ObjCPropertyDecl::OBJC_PR_copy |
2026 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002027 if ((Attributes & retainCopyNonatomic) !=
2028 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002029 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002030 Diag(PIDecl->getLocation(), diag::note_property_declare);
2031 }
Fariborz Jahanianb73e2812010-01-06 21:38:30 +00002032 DeclContext *DC = dyn_cast<DeclContext>(CCPrimary);
2033 assert(DC && "ClassDecl is not a DeclContext");
2034 DeclContext::lookup_result Found =
2035 DC->lookup(PIDecl->getDeclName());
2036 bool PropertyInPrimaryClass = false;
2037 for (; Found.first != Found.second; ++Found.first)
2038 if (isa<ObjCPropertyDecl>(*Found.first)) {
2039 PropertyInPrimaryClass = true;
2040 break;
2041 }
2042 if (!PropertyInPrimaryClass) {
2043 // Protocol is not in the primary class. Must build one for it.
2044 ObjCDeclSpec ProtocolPropertyODS;
2045 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind and
2046 // ObjCPropertyDecl::PropertyAttributeKind have identical values.
2047 // Should consolidate both into one enum type.
2048 ProtocolPropertyODS.setPropertyAttributes(
2049 (ObjCDeclSpec::ObjCPropertyAttributeKind)PIkind);
2050 DeclPtrTy ProtocolPtrTy =
2051 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
2052 PIDecl->getGetterName(),
2053 PIDecl->getSetterName(),
2054 DeclPtrTy::make(CCPrimary), isOverridingProperty,
2055 MethodImplKind);
2056 PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>();
2057 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002058 PIDecl->makeitReadWriteAttribute();
2059 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2060 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2061 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2062 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2063 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002064 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002065 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002066 << CCPrimary->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002067 Diag(PIDecl->getLocation(), diag::note_property_declare);
2068 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002069 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002070 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00002071 // class's list.
2072 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002073 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002074 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002075 // No matching property found in the primary class. Just fall thru
2076 // and add property to continuation class's primary class.
2077 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002078 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00002079 Diag(CDecl->getLocation(), diag::err_continuation_class);
2080 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002081 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002082 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002083 }
Mike Stump1eb44332009-09-09 15:08:12 +00002084
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002085 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00002086 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002087 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2088 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2089 if (T->isObjCObjectPointerType()) {
2090 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00002091 if (const ObjCInterfaceType *OIT =
John McCall183700f2009-09-21 23:43:11 +00002092 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002093 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002094 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002095 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002096 LookupProtocol(&Context.Idents.get("NSCopying")))
2097 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00002098 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002099 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002100 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002101 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00002102 if (T->isObjCInterfaceType())
2103 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Steve Naroff93983f82009-01-11 12:47:58 +00002105 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2106 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002107 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00002108 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00002109 FD.D.getIdentifier(), T);
Fariborz Jahanian6dd30fc2009-12-17 00:49:09 +00002110 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
2111 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2112 Diag(PDecl->getLocation(), diag::err_duplicate_property);
2113 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2114 PDecl->setInvalidDecl();
2115 }
2116 else
2117 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002118
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002119 if (T->isArrayType() || T->isFunctionType()) {
2120 Diag(AtLoc, diag::err_property_type) << T;
2121 PDecl->setInvalidDecl();
2122 }
Mike Stump1eb44332009-09-09 15:08:12 +00002123
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002124 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00002125
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00002126 // Regardless of setter/getter attribute, we save the default getter/setter
2127 // selector names in anticipation of declaration of setter/getter methods.
2128 PDecl->setGetterName(GetterSel);
2129 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00002130
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002131 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002132 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00002133
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002134 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002135 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00002136
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002137 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002138 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00002139
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002140 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002141 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00002142
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002143 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002144 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00002145
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002146 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002147 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00002148
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002149 if (isAssign)
2150 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00002151
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002152 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002153 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00002154
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00002155 if (MethodImplKind == tok::objc_required)
2156 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2157 else if (MethodImplKind == tok::objc_optional)
2158 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002159 // A case of continuation class adding a new property in the class. This
2160 // is not what it was meant for. However, gcc supports it and so should we.
2161 // Make sure setter/getters are declared here.
2162 if (CCPrimary)
2163 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002164
Chris Lattnerb28317a2009-03-28 19:18:32 +00002165 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002166}
2167
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002168/// ActOnPropertyImplDecl - This routine performs semantic checks and
2169/// builds the AST node for a property implementation declaration; declared
2170/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002171///
Mike Stump1eb44332009-09-09 15:08:12 +00002172Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002173 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002174 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002175 DeclPtrTy ClassCatImpDecl,
2176 IdentifierInfo *PropertyId,
2177 IdentifierInfo *PropertyIvar) {
2178 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002179 // Make sure we have a context for the property implementation declaration.
2180 if (!ClassImpDecl) {
2181 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002182 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002183 }
2184 ObjCPropertyDecl *property = 0;
2185 ObjCInterfaceDecl* IDecl = 0;
2186 // Find the class or category class where this property must have
2187 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002188 ObjCImplementationDecl *IC = 0;
2189 ObjCCategoryImplDecl* CatImplClass = 0;
2190 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002191 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002192 // We always synthesize an interface for an implementation
2193 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002194 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002195 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002196
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002197 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002198 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002199 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002200 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002201 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002202 }
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002203 if (const ObjCCategoryDecl *CD =
2204 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
2205 if (CD->getIdentifier()) {
2206 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002207 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002208 return DeclPtrTy();
2209 }
2210 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002211 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002212 if (Synthesize) {
2213 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002214 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002215 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002216 IDecl = CatImplClass->getClassInterface();
2217 if (!IDecl) {
2218 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002219 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002220 }
Mike Stump1eb44332009-09-09 15:08:12 +00002221 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002222 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002224 // If category for this implementation not found, it is an error which
2225 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002226 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002227 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002228 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002229 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002230 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002231 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002232 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002233 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002234 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002235 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002236 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002237 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002238 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002239 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002240 // Check that we have a valid, previously declared ivar for @synthesize
2241 if (Synthesize) {
2242 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002243 if (!PropertyIvar)
2244 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002245 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002246 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002247 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002248 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002249 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002250 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002251 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002252 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002253 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2254 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002255 ObjCIvarDecl::Public,
2256 (Expr *)0);
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002257 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002258 IDecl->addDecl(Ivar);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002259 property->setPropertyIvarDecl(Ivar);
2260 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002261 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002262 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002263 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002264 } else if (getLangOptions().ObjCNonFragileABI &&
2265 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002266 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002267 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002268 << ClassDeclared->getDeclName();
2269 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2270 << Ivar << Ivar->getNameAsCString();
2271 // Note! I deliberately want it to fall thru so more errors are caught.
2272 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002273 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002274
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002275 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002276 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002277 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002278 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002279 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002280 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002281 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002282 }
Mike Stump1eb44332009-09-09 15:08:12 +00002283
Chris Lattnerb28317a2009-03-28 19:18:32 +00002284 // FIXME! Rules for properties are somewhat different that those
2285 // for assignments. Use a new routine to consolidate all cases;
2286 // specifically for property redeclarations as well as for ivars.
2287 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2288 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002289 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002290 lhsType->isArithmeticType()) {
2291 Diag(PropertyLoc, diag::error_property_ivar_type)
2292 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002293 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002294 }
2295 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002296 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2297 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002298 Diag(PropertyLoc, diag::error_weak_property)
2299 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002300 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002301 }
Mike Stump1eb44332009-09-09 15:08:12 +00002302 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002303 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2304 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002305 Diag(PropertyLoc, diag::error_strong_property)
2306 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002307 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002308 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002309 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002310 } else if (PropertyIvar)
2311 // @dynamic
2312 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002313 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002314 ObjCPropertyImplDecl *PIDecl =
2315 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2316 property,
2317 (Synthesize ?
2318 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002319 : ObjCPropertyImplDecl::Dynamic),
2320 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002321 if (IC) {
2322 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002323 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002324 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002325 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2326 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002327 << PropertyIvar;
2328 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2329 }
Mike Stump1eb44332009-09-09 15:08:12 +00002330
2331 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002332 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002333 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2334 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002335 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002336 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002337 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002338 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002339 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002340 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002341 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002342 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2343 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002344 << PropertyIvar;
2345 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2346 }
Mike Stump1eb44332009-09-09 15:08:12 +00002347
2348 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002349 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002350 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2351 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002352 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002353 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002354 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002355 }
Mike Stump1eb44332009-09-09 15:08:12 +00002356
Chris Lattnerb28317a2009-03-28 19:18:32 +00002357 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002358}
Anders Carlsson15281452008-11-04 16:57:32 +00002359
Chris Lattnercc98eac2008-12-17 07:13:27 +00002360bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002361 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002362 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002363
Anders Carlsson15281452008-11-04 16:57:32 +00002364 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2365 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002366
Anders Carlsson15281452008-11-04 16:57:32 +00002367 return true;
2368}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002369
Chris Lattnercc98eac2008-12-17 07:13:27 +00002370/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2371/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002372void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002373 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002374 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002375 // Check that ClassName is a valid class
2376 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2377 if (!Class) {
2378 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2379 return;
2380 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002381 if (LangOpts.ObjCNonFragileABI) {
2382 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2383 return;
2384 }
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Chris Lattnercc98eac2008-12-17 07:13:27 +00002386 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002387 llvm::SmallVector<FieldDecl*, 32> RecFields;
2388 Context.CollectObjCIvars(Class, RecFields);
2389 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2390 for (unsigned i = 0; i < RecFields.size(); i++) {
2391 FieldDecl* ID = RecFields[i];
2392 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2393 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2394 ID->getIdentifier(), ID->getType(),
2395 ID->getBitWidth());
2396 Decls.push_back(Sema::DeclPtrTy::make(FD));
2397 }
Mike Stump1eb44332009-09-09 15:08:12 +00002398
Chris Lattnercc98eac2008-12-17 07:13:27 +00002399 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002400 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002401 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002402 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002403 if (getLangOptions().CPlusPlus)
2404 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002405 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002406 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002407 }
2408}
2409