blob: 149fe15fabec7251bdaa52f1f5feab2a7fe186ba [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
52 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000053 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000054 AddInstanceMethodToGlobalPool(MDecl);
55 else
56 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000057
Chris Lattner4d391482007-12-12 07:09:47 +000058 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000059 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +000060 PushFunctionScope();
61
Chris Lattner4d391482007-12-12 07:09:47 +000062 // Create Decl objects for each parameter, entrring them in the scope for
63 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000064
65 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000066 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +000067
Daniel Dunbar451318c2008-08-26 06:07:48 +000068 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
69 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000070
Chris Lattner8123a952008-04-10 02:22:51 +000071 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000072 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
73 E = MDecl->param_end(); PI != E; ++PI)
74 if ((*PI)->getIdentifier())
75 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000076}
77
Chris Lattnerb28317a2009-03-28 19:18:32 +000078Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000079ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
80 IdentifierInfo *ClassName, SourceLocation ClassLoc,
81 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000082 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +000083 const SourceLocation *ProtoLocs,
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,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000204 ProtoLocs, 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,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000282 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000283 SourceLocation EndProtoLoc,
284 AttributeList *AttrList) {
285 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000286 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000287 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000288 if (PDecl) {
289 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000290 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000291 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000292 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000293 // Just return the protocol we already had.
294 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000295 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000296 }
Steve Naroff61d68522009-03-05 15:22:01 +0000297 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000298 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000299 CheckForwardProtocolDeclarationForCircularDependency(
300 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
301 PList.Destroy(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Steve Narofff11b5082008-08-13 16:39:22 +0000303 // Make sure the cached decl gets a valid start location.
304 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000305 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000306 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000307 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000308 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000309 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000310 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000311 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000312 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000313 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000314 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000315 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000316 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
317 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000318 PDecl->setLocEnd(EndProtoLoc);
319 }
Mike Stump1eb44332009-09-09 15:08:12 +0000320
321 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000322 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000323}
324
325/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000326/// issues an error if they are not declared. It returns list of
327/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000328void
Chris Lattnere13b9592008-07-26 04:03:38 +0000329Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000330 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000331 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000332 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000333 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000334 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000335 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000336 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
337 LookupObjCProtocolName);
338 if (CorrectTypo(R, TUScope, 0) &&
339 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
340 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
341 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000342 Diag(PDecl->getLocation(), diag::note_previous_decl)
343 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000344 }
345 }
346
347 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000348 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000349 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000350 continue;
351 }
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000353 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000354
355 // If this is a forward declaration and we are supposed to warn in this
356 // case, do it.
357 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000358 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000359 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000360 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000361 }
362}
363
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000364/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000365/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000366///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000367void
Mike Stump1eb44332009-09-09 15:08:12 +0000368Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000369 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000370 const IdentifierInfo *inheritedName) {
Mike Stump1eb44332009-09-09 15:08:12 +0000371 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000372 Property->getPropertyAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000373 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000374 SuperProperty->getPropertyAttributes();
375 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
376 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000377 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000378 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000379 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
380 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000381 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000382 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000383 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
384 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000385 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000386 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000388 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
389 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000390 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000391 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000392 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000393 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump1eb44332009-09-09 15:08:12 +0000394 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000395 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000396 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000397 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000398
Mike Stump1eb44332009-09-09 15:08:12 +0000399 QualType LHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000400 Context.getCanonicalType(SuperProperty->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000401 QualType RHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000402 Context.getCanonicalType(Property->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Steve Naroff15edf0d2009-03-03 15:43:24 +0000404 if (!Context.typesAreCompatible(LHSType, RHSType)) {
405 // FIXME: Incorporate this test with typesAreCompatible.
406 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff4084c302009-07-23 01:01:38 +0000407 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff15edf0d2009-03-03 15:43:24 +0000408 return;
409 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
410 << Property->getType() << SuperProperty->getType() << inheritedName;
411 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000412}
413
414/// ComparePropertiesInBaseAndSuper - This routine compares property
415/// declarations in base and its super class, if any, and issues
416/// diagnostics in a variety of inconsistant situations.
417///
Chris Lattner70f19542009-02-16 21:26:43 +0000418void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000419 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
420 if (!SDecl)
421 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000422 // FIXME: O(N^2)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000423 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
424 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000425 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000426 // Does property in super class has declaration in current class?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000427 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
428 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000429 ObjCPropertyDecl *PDecl = (*I);
430 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump1eb44332009-09-09 15:08:12 +0000431 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000432 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000433 }
434 }
435}
436
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000437/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
438/// of properties declared in a protocol and compares their attribute against
439/// the same property declared in the class or category.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000440void
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000441Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000442 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000443 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
444 if (!IDecl) {
445 // Category
446 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000447 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
Fariborz Jahanian25760612010-02-15 21:55:26 +0000448 if (!CatDecl->IsClassExtension())
449 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
450 E = PDecl->prop_end(); P != E; ++P) {
451 ObjCPropertyDecl *Pr = (*P);
452 ObjCCategoryDecl::prop_iterator CP, CE;
453 // Is this property already in category's list of properties?
454 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
455 if ((*CP)->getIdentifier() == Pr->getIdentifier())
456 break;
457 if (CP != CE)
458 // Property protocol already exist in class. Diagnose any mismatch.
459 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
460 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000461 return;
462 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000463 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
464 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000465 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000466 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000467 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000468 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000469 if ((*CP)->getIdentifier() == Pr->getIdentifier())
470 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000471 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000472 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000473 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000474 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000475}
476
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000477/// CompareProperties - This routine compares properties
478/// declared in 'ClassOrProtocol' objects (which can be a class or an
479/// inherited protocol with the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000480///
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000481void Sema::CompareProperties(Decl *CDecl,
482 DeclPtrTy ClassOrProtocol) {
483 Decl *ClassDecl = ClassOrProtocol.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000484 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
485
486 if (!IDecl) {
487 // Category
488 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000489 assert (CatDecl && "CompareProperties");
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000490 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
491 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
492 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000493 // Match properties of category with those of protocol (*P)
494 MatchOneProtocolPropertiesInClass(CatDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000496 // Go thru the list of protocols for this category and recursively match
497 // their properties with those in the category.
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000498 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
499 E = CatDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000500 CompareProperties(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000501 } else {
502 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
503 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
504 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000505 MatchOneProtocolPropertiesInClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000506 }
507 return;
508 }
509
Chris Lattnerb752f282008-07-21 07:06:49 +0000510 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000511 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
512 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000513 // Match properties of class IDecl with those of protocol (*P).
514 MatchOneProtocolPropertiesInClass(IDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000516 // Go thru the list of protocols for this class and recursively match
517 // their properties with those declared in the class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000518 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
519 E = IDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000520 CompareProperties(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000521 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000522 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
523 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
524 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian107089f2010-01-18 18:41:16 +0000525 MatchOneProtocolPropertiesInClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000526 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000527}
528
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000529/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000530/// a class method in its extension.
531///
Mike Stump1eb44332009-09-09 15:08:12 +0000532void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000533 ObjCInterfaceDecl *ID) {
534 if (!ID)
535 return; // Possibly due to previous error
536
537 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000538 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
539 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000540 ObjCMethodDecl *MD = *i;
541 MethodMap[MD->getSelector()] = MD;
542 }
543
544 if (MethodMap.empty())
545 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000546 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
547 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000548 ObjCMethodDecl *Method = *i;
549 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
550 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
551 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
552 << Method->getDeclName();
553 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
554 }
555 }
556}
557
Chris Lattner58fe03b2009-04-12 08:43:13 +0000558/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000559Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000560Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000561 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000562 unsigned NumElts,
563 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000564 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000565 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Chris Lattner4d391482007-12-12 07:09:47 +0000567 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000568 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000569 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000570 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000571 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000572 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000573 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000574 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000575 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000576 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000577 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000578 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000579 }
Mike Stump1eb44332009-09-09 15:08:12 +0000580
581 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000582 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000583 Protocols.data(), Protocols.size(),
584 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000585 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000586 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000587 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000588}
589
Chris Lattnerb28317a2009-03-28 19:18:32 +0000590Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000591ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
592 IdentifierInfo *ClassName, SourceLocation ClassLoc,
593 IdentifierInfo *CategoryName,
594 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000595 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000596 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000597 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000598 SourceLocation EndProtoLoc) {
Fariborz Jahanian25760612010-02-15 21:55:26 +0000599 ObjCCategoryDecl *CDecl = 0;
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000600 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Ted Kremenek09b68972010-02-23 19:39:46 +0000601
602 /// Check that class of this category is already completely declared.
603 if (!IDecl || IDecl->isForwardDecl()) {
604 // Create an invalid ObjCCategoryDecl to serve as context for
605 // the enclosing method declarations. We mark the decl invalid
606 // to make it clear that this isn't a valid AST.
607 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
608 ClassLoc, CategoryLoc, CategoryName);
609 CDecl->setInvalidDecl();
610 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
611 return DeclPtrTy::make(CDecl);
612 }
613
614 if (!CategoryName) {
Fariborz Jahanian25760612010-02-15 21:55:26 +0000615 // Class extensions require a special treatment. Use an existing one.
Ted Kremenek09b68972010-02-23 19:39:46 +0000616 // Note that 'getClassExtension()' can return NULL.
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000617 CDecl = IDecl->getClassExtension();
Ted Kremenek09b68972010-02-23 19:39:46 +0000618 }
619
Fariborz Jahanian25760612010-02-15 21:55:26 +0000620 if (!CDecl) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000621 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
622 ClassLoc, CategoryLoc, CategoryName);
Fariborz Jahanian25760612010-02-15 21:55:26 +0000623 // FIXME: PushOnScopeChains?
624 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000625
Fariborz Jahanian25760612010-02-15 21:55:26 +0000626 CDecl->setClassInterface(IDecl);
627 // Insert first use of class extension to the list of class's categories.
628 if (!CategoryName)
629 CDecl->insertNextClassCategory();
630 }
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Chris Lattner16b34b42009-02-16 21:30:01 +0000632 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000633 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000634
Fariborz Jahanian25760612010-02-15 21:55:26 +0000635 if (CategoryName) {
636 /// Check for duplicate interface declaration for this category
637 ObjCCategoryDecl *CDeclChain;
638 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
639 CDeclChain = CDeclChain->getNextClassCategory()) {
640 if (CDeclChain->getIdentifier() == CategoryName) {
641 // Class extensions can be declared multiple times.
642 Diag(CategoryLoc, diag::warn_dup_category_def)
643 << ClassName << CategoryName;
644 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
645 break;
646 }
Chris Lattner70f19542009-02-16 21:26:43 +0000647 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000648 if (!CDeclChain)
649 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000650 }
Chris Lattner70f19542009-02-16 21:26:43 +0000651
Chris Lattner4d391482007-12-12 07:09:47 +0000652 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000653 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000654 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000655 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000656 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000657 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000658 NumProtoRefs, ProtoLocs,
659 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000660 }
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Anders Carlsson15281452008-11-04 16:57:32 +0000662 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000663 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000664}
665
666/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000667/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000668/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000669Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000670 SourceLocation AtCatImplLoc,
671 IdentifierInfo *ClassName, SourceLocation ClassLoc,
672 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000673 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000674 ObjCCategoryDecl *CatIDecl = 0;
675 if (IDecl) {
676 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
677 if (!CatIDecl) {
678 // Category @implementation with no corresponding @interface.
679 // Create and install one.
680 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000681 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000682 CatName);
683 CatIDecl->setClassInterface(IDecl);
684 CatIDecl->insertNextClassCategory();
685 }
686 }
687
Mike Stump1eb44332009-09-09 15:08:12 +0000688 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000689 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
690 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000691 /// Check that class of this category is already completely declared.
692 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000693 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000694
Douglas Gregord0434102009-01-09 00:49:46 +0000695 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000696 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000697
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000698 /// Check that CatName, category name, is not used in another implementation.
699 if (CatIDecl) {
700 if (CatIDecl->getImplementation()) {
701 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
702 << CatName;
703 Diag(CatIDecl->getImplementation()->getLocation(),
704 diag::note_previous_definition);
705 } else
706 CatIDecl->setImplementation(CDecl);
707 }
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Anders Carlsson15281452008-11-04 16:57:32 +0000709 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000710 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000711}
712
Chris Lattnerb28317a2009-03-28 19:18:32 +0000713Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000714 SourceLocation AtClassImplLoc,
715 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000716 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000717 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000718 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000719 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000720 NamedDecl *PrevDecl
721 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000722 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000723 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000724 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000725 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
726 // If this is a forward declaration of an interface, warn.
727 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000728 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000729 IDecl = 0;
730 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000731 } else {
732 // We did not find anything with the name ClassName; try to correct for
733 // typos in the class name.
734 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
735 if (CorrectTypo(R, TUScope, 0) &&
736 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000737 // Suggest the (potentially) correct interface name. However, put the
738 // fix-it hint itself in a separate note, since changing the name in
739 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000740 // provide a code-modification hint or use the typo name for recovery,
741 // because this is just a warning. The program may actually be correct.
742 Diag(ClassLoc, diag::warn_undef_interface_suggest)
743 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000744 Diag(IDecl->getLocation(), diag::note_previous_decl)
745 << R.getLookupName()
746 << CodeModificationHint::CreateReplacement(ClassLoc,
747 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000748 IDecl = 0;
749 } else {
750 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
751 }
Chris Lattner4d391482007-12-12 07:09:47 +0000752 }
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Chris Lattner4d391482007-12-12 07:09:47 +0000754 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000755 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000756 if (SuperClassname) {
757 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000758 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000759 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000760 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
761 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000762 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000763 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000764 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000765 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000766 Diag(SuperClassLoc, diag::err_undef_superclass)
767 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000768 else if (IDecl && IDecl->getSuperClass() != SDecl) {
769 // This implementation and its interface do not have the same
770 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000771 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000772 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000773 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000774 }
775 }
776 }
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Chris Lattner4d391482007-12-12 07:09:47 +0000778 if (!IDecl) {
779 // Legacy case of @implementation with no corresponding @interface.
780 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000781
Mike Stump390b4cc2009-05-16 07:39:55 +0000782 // FIXME: Do we support attributes on the @implementation? If so we should
783 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000784 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000785 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000786 IDecl->setSuperClass(SDecl);
787 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000788
789 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000790 } else {
791 // Mark the interface as being completed, even if it was just as
792 // @class ....;
793 // declaration; the user cannot reopen it.
794 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000795 }
Mike Stump1eb44332009-09-09 15:08:12 +0000796
797 ObjCImplementationDecl* IMPDecl =
798 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000799 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Anders Carlsson15281452008-11-04 16:57:32 +0000801 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000802 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Chris Lattner4d391482007-12-12 07:09:47 +0000804 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000805 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000806 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000807 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000808 Diag(IDecl->getImplementation()->getLocation(),
809 diag::note_previous_definition);
810 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000811 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000812 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000813 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000814 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000815}
816
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000817void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
818 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000819 SourceLocation RBrace) {
820 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000821 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000822 if (!IDecl)
823 return;
824 /// Check case of non-existing @interface decl.
825 /// (legacy objective-c @implementation decl without an @interface decl).
826 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000827 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000828 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000829 // Add ivar's to class's DeclContext.
830 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000831 ivars[i]->setLexicalDeclContext(ImpDecl);
832 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000833 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000834 }
835
Chris Lattner4d391482007-12-12 07:09:47 +0000836 return;
837 }
838 // If implementation has empty ivar list, just return.
839 if (numIvars == 0)
840 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Chris Lattner4d391482007-12-12 07:09:47 +0000842 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000843 if (LangOpts.ObjCNonFragileABI2) {
844 if (ImpDecl->getSuperClass())
845 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
846 for (unsigned i = 0; i < numIvars; i++) {
847 ObjCIvarDecl* ImplIvar = ivars[i];
848 if (const ObjCIvarDecl *ClsIvar =
849 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
850 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
851 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
852 continue;
853 }
854 if (ImplIvar->getAccessControl() != ObjCIvarDecl::Private)
855 Diag(ImplIvar->getLocation(), diag::err_non_private_ivar_declaration);
856 // Instance ivar to Implementation's DeclContext.
857 ImplIvar->setLexicalDeclContext(ImpDecl);
858 IDecl->makeDeclVisibleInContext(ImplIvar, false);
859 ImpDecl->addDecl(ImplIvar);
860 }
861 return;
862 }
Chris Lattner4d391482007-12-12 07:09:47 +0000863 // Check interface's Ivar list against those in the implementation.
864 // names and types must match.
865 //
Chris Lattner4d391482007-12-12 07:09:47 +0000866 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000867 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000868 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
869 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000870 ObjCIvarDecl* ImplIvar = ivars[j++];
871 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000872 assert (ImplIvar && "missing implementation ivar");
873 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Steve Naroffca331292009-03-03 14:49:36 +0000875 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000876 if (Context.getCanonicalType(ImplIvar->getType()) !=
877 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000878 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000879 << ImplIvar->getIdentifier()
880 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000881 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000882 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
883 Expr *ImplBitWidth = ImplIvar->getBitWidth();
884 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000885 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
886 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000887 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
888 << ImplIvar->getIdentifier();
889 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
890 }
Mike Stump1eb44332009-09-09 15:08:12 +0000891 }
Steve Naroffca331292009-03-03 14:49:36 +0000892 // Make sure the names are identical.
893 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000894 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000895 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000896 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000897 }
898 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000899 }
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Chris Lattner609e4c72007-12-12 18:11:49 +0000901 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000902 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000903 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000904 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000905}
906
Steve Naroff3c2eb662008-02-10 21:38:56 +0000907void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
908 bool &IncompleteImpl) {
909 if (!IncompleteImpl) {
910 Diag(ImpLoc, diag::warn_incomplete_impl);
911 IncompleteImpl = true;
912 }
Chris Lattner08631c52008-11-23 21:45:46 +0000913 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000914}
915
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000916void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
917 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000918 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000919 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000920 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
921 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000922 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000923 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
924 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000925 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Chris Lattner3aff9192009-04-11 19:58:42 +0000928 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
929 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
930 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000931 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
932 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
933 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
934 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000935 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000936
937 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000938 << ImpMethodDecl->getDeclName() << (*IF)->getType()
939 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000940 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000941 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000942}
943
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000944/// isPropertyReadonly - Return true if property is readonly, by searching
945/// for the property in the class and in its categories and implementations
946///
947bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000948 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000949 // by far the most common case.
950 if (!PDecl->isReadOnly())
951 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000952 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000953 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000954 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000955 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000957 // Main class has the property as 'readonly'. Must search
Mike Stump1eb44332009-09-09 15:08:12 +0000958 // through the category list to see if the property's
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000959 // attribute has been over-ridden to 'readwrite'.
960 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
961 Category; Category = Category->getNextClassCategory()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000962 // Even if property is ready only, if a category has a user defined setter,
963 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000964 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000965 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000966 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000967 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000968 if (P && !P->isReadOnly())
969 return false;
970 }
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000972 // Also, check for definition of a setter method in the implementation if
973 // all else failed.
974 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000975 if (ObjCImplementationDecl *IMD =
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000976 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000977 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000978 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000979 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000980 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000981 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000982 return false;
983 }
984 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000985 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000986 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000987 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000988 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000989 // If all fails, look at the super class.
990 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
991 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000992 return true;
993}
994
Mike Stump390b4cc2009-05-16 07:39:55 +0000995/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
996/// improve the efficiency of selector lookups and type checking by associating
997/// with each protocol / interface / category the flattened instance tables. If
998/// we used an immutable set to keep the table then it wouldn't add significant
999/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001000
Steve Naroffefe7f362008-02-08 22:06:17 +00001001/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001002/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001003void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1004 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001005 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001006 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001007 const llvm::DenseSet<Selector> &ClsMap,
1008 ObjCInterfaceDecl *IDecl) {
1009 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001010 ObjCInterfaceDecl *NSIDecl = 0;
1011 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001012 // check to see if class implements forwardInvocation method and objects
1013 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001014 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001015 // Under such conditions, which means that every method possible is
1016 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001017 // found" warnings.
1018 // FIXME: Use a general GetUnarySelector method for this.
1019 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1020 Selector fISelector = Context.Selectors.getSelector(1, &II);
1021 if (InsMap.count(fISelector))
1022 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1023 // need be implemented in the implementation.
1024 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1025 }
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001027 // If a method lookup fails locally we still need to look and see if
1028 // the method was implemented by a base class or an inherited
1029 // protocol. This lookup is slow, but occurs rarely in correct code
1030 // and otherwise would terminate in a warning.
1031
Chris Lattner4d391482007-12-12 07:09:47 +00001032 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001033 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001034 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001035 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001036 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001037 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001038 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001039 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001040 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001041 // Ugly, but necessary. Method declared in protcol might have
1042 // have been synthesized due to a property declared in the class which
1043 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001044 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001045 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001046 if (!MethodInClass || !MethodInClass->isSynthesized())
1047 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
1048 }
1049 }
Chris Lattner4d391482007-12-12 07:09:47 +00001050 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001051 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001052 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001053 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001054 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001055 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1056 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001057 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +00001058 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001059 }
Chris Lattner780f3292008-07-21 21:32:27 +00001060 // Check on this protocols's referenced protocols, recursively.
1061 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1062 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001063 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001064}
1065
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001066/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1067/// or protocol against those declared in their implementations.
1068///
1069void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1070 const llvm::DenseSet<Selector> &ClsMap,
1071 llvm::DenseSet<Selector> &InsMapSeen,
1072 llvm::DenseSet<Selector> &ClsMapSeen,
1073 ObjCImplDecl* IMPDecl,
1074 ObjCContainerDecl* CDecl,
1075 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001076 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001077 // Check and see if instance methods in class interface have been
1078 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001079 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1080 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001081 if (InsMapSeen.count((*I)->getSelector()))
1082 continue;
1083 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001084 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001085 !InsMap.count((*I)->getSelector())) {
1086 if (ImmediateClass)
1087 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1088 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001089 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001090 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001091 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001092 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001093 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001094 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001095 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1096 // ImpMethodDecl may be null as in a @dynamic property.
1097 if (ImpMethodDecl)
1098 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1099 }
1100 }
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001102 // Check and see if class methods in class interface have been
1103 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001104 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001105 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001106 if (ClsMapSeen.count((*I)->getSelector()))
1107 continue;
1108 ClsMapSeen.insert((*I)->getSelector());
1109 if (!ClsMap.count((*I)->getSelector())) {
1110 if (ImmediateClass)
1111 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001112 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001113 ObjCMethodDecl *ImpMethodDecl =
1114 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001115 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001116 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001117 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1118 }
1119 }
1120 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1121 // Check for any implementation of a methods declared in protocol.
1122 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1123 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001124 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1125 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001126 (*PI), IncompleteImpl, false);
1127 if (I->getSuperClass())
1128 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001129 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001130 I->getSuperClass(), IncompleteImpl, false);
1131 }
1132}
1133
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001134/// CollectImmediateProperties - This routine collects all properties in
1135/// the class and its conforming protocols; but not those it its super class.
1136void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
1137 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1138 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1139 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1140 E = IDecl->prop_end(); P != E; ++P) {
1141 ObjCPropertyDecl *Prop = (*P);
1142 PropMap[Prop->getIdentifier()] = Prop;
1143 }
1144 // scan through class's protocols.
1145 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
1146 E = IDecl->protocol_end(); PI != E; ++PI)
1147 CollectImmediateProperties((*PI), PropMap);
1148 }
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001149 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001150 if (!CATDecl->IsClassExtension())
1151 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1152 E = CATDecl->prop_end(); P != E; ++P) {
1153 ObjCPropertyDecl *Prop = (*P);
1154 PropMap[Prop->getIdentifier()] = Prop;
1155 }
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001156 // scan through class's protocols.
1157 for (ObjCInterfaceDecl::protocol_iterator PI = CATDecl->protocol_begin(),
1158 E = CATDecl->protocol_end(); PI != E; ++PI)
1159 CollectImmediateProperties((*PI), PropMap);
1160 }
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001161 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1162 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1163 E = PDecl->prop_end(); P != E; ++P) {
1164 ObjCPropertyDecl *Prop = (*P);
1165 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1166 if (!PropEntry)
1167 PropEntry = Prop;
1168 }
1169 // scan through protocol's protocols.
1170 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1171 E = PDecl->protocol_end(); PI != E; ++PI)
1172 CollectImmediateProperties((*PI), PropMap);
1173 }
1174}
1175
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001176/// LookupPropertyDecl - Looks up a property in the current class and all
1177/// its protocols.
1178ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1179 IdentifierInfo *II) {
1180 if (const ObjCInterfaceDecl *IDecl =
1181 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1182 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1183 E = IDecl->prop_end(); P != E; ++P) {
1184 ObjCPropertyDecl *Prop = (*P);
1185 if (Prop->getIdentifier() == II)
1186 return Prop;
1187 }
1188 // scan through class's protocols.
1189 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
1190 E = IDecl->protocol_end(); PI != E; ++PI) {
1191 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1192 if (Prop)
1193 return Prop;
1194 }
1195 }
1196 else if (const ObjCProtocolDecl *PDecl =
1197 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1198 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1199 E = PDecl->prop_end(); P != E; ++P) {
1200 ObjCPropertyDecl *Prop = (*P);
1201 if (Prop->getIdentifier() == II)
1202 return Prop;
1203 }
1204 // scan through protocol's protocols.
1205 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1206 E = PDecl->protocol_end(); PI != E; ++PI) {
1207 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1208 if (Prop)
1209 return Prop;
1210 }
1211 }
1212 return 0;
1213}
1214
1215
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001216void Sema::DiagnoseUnimplementedProperties(ObjCImplDecl* IMPDecl,
1217 ObjCContainerDecl *CDecl,
1218 const llvm::DenseSet<Selector>& InsMap) {
1219 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1220 CollectImmediateProperties(CDecl, PropMap);
Fariborz Jahaniana84f2e42010-01-20 17:27:59 +00001221 if (PropMap.empty())
1222 return;
1223
1224 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1225 for (ObjCImplDecl::propimpl_iterator
1226 I = IMPDecl->propimpl_begin(),
1227 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1228 PropImplMap.insert((*I)->getPropertyDecl());
1229
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001230 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1231 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1232 ObjCPropertyDecl *Prop = P->second;
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001233 // Is there a matching propery synthesize/dynamic?
Fariborz Jahaniana84f2e42010-01-20 17:27:59 +00001234 if (Prop->isInvalidDecl() ||
1235 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1236 PropImplMap.count(Prop))
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001237 continue;
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001238 if (LangOpts.ObjCNonFragileABI2) {
1239 ActOnPropertyImplDecl(IMPDecl->getLocation(),
1240 SourceLocation(),
1241 true, DeclPtrTy::make(IMPDecl),
1242 Prop->getIdentifier(),
1243 Prop->getIdentifier());
1244 continue;
1245 }
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001246 if (!InsMap.count(Prop->getGetterName())) {
1247 Diag(Prop->getLocation(),
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001248 isa<ObjCCategoryDecl>(CDecl) ?
1249 diag::warn_setter_getter_impl_required_in_category :
1250 diag::warn_setter_getter_impl_required)
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001251 << Prop->getDeclName() << Prop->getGetterName();
1252 Diag(IMPDecl->getLocation(),
1253 diag::note_property_impl_required);
1254 }
1255
1256 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1257 Diag(Prop->getLocation(),
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001258 isa<ObjCCategoryDecl>(CDecl) ?
1259 diag::warn_setter_getter_impl_required_in_category :
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001260 diag::warn_setter_getter_impl_required)
1261 << Prop->getDeclName() << Prop->getSetterName();
1262 Diag(IMPDecl->getLocation(),
1263 diag::note_property_impl_required);
1264 }
1265 }
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001266}
1267
Mike Stump1eb44332009-09-09 15:08:12 +00001268void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1269 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001270 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001271 llvm::DenseSet<Selector> InsMap;
1272 // Check and see if instance methods in class interface have been
1273 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001274 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001275 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001276 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001278 // Check and see if properties declared in the interface have either 1)
1279 // an implementation or 2) there is a @synthesize/@dynamic implementation
1280 // of the property in the @implementation.
1281 if (isa<ObjCInterfaceDecl>(CDecl))
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001282 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
1283
Chris Lattner4d391482007-12-12 07:09:47 +00001284 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001285 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001286 I = IMPDecl->classmeth_begin(),
1287 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001288 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001290 // Check for type conflict of methods declared in a class/protocol and
1291 // its implementation; if any.
1292 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001293 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1294 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001295 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Chris Lattner4d391482007-12-12 07:09:47 +00001297 // Check the protocol list for unimplemented methods in the @implementation
1298 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001299 // Check and see if class methods in class interface have been
1300 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Chris Lattnercddc8882009-03-01 00:56:52 +00001302 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001303 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001304 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001305 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001306 InsMap, ClsMap, I);
1307 // Check class extensions (unnamed categories)
1308 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1309 Categories; Categories = Categories->getNextClassCategory()) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001310 if (Categories->IsClassExtension()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001311 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1312 break;
1313 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001314 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001315 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001316 // For extended class, unimplemented methods in its protocols will
1317 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001318 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001319 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1320 E = C->protocol_end(); PI != E; ++PI)
1321 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1322 InsMap, ClsMap, C->getClassInterface());
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001323 // Report unimplemented properties in the category as well.
1324 // When reporting on missing setter/getters, do not report when
1325 // setter/getter is implemented in category's primary class
1326 // implementation.
1327 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1328 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1329 for (ObjCImplementationDecl::instmeth_iterator
1330 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1331 InsMap.insert((*I)->getSelector());
1332 }
1333 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
1334 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001335 } else
1336 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001337}
1338
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001339void
1340Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1341 ObjCContainerDecl* IDecl) {
1342 // Rules apply in non-GC mode only
1343 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1344 return;
1345 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1346 E = IDecl->prop_end();
1347 I != E; ++I) {
1348 ObjCPropertyDecl *Property = (*I);
1349 unsigned Attributes = Property->getPropertyAttributes();
1350 // We only care about readwrite atomic property.
1351 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1352 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1353 continue;
1354 if (const ObjCPropertyImplDecl *PIDecl
1355 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1356 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1357 continue;
1358 ObjCMethodDecl *GetterMethod =
1359 IMPDecl->getInstanceMethod(Property->getGetterName());
1360 ObjCMethodDecl *SetterMethod =
1361 IMPDecl->getInstanceMethod(Property->getSetterName());
1362 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1363 SourceLocation MethodLoc =
1364 (GetterMethod ? GetterMethod->getLocation()
1365 : SetterMethod->getLocation());
1366 Diag(MethodLoc, diag::warn_atomic_property_rule)
1367 << Property->getIdentifier();
1368 Diag(Property->getLocation(), diag::note_property_declare);
1369 }
1370 }
1371 }
1372}
1373
Mike Stump1eb44332009-09-09 15:08:12 +00001374/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001375Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001376Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001377 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001378 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001379 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001380 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Chris Lattner4d391482007-12-12 07:09:47 +00001382 for (unsigned i = 0; i != NumElts; ++i) {
1383 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001384 NamedDecl *PrevDecl
1385 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001386 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001387 // Maybe we will complain about the shadowed template parameter.
1388 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1389 // Just pretend that we didn't see the previous declaration.
1390 PrevDecl = 0;
1391 }
1392
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001393 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001394 // GCC apparently allows the following idiom:
1395 //
1396 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1397 // @class XCElementToggler;
1398 //
Mike Stump1eb44332009-09-09 15:08:12 +00001399 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001400 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1401 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001402 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001403 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001404 } else if (TDD) {
1405 // a forward class declaration matching a typedef name of a class refers
1406 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001407 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001408 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1409 PrevDecl = OI->getDecl();
1410 }
Chris Lattner4d391482007-12-12 07:09:47 +00001411 }
Mike Stump1eb44332009-09-09 15:08:12 +00001412 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001413 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001414 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001415 IdentList[i], IdentLocs[i], true);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001416
1417 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1418 // the current DeclContext. This prevents clients that walk DeclContext
1419 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1420 // declared later (if at all). We also take care to explicitly make
1421 // sure this declaration is visible for name lookup.
1422 PushOnScopeChains(IDecl, TUScope, false);
1423 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001424 }
1425
1426 Interfaces.push_back(IDecl);
1427 }
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Ted Kremenek321c22f2009-11-18 00:28:11 +00001429 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001430 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001431 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001432 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001433 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001434 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001435 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001436}
1437
1438
1439/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1440/// returns true, or false, accordingly.
1441/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001442bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001443 const ObjCMethodDecl *PrevMethod,
1444 bool matchBasedOnSizeAndAlignment) {
1445 QualType T1 = Context.getCanonicalType(Method->getResultType());
1446 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001448 if (T1 != T2) {
1449 // The result types are different.
1450 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001451 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001452 // Incomplete types don't have a size and alignment.
1453 if (T1->isIncompleteType() || T2->isIncompleteType())
1454 return false;
1455 // Check is based on size and alignment.
1456 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1457 return false;
1458 }
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Chris Lattner89951a82009-02-20 18:43:26 +00001460 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1461 E = Method->param_end();
1462 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Chris Lattner89951a82009-02-20 18:43:26 +00001464 for (; ParamI != E; ++ParamI, ++PrevI) {
1465 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1466 T1 = Context.getCanonicalType((*ParamI)->getType());
1467 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001468 if (T1 != T2) {
1469 // The result types are different.
1470 if (!matchBasedOnSizeAndAlignment)
1471 return false;
1472 // Incomplete types don't have a size and alignment.
1473 if (T1->isIncompleteType() || T2->isIncompleteType())
1474 return false;
1475 // Check is based on size and alignment.
1476 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1477 return false;
1478 }
Chris Lattner4d391482007-12-12 07:09:47 +00001479 }
1480 return true;
1481}
1482
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001483/// \brief Read the contents of the instance and factory method pools
1484/// for a given selector from external storage.
1485///
1486/// This routine should only be called once, when neither the instance
1487/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001488Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001489 bool isInstance) {
1490 assert(ExternalSource && "We need an external AST source");
1491 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1492 "Selector data already loaded into the instance method pool");
1493 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1494 "Selector data already loaded into the factory method pool");
1495
1496 // Read the method list from the external source.
1497 std::pair<ObjCMethodList, ObjCMethodList> Methods
1498 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001500 if (isInstance) {
1501 if (Methods.second.Method)
1502 FactoryMethodPool[Sel] = Methods.second;
1503 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001504 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001505
1506 if (Methods.first.Method)
1507 InstanceMethodPool[Sel] = Methods.first;
1508
1509 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1510}
1511
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001512void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001513 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1514 = InstanceMethodPool.find(Method->getSelector());
1515 if (Pos == InstanceMethodPool.end()) {
1516 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1517 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1518 else
1519 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1520 ObjCMethodList())).first;
1521 }
1522
1523 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001524 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001525 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001526 Entry.Method = Method;
1527 Entry.Next = 0;
1528 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001529 }
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Chris Lattnerb25df352009-03-04 05:16:45 +00001531 // We've seen a method with this name, see if we have already seen this type
1532 // signature.
1533 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1534 if (MatchTwoMethodDeclarations(Method, List->Method))
1535 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Chris Lattnerb25df352009-03-04 05:16:45 +00001537 // We have a new signature for an existing method - add it.
1538 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001539 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1540 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001541}
1542
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001543// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001544ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001545 SourceRange R,
1546 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001547 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1548 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001549 if (Pos == InstanceMethodPool.end()) {
1550 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001551 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1552 else
1553 return 0;
1554 }
1555
1556 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001557 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Steve Naroff037cda52008-09-30 14:38:43 +00001559 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001560 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1561 // This checks if the methods differ by size & alignment.
1562 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001563 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001564 }
1565 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001566 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001567 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001568 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001569 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001570 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001571 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001572 }
1573 return MethList.Method;
1574}
1575
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001576void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001577 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1578 = FactoryMethodPool.find(Method->getSelector());
1579 if (Pos == FactoryMethodPool.end()) {
1580 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1581 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1582 else
1583 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1584 ObjCMethodList())).first;
1585 }
1586
1587 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001588 if (!FirstMethod.Method) {
1589 // Haven't seen a method with this selector name yet - add it.
1590 FirstMethod.Method = Method;
1591 FirstMethod.Next = 0;
1592 } else {
1593 // We've seen a method with this name, now check the type signature(s).
1594 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001595
1596 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001597 Next = Next->Next)
1598 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Chris Lattner4d391482007-12-12 07:09:47 +00001600 if (!match) {
1601 // We have a new signature for an existing method - add it.
1602 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001603 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1604 ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001605 FirstMethod.Next = OMI;
1606 }
1607 }
1608}
1609
Mike Stump1eb44332009-09-09 15:08:12 +00001610ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001611 SourceRange R) {
1612 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1613 = FactoryMethodPool.find(Sel);
1614 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001615 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001616 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1617 else
1618 return 0;
1619 }
1620
1621 ObjCMethodList &MethList = Pos->second;
1622 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001624 if (MethList.Method && MethList.Next) {
1625 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1626 // This checks if the methods differ by size & alignment.
1627 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1628 issueWarning = true;
1629 }
1630 if (issueWarning && (MethList.Method && MethList.Next)) {
1631 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001632 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001633 << MethList.Method->getSourceRange();
1634 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001635 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001636 << Next->Method->getSourceRange();
1637 }
1638 return MethList.Method;
1639}
1640
Mike Stump1eb44332009-09-09 15:08:12 +00001641/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff0701bbb2009-01-08 17:28:14 +00001642/// have the property type and issue diagnostics if they don't.
1643/// Also synthesize a getter/setter method if none exist (and update the
1644/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1645/// methods is the "right" thing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001646void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001647 ObjCContainerDecl *CD) {
1648 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00001649
1650 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001651 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump1eb44332009-09-09 15:08:12 +00001652 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001653 property->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001655 if (SetterMethod) {
Fariborz Jahanian02deae82010-01-06 00:18:12 +00001656 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1657 property->getPropertyAttributes();
1658 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1659 Context.getCanonicalType(SetterMethod->getResultType()) !=
1660 Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001661 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001662 if (SetterMethod->param_size() != 1 ||
1663 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001664 Diag(property->getLocation(),
1665 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001666 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001667 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001668 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1669 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001670 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001671
1672 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001673 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001674 // FIXME: The synthesized property we set here is misleading. We almost always
1675 // synthesize these methods unless the user explicitly provided prototypes
1676 // (which is odd, but allowed). Sema should be typechecking that the
1677 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001678 if (!GetterMethod) {
1679 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001680 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001681 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001682 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1683 property->getLocation(), property->getGetterName(),
1684 property->getType(), CD, true, false, true,
1685 (property->getPropertyImplementation() ==
1686 ObjCPropertyDecl::Optional) ?
1687 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001688 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001689 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001690 } else
1691 // A user declared getter will be synthesize when @synthesize of
1692 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001693 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001694 property->setGetterMethodDecl(GetterMethod);
1695
1696 // Skip setter if property is read-only.
1697 if (!property->isReadOnly()) {
1698 // Find the default setter and if one not found, add one.
1699 if (!SetterMethod) {
1700 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001701 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001702 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001703 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1704 property->getLocation(),
1705 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001706 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001707 (property->getPropertyImplementation() ==
1708 ObjCPropertyDecl::Optional) ?
1709 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001710 ObjCMethodDecl::Required);
1711 // Invent the arguments for the setter. We don't bother making a
1712 // nice name for the argument.
1713 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001714 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001715 property->getIdentifier(),
1716 property->getType(),
John McCalla93c9342009-12-07 02:54:59 +00001717 /*TInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001718 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001719 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001720 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001721 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001722 } else
1723 // A user declared setter will be synthesize when @synthesize of
1724 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001725 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001726 property->setSetterMethodDecl(SetterMethod);
1727 }
Mike Stump1eb44332009-09-09 15:08:12 +00001728 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001729 // handle the following, which is supported by GCC (and part of the design).
1730 //
1731 // @interface Foo
1732 // @property double bar;
1733 // @end
1734 //
1735 // void thisIsUnfortunate() {
1736 // id foo;
1737 // double bar = [foo bar];
1738 // }
1739 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001740 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001741 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001742 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001743 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001744}
1745
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001746/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1747/// identical selector names in current and its super classes and issues
1748/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001749void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1750 ObjCMethodDecl *Method,
1751 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001752 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1753 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001755 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001756 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001757 SD->lookupMethod(Method->getSelector(), IsInstance);
1758 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001759 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001760 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001761 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001762 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1763 E = Method->param_end();
1764 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1765 for (; ParamI != E; ++ParamI, ++PrevI) {
1766 // Number of parameters are the same and is guaranteed by selector match.
1767 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1768 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1769 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1770 // If type of arguement of method in this class does not match its
1771 // respective argument type in the super class method, issue warning;
1772 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001773 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001774 << T1 << T2;
1775 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1776 return;
1777 }
1778 }
1779 ID = SD;
1780 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001781}
1782
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001783/// DiagnoseDuplicateIvars -
1784/// Check for duplicate ivars in the entire class at the start of
1785/// @implementation. This becomes necesssary because class extension can
1786/// add ivars to a class in random order which will not be known until
1787/// class's @implementation is seen.
1788void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1789 ObjCInterfaceDecl *SID) {
1790 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1791 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1792 ObjCIvarDecl* Ivar = (*IVI);
1793 if (Ivar->isInvalidDecl())
1794 continue;
1795 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1796 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1797 if (prevIvar) {
1798 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1799 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1800 Ivar->setInvalidDecl();
1801 }
1802 }
1803 }
1804}
1805
Steve Naroffa56f6162007-12-18 01:30:32 +00001806// Note: For class/category implemenations, allMethods/allProperties is
1807// always null.
Ted Kremenek782f2f52010-01-07 01:20:12 +00001808void Sema::ActOnAtEnd(SourceRange AtEnd,
1809 DeclPtrTy classDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001810 DeclPtrTy *allMethods, unsigned allNum,
1811 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001812 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001813 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001814
Steve Naroffa56f6162007-12-18 01:30:32 +00001815 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1816 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001817 // should be true.
1818 if (!ClassDecl)
1819 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001820
Mike Stump1eb44332009-09-09 15:08:12 +00001821 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001822 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1823 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001824 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001825
Ted Kremenek782f2f52010-01-07 01:20:12 +00001826 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1827 // FIXME: This is wrong. We shouldn't be pretending that there is
1828 // an '@end' in the declaration.
1829 SourceLocation L = ClassDecl->getLocation();
1830 AtEnd.setBegin(L);
1831 AtEnd.setEnd(L);
1832 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001833 }
1834
Steve Naroff0701bbb2009-01-08 17:28:14 +00001835 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001836
1837 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1838 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1839 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1840
Chris Lattner4d391482007-12-12 07:09:47 +00001841 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001842 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001843 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001844
1845 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001846 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001847 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001848 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001849 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001850 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001851 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001852 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001853 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001854 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001855 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001856 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001857 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001858 InsMap[Method->getSelector()] = Method;
1859 /// The following allows us to typecheck messages to "id".
1860 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001861 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001862 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001863 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001864 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001865 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001866 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001867 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001868 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001869 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001870 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001871 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001872 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001873 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001874 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001875 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001876 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001877 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001878 /// The following allows us to typecheck messages to "Class".
1879 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001880 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001881 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001882 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001883 }
1884 }
1885 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001886 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001887 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001888 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001889 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001890 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001891 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001892 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001893 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001894 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001895
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001896 // Compare protocol properties with those in category
1897 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian25760612010-02-15 21:55:26 +00001898 if (C->IsClassExtension())
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001899 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001900 }
Steve Naroff09c47192009-01-09 15:36:25 +00001901 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001902 if (CDecl->getIdentifier())
1903 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1904 // user-defined setter/getter. It also synthesizes setter/getter methods
1905 // and adds them to the DeclContext and global method pools.
1906 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1907 E = CDecl->prop_end();
1908 I != E; ++I)
1909 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001910 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001911 }
1912 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001913 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001914 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001915 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001916 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001917 if (LangOpts.ObjCNonFragileABI2)
1918 while (IDecl->getSuperClass()) {
1919 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1920 IDecl = IDecl->getSuperClass();
1921 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001922 }
Mike Stump1eb44332009-09-09 15:08:12 +00001923 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001924 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001925 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Chris Lattner4d391482007-12-12 07:09:47 +00001927 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001928 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001929 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001930 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001931 Categories; Categories = Categories->getNextClassCategory()) {
1932 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001933 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001934 break;
1935 }
1936 }
1937 }
1938 }
Chris Lattner682bf922009-03-29 16:50:03 +00001939 if (isInterfaceDeclKind) {
1940 // Reject invalid vardecls.
1941 for (unsigned i = 0; i != tuvNum; i++) {
1942 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1943 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1944 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001945 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001946 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001947 }
Chris Lattner682bf922009-03-29 16:50:03 +00001948 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001949 }
Chris Lattner4d391482007-12-12 07:09:47 +00001950}
1951
1952
1953/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1954/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001955static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001956CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1957 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1958 if (PQTVal & ObjCDeclSpec::DQ_In)
1959 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1960 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1961 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1962 if (PQTVal & ObjCDeclSpec::DQ_Out)
1963 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1964 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1965 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1966 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1967 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1968 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1969 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001970
1971 return ret;
1972}
1973
Chris Lattnerb28317a2009-03-28 19:18:32 +00001974Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001975 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001976 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001977 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001978 Selector Sel,
1979 // optional arguments. The number of types/arguments is obtained
1980 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001981 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001982 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001983 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1984 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001985 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001986
1987 // Make sure we can establish a context for the method.
1988 if (!ClassDecl) {
1989 Diag(MethodLoc, diag::error_missing_method_context);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +00001990 getLabelMap().clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001991 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001992 }
Chris Lattner4d391482007-12-12 07:09:47 +00001993 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001994
Steve Naroffccef3712009-02-20 22:59:16 +00001995 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001996 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Steve Naroffccef3712009-02-20 22:59:16 +00001998 // Methods cannot return interface types. All ObjC objects are
1999 // passed by reference.
2000 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002001 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2002 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002003 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00002004 }
2005 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002006 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00002007
2008 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002009 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00002010 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002011 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00002012 false,
Mike Stump1eb44332009-09-09 15:08:12 +00002013 MethodDeclKind == tok::objc_optional ?
2014 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002015 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Chris Lattner0ed844b2008-04-04 06:12:32 +00002017 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Chris Lattner7db638d2009-04-11 19:42:43 +00002019 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002020 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002021 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002022
Chris Lattnere294d3f2009-04-11 18:57:04 +00002023 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002024 ArgType = Context.getObjCIdType();
2025 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002026 } else {
John McCall58e46772009-10-23 21:48:59 +00002027 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002028 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002029 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002030 }
Mike Stump1eb44332009-09-09 15:08:12 +00002031
John McCall58e46772009-10-23 21:48:59 +00002032 ParmVarDecl* Param
2033 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
2034 ArgInfo[i].Name, ArgType, DI,
2035 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002036
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002037 if (ArgType->isObjCInterfaceType()) {
2038 Diag(ArgInfo[i].NameLoc,
2039 diag::err_object_cannot_be_passed_returned_by_value)
2040 << 1 << ArgType;
2041 Param->setInvalidDecl();
2042 }
Mike Stump1eb44332009-09-09 15:08:12 +00002043
Chris Lattner0ed844b2008-04-04 06:12:32 +00002044 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002045 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002046
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002047 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002048 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002049
Chris Lattner0ed844b2008-04-04 06:12:32 +00002050 Params.push_back(Param);
2051 }
2052
Jay Foadbeaaccd2009-05-21 09:52:38 +00002053 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002054 ObjCMethod->setObjCDeclQualifier(
2055 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
2056 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00002057
2058 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002059 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002060
John McCall54abf7d2009-11-04 02:18:39 +00002061 const ObjCMethodDecl *InterfaceMD = 0;
2062
Mike Stump1eb44332009-09-09 15:08:12 +00002063 // For implementations (which can be very "coarse grain"), we add the
2064 // method now. This allows the AST to implement lookup methods that work
2065 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00002066 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002067 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002068 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002069 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002070 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2071 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002072 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002073 PrevMethod = ImpDecl->getClassMethod(Sel);
2074 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002075 }
John McCall54abf7d2009-11-04 02:18:39 +00002076 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
2077 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002078 if (AttrList)
2079 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00002080 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002081 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002082 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002083 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
2084 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002085 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002086 PrevMethod = CatImpDecl->getClassMethod(Sel);
2087 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002088 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002089 if (AttrList)
2090 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00002091 }
2092 if (PrevMethod) {
2093 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002094 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002095 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002096 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002097 }
John McCall54abf7d2009-11-04 02:18:39 +00002098
2099 // If the interface declared this method, and it was deprecated there,
2100 // mark it deprecated here.
2101 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
2102 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
2103
Chris Lattnerb28317a2009-03-28 19:18:32 +00002104 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002105}
2106
Mike Stump1eb44332009-09-09 15:08:12 +00002107void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002108 SourceLocation Loc,
2109 unsigned &Attributes) {
2110 // FIXME: Improve the reported location.
2111
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002112 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002113 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002114 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
2115 ObjCDeclSpec::DQ_PR_assign |
2116 ObjCDeclSpec::DQ_PR_copy |
2117 ObjCDeclSpec::DQ_PR_retain))) {
2118 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
2119 "readwrite" :
2120 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
2121 "assign" :
2122 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
2123 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00002124
2125 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00002126 diag::err_objc_property_attr_mutually_exclusive :
2127 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00002128 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002129 }
2130
2131 // Check for copy or retain on non-object types.
2132 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00002133 !PropertyTy->isObjCObjectPointerType() &&
2134 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00002135 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002136 Diag(Loc, diag::err_objc_property_requires_object)
2137 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002138 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
2139 }
2140
2141 // Check for more than one of { assign, copy, retain }.
2142 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2143 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002144 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2145 << "assign" << "copy";
2146 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00002147 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002148 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002149 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2150 << "assign" << "retain";
2151 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002152 }
2153 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2154 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002155 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2156 << "copy" << "retain";
2157 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002158 }
2159 }
2160
2161 // Warn if user supplied no assignment attribute, property is
2162 // readwrite, and this is an object type.
2163 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
2164 ObjCDeclSpec::DQ_PR_retain)) &&
2165 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00002166 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002167 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00002168 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002169 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
2170
2171 // If non-gc code warn that this is likely inappropriate.
2172 if (getLangOptions().getGCMode() == LangOptions::NonGC)
2173 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002174
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002175 // FIXME: Implement warning dependent on NSCopying being
2176 // implemented. See also:
2177 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2178 // (please trim this list while you are at it).
2179 }
Mike Stump046efd92009-05-07 23:06:50 +00002180
2181 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2182 && getLangOptions().getGCMode() == LangOptions::GCOnly
2183 && PropertyTy->isBlockPointerType())
2184 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002185}
2186
Mike Stump1eb44332009-09-09 15:08:12 +00002187Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002188 FieldDeclarator &FD,
2189 ObjCDeclSpec &ODS,
2190 Selector GetterSel,
2191 Selector SetterSel,
2192 DeclPtrTy ClassCategory,
2193 bool *isOverridingProperty,
2194 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002195 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002196 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
2197 // default is readwrite!
2198 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00002199 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002200 // not retain or copy
2201 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00002202 (isReadWrite &&
2203 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002204 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
2205 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahaniandd69aae2009-12-16 18:03:30 +00002206 if (T->isReferenceType()) {
2207 Diag(AtLoc, diag::error_reference_property);
2208 return DeclPtrTy();
2209 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002210 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002211 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002212 // May modify Attributes.
2213 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002214 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanian25760612010-02-15 21:55:26 +00002215 if (CDecl->IsClassExtension()) {
2216 // Diagnose if this property is already in continuation class.
2217 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2218 assert(DC && "ClassDecl is not a DeclContext");
2219 DeclContext::lookup_result Found = DC->lookup(FD.D.getIdentifier());
2220 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2221 Diag(AtLoc, diag::err_duplicate_property);
2222 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2223 return DeclPtrTy();
2224 }
2225 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
2226 FD.D.getIdentifierLoc(),
2227 FD.D.getIdentifier(),
2228 AtLoc, T);
2229 DC->addDecl(PDecl);
2230
Mike Stump1eb44332009-09-09 15:08:12 +00002231 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002232 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002233 if ((CCPrimary = CDecl->getClassInterface())) {
2234 // Find the property in continuation class's primary class only.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002235 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002236 if (ObjCPropertyDecl *PIDecl =
2237 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002238 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00002239 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002240 unsigned PIkind = PIDecl->getPropertyAttributes();
2241 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002242 unsigned retainCopyNonatomic =
Fariborz Jahaniand669be52009-11-06 22:59:12 +00002243 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002244 ObjCPropertyDecl::OBJC_PR_copy |
2245 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002246 if ((Attributes & retainCopyNonatomic) !=
2247 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002248 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002249 Diag(PIDecl->getLocation(), diag::note_property_declare);
2250 }
Fariborz Jahanianb73e2812010-01-06 21:38:30 +00002251 DeclContext *DC = dyn_cast<DeclContext>(CCPrimary);
2252 assert(DC && "ClassDecl is not a DeclContext");
2253 DeclContext::lookup_result Found =
2254 DC->lookup(PIDecl->getDeclName());
2255 bool PropertyInPrimaryClass = false;
2256 for (; Found.first != Found.second; ++Found.first)
2257 if (isa<ObjCPropertyDecl>(*Found.first)) {
2258 PropertyInPrimaryClass = true;
2259 break;
2260 }
2261 if (!PropertyInPrimaryClass) {
2262 // Protocol is not in the primary class. Must build one for it.
2263 ObjCDeclSpec ProtocolPropertyODS;
2264 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind and
2265 // ObjCPropertyDecl::PropertyAttributeKind have identical values.
2266 // Should consolidate both into one enum type.
2267 ProtocolPropertyODS.setPropertyAttributes(
2268 (ObjCDeclSpec::ObjCPropertyAttributeKind)PIkind);
2269 DeclPtrTy ProtocolPtrTy =
2270 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
2271 PIDecl->getGetterName(),
2272 PIDecl->getSetterName(),
2273 DeclPtrTy::make(CCPrimary), isOverridingProperty,
2274 MethodImplKind);
2275 PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>();
2276 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002277 PIDecl->makeitReadWriteAttribute();
2278 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2279 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2280 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2281 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2282 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002283 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002284 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002285 << CCPrimary->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002286 Diag(PIDecl->getLocation(), diag::note_property_declare);
2287 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002288 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002289 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00002290 // class's list.
2291 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002292 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002293 }
Fariborz Jahanian25760612010-02-15 21:55:26 +00002294
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002295 // No matching property found in the primary class. Just fall thru
2296 // and add property to continuation class's primary class.
2297 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002298 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00002299 Diag(CDecl->getLocation(), diag::err_continuation_class);
2300 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002301 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002302 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002303 }
Mike Stump1eb44332009-09-09 15:08:12 +00002304
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002305 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00002306 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002307 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2308 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2309 if (T->isObjCObjectPointerType()) {
2310 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00002311 if (const ObjCInterfaceType *OIT =
John McCall183700f2009-09-21 23:43:11 +00002312 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002313 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002314 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002315 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002316 LookupProtocol(&Context.Idents.get("NSCopying")))
2317 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00002318 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002319 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002320 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002321 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00002322 if (T->isObjCInterfaceType())
2323 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002324
Steve Naroff93983f82009-01-11 12:47:58 +00002325 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2326 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002327 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00002328 FD.D.getIdentifierLoc(),
Fariborz Jahaniand0502402010-01-21 17:36:00 +00002329 FD.D.getIdentifier(),
2330 AtLoc, T);
Fariborz Jahanian6dd30fc2009-12-17 00:49:09 +00002331 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
2332 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2333 Diag(PDecl->getLocation(), diag::err_duplicate_property);
2334 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2335 PDecl->setInvalidDecl();
2336 }
2337 else
2338 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002339
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002340 if (T->isArrayType() || T->isFunctionType()) {
2341 Diag(AtLoc, diag::err_property_type) << T;
2342 PDecl->setInvalidDecl();
2343 }
Mike Stump1eb44332009-09-09 15:08:12 +00002344
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002345 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00002346
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00002347 // Regardless of setter/getter attribute, we save the default getter/setter
2348 // selector names in anticipation of declaration of setter/getter methods.
2349 PDecl->setGetterName(GetterSel);
2350 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00002351
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002352 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002353 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00002354
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002355 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002356 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00002357
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002358 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002359 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00002360
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002361 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002362 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00002363
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002364 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002365 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00002366
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002367 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002368 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00002369
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002370 if (isAssign)
2371 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00002372
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002373 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002374 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00002375
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00002376 if (MethodImplKind == tok::objc_required)
2377 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2378 else if (MethodImplKind == tok::objc_optional)
2379 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002380 // A case of continuation class adding a new property in the class. This
2381 // is not what it was meant for. However, gcc supports it and so should we.
2382 // Make sure setter/getters are declared here.
2383 if (CCPrimary)
2384 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Chris Lattnerb28317a2009-03-28 19:18:32 +00002386 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002387}
2388
Fariborz Jahanian77e2dde2010-02-09 21:49:50 +00002389ObjCIvarDecl*
2390Sema::SynthesizeNewPropertyIvar(ObjCInterfaceDecl *IDecl,
2391 IdentifierInfo *NameII) {
2392 ObjCIvarDecl *Ivar = 0;
2393 ObjCPropertyDecl *Prop = LookupPropertyDecl(IDecl, NameII);
2394 if (Prop && !Prop->isInvalidDecl()) {
2395 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
2396 QualType PropType = Context.getCanonicalType(Prop->getType());
2397 assert(EnclosingContext &&
2398 "null DeclContext for synthesized ivar - SynthesizeNewPropertyIvar");
2399 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext,
2400 Prop->getLocation(),
2401 NameII, PropType, /*Dinfo=*/0,
2402 ObjCIvarDecl::Public,
2403 (Expr *)0);
2404 Ivar->setLexicalDeclContext(IDecl);
2405 IDecl->addDecl(Ivar);
2406 Prop->setPropertyIvarDecl(Ivar);
2407 }
2408 return Ivar;
2409}
2410
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002411/// ActOnPropertyImplDecl - This routine performs semantic checks and
2412/// builds the AST node for a property implementation declaration; declared
2413/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002414///
Mike Stump1eb44332009-09-09 15:08:12 +00002415Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002416 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002417 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002418 DeclPtrTy ClassCatImpDecl,
2419 IdentifierInfo *PropertyId,
2420 IdentifierInfo *PropertyIvar) {
2421 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002422 // Make sure we have a context for the property implementation declaration.
2423 if (!ClassImpDecl) {
2424 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002425 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002426 }
2427 ObjCPropertyDecl *property = 0;
2428 ObjCInterfaceDecl* IDecl = 0;
2429 // Find the class or category class where this property must have
2430 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002431 ObjCImplementationDecl *IC = 0;
2432 ObjCCategoryImplDecl* CatImplClass = 0;
2433 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002434 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002435 // We always synthesize an interface for an implementation
2436 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002437 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002438 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002439
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002440 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002441 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002442 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002443 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002444 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002445 }
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002446 if (const ObjCCategoryDecl *CD =
2447 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002448 if (!CD->IsClassExtension()) {
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002449 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002450 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002451 return DeclPtrTy();
2452 }
2453 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002454 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002455 if (Synthesize) {
2456 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002457 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002458 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002459 IDecl = CatImplClass->getClassInterface();
2460 if (!IDecl) {
2461 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002462 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002463 }
Mike Stump1eb44332009-09-09 15:08:12 +00002464 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002465 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002466
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002467 // If category for this implementation not found, it is an error which
2468 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002469 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002470 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002471 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002472 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002473 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002474 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002475 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002476 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002477 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002478 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002479 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002480 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002481 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002482 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002483 // Check that we have a valid, previously declared ivar for @synthesize
2484 if (Synthesize) {
2485 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002486 if (!PropertyIvar)
2487 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002488 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002489 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002490 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002491 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002492 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002493 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002494 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002495 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002496 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2497 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002498 ObjCIvarDecl::Public,
2499 (Expr *)0);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00002500 IDecl->makeDeclVisibleInContext(Ivar, false);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002501 property->setPropertyIvarDecl(Ivar);
2502 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002503 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002504 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002505 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002506 } else if (getLangOptions().ObjCNonFragileABI &&
2507 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002508 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002509 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002510 << ClassDeclared->getDeclName();
2511 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2512 << Ivar << Ivar->getNameAsCString();
2513 // Note! I deliberately want it to fall thru so more errors are caught.
2514 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002515 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002516
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002517 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002518 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002519 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002520 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002521 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002522 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002523 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002524 }
Mike Stump1eb44332009-09-09 15:08:12 +00002525
Chris Lattnerb28317a2009-03-28 19:18:32 +00002526 // FIXME! Rules for properties are somewhat different that those
2527 // for assignments. Use a new routine to consolidate all cases;
2528 // specifically for property redeclarations as well as for ivars.
2529 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2530 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002531 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002532 lhsType->isArithmeticType()) {
2533 Diag(PropertyLoc, diag::error_property_ivar_type)
2534 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002535 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002536 }
2537 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002538 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2539 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002540 Diag(PropertyLoc, diag::error_weak_property)
2541 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002542 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002543 }
Mike Stump1eb44332009-09-09 15:08:12 +00002544 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002545 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2546 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002547 Diag(PropertyLoc, diag::error_strong_property)
2548 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002549 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002550 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002551 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002552 } else if (PropertyIvar)
2553 // @dynamic
2554 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002555 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002556 ObjCPropertyImplDecl *PIDecl =
2557 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2558 property,
2559 (Synthesize ?
2560 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002561 : ObjCPropertyImplDecl::Dynamic),
2562 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002563 if (IC) {
2564 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002565 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002566 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002567 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2568 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002569 << PropertyIvar;
2570 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2571 }
Mike Stump1eb44332009-09-09 15:08:12 +00002572
2573 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002574 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002575 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2576 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002577 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002578 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002579 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002580 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002581 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002582 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002583 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002584 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2585 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002586 << PropertyIvar;
2587 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2588 }
Mike Stump1eb44332009-09-09 15:08:12 +00002589
2590 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002591 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002592 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2593 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002594 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002595 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002596 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002597 }
Mike Stump1eb44332009-09-09 15:08:12 +00002598
Chris Lattnerb28317a2009-03-28 19:18:32 +00002599 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002600}
Anders Carlsson15281452008-11-04 16:57:32 +00002601
Chris Lattnercc98eac2008-12-17 07:13:27 +00002602bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002603 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002604 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002605
Anders Carlsson15281452008-11-04 16:57:32 +00002606 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2607 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Anders Carlsson15281452008-11-04 16:57:32 +00002609 return true;
2610}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002611
Chris Lattnercc98eac2008-12-17 07:13:27 +00002612/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2613/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002614void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002615 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002616 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002617 // Check that ClassName is a valid class
2618 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2619 if (!Class) {
2620 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2621 return;
2622 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002623 if (LangOpts.ObjCNonFragileABI) {
2624 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2625 return;
2626 }
Mike Stump1eb44332009-09-09 15:08:12 +00002627
Chris Lattnercc98eac2008-12-17 07:13:27 +00002628 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002629 llvm::SmallVector<FieldDecl*, 32> RecFields;
2630 Context.CollectObjCIvars(Class, RecFields);
2631 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2632 for (unsigned i = 0; i < RecFields.size(); i++) {
2633 FieldDecl* ID = RecFields[i];
2634 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2635 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2636 ID->getIdentifier(), ID->getType(),
2637 ID->getBitWidth());
2638 Decls.push_back(Sema::DeclPtrTy::make(FD));
2639 }
Mike Stump1eb44332009-09-09 15:08:12 +00002640
Chris Lattnercc98eac2008-12-17 07:13:27 +00002641 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002642 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002643 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002644 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002645 if (getLangOptions().CPlusPlus)
2646 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002647 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002648 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002649 }
2650}
2651