blob: 5cf48d6da0c861457e755dce1fb47e3dd5ac2a4e [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;
28 if (Context.isObjCObjectPointerType(property->getType()))
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) {
31 Diag(Loc, diag::warn_accessor_property_type_mismatch)
32 << 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>());
Steve Naroff394f3f42008-07-25 17:57:26 +000046
47 // 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);
Chris Lattner4d391482007-12-12 07:09:47 +000058
59 // 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());
Chris Lattner4d391482007-12-12 07:09:47 +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");
85
86 // 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 }
99
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 {
Douglas Gregord0434102009-01-09 00:49:46 +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);
Chris Lattner4d391482007-12-12 07:09:47 +0000120
Steve Naroffa7503a72009-04-23 15:15:40 +0000121 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000122 }
123
124 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);
Chris Lattner3c73c412008-11-19 08:23:25 +0000127
Steve Naroff818cb9e2009-02-04 17:14:05 +0000128 ObjCInterfaceDecl *SuperClassDecl =
129 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000130
131 // Diagnose classes that inherit from deprecated classes.
132 if (SuperClassDecl)
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000133 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000134
Steve Naroff818cb9e2009-02-04 17:14:05 +0000135 if (PrevDecl && SuperClassDecl == 0) {
136 // The previous declaration was not a class decl. Check if we have a
137 // typedef. If we do, get the underlying class type.
138 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
139 QualType T = TDecl->getUnderlyingType();
140 if (T->isObjCInterfaceType()) {
141 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
142 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
143 }
144 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000145
Steve Naroff818cb9e2009-02-04 17:14:05 +0000146 // This handles the following case:
147 //
148 // typedef int SuperClass;
149 // @interface MyClass : SuperClass {} @end
150 //
151 if (!SuperClassDecl) {
152 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
153 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
154 }
155 }
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000156
Steve Naroff818cb9e2009-02-04 17:14:05 +0000157 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
158 if (!SuperClassDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000159 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000160 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000161 else if (SuperClassDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000162 Diag(SuperLoc, diag::err_undef_superclass)
Steve Naroff818cb9e2009-02-04 17:14:05 +0000163 << SuperClassDecl->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000164 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000165 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000166 IDecl->setSuperClass(SuperClassDecl);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000167 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000168 IDecl->setLocEnd(SuperLoc);
169 } else { // we have a root class.
170 IDecl->setLocEnd(ClassLoc);
171 }
172
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000173 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000174 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000175 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
176 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000177 IDecl->setLocEnd(EndProtoLoc);
178 }
Anders Carlsson15281452008-11-04 16:57:32 +0000179
180 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000181 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000182}
183
184/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000185/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000186Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
187 IdentifierInfo *AliasName,
188 SourceLocation AliasLocation,
189 IdentifierInfo *ClassName,
190 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000191 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000192 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000193 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000194 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000195 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000196 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000197 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000198 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000199 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000200 }
201 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000202 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000203 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
204 QualType T = TDecl->getUnderlyingType();
205 if (T->isObjCInterfaceType()) {
206 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
207 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000208 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000209 }
210 }
211 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000212 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
213 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000214 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000215 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000216 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000217 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000218 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000219
220 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000221 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000222 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000223
Anders Carlsson15281452008-11-04 16:57:32 +0000224 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000225 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000226
Chris Lattnerb28317a2009-03-28 19:18:32 +0000227 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000228}
229
Steve Naroff61d68522009-03-05 15:22:01 +0000230void Sema::CheckForwardProtocolDeclarationForCircularDependency(
231 IdentifierInfo *PName,
232 SourceLocation &Ploc, SourceLocation PrevLoc,
233 const ObjCList<ObjCProtocolDecl> &PList)
234{
235 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
236 E = PList.end(); I != E; ++I) {
237
Douglas Gregor6e378de2009-04-23 23:18:26 +0000238 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000239 if (PDecl->getIdentifier() == PName) {
240 Diag(Ploc, diag::err_protocol_has_circular_dependency);
241 Diag(PrevLoc, diag::note_previous_definition);
242 }
243 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
244 PDecl->getLocation(), PDecl->getReferencedProtocols());
245 }
246 }
247}
248
Chris Lattnerb28317a2009-03-28 19:18:32 +0000249Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000250Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
251 IdentifierInfo *ProtocolName,
252 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000253 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000254 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000255 SourceLocation EndProtoLoc,
256 AttributeList *AttrList) {
257 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000258 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000259 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000260 if (PDecl) {
261 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000262 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000263 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000264 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000265 // Just return the protocol we already had.
266 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000267 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000268 }
Steve Naroff61d68522009-03-05 15:22:01 +0000269 ObjCList<ObjCProtocolDecl> PList;
270 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
271 CheckForwardProtocolDeclarationForCircularDependency(
272 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
273 PList.Destroy(Context);
274
Steve Narofff11b5082008-08-13 16:39:22 +0000275 // Make sure the cached decl gets a valid start location.
276 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000277 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000278 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000279 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
280 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000281 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000282 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000283 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000284 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000285 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000286 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000287 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000288 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000289 PDecl->setLocEnd(EndProtoLoc);
290 }
Anders Carlsson15281452008-11-04 16:57:32 +0000291
292 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000293 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000294}
295
296/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000297/// issues an error if they are not declared. It returns list of
298/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000299void
Chris Lattnere13b9592008-07-26 04:03:38 +0000300Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000301 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000302 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000303 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000304 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000305 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000306 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000307 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000308 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000309 continue;
310 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000311
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000312 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000313
314 // If this is a forward declaration and we are supposed to warn in this
315 // case, do it.
316 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000317 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000318 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000319 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000320 }
321}
322
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000323/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000324/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000325///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000326void
327Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
328 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000329 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000330 ObjCPropertyDecl::PropertyAttributeKind CAttr =
331 Property->getPropertyAttributes();
332 ObjCPropertyDecl::PropertyAttributeKind SAttr =
333 SuperProperty->getPropertyAttributes();
334 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
335 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000336 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000337 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000338 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
339 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000340 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000341 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000342 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
343 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
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() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000346
347 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
348 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000349 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000350 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000351 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000352 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000353 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000354 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000355 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000356 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000357
358 QualType LHSType =
359 Context.getCanonicalType(SuperProperty->getType());
360 QualType RHSType =
361 Context.getCanonicalType(Property->getType());
362
363 if (!Context.typesAreCompatible(LHSType, RHSType)) {
364 // FIXME: Incorporate this test with typesAreCompatible.
365 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
366 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
367 return;
368 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
369 << Property->getType() << SuperProperty->getType() << inheritedName;
370 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000371}
372
373/// ComparePropertiesInBaseAndSuper - This routine compares property
374/// declarations in base and its super class, if any, and issues
375/// diagnostics in a variety of inconsistant situations.
376///
Chris Lattner70f19542009-02-16 21:26:43 +0000377void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000378 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
379 if (!SDecl)
380 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000381 // FIXME: O(N^2)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000382 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
383 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000384 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000385 // Does property in super class has declaration in current class?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000386 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
387 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000388 ObjCPropertyDecl *PDecl = (*I);
389 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000390 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000391 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000392 }
393 }
394}
395
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000396/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
397/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000398/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000399void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000400Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000401 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000402 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
403 if (!IDecl) {
404 // Category
405 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
406 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000407 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
408 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000409 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000410 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000411 // Is this property already in category's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000412 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000413 if ((*CP)->getIdentifier() == Pr->getIdentifier())
414 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000415 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000416 // Property protocol already exist in class. Diagnose any mismatch.
417 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
418 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000419 return;
420 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000421 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
422 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000423 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000424 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000425 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000426 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000427 if ((*CP)->getIdentifier() == Pr->getIdentifier())
428 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000429 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000430 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000431 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000432 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000433}
434
435/// MergeProtocolPropertiesIntoClass - This routine merges properties
436/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000437/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000438///
Chris Lattner70f19542009-02-16 21:26:43 +0000439void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000440 DeclPtrTy MergeItsProtocols) {
441 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000442 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
443
444 if (!IDecl) {
445 // Category
446 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
447 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
448 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
449 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
450 E = MDecl->protocol_end(); P != E; ++P)
451 // Merge properties of category (*P) into IDECL's
452 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
453
454 // Go thru the list of protocols for this category and recursively merge
455 // their properties into this class as well.
456 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
457 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000458 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000459 } else {
460 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
461 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
462 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000463 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000464 }
465 return;
466 }
467
Chris Lattnerb752f282008-07-21 07:06:49 +0000468 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000469 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
470 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000471 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000472 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
473
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000474 // Go thru the list of protocols for this class and recursively merge
475 // their properties into this class as well.
476 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
477 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000478 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000479 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000480 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
481 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
482 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000483 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000484 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000485}
486
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000487/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000488/// a class method in its extension.
489///
490void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
491 ObjCInterfaceDecl *ID) {
492 if (!ID)
493 return; // Possibly due to previous error
494
495 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000496 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
497 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000498 ObjCMethodDecl *MD = *i;
499 MethodMap[MD->getSelector()] = MD;
500 }
501
502 if (MethodMap.empty())
503 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000504 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
505 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000506 ObjCMethodDecl *Method = *i;
507 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
508 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
509 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
510 << Method->getDeclName();
511 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
512 }
513 }
514}
515
Chris Lattner58fe03b2009-04-12 08:43:13 +0000516/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000517Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000518Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000519 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000520 unsigned NumElts,
521 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000522 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000523
524 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000525 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000526 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000527 if (PDecl == 0) { // Not already seen?
528 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
529 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000530 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000531 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000532 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000533 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000534 Protocols.push_back(PDecl);
535 }
Anders Carlsson15281452008-11-04 16:57:32 +0000536
537 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000538 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000539 &Protocols[0], Protocols.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000540 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000541 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000542 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000543}
544
Chris Lattnerb28317a2009-03-28 19:18:32 +0000545Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000546ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
547 IdentifierInfo *ClassName, SourceLocation ClassLoc,
548 IdentifierInfo *CategoryName,
549 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000550 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000551 unsigned NumProtoRefs,
552 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000553 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000554 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
555 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000556 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000557
558 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000559 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000560 if (!IDecl || IDecl->isForwardDecl()) {
561 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000562 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000563 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000564 }
Chris Lattner4d391482007-12-12 07:09:47 +0000565
Chris Lattner70f19542009-02-16 21:26:43 +0000566 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000567
568 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000569 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000570
571 /// Check for duplicate interface declaration for this category
572 ObjCCategoryDecl *CDeclChain;
573 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
574 CDeclChain = CDeclChain->getNextClassCategory()) {
575 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
576 Diag(CategoryLoc, diag::warn_dup_category_def)
577 << ClassName << CategoryName;
578 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
579 break;
580 }
581 }
582 if (!CDeclChain)
583 CDecl->insertNextClassCategory();
584
Chris Lattner4d391482007-12-12 07:09:47 +0000585 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000586 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000587 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000588 }
Anders Carlsson15281452008-11-04 16:57:32 +0000589
590 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000591 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000592}
593
594/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000595/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000596/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000597Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000598 SourceLocation AtCatImplLoc,
599 IdentifierInfo *ClassName, SourceLocation ClassLoc,
600 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000601 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000602 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000603 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
604 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000605 /// Check that class of this category is already completely declared.
606 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000607 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000608
Douglas Gregord0434102009-01-09 00:49:46 +0000609 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000610 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000611
Chris Lattner4d391482007-12-12 07:09:47 +0000612 /// TODO: Check that CatName, category name, is not used in another
613 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000614 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000615
616 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000617 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000618}
619
Chris Lattnerb28317a2009-03-28 19:18:32 +0000620Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000621 SourceLocation AtClassImplLoc,
622 IdentifierInfo *ClassName, SourceLocation ClassLoc,
623 IdentifierInfo *SuperClassname,
624 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000625 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000626 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000627 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000628 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000629 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000630 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000631 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000632 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000633 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000634 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000635 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000636 IDecl = 0;
637 }
Chris Lattner4d391482007-12-12 07:09:47 +0000638 }
639
640 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000641 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000642 if (SuperClassname) {
643 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000644 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000645 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000646 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
647 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000648 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000649 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000650 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000651 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000652 Diag(SuperClassLoc, diag::err_undef_superclass)
653 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000654 else if (IDecl && IDecl->getSuperClass() != SDecl) {
655 // This implementation and its interface do not have the same
656 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000657 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000658 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000659 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000660 }
661 }
662 }
663
664 if (!IDecl) {
665 // Legacy case of @implementation with no corresponding @interface.
666 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000667
Mike Stump390b4cc2009-05-16 07:39:55 +0000668 // FIXME: Do we support attributes on the @implementation? If so we should
669 // copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000670 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
671 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000672 IDecl->setSuperClass(SDecl);
673 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000674
675 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000676 } else {
677 // Mark the interface as being completed, even if it was just as
678 // @class ....;
679 // declaration; the user cannot reopen it.
680 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000681 }
682
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000683 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000684 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000685 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000686
Anders Carlsson15281452008-11-04 16:57:32 +0000687 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000688 return DeclPtrTy::make(IMPDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000689
Chris Lattner4d391482007-12-12 07:09:47 +0000690 // Check that there is no duplicate implementation of this class.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000691 if (LookupObjCImplementation(ClassName))
Chris Lattner75c9cae2008-03-16 20:53:07 +0000692 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000693 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000694 else // add it to the list.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000695 PushOnScopeChains(IMPDecl, TUScope);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000696 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000697}
698
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000699void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
700 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000701 SourceLocation RBrace) {
702 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000703 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000704 if (!IDecl)
705 return;
706 /// Check case of non-existing @interface decl.
707 /// (legacy objective-c @implementation decl without an @interface decl).
708 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000709 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000710 IDecl->setIVarList(ivars, numIvars, Context);
711 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000712 return;
713 }
714 // If implementation has empty ivar list, just return.
715 if (numIvars == 0)
716 return;
717
718 assert(ivars && "missing @implementation ivars");
719
720 // Check interface's Ivar list against those in the implementation.
721 // names and types must match.
722 //
Chris Lattner4d391482007-12-12 07:09:47 +0000723 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000724 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000725 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
726 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000727 ObjCIvarDecl* ImplIvar = ivars[j++];
728 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000729 assert (ImplIvar && "missing implementation ivar");
730 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000731
732 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000733 if (Context.getCanonicalType(ImplIvar->getType()) !=
734 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000735 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000736 << ImplIvar->getIdentifier()
737 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000738 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000739 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
740 Expr *ImplBitWidth = ImplIvar->getBitWidth();
741 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000742 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
743 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000744 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
745 << ImplIvar->getIdentifier();
746 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
747 }
748 }
749 // Make sure the names are identical.
750 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000751 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000752 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000753 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000754 }
755 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000756 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000757
758 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000759 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000760 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000761 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000762}
763
Steve Naroff3c2eb662008-02-10 21:38:56 +0000764void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
765 bool &IncompleteImpl) {
766 if (!IncompleteImpl) {
767 Diag(ImpLoc, diag::warn_incomplete_impl);
768 IncompleteImpl = true;
769 }
Chris Lattner08631c52008-11-23 21:45:46 +0000770 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000771}
772
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000773void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
774 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000775 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000776 ImpMethodDecl->getResultType()) &&
777 !QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
778 ImpMethodDecl->getResultType())) {
Chris Lattner3aff9192009-04-11 19:58:42 +0000779 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
780 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
781 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000782 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
783 }
Chris Lattner3aff9192009-04-11 19:58:42 +0000784
785 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
786 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
787 IM != EM; ++IM, ++IF) {
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000788 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()) ||
789 QualifiedIdConformsQualifiedId((*IF)->getType(), (*IM)->getType()))
Chris Lattner3aff9192009-04-11 19:58:42 +0000790 continue;
791
792 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
793 << ImpMethodDecl->getDeclName() << (*IF)->getType()
794 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000795 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000796 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000797}
798
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000799/// isPropertyReadonly - Return true if property is readonly, by searching
800/// for the property in the class and in its categories and implementations
801///
802bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000803 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000804 // by far the most common case.
805 if (!PDecl->isReadOnly())
806 return false;
807 // Even if property is ready only, if interface has a user defined setter,
808 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000809 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000810 return false;
811
812 // Main class has the property as 'readonly'. Must search
813 // through the category list to see if the property's
814 // attribute has been over-ridden to 'readwrite'.
815 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
816 Category; Category = Category->getNextClassCategory()) {
817 // Even if property is ready only, if a category has a user defined setter,
818 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000819 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000820 return false;
821 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000822 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000823 if (P && !P->isReadOnly())
824 return false;
825 }
826
827 // Also, check for definition of a setter method in the implementation if
828 // all else failed.
829 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
830 if (ObjCImplementationDecl *IMD =
831 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000832 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000833 return false;
834 }
835 else if (ObjCCategoryImplDecl *CIMD =
836 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000837 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000838 return false;
839 }
840 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000841 // Lastly, look through the implementation (if one is in scope).
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000842 if (ObjCImplementationDecl *ImpDecl
843 = LookupObjCImplementation(IDecl->getIdentifier()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000844 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000845 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000846 // If all fails, look at the super class.
847 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
848 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000849 return true;
850}
851
Mike Stump390b4cc2009-05-16 07:39:55 +0000852/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
853/// improve the efficiency of selector lookups and type checking by associating
854/// with each protocol / interface / category the flattened instance tables. If
855/// we used an immutable set to keep the table then it wouldn't add significant
856/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000857
Steve Naroffefe7f362008-02-08 22:06:17 +0000858/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000859/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000860void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
861 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000862 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000863 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000864 const llvm::DenseSet<Selector> &ClsMap,
865 ObjCInterfaceDecl *IDecl) {
866 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000867 ObjCInterfaceDecl *NSIDecl = 0;
868 if (getLangOptions().NeXTRuntime) {
869 // check to see if class implements forwardInvocation method and objects
870 // of this class are derived from 'NSProxy' so that to forward requests
871 // from one object to another.
872 // Under such conditions, which means that every method possible is
873 // implemented in the class, we should not issue "Method definition not
874 // found" warnings.
875 // FIXME: Use a general GetUnarySelector method for this.
876 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
877 Selector fISelector = Context.Selectors.getSelector(1, &II);
878 if (InsMap.count(fISelector))
879 // Is IDecl derived from 'NSProxy'? If so, no instance methods
880 // need be implemented in the implementation.
881 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
882 }
883
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000884 // If a method lookup fails locally we still need to look and see if
885 // the method was implemented by a base class or an inherited
886 // protocol. This lookup is slow, but occurs rarely in correct code
887 // and otherwise would terminate in a warning.
888
Chris Lattner4d391482007-12-12 07:09:47 +0000889 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000890 if (!NSIDecl)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000891 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
892 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000893 ObjCMethodDecl *method = *I;
894 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
895 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
896 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000897 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000898 // Ugly, but necessary. Method declared in protcol might have
899 // have been synthesized due to a property declared in the class which
900 // uses the protocol.
901 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000902 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000903 if (!MethodInClass || !MethodInClass->isSynthesized())
904 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
905 }
906 }
Chris Lattner4d391482007-12-12 07:09:47 +0000907 // check unimplemented class methods
Douglas Gregor6ab35242009-04-09 21:40:53 +0000908 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000909 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000910 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000911 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000912 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
913 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000914 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000915 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000916 }
Chris Lattner780f3292008-07-21 21:32:27 +0000917 // Check on this protocols's referenced protocols, recursively.
918 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
919 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000920 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000921}
922
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000923/// MatchAllMethodDeclarations - Check methods declaraed in interface or
924/// or protocol against those declared in their implementations.
925///
926void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
927 const llvm::DenseSet<Selector> &ClsMap,
928 llvm::DenseSet<Selector> &InsMapSeen,
929 llvm::DenseSet<Selector> &ClsMapSeen,
930 ObjCImplDecl* IMPDecl,
931 ObjCContainerDecl* CDecl,
932 bool &IncompleteImpl,
933 bool ImmediateClass)
934{
935 // Check and see if instance methods in class interface have been
936 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000937 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
938 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000939 if (InsMapSeen.count((*I)->getSelector()))
940 continue;
941 InsMapSeen.insert((*I)->getSelector());
942 if (!(*I)->isSynthesized() &&
943 !InsMap.count((*I)->getSelector())) {
944 if (ImmediateClass)
945 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
946 continue;
947 }
948 else {
949 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000950 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000951 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000952 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000953 assert(IntfMethodDecl &&
954 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
955 // ImpMethodDecl may be null as in a @dynamic property.
956 if (ImpMethodDecl)
957 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
958 }
959 }
960
961 // Check and see if class methods in class interface have been
962 // implemented in the implementation class. If so, their types match.
963 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000964 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000965 if (ClsMapSeen.count((*I)->getSelector()))
966 continue;
967 ClsMapSeen.insert((*I)->getSelector());
968 if (!ClsMap.count((*I)->getSelector())) {
969 if (ImmediateClass)
970 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
971 }
972 else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000973 ObjCMethodDecl *ImpMethodDecl =
974 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000975 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000976 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000977 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
978 }
979 }
980 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
981 // Check for any implementation of a methods declared in protocol.
982 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
983 E = I->protocol_end(); PI != E; ++PI)
984 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
985 IMPDecl,
986 (*PI), IncompleteImpl, false);
987 if (I->getSuperClass())
988 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
989 IMPDecl,
990 I->getSuperClass(), IncompleteImpl, false);
991 }
992}
993
Chris Lattnercddc8882009-03-01 00:56:52 +0000994void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
995 ObjCContainerDecl* CDecl,
996 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000997 llvm::DenseSet<Selector> InsMap;
998 // Check and see if instance methods in class interface have been
999 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001000 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001001 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001002 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +00001003
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001004 // Check and see if properties declared in the interface have either 1)
1005 // an implementation or 2) there is a @synthesize/@dynamic implementation
1006 // of the property in the @implementation.
1007 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001008 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1009 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001010 ObjCPropertyDecl *Prop = (*P);
1011 if (Prop->isInvalidDecl())
1012 continue;
1013 ObjCPropertyImplDecl *PI = 0;
1014 // Is there a matching propery synthesize/dynamic?
Douglas Gregor653f1b12009-04-23 01:02:12 +00001015 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001016 I = IMPDecl->propimpl_begin(),
1017 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001018 if ((*I)->getPropertyDecl() == Prop) {
1019 PI = (*I);
1020 break;
1021 }
1022 if (PI)
1023 continue;
1024 if (!InsMap.count(Prop->getGetterName())) {
1025 Diag(Prop->getLocation(),
1026 diag::warn_setter_getter_impl_required)
1027 << Prop->getDeclName() << Prop->getGetterName();
1028 Diag(IMPDecl->getLocation(),
1029 diag::note_property_impl_required);
1030 }
1031
1032 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1033 Diag(Prop->getLocation(),
1034 diag::warn_setter_getter_impl_required)
1035 << Prop->getDeclName() << Prop->getSetterName();
1036 Diag(IMPDecl->getLocation(),
1037 diag::note_property_impl_required);
1038 }
1039 }
1040
Chris Lattner4d391482007-12-12 07:09:47 +00001041 llvm::DenseSet<Selector> ClsMap;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001042 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001043 I = IMPDecl->classmeth_begin(),
1044 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001045 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +00001046
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001047 // Check for type conflict of methods declared in a class/protocol and
1048 // its implementation; if any.
1049 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1050 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1051 IMPDecl, CDecl,
1052 IncompleteImpl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001053
1054 // Check the protocol list for unimplemented methods in the @implementation
1055 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001056 // Check and see if class methods in class interface have been
1057 // implemented in the implementation class.
1058
Chris Lattnercddc8882009-03-01 00:56:52 +00001059 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001060 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001061 E = I->protocol_end(); PI != E; ++PI)
1062 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1063 InsMap, ClsMap, I);
1064 // Check class extensions (unnamed categories)
1065 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1066 Categories; Categories = Categories->getNextClassCategory()) {
1067 if (!Categories->getIdentifier()) {
1068 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1069 break;
1070 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001071 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001072 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1073 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1074 E = C->protocol_end(); PI != E; ++PI)
1075 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1076 InsMap, ClsMap, C->getClassInterface());
1077 } else
1078 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001079}
1080
1081/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001082Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001083Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001084 IdentifierInfo **IdentList,
1085 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001086 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +00001087
1088 for (unsigned i = 0; i != NumElts; ++i) {
1089 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001090 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001091 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001092 // Maybe we will complain about the shadowed template parameter.
1093 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1094 // Just pretend that we didn't see the previous declaration.
1095 PrevDecl = 0;
1096 }
1097
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001098 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001099 // GCC apparently allows the following idiom:
1100 //
1101 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1102 // @class XCElementToggler;
1103 //
1104 // FIXME: Make an extension?
1105 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1106 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001107 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001108 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +00001109 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001110 else if (TDD) {
1111 // a forward class declaration matching a typedef name of a class
1112 // refers to the underlying class.
1113 if (ObjCInterfaceType * OI =
1114 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1115 PrevDecl = OI->getDecl();
1116 }
Chris Lattner4d391482007-12-12 07:09:47 +00001117 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001118 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001119 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +00001120 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1121 IdentList[i], SourceLocation(), true);
Steve Naroff8f06f842009-04-23 16:00:56 +00001122 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +00001123 }
1124
1125 Interfaces.push_back(IDecl);
1126 }
1127
Douglas Gregord0434102009-01-09 00:49:46 +00001128 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001129 &Interfaces[0],
1130 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001131 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001132 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001133 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001134}
1135
1136
1137/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1138/// returns true, or false, accordingly.
1139/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001140bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001141 const ObjCMethodDecl *PrevMethod,
1142 bool matchBasedOnSizeAndAlignment) {
1143 QualType T1 = Context.getCanonicalType(Method->getResultType());
1144 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1145
1146 if (T1 != T2) {
1147 // The result types are different.
1148 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001149 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001150 // Incomplete types don't have a size and alignment.
1151 if (T1->isIncompleteType() || T2->isIncompleteType())
1152 return false;
1153 // Check is based on size and alignment.
1154 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1155 return false;
1156 }
Chris Lattner89951a82009-02-20 18:43:26 +00001157
1158 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1159 E = Method->param_end();
1160 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1161
1162 for (; ParamI != E; ++ParamI, ++PrevI) {
1163 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1164 T1 = Context.getCanonicalType((*ParamI)->getType());
1165 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001166 if (T1 != T2) {
1167 // The result types are different.
1168 if (!matchBasedOnSizeAndAlignment)
1169 return false;
1170 // Incomplete types don't have a size and alignment.
1171 if (T1->isIncompleteType() || T2->isIncompleteType())
1172 return false;
1173 // Check is based on size and alignment.
1174 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1175 return false;
1176 }
Chris Lattner4d391482007-12-12 07:09:47 +00001177 }
1178 return true;
1179}
1180
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001181/// \brief Read the contents of the instance and factory method pools
1182/// for a given selector from external storage.
1183///
1184/// This routine should only be called once, when neither the instance
1185/// nor the factory method pool has an entry for this selector.
1186Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
1187 bool isInstance) {
1188 assert(ExternalSource && "We need an external AST source");
1189 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1190 "Selector data already loaded into the instance method pool");
1191 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1192 "Selector data already loaded into the factory method pool");
1193
1194 // Read the method list from the external source.
1195 std::pair<ObjCMethodList, ObjCMethodList> Methods
1196 = ExternalSource->ReadMethodPool(Sel);
1197
1198 if (isInstance) {
1199 if (Methods.second.Method)
1200 FactoryMethodPool[Sel] = Methods.second;
1201 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
1202 }
1203
1204 if (Methods.first.Method)
1205 InstanceMethodPool[Sel] = Methods.first;
1206
1207 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1208}
1209
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001210void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001211 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1212 = InstanceMethodPool.find(Method->getSelector());
1213 if (Pos == InstanceMethodPool.end()) {
1214 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1215 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1216 else
1217 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1218 ObjCMethodList())).first;
1219 }
1220
1221 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001222 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001223 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001224 Entry.Method = Method;
1225 Entry.Next = 0;
1226 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001227 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001228
1229 // We've seen a method with this name, see if we have already seen this type
1230 // signature.
1231 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1232 if (MatchTwoMethodDeclarations(Method, List->Method))
1233 return;
1234
1235 // We have a new signature for an existing method - add it.
1236 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1237 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001238}
1239
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001240// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001241ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1242 SourceRange R) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001243 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1244 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001245 if (Pos == InstanceMethodPool.end()) {
1246 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001247 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1248 else
1249 return 0;
1250 }
1251
1252 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001253 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001254
1255 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001256 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1257 // This checks if the methods differ by size & alignment.
1258 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1259 issueWarning = true;
1260 }
1261 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001262 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001263 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001264 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001265 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001266 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001267 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001268 }
1269 return MethList.Method;
1270}
1271
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001272void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001273 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1274 = FactoryMethodPool.find(Method->getSelector());
1275 if (Pos == FactoryMethodPool.end()) {
1276 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1277 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1278 else
1279 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1280 ObjCMethodList())).first;
1281 }
1282
1283 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001284 if (!FirstMethod.Method) {
1285 // Haven't seen a method with this selector name yet - add it.
1286 FirstMethod.Method = Method;
1287 FirstMethod.Next = 0;
1288 } else {
1289 // We've seen a method with this name, now check the type signature(s).
1290 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1291
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001292 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001293 Next = Next->Next)
1294 match = MatchTwoMethodDeclarations(Method, Next->Method);
1295
1296 if (!match) {
1297 // We have a new signature for an existing method - add it.
1298 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001299 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001300 FirstMethod.Next = OMI;
1301 }
1302 }
1303}
1304
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001305ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
1306 SourceRange R) {
1307 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1308 = FactoryMethodPool.find(Sel);
1309 if (Pos == FactoryMethodPool.end()) {
1310 if (ExternalSource && !InstanceMethodPool.count(Sel))
1311 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1312 else
1313 return 0;
1314 }
1315
1316 ObjCMethodList &MethList = Pos->second;
1317 bool issueWarning = false;
1318
1319 if (MethList.Method && MethList.Next) {
1320 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1321 // This checks if the methods differ by size & alignment.
1322 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1323 issueWarning = true;
1324 }
1325 if (issueWarning && (MethList.Method && MethList.Next)) {
1326 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1327 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1328 << MethList.Method->getSourceRange();
1329 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1330 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1331 << Next->Method->getSourceRange();
1332 }
1333 return MethList.Method;
1334}
1335
Steve Naroff0701bbb2009-01-08 17:28:14 +00001336/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1337/// have the property type and issue diagnostics if they don't.
1338/// Also synthesize a getter/setter method if none exist (and update the
1339/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1340/// methods is the "right" thing to do.
1341void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1342 ObjCContainerDecl *CD) {
1343 ObjCMethodDecl *GetterMethod, *SetterMethod;
1344
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001345 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1346 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001347 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1348 property->getLocation());
1349
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001350 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001351 if (Context.getCanonicalType(SetterMethod->getResultType())
1352 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001353 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001354 if (SetterMethod->param_size() != 1 ||
1355 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001356 Diag(property->getLocation(),
Fariborz Jahanian4c2743f2009-05-08 19:36:34 +00001357 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001358 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001359 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001360 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1361 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001362 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001363
1364 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001365 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001366 // FIXME: The synthesized property we set here is misleading. We almost always
1367 // synthesize these methods unless the user explicitly provided prototypes
1368 // (which is odd, but allowed). Sema should be typechecking that the
1369 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001370 if (!GetterMethod) {
1371 // No instance method of same name as property getter name was found.
1372 // Declare a getter method and add it to the list of methods
1373 // for this class.
1374 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1375 property->getLocation(), property->getGetterName(),
1376 property->getType(), CD, true, false, true,
1377 (property->getPropertyImplementation() ==
1378 ObjCPropertyDecl::Optional) ?
1379 ObjCMethodDecl::Optional :
1380 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001381 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001382 } else
1383 // A user declared getter will be synthesize when @synthesize of
1384 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001385 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001386 property->setGetterMethodDecl(GetterMethod);
1387
1388 // Skip setter if property is read-only.
1389 if (!property->isReadOnly()) {
1390 // Find the default setter and if one not found, add one.
1391 if (!SetterMethod) {
1392 // No instance method of same name as property setter name was found.
1393 // Declare a setter method and add it to the list of methods
1394 // for this class.
1395 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1396 property->getLocation(),
1397 property->getSetterName(),
1398 Context.VoidTy, CD, true, false, true,
1399 (property->getPropertyImplementation() ==
1400 ObjCPropertyDecl::Optional) ?
1401 ObjCMethodDecl::Optional :
1402 ObjCMethodDecl::Required);
1403 // Invent the arguments for the setter. We don't bother making a
1404 // nice name for the argument.
1405 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001406 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001407 property->getIdentifier(),
1408 property->getType(),
1409 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001410 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001411 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001412 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001413 } else
1414 // A user declared setter will be synthesize when @synthesize of
1415 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001416 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001417 property->setSetterMethodDecl(SetterMethod);
1418 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001419 // Add any synthesized methods to the global pool. This allows us to
1420 // handle the following, which is supported by GCC (and part of the design).
1421 //
1422 // @interface Foo
1423 // @property double bar;
1424 // @end
1425 //
1426 // void thisIsUnfortunate() {
1427 // id foo;
1428 // double bar = [foo bar];
1429 // }
1430 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001431 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001432 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001433 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001434 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001435}
1436
Steve Naroffa56f6162007-12-18 01:30:32 +00001437// Note: For class/category implemenations, allMethods/allProperties is
1438// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001439void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1440 DeclPtrTy *allMethods, unsigned allNum,
1441 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001442 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001443 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001444
Steve Naroffa56f6162007-12-18 01:30:32 +00001445 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1446 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001447 // should be true.
1448 if (!ClassDecl)
1449 return;
1450
Chris Lattner4d391482007-12-12 07:09:47 +00001451 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001452 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1453 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001454 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001455
Steve Naroff0701bbb2009-01-08 17:28:14 +00001456 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001457
1458 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1459 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1460 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1461
Chris Lattner4d391482007-12-12 07:09:47 +00001462 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001463 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001464 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001465
1466 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001467 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001468 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001469 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001470 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1471 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001472 if ((isInterfaceDeclKind && PrevMethod && !match)
1473 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001474 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001475 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001476 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001477 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001478 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001479 InsMap[Method->getSelector()] = Method;
1480 /// The following allows us to typecheck messages to "id".
1481 AddInstanceMethodToGlobalPool(Method);
1482 }
1483 }
1484 else {
1485 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001486 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001487 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1488 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001489 if ((isInterfaceDeclKind && PrevMethod && !match)
1490 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001491 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001492 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001493 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001494 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001495 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001496 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001497 /// The following allows us to typecheck messages to "Class".
1498 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001499 }
1500 }
1501 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001502 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001503 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001504 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001505 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001506 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001507 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001508 // Categories are used to extend the class by declaring new methods.
1509 // By the same token, they are also used to add new properties. No
1510 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001511
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001512 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001513 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001514 if (C->getIdentifier() == 0)
1515 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001516 }
Steve Naroff09c47192009-01-09 15:36:25 +00001517 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1518 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1519 // user-defined setter/getter. It also synthesizes setter/getter methods
1520 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001521 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1522 E = CDecl->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001523 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001524 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001525 CDecl->setAtEndLoc(AtEndLoc);
1526 }
1527 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001528 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001529 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001530 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001531 } else if (ObjCCategoryImplDecl* CatImplClass =
1532 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001533 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001534
Chris Lattner4d391482007-12-12 07:09:47 +00001535 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001536 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001537 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001538 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001539 Categories; Categories = Categories->getNextClassCategory()) {
1540 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001541 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001542 break;
1543 }
1544 }
1545 }
1546 }
Chris Lattner682bf922009-03-29 16:50:03 +00001547 if (isInterfaceDeclKind) {
1548 // Reject invalid vardecls.
1549 for (unsigned i = 0; i != tuvNum; i++) {
1550 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1551 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1552 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001553 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001554 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001555 }
Chris Lattner682bf922009-03-29 16:50:03 +00001556 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001557 }
Chris Lattner4d391482007-12-12 07:09:47 +00001558}
1559
1560
1561/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1562/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001563static Decl::ObjCDeclQualifier
1564CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1565 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1566 if (PQTVal & ObjCDeclSpec::DQ_In)
1567 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1568 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1569 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1570 if (PQTVal & ObjCDeclSpec::DQ_Out)
1571 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1572 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1573 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1574 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1575 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1576 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1577 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001578
1579 return ret;
1580}
1581
Chris Lattnerb28317a2009-03-28 19:18:32 +00001582Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001583 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001584 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001585 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001586 Selector Sel,
1587 // optional arguments. The number of types/arguments is obtained
1588 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001589 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001590 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001591 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1592 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001593 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001594
1595 // Make sure we can establish a context for the method.
1596 if (!ClassDecl) {
1597 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001598 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001599 }
Chris Lattner4d391482007-12-12 07:09:47 +00001600 QualType resultDeclType;
1601
Steve Naroffccef3712009-02-20 22:59:16 +00001602 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001603 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001604
1605 // Methods cannot return interface types. All ObjC objects are
1606 // passed by reference.
1607 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001608 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1609 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001610 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001611 }
1612 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001613 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001614
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001615 ObjCMethodDecl* ObjCMethod =
1616 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner32d3f9c2009-03-29 04:30:19 +00001617 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001618 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001619 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001620 MethodDeclKind == tok::objc_optional ?
1621 ObjCMethodDecl::Optional :
1622 ObjCMethodDecl::Required);
1623
Chris Lattner0ed844b2008-04-04 06:12:32 +00001624 llvm::SmallVector<ParmVarDecl*, 16> Params;
1625
Chris Lattner7db638d2009-04-11 19:42:43 +00001626 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001627 QualType ArgType, UnpromotedArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001628
Chris Lattnere294d3f2009-04-11 18:57:04 +00001629 if (ArgInfo[i].Type == 0) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001630 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001631 } else {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001632 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff6082c622008-12-09 19:36:17 +00001633 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001634 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001635 }
1636
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001637 ParmVarDecl* Param;
Chris Lattner2dd979f2009-04-11 19:08:56 +00001638 if (ArgType == UnpromotedArgType)
Chris Lattner7db638d2009-04-11 19:42:43 +00001639 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001640 ArgInfo[i].Name, ArgType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001641 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001642 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001643 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner7db638d2009-04-11 19:42:43 +00001644 ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001645 ArgInfo[i].Name, ArgType,
1646 UnpromotedArgType,
Douglas Gregor64650af2009-02-02 23:39:07 +00001647 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001648
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001649 if (ArgType->isObjCInterfaceType()) {
1650 Diag(ArgInfo[i].NameLoc,
1651 diag::err_object_cannot_be_passed_returned_by_value)
1652 << 1 << ArgType;
1653 Param->setInvalidDecl();
1654 }
1655
Chris Lattner0ed844b2008-04-04 06:12:32 +00001656 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001657 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001658
1659 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001660 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001661
Chris Lattner0ed844b2008-04-04 06:12:32 +00001662 Params.push_back(Param);
1663 }
1664
Jay Foadbeaaccd2009-05-21 09:52:38 +00001665 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001666 ObjCMethod->setObjCDeclQualifier(
1667 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1668 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001669
1670 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001671 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Ted Kremenekb27d1172009-04-30 18:41:06 +00001672
Chris Lattner4d391482007-12-12 07:09:47 +00001673 // For implementations (which can be very "coarse grain"), we add the
1674 // method now. This allows the AST to implement lookup methods that work
1675 // incrementally (without waiting until we parse the @end). It also allows
1676 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001677 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001678 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001679 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001680 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1681 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001682 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001683 PrevMethod = ImpDecl->getClassMethod(Sel);
1684 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001685 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001686 if (AttrList)
1687 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001688 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001689 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001690 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001691 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001692 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1693 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001694 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001695 PrevMethod = CatImpDecl->getClassMethod(Sel);
1696 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001697 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001698 if (AttrList)
1699 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001700 }
1701 if (PrevMethod) {
1702 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001703 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001704 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001705 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001706 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001707 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001708}
1709
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001710void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1711 SourceLocation Loc,
1712 unsigned &Attributes) {
1713 // FIXME: Improve the reported location.
1714
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001715 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001716 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001717 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1718 ObjCDeclSpec::DQ_PR_assign |
1719 ObjCDeclSpec::DQ_PR_copy |
1720 ObjCDeclSpec::DQ_PR_retain))) {
1721 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1722 "readwrite" :
1723 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1724 "assign" :
1725 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1726 "copy" : "retain";
1727
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001728 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001729 diag::err_objc_property_attr_mutually_exclusive :
1730 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001731 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001732 }
1733
1734 // Check for copy or retain on non-object types.
1735 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1736 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001737 Diag(Loc, diag::err_objc_property_requires_object)
1738 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001739 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1740 }
1741
1742 // Check for more than one of { assign, copy, retain }.
1743 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1744 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001745 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1746 << "assign" << "copy";
1747 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001748 }
1749 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001750 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1751 << "assign" << "retain";
1752 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001753 }
1754 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1755 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001756 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1757 << "copy" << "retain";
1758 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001759 }
1760 }
1761
1762 // Warn if user supplied no assignment attribute, property is
1763 // readwrite, and this is an object type.
1764 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1765 ObjCDeclSpec::DQ_PR_retain)) &&
1766 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1767 Context.isObjCObjectPointerType(PropertyTy)) {
1768 // Skip this warning in gc-only mode.
1769 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1770 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1771
1772 // If non-gc code warn that this is likely inappropriate.
1773 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1774 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1775
1776 // FIXME: Implement warning dependent on NSCopying being
1777 // implemented. See also:
1778 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1779 // (please trim this list while you are at it).
1780 }
Mike Stump046efd92009-05-07 23:06:50 +00001781
1782 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1783 && getLangOptions().getGCMode() == LangOptions::GCOnly
1784 && PropertyTy->isBlockPointerType())
1785 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001786}
1787
Chris Lattnerb28317a2009-03-28 19:18:32 +00001788Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1789 FieldDeclarator &FD,
1790 ObjCDeclSpec &ODS,
1791 Selector GetterSel,
1792 Selector SetterSel,
1793 DeclPtrTy ClassCategory,
1794 bool *isOverridingProperty,
1795 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001796 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001797 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1798 // default is readwrite!
1799 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1800 // property is defaulted to 'assign' if it is readwrite and is
1801 // not retain or copy
1802 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1803 (isReadWrite &&
1804 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1805 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1806 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001807 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001808 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001809 // May modify Attributes.
1810 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001811 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1812 if (!CDecl->getIdentifier()) {
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001813 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001814 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001815 if ((CCPrimary = CDecl->getClassInterface())) {
1816 // Find the property in continuation class's primary class only.
1817 ObjCPropertyDecl *PIDecl = 0;
1818 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001819 for (ObjCInterfaceDecl::prop_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001820 I = CCPrimary->prop_begin(), E = CCPrimary->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001821 I != E; ++I)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001822 if ((*I)->getIdentifier() == PropertyId) {
1823 PIDecl = *I;
1824 break;
1825 }
1826
1827 if (PIDecl) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001828 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001829 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001830 unsigned PIkind = PIDecl->getPropertyAttributes();
1831 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001832 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001833 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1834 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001835 PIDecl->makeitReadWriteAttribute();
1836 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1837 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1838 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1839 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1840 PIDecl->setSetterName(SetterSel);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001841 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001842 else
1843 Diag(AtLoc, diag::err_use_continuation_class)
1844 << CCPrimary->getDeclName();
1845 *isOverridingProperty = true;
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001846 // Make sure setter decl is synthesized, and added to primary
1847 // class's list.
1848 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001849 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001850 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001851 // No matching property found in the primary class. Just fall thru
1852 // and add property to continuation class's primary class.
1853 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001854 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001855 Diag(CDecl->getLocation(), diag::err_continuation_class);
1856 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001857 return DeclPtrTy();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001858 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001859 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001860
Steve Naroff93983f82009-01-11 12:47:58 +00001861 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1862 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001863 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1864 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001865 FD.D.getIdentifier(), T);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001866 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001867
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001868 if (T->isArrayType() || T->isFunctionType()) {
1869 Diag(AtLoc, diag::err_property_type) << T;
1870 PDecl->setInvalidDecl();
1871 }
1872
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001873 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001874
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001875 // Regardless of setter/getter attribute, we save the default getter/setter
1876 // selector names in anticipation of declaration of setter/getter methods.
1877 PDecl->setGetterName(GetterSel);
1878 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001879
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001880 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001881 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001882
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001883 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001884 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001885
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001886 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001887 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001888
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001889 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001890 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001891
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001892 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001893 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001894
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001895 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001896 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001897
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001898 if (isAssign)
1899 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1900
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001901 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001902 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001903
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001904 if (MethodImplKind == tok::objc_required)
1905 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1906 else if (MethodImplKind == tok::objc_optional)
1907 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001908 // A case of continuation class adding a new property in the class. This
1909 // is not what it was meant for. However, gcc supports it and so should we.
1910 // Make sure setter/getters are declared here.
1911 if (CCPrimary)
1912 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001913
Chris Lattnerb28317a2009-03-28 19:18:32 +00001914 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001915}
1916
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001917/// ActOnPropertyImplDecl - This routine performs semantic checks and
1918/// builds the AST node for a property implementation declaration; declared
1919/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001920///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001921Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1922 SourceLocation PropertyLoc,
1923 bool Synthesize,
1924 DeclPtrTy ClassCatImpDecl,
1925 IdentifierInfo *PropertyId,
1926 IdentifierInfo *PropertyIvar) {
1927 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001928 // Make sure we have a context for the property implementation declaration.
1929 if (!ClassImpDecl) {
1930 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001931 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001932 }
1933 ObjCPropertyDecl *property = 0;
1934 ObjCInterfaceDecl* IDecl = 0;
1935 // Find the class or category class where this property must have
1936 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001937 ObjCImplementationDecl *IC = 0;
1938 ObjCCategoryImplDecl* CatImplClass = 0;
1939 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001940 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001941 // We always synthesize an interface for an implementation
1942 // without an interface decl. So, IDecl is always non-zero.
1943 assert(IDecl &&
1944 "ActOnPropertyImplDecl - @implementation without @interface");
1945
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001946 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001947 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001948 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001949 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001950 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001951 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001952 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001953 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001954 if (Synthesize) {
1955 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001956 return DeclPtrTy();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001957 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001958 IDecl = CatImplClass->getClassInterface();
1959 if (!IDecl) {
1960 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001961 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001962 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001963 ObjCCategoryDecl *Category =
1964 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1965
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001966 // If category for this implementation not found, it is an error which
1967 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001968 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00001969 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001970 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001971 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001972 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001973 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001974 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001975 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001976 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001977 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001978 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001979 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001980 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001981 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001982 // Check that we have a valid, previously declared ivar for @synthesize
1983 if (Synthesize) {
1984 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001985 if (!PropertyIvar)
1986 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00001987 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001988 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001989 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001990 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001991 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00001992 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
1993 assert(EnclosingContext &&
1994 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
1995 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001996 PropertyIvar, PropType,
1997 ObjCIvarDecl::Public,
1998 (Expr *)0);
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00001999 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002000 IDecl->addDecl(Ivar);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002001 property->setPropertyIvarDecl(Ivar);
2002 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002003 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002004 // Note! I deliberately want it to fall thru so, we have a
2005 // a property implementation and to avoid future warnings.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002006 }
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002007 else if (getLangOptions().ObjCNonFragileABI &&
Fariborz Jahanian6e5201b2009-04-29 21:45:02 +00002008 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002009 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002010 << property->getDeclName() << Ivar->getDeclName()
2011 << ClassDeclared->getDeclName();
2012 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2013 << Ivar << Ivar->getNameAsCString();
2014 // Note! I deliberately want it to fall thru so more errors are caught.
2015 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002016 QualType IvarType = Context.getCanonicalType(Ivar->getType());
2017
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002018 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002019 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002020 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002021 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002022 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002023 // Note! I deliberately want it to fall thru so, we have a
2024 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002025 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002026
2027 // FIXME! Rules for properties are somewhat different that those
2028 // for assignments. Use a new routine to consolidate all cases;
2029 // specifically for property redeclarations as well as for ivars.
2030 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2031 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
2032 if (lhsType != rhsType &&
2033 lhsType->isArithmeticType()) {
2034 Diag(PropertyLoc, diag::error_property_ivar_type)
2035 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002036 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002037 }
2038 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002039 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2040 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002041 Diag(PropertyLoc, diag::error_weak_property)
2042 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002043 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002044 }
2045 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002046 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2047 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002048 Diag(PropertyLoc, diag::error_strong_property)
2049 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002050 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002051 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002052 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002053 } else if (PropertyIvar)
2054 // @dynamic
2055 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002056 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002057 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00002058 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2059 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002060 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002061 ObjCPropertyImplDecl::Synthesize
2062 : ObjCPropertyImplDecl::Dynamic),
2063 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002064 if (IC) {
2065 if (Synthesize)
2066 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002067 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002068 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2069 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2070 << PropertyIvar;
2071 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2072 }
2073
Douglas Gregor653f1b12009-04-23 01:02:12 +00002074 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002075 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002076 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2077 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002078 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002079 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002080 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002081 }
2082 else {
2083 if (Synthesize)
2084 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002085 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002086 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2087 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2088 << PropertyIvar;
2089 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2090 }
2091
2092 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002093 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002094 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2095 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002096 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002097 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002098 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002099 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002100
Chris Lattnerb28317a2009-03-28 19:18:32 +00002101 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002102}
Anders Carlsson15281452008-11-04 16:57:32 +00002103
Chris Lattnercc98eac2008-12-17 07:13:27 +00002104bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002105 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002106 return false;
2107
2108 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2109 D->setInvalidDecl();
2110
2111 return true;
2112}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002113
Chris Lattnercc98eac2008-12-17 07:13:27 +00002114/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2115/// instance variables of ClassName into Decls.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002116void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002117 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002118 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002119 // Check that ClassName is a valid class
2120 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2121 if (!Class) {
2122 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2123 return;
2124 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002125 if (LangOpts.ObjCNonFragileABI) {
2126 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2127 return;
2128 }
2129
Chris Lattnercc98eac2008-12-17 07:13:27 +00002130 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002131 llvm::SmallVector<FieldDecl*, 32> RecFields;
2132 Context.CollectObjCIvars(Class, RecFields);
2133 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2134 for (unsigned i = 0; i < RecFields.size(); i++) {
2135 FieldDecl* ID = RecFields[i];
2136 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2137 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2138 ID->getIdentifier(), ID->getType(),
2139 ID->getBitWidth());
2140 Decls.push_back(Sema::DeclPtrTy::make(FD));
2141 }
Chris Lattnercc98eac2008-12-17 07:13:27 +00002142
2143 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002144 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002145 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002146 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002147 if (getLangOptions().CPlusPlus)
2148 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002149 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002150 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002151 }
2152}
2153