blob: 5b52ba9c8c4ece329bf3caf139b626881a3afecd [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 Gregorf0aaf7a2009-04-24 21:10:55 +000015#include "clang/Sema/ExternalSemaSource.h"
Steve Naroffca331292009-03-03 14:49:36 +000016#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000020using namespace clang;
21
Fariborz Jahanianc001e892009-05-08 20:20:55 +000022bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
23 ObjCMethodDecl *GetterMethod,
24 SourceLocation Loc) {
25 if (GetterMethod &&
26 GetterMethod->getResultType() != property->getType()) {
27 AssignConvertType result = Incompatible;
Steve Narofff4954562009-07-16 15:41:00 +000028 if (property->getType()->isObjCObjectPointerType())
Fariborz Jahanian7aaa4092009-05-08 21:10:00 +000029 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
Fariborz Jahanianc001e892009-05-08 20:20:55 +000030 if (result != Compatible) {
Mike Stump1eb44332009-09-09 15:08:12 +000031 Diag(Loc, diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianc001e892009-05-08 20:20:55 +000032 << property->getDeclName()
33 << GetterMethod->getSelector();
34 Diag(GetterMethod->getLocation(), diag::note_declared_at);
35 return true;
36 }
37 }
38 return false;
39}
40
Steve Naroffebf64432009-02-28 16:59:13 +000041/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000042/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000043void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000044 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000045 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump1eb44332009-09-09 15:08:12 +000046
Steve Naroff394f3f42008-07-25 17:57:26 +000047 // If we don't have a valid method decl, simply return.
48 if (!MDecl)
49 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000050
Chris Lattner38c5ebd2009-04-19 05:21:20 +000051 CurFunctionNeedsScopeChecking = false;
52
Steve Naroffa56f6162007-12-18 01:30:32 +000053 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000054 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000055 AddInstanceMethodToGlobalPool(MDecl);
56 else
57 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000058
Chris Lattner4d391482007-12-12 07:09:47 +000059 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000060 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000061
62 // 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,
Chris Lattner7caeabd2008-07-21 22:17:28 +000083 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000084 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000085
Chris Lattner4d391482007-12-12 07:09:47 +000086 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000087 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000088 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000089 // Maybe we will complain about the shadowed template parameter.
90 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
91 // Just pretend that we didn't see the previous declaration.
92 PrevDecl = 0;
93 }
94
Ted Kremeneka526c5c2008-01-07 19:49:32 +000095 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000096 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000097 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000098 }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000100 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000101 if (IDecl) {
102 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000103 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000104 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000105 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000106 Diag(IDecl->getLocation(), diag::note_previous_definition);
107
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000108 // Return the previous class interface.
109 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000110 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000111 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000112 IDecl->setLocation(AtInterfaceLoc);
113 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000114 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000115 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000116 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000117 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000118 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000119 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Steve Naroffa7503a72009-04-23 15:15:40 +0000121 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Chris Lattner4d391482007-12-12 07:09:47 +0000124 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000125 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000126 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000127 if (PrevDecl == IDecl) {
128 Diag(SuperLoc, diag::err_recursive_superclass)
129 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
130 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000131 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000132 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000133 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000134
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000135 // Diagnose classes that inherit from deprecated classes.
136 if (SuperClassDecl)
137 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000139 if (PrevDecl && SuperClassDecl == 0) {
140 // The previous declaration was not a class decl. Check if we have a
141 // typedef. If we do, get the underlying class type.
142 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
143 QualType T = TDecl->getUnderlyingType();
144 if (T->isObjCInterfaceType()) {
145 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
146 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
147 }
148 }
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000150 // This handles the following case:
151 //
152 // typedef int SuperClass;
153 // @interface MyClass : SuperClass {} @end
154 //
155 if (!SuperClassDecl) {
156 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
157 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000158 }
159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000161 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
162 if (!SuperClassDecl)
163 Diag(SuperLoc, diag::err_undef_superclass)
164 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
165 else if (SuperClassDecl->isForwardDecl())
166 Diag(SuperLoc, diag::err_undef_superclass)
167 << SuperClassDecl->getDeclName() << ClassName
168 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000169 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000170 IDecl->setSuperClass(SuperClassDecl);
171 IDecl->setSuperClassLoc(SuperLoc);
172 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000173 }
Chris Lattner4d391482007-12-12 07:09:47 +0000174 } else { // we have a root class.
175 IDecl->setLocEnd(ClassLoc);
176 }
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000178 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000179 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000180 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
181 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000182 IDecl->setLocEnd(EndProtoLoc);
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Anders Carlsson15281452008-11-04 16:57:32 +0000185 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000186 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000187}
188
189/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000190/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000191Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000192 IdentifierInfo *AliasName,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000193 SourceLocation AliasLocation,
194 IdentifierInfo *ClassName,
195 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000196 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000197 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000198 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000199 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000200 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000201 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000202 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000203 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000204 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000205 }
206 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000207 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000208 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
209 QualType T = TDecl->getUnderlyingType();
210 if (T->isObjCInterfaceType()) {
211 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
212 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000213 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000214 }
215 }
216 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000217 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
218 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000219 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000220 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000221 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000222 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000223 }
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000225 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000226 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000227 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Anders Carlsson15281452008-11-04 16:57:32 +0000229 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000230 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000231
Chris Lattnerb28317a2009-03-28 19:18:32 +0000232 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000233}
234
Steve Naroff61d68522009-03-05 15:22:01 +0000235void Sema::CheckForwardProtocolDeclarationForCircularDependency(
236 IdentifierInfo *PName,
237 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000238 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000239 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
240 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Douglas Gregor6e378de2009-04-23 23:18:26 +0000242 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000243 if (PDecl->getIdentifier() == PName) {
244 Diag(Ploc, diag::err_protocol_has_circular_dependency);
245 Diag(PrevLoc, diag::note_previous_definition);
246 }
Mike Stump1eb44332009-09-09 15:08:12 +0000247 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000248 PDecl->getLocation(), PDecl->getReferencedProtocols());
249 }
250 }
251}
252
Chris Lattnerb28317a2009-03-28 19:18:32 +0000253Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000254Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
255 IdentifierInfo *ProtocolName,
256 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000257 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000258 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000259 SourceLocation EndProtoLoc,
260 AttributeList *AttrList) {
261 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000262 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000263 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000264 if (PDecl) {
265 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000266 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000267 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000268 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000269 // Just return the protocol we already had.
270 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000271 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000272 }
Steve Naroff61d68522009-03-05 15:22:01 +0000273 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000274 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000275 CheckForwardProtocolDeclarationForCircularDependency(
276 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
277 PList.Destroy(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Steve Narofff11b5082008-08-13 16:39:22 +0000279 // Make sure the cached decl gets a valid start location.
280 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000281 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000282 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000283 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000284 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000285 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000286 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000287 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000288 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000289 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000290 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000291 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000292 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000293 PDecl->setLocEnd(EndProtoLoc);
294 }
Mike Stump1eb44332009-09-09 15:08:12 +0000295
296 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000297 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000298}
299
300/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000301/// issues an error if they are not declared. It returns list of
302/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000303void
Chris Lattnere13b9592008-07-26 04:03:38 +0000304Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000305 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000306 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000307 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000308 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000309 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000310 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000311 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000312 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000313 continue;
314 }
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000316 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000317
318 // If this is a forward declaration and we are supposed to warn in this
319 // case, do it.
320 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000321 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000322 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000323 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000324 }
325}
326
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000327/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000328/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000329///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000330void
Mike Stump1eb44332009-09-09 15:08:12 +0000331Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000332 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000333 const IdentifierInfo *inheritedName) {
Mike Stump1eb44332009-09-09 15:08:12 +0000334 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000335 Property->getPropertyAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000336 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000337 SuperProperty->getPropertyAttributes();
338 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
339 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000340 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000341 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000342 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
343 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000344 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000345 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000346 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
347 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000348 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000349 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000351 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
352 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000353 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000354 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000355 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000356 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump1eb44332009-09-09 15:08:12 +0000357 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000358 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000359 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000360 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000361
Mike Stump1eb44332009-09-09 15:08:12 +0000362 QualType LHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000363 Context.getCanonicalType(SuperProperty->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000364 QualType RHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000365 Context.getCanonicalType(Property->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Steve Naroff15edf0d2009-03-03 15:43:24 +0000367 if (!Context.typesAreCompatible(LHSType, RHSType)) {
368 // FIXME: Incorporate this test with typesAreCompatible.
369 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff4084c302009-07-23 01:01:38 +0000370 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff15edf0d2009-03-03 15:43:24 +0000371 return;
372 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
373 << Property->getType() << SuperProperty->getType() << inheritedName;
374 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000375}
376
377/// ComparePropertiesInBaseAndSuper - This routine compares property
378/// declarations in base and its super class, if any, and issues
379/// diagnostics in a variety of inconsistant situations.
380///
Chris Lattner70f19542009-02-16 21:26:43 +0000381void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000382 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
383 if (!SDecl)
384 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000385 // FIXME: O(N^2)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000386 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
387 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000388 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000389 // Does property in super class has declaration in current class?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000390 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
391 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000392 ObjCPropertyDecl *PDecl = (*I);
393 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump1eb44332009-09-09 15:08:12 +0000394 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000395 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000396 }
397 }
398}
399
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000400/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
401/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000402/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000403void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000404Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000405 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000406 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
407 if (!IDecl) {
408 // Category
409 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
410 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000411 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
412 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000413 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000414 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000415 // Is this property already in category's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000416 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000417 if ((*CP)->getIdentifier() == Pr->getIdentifier())
418 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000419 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000420 // Property protocol already exist in class. Diagnose any mismatch.
421 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
422 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000423 return;
424 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000425 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
426 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000427 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000428 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000429 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000430 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000431 if ((*CP)->getIdentifier() == Pr->getIdentifier())
432 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000433 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000434 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000435 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000436 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000437}
438
439/// MergeProtocolPropertiesIntoClass - This routine merges properties
440/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000441/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000442///
Chris Lattner70f19542009-02-16 21:26:43 +0000443void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000444 DeclPtrTy MergeItsProtocols) {
445 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000446 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
447
448 if (!IDecl) {
449 // Category
450 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
451 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
452 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
453 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
454 E = MDecl->protocol_end(); P != E; ++P)
455 // Merge properties of category (*P) into IDECL's
456 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000458 // Go thru the list of protocols for this category and recursively merge
459 // their properties into this class as well.
460 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
461 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000462 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000463 } else {
464 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
465 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
466 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000467 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000468 }
469 return;
470 }
471
Chris Lattnerb752f282008-07-21 07:06:49 +0000472 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000473 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
474 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000475 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000476 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000478 // Go thru the list of protocols for this class and recursively merge
479 // their properties into this class as well.
480 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
481 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000482 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000483 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000484 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
485 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
486 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000487 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000488 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000489}
490
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000491/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000492/// a class method in its extension.
493///
Mike Stump1eb44332009-09-09 15:08:12 +0000494void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000495 ObjCInterfaceDecl *ID) {
496 if (!ID)
497 return; // Possibly due to previous error
498
499 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000500 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
501 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000502 ObjCMethodDecl *MD = *i;
503 MethodMap[MD->getSelector()] = MD;
504 }
505
506 if (MethodMap.empty())
507 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000508 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
509 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000510 ObjCMethodDecl *Method = *i;
511 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
512 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
513 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
514 << Method->getDeclName();
515 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
516 }
517 }
518}
519
Chris Lattner58fe03b2009-04-12 08:43:13 +0000520/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000521Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000522Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000523 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000524 unsigned NumElts,
525 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000526 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Chris Lattner4d391482007-12-12 07:09:47 +0000528 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000529 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000530 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000531 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000532 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000533 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000534 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000535 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000536 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000537 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000538 Protocols.push_back(PDecl);
539 }
Mike Stump1eb44332009-09-09 15:08:12 +0000540
541 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000542 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000543 &Protocols[0], Protocols.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000544 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000545 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000546 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000547}
548
Chris Lattnerb28317a2009-03-28 19:18:32 +0000549Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000550ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
551 IdentifierInfo *ClassName, SourceLocation ClassLoc,
552 IdentifierInfo *CategoryName,
553 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000554 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000555 unsigned NumProtoRefs,
556 SourceLocation EndProtoLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000557 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000558 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
559 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000560 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000561
562 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000563 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000564 if (!IDecl || IDecl->isForwardDecl()) {
565 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000566 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000567 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000568 }
Chris Lattner4d391482007-12-12 07:09:47 +0000569
Chris Lattner70f19542009-02-16 21:26:43 +0000570 CDecl->setClassInterface(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Chris Lattner16b34b42009-02-16 21:30:01 +0000572 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000573 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000574
575 /// Check for duplicate interface declaration for this category
576 ObjCCategoryDecl *CDeclChain;
577 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
578 CDeclChain = CDeclChain->getNextClassCategory()) {
579 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
580 Diag(CategoryLoc, diag::warn_dup_category_def)
581 << ClassName << CategoryName;
582 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
583 break;
584 }
585 }
586 if (!CDeclChain)
587 CDecl->insertNextClassCategory();
588
Chris Lattner4d391482007-12-12 07:09:47 +0000589 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000590 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000591 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000592 }
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Anders Carlsson15281452008-11-04 16:57:32 +0000594 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000595 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000596}
597
598/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000599/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000600/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000601Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000602 SourceLocation AtCatImplLoc,
603 IdentifierInfo *ClassName, SourceLocation ClassLoc,
604 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000605 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000606 ObjCCategoryDecl *CatIDecl = 0;
607 if (IDecl) {
608 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
609 if (!CatIDecl) {
610 // Category @implementation with no corresponding @interface.
611 // Create and install one.
612 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
613 CatName);
614 CatIDecl->setClassInterface(IDecl);
615 CatIDecl->insertNextClassCategory();
616 }
617 }
618
Mike Stump1eb44332009-09-09 15:08:12 +0000619 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000620 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
621 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000622 /// Check that class of this category is already completely declared.
623 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000624 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000625
Douglas Gregord0434102009-01-09 00:49:46 +0000626 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000627 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000628
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000629 /// Check that CatName, category name, is not used in another implementation.
630 if (CatIDecl) {
631 if (CatIDecl->getImplementation()) {
632 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
633 << CatName;
634 Diag(CatIDecl->getImplementation()->getLocation(),
635 diag::note_previous_definition);
636 } else
637 CatIDecl->setImplementation(CDecl);
638 }
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Anders Carlsson15281452008-11-04 16:57:32 +0000640 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000641 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000642}
643
Chris Lattnerb28317a2009-03-28 19:18:32 +0000644Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000645 SourceLocation AtClassImplLoc,
646 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000647 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000648 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000649 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000650 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000651 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000652 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000653 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000654 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000655 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000656 // Is there an interface declaration of this class; if not, warn!
Mike Stump1eb44332009-09-09 15:08:12 +0000657 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000658 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000659 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000660 IDecl = 0;
661 }
Chris Lattner4d391482007-12-12 07:09:47 +0000662 }
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Chris Lattner4d391482007-12-12 07:09:47 +0000664 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000665 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000666 if (SuperClassname) {
667 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000668 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000669 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000670 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
671 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000672 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000673 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000674 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000675 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000676 Diag(SuperClassLoc, diag::err_undef_superclass)
677 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000678 else if (IDecl && IDecl->getSuperClass() != SDecl) {
679 // This implementation and its interface do not have the same
680 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000681 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000682 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000683 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000684 }
685 }
686 }
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Chris Lattner4d391482007-12-12 07:09:47 +0000688 if (!IDecl) {
689 // Legacy case of @implementation with no corresponding @interface.
690 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000691
Mike Stump390b4cc2009-05-16 07:39:55 +0000692 // FIXME: Do we support attributes on the @implementation? If so we should
693 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000694 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000695 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000696 IDecl->setSuperClass(SDecl);
697 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000698
699 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000700 } else {
701 // Mark the interface as being completed, even if it was just as
702 // @class ....;
703 // declaration; the user cannot reopen it.
704 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
707 ObjCImplementationDecl* IMPDecl =
708 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000709 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Anders Carlsson15281452008-11-04 16:57:32 +0000711 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000712 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Chris Lattner4d391482007-12-12 07:09:47 +0000714 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000715 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000716 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000717 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000718 Diag(IDecl->getImplementation()->getLocation(),
719 diag::note_previous_definition);
720 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000721 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000722 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000723 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000724 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000725}
726
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000727void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
728 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000729 SourceLocation RBrace) {
730 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000731 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000732 if (!IDecl)
733 return;
734 /// Check case of non-existing @interface decl.
735 /// (legacy objective-c @implementation decl without an @interface decl).
736 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000737 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000738 IDecl->setIVarList(ivars, numIvars, Context);
739 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000740 return;
741 }
742 // If implementation has empty ivar list, just return.
743 if (numIvars == 0)
744 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Chris Lattner4d391482007-12-12 07:09:47 +0000746 assert(ivars && "missing @implementation ivars");
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Chris Lattner4d391482007-12-12 07:09:47 +0000748 // Check interface's Ivar list against those in the implementation.
749 // names and types must match.
750 //
Chris Lattner4d391482007-12-12 07:09:47 +0000751 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000752 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000753 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
754 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000755 ObjCIvarDecl* ImplIvar = ivars[j++];
756 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000757 assert (ImplIvar && "missing implementation ivar");
758 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Steve Naroffca331292009-03-03 14:49:36 +0000760 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000761 if (Context.getCanonicalType(ImplIvar->getType()) !=
762 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000763 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000764 << ImplIvar->getIdentifier()
765 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000766 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000767 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
768 Expr *ImplBitWidth = ImplIvar->getBitWidth();
769 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000770 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
771 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000772 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
773 << ImplIvar->getIdentifier();
774 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776 }
Steve Naroffca331292009-03-03 14:49:36 +0000777 // Make sure the names are identical.
778 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000779 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000780 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000781 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000782 }
783 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Chris Lattner609e4c72007-12-12 18:11:49 +0000786 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000787 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000788 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000789 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000790}
791
Steve Naroff3c2eb662008-02-10 21:38:56 +0000792void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
793 bool &IncompleteImpl) {
794 if (!IncompleteImpl) {
795 Diag(ImpLoc, diag::warn_incomplete_impl);
796 IncompleteImpl = true;
797 }
Chris Lattner08631c52008-11-23 21:45:46 +0000798 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000799}
800
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000801void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
802 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000803 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000804 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000805 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
806 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000807 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000808 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
809 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000810 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
811 }
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Chris Lattner3aff9192009-04-11 19:58:42 +0000813 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
814 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
815 IM != EM; ++IM, ++IF) {
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000816 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000817 Context.QualifiedIdConformsQualifiedId((*IF)->getType(),
Steve Naroff4084c302009-07-23 01:01:38 +0000818 (*IM)->getType()))
Chris Lattner3aff9192009-04-11 19:58:42 +0000819 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000820
821 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000822 << ImpMethodDecl->getDeclName() << (*IF)->getType()
823 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000824 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000825 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000826}
827
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000828/// isPropertyReadonly - Return true if property is readonly, by searching
829/// for the property in the class and in its categories and implementations
830///
831bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000832 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000833 // by far the most common case.
834 if (!PDecl->isReadOnly())
835 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000836 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000837 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000838 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000839 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000841 // Main class has the property as 'readonly'. Must search
Mike Stump1eb44332009-09-09 15:08:12 +0000842 // through the category list to see if the property's
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000843 // attribute has been over-ridden to 'readwrite'.
844 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
845 Category; Category = Category->getNextClassCategory()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000846 // Even if property is ready only, if a category has a user defined setter,
847 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000848 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000849 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000850 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000851 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000852 if (P && !P->isReadOnly())
853 return false;
854 }
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000856 // Also, check for definition of a setter method in the implementation if
857 // all else failed.
858 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000859 if (ObjCImplementationDecl *IMD =
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000860 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000861 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000862 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000863 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000864 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000865 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000866 return false;
867 }
868 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000869 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000870 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000871 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000872 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000873 // If all fails, look at the super class.
874 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
875 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000876 return true;
877}
878
Mike Stump390b4cc2009-05-16 07:39:55 +0000879/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
880/// improve the efficiency of selector lookups and type checking by associating
881/// with each protocol / interface / category the flattened instance tables. If
882/// we used an immutable set to keep the table then it wouldn't add significant
883/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000884
Steve Naroffefe7f362008-02-08 22:06:17 +0000885/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000886/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000887void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
888 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000889 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000890 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000891 const llvm::DenseSet<Selector> &ClsMap,
892 ObjCInterfaceDecl *IDecl) {
893 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000894 ObjCInterfaceDecl *NSIDecl = 0;
895 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000896 // check to see if class implements forwardInvocation method and objects
897 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000898 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000899 // Under such conditions, which means that every method possible is
900 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000901 // found" warnings.
902 // FIXME: Use a general GetUnarySelector method for this.
903 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
904 Selector fISelector = Context.Selectors.getSelector(1, &II);
905 if (InsMap.count(fISelector))
906 // Is IDecl derived from 'NSProxy'? If so, no instance methods
907 // need be implemented in the implementation.
908 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
909 }
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000911 // If a method lookup fails locally we still need to look and see if
912 // the method was implemented by a base class or an inherited
913 // protocol. This lookup is slow, but occurs rarely in correct code
914 // and otherwise would terminate in a warning.
915
Chris Lattner4d391482007-12-12 07:09:47 +0000916 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000917 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000918 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000919 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000920 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000921 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000922 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000923 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000924 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000925 // Ugly, but necessary. Method declared in protcol might have
926 // have been synthesized due to a property declared in the class which
927 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000928 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000929 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000930 if (!MethodInClass || !MethodInClass->isSynthesized())
931 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
932 }
933 }
Chris Lattner4d391482007-12-12 07:09:47 +0000934 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000935 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000936 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000937 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000938 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000939 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
940 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000941 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000942 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000943 }
Chris Lattner780f3292008-07-21 21:32:27 +0000944 // Check on this protocols's referenced protocols, recursively.
945 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
946 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000947 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000948}
949
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000950/// MatchAllMethodDeclarations - Check methods declaraed in interface or
951/// or protocol against those declared in their implementations.
952///
953void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
954 const llvm::DenseSet<Selector> &ClsMap,
955 llvm::DenseSet<Selector> &InsMapSeen,
956 llvm::DenseSet<Selector> &ClsMapSeen,
957 ObjCImplDecl* IMPDecl,
958 ObjCContainerDecl* CDecl,
959 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +0000960 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000961 // Check and see if instance methods in class interface have been
962 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000963 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
964 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000965 if (InsMapSeen.count((*I)->getSelector()))
966 continue;
967 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000968 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000969 !InsMap.count((*I)->getSelector())) {
970 if (ImmediateClass)
971 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
972 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000973 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000974 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000975 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000976 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000977 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000978 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000979 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
980 // ImpMethodDecl may be null as in a @dynamic property.
981 if (ImpMethodDecl)
982 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
983 }
984 }
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000986 // Check and see if class methods in class interface have been
987 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +0000988 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000989 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000990 if (ClsMapSeen.count((*I)->getSelector()))
991 continue;
992 ClsMapSeen.insert((*I)->getSelector());
993 if (!ClsMap.count((*I)->getSelector())) {
994 if (ImmediateClass)
995 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000996 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000997 ObjCMethodDecl *ImpMethodDecl =
998 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000999 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001000 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001001 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1002 }
1003 }
1004 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1005 // Check for any implementation of a methods declared in protocol.
1006 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1007 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001008 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1009 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001010 (*PI), IncompleteImpl, false);
1011 if (I->getSuperClass())
1012 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001013 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001014 I->getSuperClass(), IncompleteImpl, false);
1015 }
1016}
1017
Mike Stump1eb44332009-09-09 15:08:12 +00001018void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1019 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001020 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001021 llvm::DenseSet<Selector> InsMap;
1022 // Check and see if instance methods in class interface have been
1023 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001024 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001025 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001026 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001028 // Check and see if properties declared in the interface have either 1)
1029 // an implementation or 2) there is a @synthesize/@dynamic implementation
1030 // of the property in the @implementation.
1031 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001032 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1033 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001034 ObjCPropertyDecl *Prop = (*P);
1035 if (Prop->isInvalidDecl())
1036 continue;
1037 ObjCPropertyImplDecl *PI = 0;
1038 // Is there a matching propery synthesize/dynamic?
Mike Stump1eb44332009-09-09 15:08:12 +00001039 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001040 I = IMPDecl->propimpl_begin(),
1041 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001042 if ((*I)->getPropertyDecl() == Prop) {
1043 PI = (*I);
1044 break;
1045 }
1046 if (PI)
1047 continue;
1048 if (!InsMap.count(Prop->getGetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001049 Diag(Prop->getLocation(),
1050 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001051 << Prop->getDeclName() << Prop->getGetterName();
1052 Diag(IMPDecl->getLocation(),
1053 diag::note_property_impl_required);
1054 }
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001056 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001057 Diag(Prop->getLocation(),
1058 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001059 << Prop->getDeclName() << Prop->getSetterName();
1060 Diag(IMPDecl->getLocation(),
1061 diag::note_property_impl_required);
1062 }
1063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Chris Lattner4d391482007-12-12 07:09:47 +00001065 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001066 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001067 I = IMPDecl->classmeth_begin(),
1068 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001069 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001071 // Check for type conflict of methods declared in a class/protocol and
1072 // its implementation; if any.
1073 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001074 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1075 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001076 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Chris Lattner4d391482007-12-12 07:09:47 +00001078 // Check the protocol list for unimplemented methods in the @implementation
1079 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001080 // Check and see if class methods in class interface have been
1081 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Chris Lattnercddc8882009-03-01 00:56:52 +00001083 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001084 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001085 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001086 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001087 InsMap, ClsMap, I);
1088 // Check class extensions (unnamed categories)
1089 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1090 Categories; Categories = Categories->getNextClassCategory()) {
1091 if (!Categories->getIdentifier()) {
1092 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1093 break;
1094 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001095 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001096 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1097 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1098 E = C->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001099 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001100 InsMap, ClsMap, C->getClassInterface());
1101 } else
1102 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001103}
1104
Mike Stump1eb44332009-09-09 15:08:12 +00001105/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001106Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001107Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001108 IdentifierInfo **IdentList,
1109 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001110 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Chris Lattner4d391482007-12-12 07:09:47 +00001112 for (unsigned i = 0; i != NumElts; ++i) {
1113 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001114 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001115 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001116 // Maybe we will complain about the shadowed template parameter.
1117 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1118 // Just pretend that we didn't see the previous declaration.
1119 PrevDecl = 0;
1120 }
1121
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001122 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001123 // GCC apparently allows the following idiom:
1124 //
1125 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1126 // @class XCElementToggler;
1127 //
Mike Stump1eb44332009-09-09 15:08:12 +00001128 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001129 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1130 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001131 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001132 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001133 } else if (TDD) {
1134 // a forward class declaration matching a typedef name of a class refers
1135 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001136 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001137 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1138 PrevDecl = OI->getDecl();
1139 }
Chris Lattner4d391482007-12-12 07:09:47 +00001140 }
Mike Stump1eb44332009-09-09 15:08:12 +00001141 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001142 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001143 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregord0434102009-01-09 00:49:46 +00001144 IdentList[i], SourceLocation(), true);
Steve Naroff8f06f842009-04-23 16:00:56 +00001145 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +00001146 }
1147
1148 Interfaces.push_back(IDecl);
1149 }
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Douglas Gregord0434102009-01-09 00:49:46 +00001151 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001152 &Interfaces[0],
1153 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001154 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001155 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001156 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001157}
1158
1159
1160/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1161/// returns true, or false, accordingly.
1162/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001163bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001164 const ObjCMethodDecl *PrevMethod,
1165 bool matchBasedOnSizeAndAlignment) {
1166 QualType T1 = Context.getCanonicalType(Method->getResultType());
1167 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001169 if (T1 != T2) {
1170 // The result types are different.
1171 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001172 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001173 // Incomplete types don't have a size and alignment.
1174 if (T1->isIncompleteType() || T2->isIncompleteType())
1175 return false;
1176 // Check is based on size and alignment.
1177 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1178 return false;
1179 }
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Chris Lattner89951a82009-02-20 18:43:26 +00001181 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1182 E = Method->param_end();
1183 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Chris Lattner89951a82009-02-20 18:43:26 +00001185 for (; ParamI != E; ++ParamI, ++PrevI) {
1186 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1187 T1 = Context.getCanonicalType((*ParamI)->getType());
1188 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001189 if (T1 != T2) {
1190 // The result types are different.
1191 if (!matchBasedOnSizeAndAlignment)
1192 return false;
1193 // Incomplete types don't have a size and alignment.
1194 if (T1->isIncompleteType() || T2->isIncompleteType())
1195 return false;
1196 // Check is based on size and alignment.
1197 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1198 return false;
1199 }
Chris Lattner4d391482007-12-12 07:09:47 +00001200 }
1201 return true;
1202}
1203
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001204/// \brief Read the contents of the instance and factory method pools
1205/// for a given selector from external storage.
1206///
1207/// This routine should only be called once, when neither the instance
1208/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001209Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001210 bool isInstance) {
1211 assert(ExternalSource && "We need an external AST source");
1212 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1213 "Selector data already loaded into the instance method pool");
1214 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1215 "Selector data already loaded into the factory method pool");
1216
1217 // Read the method list from the external source.
1218 std::pair<ObjCMethodList, ObjCMethodList> Methods
1219 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001221 if (isInstance) {
1222 if (Methods.second.Method)
1223 FactoryMethodPool[Sel] = Methods.second;
1224 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001225 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001226
1227 if (Methods.first.Method)
1228 InstanceMethodPool[Sel] = Methods.first;
1229
1230 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1231}
1232
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001233void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001234 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1235 = InstanceMethodPool.find(Method->getSelector());
1236 if (Pos == InstanceMethodPool.end()) {
1237 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1238 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1239 else
1240 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1241 ObjCMethodList())).first;
1242 }
1243
1244 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001245 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001246 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001247 Entry.Method = Method;
1248 Entry.Next = 0;
1249 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001250 }
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Chris Lattnerb25df352009-03-04 05:16:45 +00001252 // We've seen a method with this name, see if we have already seen this type
1253 // signature.
1254 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1255 if (MatchTwoMethodDeclarations(Method, List->Method))
1256 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Chris Lattnerb25df352009-03-04 05:16:45 +00001258 // We have a new signature for an existing method - add it.
1259 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1260 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001261}
1262
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001263// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001264ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001265 SourceRange R,
1266 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001267 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1268 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001269 if (Pos == InstanceMethodPool.end()) {
1270 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001271 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1272 else
1273 return 0;
1274 }
1275
1276 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001277 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Steve Naroff037cda52008-09-30 14:38:43 +00001279 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001280 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1281 // This checks if the methods differ by size & alignment.
1282 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001283 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001284 }
1285 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001286 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001287 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001288 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001289 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001290 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001291 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001292 }
1293 return MethList.Method;
1294}
1295
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001296void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001297 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1298 = FactoryMethodPool.find(Method->getSelector());
1299 if (Pos == FactoryMethodPool.end()) {
1300 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1301 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1302 else
1303 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1304 ObjCMethodList())).first;
1305 }
1306
1307 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001308 if (!FirstMethod.Method) {
1309 // Haven't seen a method with this selector name yet - add it.
1310 FirstMethod.Method = Method;
1311 FirstMethod.Next = 0;
1312 } else {
1313 // We've seen a method with this name, now check the type signature(s).
1314 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001315
1316 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001317 Next = Next->Next)
1318 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Chris Lattner4d391482007-12-12 07:09:47 +00001320 if (!match) {
1321 // We have a new signature for an existing method - add it.
1322 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001323 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001324 FirstMethod.Next = OMI;
1325 }
1326 }
1327}
1328
Mike Stump1eb44332009-09-09 15:08:12 +00001329ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001330 SourceRange R) {
1331 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1332 = FactoryMethodPool.find(Sel);
1333 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001334 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001335 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1336 else
1337 return 0;
1338 }
1339
1340 ObjCMethodList &MethList = Pos->second;
1341 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001343 if (MethList.Method && MethList.Next) {
1344 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1345 // This checks if the methods differ by size & alignment.
1346 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1347 issueWarning = true;
1348 }
1349 if (issueWarning && (MethList.Method && MethList.Next)) {
1350 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1351 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1352 << MethList.Method->getSourceRange();
1353 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1354 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1355 << Next->Method->getSourceRange();
1356 }
1357 return MethList.Method;
1358}
1359
Mike Stump1eb44332009-09-09 15:08:12 +00001360/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff0701bbb2009-01-08 17:28:14 +00001361/// have the property type and issue diagnostics if they don't.
1362/// Also synthesize a getter/setter method if none exist (and update the
1363/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1364/// methods is the "right" thing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001365void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001366 ObjCContainerDecl *CD) {
1367 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00001368
1369 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001370 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump1eb44332009-09-09 15:08:12 +00001371 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001372 property->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001374 if (SetterMethod) {
Mike Stump1eb44332009-09-09 15:08:12 +00001375 if (Context.getCanonicalType(SetterMethod->getResultType())
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001376 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001377 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001378 if (SetterMethod->param_size() != 1 ||
1379 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001380 Diag(property->getLocation(),
1381 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001382 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001383 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001384 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1385 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001386 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001387
1388 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001389 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001390 // FIXME: The synthesized property we set here is misleading. We almost always
1391 // synthesize these methods unless the user explicitly provided prototypes
1392 // (which is odd, but allowed). Sema should be typechecking that the
1393 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001394 if (!GetterMethod) {
1395 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001396 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001397 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001398 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1399 property->getLocation(), property->getGetterName(),
1400 property->getType(), CD, true, false, true,
1401 (property->getPropertyImplementation() ==
1402 ObjCPropertyDecl::Optional) ?
1403 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001404 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001405 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001406 } else
1407 // A user declared getter will be synthesize when @synthesize of
1408 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001409 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001410 property->setGetterMethodDecl(GetterMethod);
1411
1412 // Skip setter if property is read-only.
1413 if (!property->isReadOnly()) {
1414 // Find the default setter and if one not found, add one.
1415 if (!SetterMethod) {
1416 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001417 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001418 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001419 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1420 property->getLocation(),
1421 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001422 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001423 (property->getPropertyImplementation() ==
1424 ObjCPropertyDecl::Optional) ?
1425 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001426 ObjCMethodDecl::Required);
1427 // Invent the arguments for the setter. We don't bother making a
1428 // nice name for the argument.
1429 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001430 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001431 property->getIdentifier(),
1432 property->getType(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001433 /*DInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001434 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001435 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001436 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001437 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001438 } else
1439 // A user declared setter will be synthesize when @synthesize of
1440 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001441 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001442 property->setSetterMethodDecl(SetterMethod);
1443 }
Mike Stump1eb44332009-09-09 15:08:12 +00001444 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001445 // handle the following, which is supported by GCC (and part of the design).
1446 //
1447 // @interface Foo
1448 // @property double bar;
1449 // @end
1450 //
1451 // void thisIsUnfortunate() {
1452 // id foo;
1453 // double bar = [foo bar];
1454 // }
1455 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001456 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001457 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001458 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001459 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001460}
1461
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001462/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1463/// identical selector names in current and its super classes and issues
1464/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001465void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1466 ObjCMethodDecl *Method,
1467 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001468 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1469 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001471 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001472 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001473 SD->lookupMethod(Method->getSelector(), IsInstance);
1474 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001475 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001476 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001477 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001478 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1479 E = Method->param_end();
1480 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1481 for (; ParamI != E; ++ParamI, ++PrevI) {
1482 // Number of parameters are the same and is guaranteed by selector match.
1483 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1484 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1485 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1486 // If type of arguement of method in this class does not match its
1487 // respective argument type in the super class method, issue warning;
1488 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001489 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001490 << T1 << T2;
1491 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1492 return;
1493 }
1494 }
1495 ID = SD;
1496 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001497}
1498
Steve Naroffa56f6162007-12-18 01:30:32 +00001499// Note: For class/category implemenations, allMethods/allProperties is
1500// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001501void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1502 DeclPtrTy *allMethods, unsigned allNum,
1503 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001504 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001505 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001506
Steve Naroffa56f6162007-12-18 01:30:32 +00001507 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1508 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001509 // should be true.
1510 if (!ClassDecl)
1511 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001512
1513 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001514 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1515 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001516 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001517
Steve Naroff0701bbb2009-01-08 17:28:14 +00001518 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001519
1520 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1521 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1522 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1523
Chris Lattner4d391482007-12-12 07:09:47 +00001524 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001525 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001526 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001527
1528 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001529 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001530 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001531 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001532 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001533 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001534 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001535 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001536 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001537 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001538 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001539 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001540 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001541 InsMap[Method->getSelector()] = Method;
1542 /// The following allows us to typecheck messages to "id".
1543 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001544 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001545 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001546 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001547 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001548 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001549 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001550 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001551 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001552 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001553 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001554 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001555 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001556 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001557 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001558 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001559 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001560 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001561 /// The following allows us to typecheck messages to "Class".
1562 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001563 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001564 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001565 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001566 }
1567 }
1568 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001569 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001570 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001571 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001572 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001573 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001574 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001575 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001576 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001577 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001578
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001579 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001580 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001581 if (C->getIdentifier() == 0)
1582 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001583 }
Steve Naroff09c47192009-01-09 15:36:25 +00001584 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1585 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1586 // user-defined setter/getter. It also synthesizes setter/getter methods
1587 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001588 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1589 E = CDecl->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001590 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001591 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001592 CDecl->setAtEndLoc(AtEndLoc);
1593 }
1594 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001595 IC->setAtEndLoc(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001596 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001597 ImplMethodsVsClassMethods(IC, IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001598 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001599 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001600 CatImplClass->setAtEndLoc(AtEndLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Chris Lattner4d391482007-12-12 07:09:47 +00001602 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001603 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001604 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001605 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001606 Categories; Categories = Categories->getNextClassCategory()) {
1607 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001608 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001609 break;
1610 }
1611 }
1612 }
1613 }
Chris Lattner682bf922009-03-29 16:50:03 +00001614 if (isInterfaceDeclKind) {
1615 // Reject invalid vardecls.
1616 for (unsigned i = 0; i != tuvNum; i++) {
1617 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1618 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1619 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001620 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001621 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001622 }
Chris Lattner682bf922009-03-29 16:50:03 +00001623 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001624 }
Chris Lattner4d391482007-12-12 07:09:47 +00001625}
1626
1627
1628/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1629/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001630static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001631CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1632 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1633 if (PQTVal & ObjCDeclSpec::DQ_In)
1634 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1635 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1636 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1637 if (PQTVal & ObjCDeclSpec::DQ_Out)
1638 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1639 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1640 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1641 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1642 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1643 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1644 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001645
1646 return ret;
1647}
1648
Chris Lattnerb28317a2009-03-28 19:18:32 +00001649Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001650 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001651 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001652 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001653 Selector Sel,
1654 // optional arguments. The number of types/arguments is obtained
1655 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001656 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001657 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001658 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1659 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001660 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001661
1662 // Make sure we can establish a context for the method.
1663 if (!ClassDecl) {
1664 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanian32844b32009-08-28 17:52:37 +00001665 FunctionLabelMap.clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001666 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001667 }
Chris Lattner4d391482007-12-12 07:09:47 +00001668 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Steve Naroffccef3712009-02-20 22:59:16 +00001670 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001671 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Steve Naroffccef3712009-02-20 22:59:16 +00001673 // Methods cannot return interface types. All ObjC objects are
1674 // passed by reference.
1675 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001676 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1677 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001678 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001679 }
1680 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001681 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001682
1683 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001684 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00001685 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001686 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001687 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001688 MethodDeclKind == tok::objc_optional ?
1689 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001690 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Chris Lattner0ed844b2008-04-04 06:12:32 +00001692 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Chris Lattner7db638d2009-04-11 19:42:43 +00001694 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001695 QualType ArgType, UnpromotedArgType;
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Chris Lattnere294d3f2009-04-11 18:57:04 +00001697 if (ArgInfo[i].Type == 0) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001698 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001699 } else {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001700 UnpromotedArgType = ArgType = GetTypeFromParser(ArgInfo[i].Type);
Steve Naroff6082c622008-12-09 19:36:17 +00001701 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001702 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001703 }
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001705 ParmVarDecl* Param;
Chris Lattner2dd979f2009-04-11 19:08:56 +00001706 if (ArgType == UnpromotedArgType)
Chris Lattner7db638d2009-04-11 19:42:43 +00001707 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001708 ArgInfo[i].Name, ArgType,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001709 /*DInfo=*/0, //FIXME: Pass info here.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001710 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001711 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001712 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner7db638d2009-04-11 19:42:43 +00001713 ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001714 ArgInfo[i].Name, ArgType,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001715 /*DInfo=*/0, //FIXME: Pass info here.
Chris Lattner2dd979f2009-04-11 19:08:56 +00001716 UnpromotedArgType,
Douglas Gregor64650af2009-02-02 23:39:07 +00001717 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001719 if (ArgType->isObjCInterfaceType()) {
1720 Diag(ArgInfo[i].NameLoc,
1721 diag::err_object_cannot_be_passed_returned_by_value)
1722 << 1 << ArgType;
1723 Param->setInvalidDecl();
1724 }
Mike Stump1eb44332009-09-09 15:08:12 +00001725
Chris Lattner0ed844b2008-04-04 06:12:32 +00001726 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001727 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001729 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001730 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001731
Chris Lattner0ed844b2008-04-04 06:12:32 +00001732 Params.push_back(Param);
1733 }
1734
Jay Foadbeaaccd2009-05-21 09:52:38 +00001735 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001736 ObjCMethod->setObjCDeclQualifier(
1737 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1738 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001739
1740 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001741 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001742
1743 // For implementations (which can be very "coarse grain"), we add the
1744 // method now. This allows the AST to implement lookup methods that work
1745 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001746 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001747 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001748 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001749 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001750 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1751 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001752 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001753 PrevMethod = ImpDecl->getClassMethod(Sel);
1754 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001755 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001756 if (AttrList)
1757 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001758 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001759 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001760 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001761 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1762 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001763 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001764 PrevMethod = CatImpDecl->getClassMethod(Sel);
1765 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001766 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001767 if (AttrList)
1768 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001769 }
1770 if (PrevMethod) {
1771 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001772 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001773 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001774 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001775 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001776 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001777}
1778
Mike Stump1eb44332009-09-09 15:08:12 +00001779void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001780 SourceLocation Loc,
1781 unsigned &Attributes) {
1782 // FIXME: Improve the reported location.
1783
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001784 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001785 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001786 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1787 ObjCDeclSpec::DQ_PR_assign |
1788 ObjCDeclSpec::DQ_PR_copy |
1789 ObjCDeclSpec::DQ_PR_retain))) {
1790 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1791 "readwrite" :
1792 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1793 "assign" :
1794 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1795 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00001796
1797 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001798 diag::err_objc_property_attr_mutually_exclusive :
1799 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001800 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001801 }
1802
1803 // Check for copy or retain on non-object types.
1804 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001805 !PropertyTy->isObjCObjectPointerType() &&
1806 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00001807 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001808 Diag(Loc, diag::err_objc_property_requires_object)
1809 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001810 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1811 }
1812
1813 // Check for more than one of { assign, copy, retain }.
1814 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1815 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001816 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1817 << "assign" << "copy";
1818 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00001819 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001820 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001821 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1822 << "assign" << "retain";
1823 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001824 }
1825 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1826 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001827 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1828 << "copy" << "retain";
1829 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001830 }
1831 }
1832
1833 // Warn if user supplied no assignment attribute, property is
1834 // readwrite, and this is an object type.
1835 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1836 ObjCDeclSpec::DQ_PR_retain)) &&
1837 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00001838 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001839 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00001840 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001841 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1842
1843 // If non-gc code warn that this is likely inappropriate.
1844 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1845 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001847 // FIXME: Implement warning dependent on NSCopying being
1848 // implemented. See also:
1849 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1850 // (please trim this list while you are at it).
1851 }
Mike Stump046efd92009-05-07 23:06:50 +00001852
1853 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1854 && getLangOptions().getGCMode() == LangOptions::GCOnly
1855 && PropertyTy->isBlockPointerType())
1856 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001857}
1858
Mike Stump1eb44332009-09-09 15:08:12 +00001859Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001860 FieldDeclarator &FD,
1861 ObjCDeclSpec &ODS,
1862 Selector GetterSel,
1863 Selector SetterSel,
1864 DeclPtrTy ClassCategory,
1865 bool *isOverridingProperty,
1866 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001867 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001868 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1869 // default is readwrite!
1870 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00001871 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001872 // not retain or copy
1873 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001874 (isReadWrite &&
1875 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001876 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1877 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001878 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001879 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001880 // May modify Attributes.
1881 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001882 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1883 if (!CDecl->getIdentifier()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001884 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001885 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001886 if ((CCPrimary = CDecl->getClassInterface())) {
1887 // Find the property in continuation class's primary class only.
1888 ObjCPropertyDecl *PIDecl = 0;
1889 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00001890 for (ObjCInterfaceDecl::prop_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001891 I = CCPrimary->prop_begin(), E = CCPrimary->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001892 I != E; ++I)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001893 if ((*I)->getIdentifier() == PropertyId) {
1894 PIDecl = *I;
1895 break;
1896 }
Mike Stump1eb44332009-09-09 15:08:12 +00001897
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001898 if (PIDecl) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001899 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001900 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001901 unsigned PIkind = PIDecl->getPropertyAttributes();
1902 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001903 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001904 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1905 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001906 PIDecl->makeitReadWriteAttribute();
1907 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1908 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1909 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1910 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1911 PIDecl->setSetterName(SetterSel);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001912 } else
Mike Stump1eb44332009-09-09 15:08:12 +00001913 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001914 << CCPrimary->getDeclName();
1915 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001916 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001917 // class's list.
1918 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001919 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001920 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001921 // No matching property found in the primary class. Just fall thru
1922 // and add property to continuation class's primary class.
1923 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001924 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001925 Diag(CDecl->getLocation(), diag::err_continuation_class);
1926 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001927 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001928 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001929 }
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001931 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00001932 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001933 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
1934 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
1935 if (T->isObjCObjectPointerType()) {
1936 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001937 if (const ObjCInterfaceType *OIT =
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00001938 InterfaceTy->getAsObjCInterfaceType()) {
1939 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001940 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001941 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001942 LookupProtocol(&Context.Idents.get("NSCopying")))
1943 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00001944 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001945 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00001946 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001947 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00001948 if (T->isObjCInterfaceType())
1949 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00001950
Steve Naroff93983f82009-01-11 12:47:58 +00001951 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1952 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001953 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00001954 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001955 FD.D.getIdentifier(), T);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001956 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001957
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001958 if (T->isArrayType() || T->isFunctionType()) {
1959 Diag(AtLoc, diag::err_property_type) << T;
1960 PDecl->setInvalidDecl();
1961 }
Mike Stump1eb44332009-09-09 15:08:12 +00001962
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001963 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001964
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001965 // Regardless of setter/getter attribute, we save the default getter/setter
1966 // selector names in anticipation of declaration of setter/getter methods.
1967 PDecl->setGetterName(GetterSel);
1968 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001970 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001971 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001973 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001974 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00001975
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001976 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001977 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001979 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001980 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001982 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001983 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001985 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001986 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00001987
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001988 if (isAssign)
1989 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001991 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001992 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001994 if (MethodImplKind == tok::objc_required)
1995 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1996 else if (MethodImplKind == tok::objc_optional)
1997 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001998 // A case of continuation class adding a new property in the class. This
1999 // is not what it was meant for. However, gcc supports it and so should we.
2000 // Make sure setter/getters are declared here.
2001 if (CCPrimary)
2002 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002003
Chris Lattnerb28317a2009-03-28 19:18:32 +00002004 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002005}
2006
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002007/// ActOnPropertyImplDecl - This routine performs semantic checks and
2008/// builds the AST node for a property implementation declaration; declared
2009/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002010///
Mike Stump1eb44332009-09-09 15:08:12 +00002011Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002012 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002013 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002014 DeclPtrTy ClassCatImpDecl,
2015 IdentifierInfo *PropertyId,
2016 IdentifierInfo *PropertyIvar) {
2017 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002018 // Make sure we have a context for the property implementation declaration.
2019 if (!ClassImpDecl) {
2020 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002021 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002022 }
2023 ObjCPropertyDecl *property = 0;
2024 ObjCInterfaceDecl* IDecl = 0;
2025 // Find the class or category class where this property must have
2026 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002027 ObjCImplementationDecl *IC = 0;
2028 ObjCCategoryImplDecl* CatImplClass = 0;
2029 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002030 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002031 // We always synthesize an interface for an implementation
2032 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002033 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002034 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002035
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002036 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002037 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002038 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002039 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002040 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002041 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002042 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002043 if (Synthesize) {
2044 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002045 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002046 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002047 IDecl = CatImplClass->getClassInterface();
2048 if (!IDecl) {
2049 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002050 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002051 }
Mike Stump1eb44332009-09-09 15:08:12 +00002052 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002053 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002054
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002055 // If category for this implementation not found, it is an error which
2056 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002057 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002058 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002059 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002060 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002061 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002062 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002063 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002064 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002065 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002066 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002067 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002068 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002069 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002070 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002071 // Check that we have a valid, previously declared ivar for @synthesize
2072 if (Synthesize) {
2073 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002074 if (!PropertyIvar)
2075 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002076 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002077 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002078 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002079 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002080 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002081 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002082 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002083 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002084 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2085 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002086 ObjCIvarDecl::Public,
2087 (Expr *)0);
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002088 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002089 IDecl->addDecl(Ivar);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002090 property->setPropertyIvarDecl(Ivar);
2091 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002092 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002093 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002094 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002095 } else if (getLangOptions().ObjCNonFragileABI &&
2096 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002097 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002098 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002099 << ClassDeclared->getDeclName();
2100 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2101 << Ivar << Ivar->getNameAsCString();
2102 // Note! I deliberately want it to fall thru so more errors are caught.
2103 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002104 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002105
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002106 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002107 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002108 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002109 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002110 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002111 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002112 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002113 }
Mike Stump1eb44332009-09-09 15:08:12 +00002114
Chris Lattnerb28317a2009-03-28 19:18:32 +00002115 // FIXME! Rules for properties are somewhat different that those
2116 // for assignments. Use a new routine to consolidate all cases;
2117 // specifically for property redeclarations as well as for ivars.
2118 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2119 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002120 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002121 lhsType->isArithmeticType()) {
2122 Diag(PropertyLoc, diag::error_property_ivar_type)
2123 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002124 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002125 }
2126 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002127 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2128 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002129 Diag(PropertyLoc, diag::error_weak_property)
2130 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002131 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002132 }
Mike Stump1eb44332009-09-09 15:08:12 +00002133 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002134 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2135 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002136 Diag(PropertyLoc, diag::error_strong_property)
2137 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002138 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002139 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002140 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002141 } else if (PropertyIvar)
2142 // @dynamic
2143 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002144 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002145 ObjCPropertyImplDecl *PIDecl =
2146 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2147 property,
2148 (Synthesize ?
2149 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002150 : ObjCPropertyImplDecl::Dynamic),
2151 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002152 if (IC) {
2153 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002154 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002155 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002156 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2157 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002158 << PropertyIvar;
2159 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2160 }
Mike Stump1eb44332009-09-09 15:08:12 +00002161
2162 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002163 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002164 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2165 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002166 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002167 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002168 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002169 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002170 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002171 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002172 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002173 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2174 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002175 << PropertyIvar;
2176 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2177 }
Mike Stump1eb44332009-09-09 15:08:12 +00002178
2179 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002180 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002181 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2182 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002183 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002184 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002185 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002186 }
Mike Stump1eb44332009-09-09 15:08:12 +00002187
Chris Lattnerb28317a2009-03-28 19:18:32 +00002188 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002189}
Anders Carlsson15281452008-11-04 16:57:32 +00002190
Chris Lattnercc98eac2008-12-17 07:13:27 +00002191bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002192 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002193 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002194
Anders Carlsson15281452008-11-04 16:57:32 +00002195 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2196 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002197
Anders Carlsson15281452008-11-04 16:57:32 +00002198 return true;
2199}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002200
Chris Lattnercc98eac2008-12-17 07:13:27 +00002201/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2202/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002203void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002204 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002205 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002206 // Check that ClassName is a valid class
2207 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2208 if (!Class) {
2209 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2210 return;
2211 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002212 if (LangOpts.ObjCNonFragileABI) {
2213 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2214 return;
2215 }
Mike Stump1eb44332009-09-09 15:08:12 +00002216
Chris Lattnercc98eac2008-12-17 07:13:27 +00002217 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002218 llvm::SmallVector<FieldDecl*, 32> RecFields;
2219 Context.CollectObjCIvars(Class, RecFields);
2220 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2221 for (unsigned i = 0; i < RecFields.size(); i++) {
2222 FieldDecl* ID = RecFields[i];
2223 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2224 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2225 ID->getIdentifier(), ID->getType(),
2226 ID->getBitWidth());
2227 Decls.push_back(Sema::DeclPtrTy::make(FD));
2228 }
Mike Stump1eb44332009-09-09 15:08:12 +00002229
Chris Lattnercc98eac2008-12-17 07:13:27 +00002230 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002231 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002232 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002233 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002234 if (getLangOptions().CPlusPlus)
2235 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002236 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002237 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002238 }
2239}
2240