blob: d854f0b50c6d5fb3e6ec7d2816f9f3dec1bcfabe [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)
119 ProcessDeclAttributeList(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)
285 ProcessDeclAttributeList(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)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000382 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
383 E = SDecl->prop_end(Context); 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?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000386 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
387 E = IDecl->prop_end(Context); 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");
Douglas Gregor6ab35242009-04-09 21:40:53 +0000407 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
408 E = PDecl->prop_end(Context); 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?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000412 for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000413 CP != CE; ++CP)
414 if ((*CP)->getIdentifier() == Pr->getIdentifier())
415 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000416 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000417 // Property protocol already exist in class. Diagnose any mismatch.
418 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
419 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000420 return;
421 }
Douglas Gregor6ab35242009-04-09 21:40:53 +0000422 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
423 E = PDecl->prop_end(Context); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000424 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000425 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000426 // Is this property already in class's list of properties?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000427 for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context);
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000428 CP != CE; ++CP)
429 if ((*CP)->getIdentifier() == Pr->getIdentifier())
430 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000431 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000432 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000433 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000434 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000435}
436
437/// MergeProtocolPropertiesIntoClass - This routine merges properties
438/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000439/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000440///
Chris Lattner70f19542009-02-16 21:26:43 +0000441void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000442 DeclPtrTy MergeItsProtocols) {
443 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000444 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
445
446 if (!IDecl) {
447 // Category
448 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
449 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
450 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
451 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
452 E = MDecl->protocol_end(); P != E; ++P)
453 // Merge properties of category (*P) into IDECL's
454 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
455
456 // Go thru the list of protocols for this category and recursively merge
457 // their properties into this class as well.
458 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
459 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000460 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000461 } else {
462 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
463 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
464 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000465 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000466 }
467 return;
468 }
469
Chris Lattnerb752f282008-07-21 07:06:49 +0000470 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000471 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
472 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000473 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000474 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
475
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000476 // Go thru the list of protocols for this class and recursively merge
477 // their properties into this class as well.
478 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
479 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000480 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000481 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000482 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
483 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
484 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000485 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000486 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000487}
488
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000489/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000490/// a class method in its extension.
491///
492void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
493 ObjCInterfaceDecl *ID) {
494 if (!ID)
495 return; // Possibly due to previous error
496
497 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000498 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
499 e = ID->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000500 ObjCMethodDecl *MD = *i;
501 MethodMap[MD->getSelector()] = MD;
502 }
503
504 if (MethodMap.empty())
505 return;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000506 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
507 e = CAT->meth_end(Context); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000508 ObjCMethodDecl *Method = *i;
509 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
510 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
511 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
512 << Method->getDeclName();
513 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
514 }
515 }
516}
517
Chris Lattner58fe03b2009-04-12 08:43:13 +0000518/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000519Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000520Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000521 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000522 unsigned NumElts,
523 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000524 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000525
526 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000527 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000528 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000529 if (PDecl == 0) { // Not already seen?
530 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
531 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000532 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000533 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000534 if (attrList)
535 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000536 Protocols.push_back(PDecl);
537 }
Anders Carlsson15281452008-11-04 16:57:32 +0000538
539 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000540 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000541 &Protocols[0], Protocols.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000542 CurContext->addDecl(Context, PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000543 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000544 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000545}
546
Chris Lattnerb28317a2009-03-28 19:18:32 +0000547Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000548ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
549 IdentifierInfo *ClassName, SourceLocation ClassLoc,
550 IdentifierInfo *CategoryName,
551 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000552 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000553 unsigned NumProtoRefs,
554 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000555 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000556 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
557 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000558 CurContext->addDecl(Context, CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000559
560 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000561 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000562 if (!IDecl || IDecl->isForwardDecl()) {
563 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000564 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000565 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000566 }
Chris Lattner4d391482007-12-12 07:09:47 +0000567
Chris Lattner70f19542009-02-16 21:26:43 +0000568 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000569
570 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000571 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000572
573 /// Check for duplicate interface declaration for this category
574 ObjCCategoryDecl *CDeclChain;
575 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
576 CDeclChain = CDeclChain->getNextClassCategory()) {
577 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
578 Diag(CategoryLoc, diag::warn_dup_category_def)
579 << ClassName << CategoryName;
580 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
581 break;
582 }
583 }
584 if (!CDeclChain)
585 CDecl->insertNextClassCategory();
586
Chris Lattner4d391482007-12-12 07:09:47 +0000587 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000588 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000589 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000590 }
Anders Carlsson15281452008-11-04 16:57:32 +0000591
592 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000593 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000594}
595
596/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000597/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000598/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000599Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000600 SourceLocation AtCatImplLoc,
601 IdentifierInfo *ClassName, SourceLocation ClassLoc,
602 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000603 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000604 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000605 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
606 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000607 /// Check that class of this category is already completely declared.
608 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000609 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000610
Douglas Gregord0434102009-01-09 00:49:46 +0000611 // FIXME: PushOnScopeChains?
Douglas Gregor6ab35242009-04-09 21:40:53 +0000612 CurContext->addDecl(Context, CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000613
Chris Lattner4d391482007-12-12 07:09:47 +0000614 /// TODO: Check that CatName, category name, is not used in another
615 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000616 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000617
618 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000619 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000620}
621
Chris Lattnerb28317a2009-03-28 19:18:32 +0000622Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000623 SourceLocation AtClassImplLoc,
624 IdentifierInfo *ClassName, SourceLocation ClassLoc,
625 IdentifierInfo *SuperClassname,
626 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000627 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000628 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000629 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000630 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000631 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000632 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000633 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000634 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000635 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000636 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000637 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000638 IDecl = 0;
639 }
Chris Lattner4d391482007-12-12 07:09:47 +0000640 }
641
642 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000643 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000644 if (SuperClassname) {
645 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000646 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000647 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000648 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
649 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000650 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000651 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000652 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000653 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000654 Diag(SuperClassLoc, diag::err_undef_superclass)
655 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000656 else if (IDecl && IDecl->getSuperClass() != SDecl) {
657 // This implementation and its interface do not have the same
658 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000659 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000660 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000661 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000662 }
663 }
664 }
665
666 if (!IDecl) {
667 // Legacy case of @implementation with no corresponding @interface.
668 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000669
670 // FIXME: Do we support attributes on the @implementation? If so
671 // we should copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000672 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
673 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000674 IDecl->setSuperClass(SDecl);
675 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000676
677 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000678 } else {
679 // Mark the interface as being completed, even if it was just as
680 // @class ....;
681 // declaration; the user cannot reopen it.
682 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000683 }
684
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000685 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000686 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000687 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000688
Anders Carlsson15281452008-11-04 16:57:32 +0000689 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000690 return DeclPtrTy::make(IMPDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000691
Chris Lattner4d391482007-12-12 07:09:47 +0000692 // Check that there is no duplicate implementation of this class.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000693 if (LookupObjCImplementation(ClassName))
Chris Lattner75c9cae2008-03-16 20:53:07 +0000694 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000695 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000696 else // add it to the list.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000697 PushOnScopeChains(IMPDecl, TUScope);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000698 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000699}
700
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000701void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
702 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000703 SourceLocation RBrace) {
704 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000705 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000706 if (!IDecl)
707 return;
708 /// Check case of non-existing @interface decl.
709 /// (legacy objective-c @implementation decl without an @interface decl).
710 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000711 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000712 IDecl->setIVarList(ivars, numIvars, Context);
713 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000714 return;
715 }
716 // If implementation has empty ivar list, just return.
717 if (numIvars == 0)
718 return;
719
720 assert(ivars && "missing @implementation ivars");
721
722 // Check interface's Ivar list against those in the implementation.
723 // names and types must match.
724 //
Chris Lattner4d391482007-12-12 07:09:47 +0000725 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000726 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000727 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
728 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000729 ObjCIvarDecl* ImplIvar = ivars[j++];
730 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000731 assert (ImplIvar && "missing implementation ivar");
732 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000733
734 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000735 if (Context.getCanonicalType(ImplIvar->getType()) !=
736 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000737 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000738 << ImplIvar->getIdentifier()
739 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000740 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000741 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
742 Expr *ImplBitWidth = ImplIvar->getBitWidth();
743 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000744 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
745 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000746 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
747 << ImplIvar->getIdentifier();
748 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
749 }
750 }
751 // Make sure the names are identical.
752 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000753 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000754 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000755 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000756 }
757 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000758 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000759
760 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000761 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000762 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000763 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000764}
765
Steve Naroff3c2eb662008-02-10 21:38:56 +0000766void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
767 bool &IncompleteImpl) {
768 if (!IncompleteImpl) {
769 Diag(ImpLoc, diag::warn_incomplete_impl);
770 IncompleteImpl = true;
771 }
Chris Lattner08631c52008-11-23 21:45:46 +0000772 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000773}
774
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000775void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
776 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000777 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Chris Lattner3aff9192009-04-11 19:58:42 +0000778 ImpMethodDecl->getResultType())) {
779 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) {
788 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()))
789 continue;
790
791 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
792 << ImpMethodDecl->getDeclName() << (*IF)->getType()
793 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000794 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000795 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000796}
797
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000798/// isPropertyReadonly - Return true if property is readonly, by searching
799/// for the property in the class and in its categories and implementations
800///
801bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000802 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000803 // by far the most common case.
804 if (!PDecl->isReadOnly())
805 return false;
806 // Even if property is ready only, if interface has a user defined setter,
807 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000808 if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000809 return false;
810
811 // Main class has the property as 'readonly'. Must search
812 // through the category list to see if the property's
813 // attribute has been over-ridden to 'readwrite'.
814 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
815 Category; Category = Category->getNextClassCategory()) {
816 // Even if property is ready only, if a category has a user defined setter,
817 // it is not considered read only.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000818 if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000819 return false;
820 ObjCPropertyDecl *P =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000821 Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000822 if (P && !P->isReadOnly())
823 return false;
824 }
825
826 // Also, check for definition of a setter method in the implementation if
827 // all else failed.
828 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
829 if (ObjCImplementationDecl *IMD =
830 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000831 if (IMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000832 return false;
833 }
834 else if (ObjCCategoryImplDecl *CIMD =
835 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Douglas Gregor653f1b12009-04-23 01:02:12 +0000836 if (CIMD->getInstanceMethod(Context, PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000837 return false;
838 }
839 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000840 // Lastly, look through the implementation (if one is in scope).
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000841 if (ObjCImplementationDecl *ImpDecl
842 = LookupObjCImplementation(IDecl->getIdentifier()))
Douglas Gregor653f1b12009-04-23 01:02:12 +0000843 if (ImpDecl->getInstanceMethod(Context, PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000844 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000845 // If all fails, look at the super class.
846 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
847 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000848 return true;
849}
850
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000851/// FIXME: Type hierarchies in Objective-C can be deep. We could most
852/// likely improve the efficiency of selector lookups and type
853/// checking by associating with each protocol / interface / category
854/// the flattened instance tables. If we used an immutable set to keep
855/// the table then it wouldn't add significant memory cost and it
856/// would be handy for lookups.
857
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();
867
868 // If a method lookup fails locally we still need to look and see if
869 // the method was implemented by a base class or an inherited
870 // protocol. This lookup is slow, but occurs rarely in correct code
871 // and otherwise would terminate in a warning.
872
Chris Lattner4d391482007-12-12 07:09:47 +0000873 // check unimplemented instance methods.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000874 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context),
875 E = PDecl->instmeth_end(Context); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000876 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000877 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000878 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000879 (!Super ||
880 !Super->lookupInstanceMethod(Context, method->getSelector()))) {
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000881 // Ugly, but necessary. Method declared in protcol might have
882 // have been synthesized due to a property declared in the class which
883 // uses the protocol.
884 ObjCMethodDecl *MethodInClass =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000885 IDecl->lookupInstanceMethod(Context, method->getSelector());
Fariborz Jahanianb072b712009-04-03 21:51:32 +0000886 if (!MethodInClass || !MethodInClass->isSynthesized())
887 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
888 }
Chris Lattner4d391482007-12-12 07:09:47 +0000889 }
890 // check unimplemented class methods
Douglas Gregor6ab35242009-04-09 21:40:53 +0000891 for (ObjCProtocolDecl::classmeth_iterator
892 I = PDecl->classmeth_begin(Context),
893 E = PDecl->classmeth_end(Context);
894 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000895 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000896 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
897 !ClsMap.count(method->getSelector()) &&
Douglas Gregor6ab35242009-04-09 21:40:53 +0000898 (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000899 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000900 }
Chris Lattner780f3292008-07-21 21:32:27 +0000901 // Check on this protocols's referenced protocols, recursively.
902 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
903 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000904 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000905}
906
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000907/// MatchAllMethodDeclarations - Check methods declaraed in interface or
908/// or protocol against those declared in their implementations.
909///
910void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
911 const llvm::DenseSet<Selector> &ClsMap,
912 llvm::DenseSet<Selector> &InsMapSeen,
913 llvm::DenseSet<Selector> &ClsMapSeen,
914 ObjCImplDecl* IMPDecl,
915 ObjCContainerDecl* CDecl,
916 bool &IncompleteImpl,
917 bool ImmediateClass)
918{
919 // Check and see if instance methods in class interface have been
920 // implemented in the implementation class. If so, their types match.
921 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
922 E = CDecl->instmeth_end(Context); I != E; ++I) {
923 if (InsMapSeen.count((*I)->getSelector()))
924 continue;
925 InsMapSeen.insert((*I)->getSelector());
926 if (!(*I)->isSynthesized() &&
927 !InsMap.count((*I)->getSelector())) {
928 if (ImmediateClass)
929 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
930 continue;
931 }
932 else {
933 ObjCMethodDecl *ImpMethodDecl =
934 IMPDecl->getInstanceMethod(Context, (*I)->getSelector());
935 ObjCMethodDecl *IntfMethodDecl =
936 CDecl->getInstanceMethod(Context, (*I)->getSelector());
937 assert(IntfMethodDecl &&
938 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
939 // ImpMethodDecl may be null as in a @dynamic property.
940 if (ImpMethodDecl)
941 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
942 }
943 }
944
945 // Check and see if class methods in class interface have been
946 // implemented in the implementation class. If so, their types match.
947 for (ObjCInterfaceDecl::classmeth_iterator
948 I = CDecl->classmeth_begin(Context),
949 E = CDecl->classmeth_end(Context);
950 I != E; ++I) {
951 if (ClsMapSeen.count((*I)->getSelector()))
952 continue;
953 ClsMapSeen.insert((*I)->getSelector());
954 if (!ClsMap.count((*I)->getSelector())) {
955 if (ImmediateClass)
956 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
957 }
958 else {
959 ObjCMethodDecl *ImpMethodDecl =
960 IMPDecl->getClassMethod(Context, (*I)->getSelector());
961 ObjCMethodDecl *IntfMethodDecl =
962 CDecl->getClassMethod(Context, (*I)->getSelector());
963 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
964 }
965 }
966 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
967 // Check for any implementation of a methods declared in protocol.
968 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
969 E = I->protocol_end(); PI != E; ++PI)
970 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
971 IMPDecl,
972 (*PI), IncompleteImpl, false);
973 if (I->getSuperClass())
974 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
975 IMPDecl,
976 I->getSuperClass(), IncompleteImpl, false);
977 }
978}
979
Chris Lattnercddc8882009-03-01 00:56:52 +0000980void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
981 ObjCContainerDecl* CDecl,
982 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000983 llvm::DenseSet<Selector> InsMap;
984 // Check and see if instance methods in class interface have been
985 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000986 for (ObjCImplementationDecl::instmeth_iterator
987 I = IMPDecl->instmeth_begin(Context),
988 E = IMPDecl->instmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000989 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000990
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000991 // Check and see if properties declared in the interface have either 1)
992 // an implementation or 2) there is a @synthesize/@dynamic implementation
993 // of the property in the @implementation.
994 if (isa<ObjCInterfaceDecl>(CDecl))
995 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(Context),
996 E = CDecl->prop_end(Context); P != E; ++P) {
997 ObjCPropertyDecl *Prop = (*P);
998 if (Prop->isInvalidDecl())
999 continue;
1000 ObjCPropertyImplDecl *PI = 0;
1001 // Is there a matching propery synthesize/dynamic?
Douglas Gregor653f1b12009-04-23 01:02:12 +00001002 for (ObjCImplDecl::propimpl_iterator
1003 I = IMPDecl->propimpl_begin(Context),
1004 EI = IMPDecl->propimpl_end(Context); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001005 if ((*I)->getPropertyDecl() == Prop) {
1006 PI = (*I);
1007 break;
1008 }
1009 if (PI)
1010 continue;
1011 if (!InsMap.count(Prop->getGetterName())) {
1012 Diag(Prop->getLocation(),
1013 diag::warn_setter_getter_impl_required)
1014 << Prop->getDeclName() << Prop->getGetterName();
1015 Diag(IMPDecl->getLocation(),
1016 diag::note_property_impl_required);
1017 }
1018
1019 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1020 Diag(Prop->getLocation(),
1021 diag::warn_setter_getter_impl_required)
1022 << Prop->getDeclName() << Prop->getSetterName();
1023 Diag(IMPDecl->getLocation(),
1024 diag::note_property_impl_required);
1025 }
1026 }
1027
Chris Lattner4d391482007-12-12 07:09:47 +00001028 llvm::DenseSet<Selector> ClsMap;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001029 for (ObjCImplementationDecl::classmeth_iterator
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001030 I = IMPDecl->classmeth_begin(Context),
1031 E = IMPDecl->classmeth_end(Context); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001032 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +00001033
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001034 // Check for type conflict of methods declared in a class/protocol and
1035 // its implementation; if any.
1036 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1037 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1038 IMPDecl, CDecl,
1039 IncompleteImpl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001040
1041 // Check the protocol list for unimplemented methods in the @implementation
1042 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001043 // Check and see if class methods in class interface have been
1044 // implemented in the implementation class.
1045
Chris Lattnercddc8882009-03-01 00:56:52 +00001046 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001047 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001048 E = I->protocol_end(); PI != E; ++PI)
1049 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1050 InsMap, ClsMap, I);
1051 // Check class extensions (unnamed categories)
1052 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1053 Categories; Categories = Categories->getNextClassCategory()) {
1054 if (!Categories->getIdentifier()) {
1055 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1056 break;
1057 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001058 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001059 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1060 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1061 E = C->protocol_end(); PI != E; ++PI)
1062 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1063 InsMap, ClsMap, C->getClassInterface());
1064 } else
1065 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001066}
1067
1068/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001069Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001070Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001071 IdentifierInfo **IdentList,
1072 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001073 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +00001074
1075 for (unsigned i = 0; i != NumElts; ++i) {
1076 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001077 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001078 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001079 // Maybe we will complain about the shadowed template parameter.
1080 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1081 // Just pretend that we didn't see the previous declaration.
1082 PrevDecl = 0;
1083 }
1084
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001085 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001086 // GCC apparently allows the following idiom:
1087 //
1088 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1089 // @class XCElementToggler;
1090 //
1091 // FIXME: Make an extension?
1092 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1093 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001094 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001095 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +00001096 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001097 else if (TDD) {
1098 // a forward class declaration matching a typedef name of a class
1099 // refers to the underlying class.
1100 if (ObjCInterfaceType * OI =
1101 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1102 PrevDecl = OI->getDecl();
1103 }
Chris Lattner4d391482007-12-12 07:09:47 +00001104 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001105 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001106 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +00001107 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1108 IdentList[i], SourceLocation(), true);
Steve Naroff8f06f842009-04-23 16:00:56 +00001109 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +00001110 }
1111
1112 Interfaces.push_back(IDecl);
1113 }
1114
Douglas Gregord0434102009-01-09 00:49:46 +00001115 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001116 &Interfaces[0],
1117 Interfaces.size());
Douglas Gregor6ab35242009-04-09 21:40:53 +00001118 CurContext->addDecl(Context, CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001119 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001120 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001121}
1122
1123
1124/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1125/// returns true, or false, accordingly.
1126/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001127bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001128 const ObjCMethodDecl *PrevMethod,
1129 bool matchBasedOnSizeAndAlignment) {
1130 QualType T1 = Context.getCanonicalType(Method->getResultType());
1131 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1132
1133 if (T1 != T2) {
1134 // The result types are different.
1135 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001136 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001137 // Incomplete types don't have a size and alignment.
1138 if (T1->isIncompleteType() || T2->isIncompleteType())
1139 return false;
1140 // Check is based on size and alignment.
1141 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1142 return false;
1143 }
Chris Lattner89951a82009-02-20 18:43:26 +00001144
1145 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1146 E = Method->param_end();
1147 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1148
1149 for (; ParamI != E; ++ParamI, ++PrevI) {
1150 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1151 T1 = Context.getCanonicalType((*ParamI)->getType());
1152 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001153 if (T1 != T2) {
1154 // The result types are different.
1155 if (!matchBasedOnSizeAndAlignment)
1156 return false;
1157 // Incomplete types don't have a size and alignment.
1158 if (T1->isIncompleteType() || T2->isIncompleteType())
1159 return false;
1160 // Check is based on size and alignment.
1161 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1162 return false;
1163 }
Chris Lattner4d391482007-12-12 07:09:47 +00001164 }
1165 return true;
1166}
1167
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001168/// \brief Read the contents of the instance and factory method pools
1169/// for a given selector from external storage.
1170///
1171/// This routine should only be called once, when neither the instance
1172/// nor the factory method pool has an entry for this selector.
1173Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
1174 bool isInstance) {
1175 assert(ExternalSource && "We need an external AST source");
1176 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1177 "Selector data already loaded into the instance method pool");
1178 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1179 "Selector data already loaded into the factory method pool");
1180
1181 // Read the method list from the external source.
1182 std::pair<ObjCMethodList, ObjCMethodList> Methods
1183 = ExternalSource->ReadMethodPool(Sel);
1184
1185 if (isInstance) {
1186 if (Methods.second.Method)
1187 FactoryMethodPool[Sel] = Methods.second;
1188 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
1189 }
1190
1191 if (Methods.first.Method)
1192 InstanceMethodPool[Sel] = Methods.first;
1193
1194 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1195}
1196
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001197void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001198 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1199 = InstanceMethodPool.find(Method->getSelector());
1200 if (Pos == InstanceMethodPool.end()) {
1201 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1202 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1203 else
1204 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1205 ObjCMethodList())).first;
1206 }
1207
1208 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001209 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001210 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001211 Entry.Method = Method;
1212 Entry.Next = 0;
1213 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001214 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001215
1216 // We've seen a method with this name, see if we have already seen this type
1217 // signature.
1218 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1219 if (MatchTwoMethodDeclarations(Method, List->Method))
1220 return;
1221
1222 // We have a new signature for an existing method - add it.
1223 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1224 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001225}
1226
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001227// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001228ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1229 SourceRange R) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001230 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1231 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001232 if (Pos == InstanceMethodPool.end()) {
1233 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001234 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1235 else
1236 return 0;
1237 }
1238
1239 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001240 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001241
1242 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001243 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1244 // This checks if the methods differ by size & alignment.
1245 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1246 issueWarning = true;
1247 }
1248 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001249 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001250 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001251 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001252 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001253 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001254 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001255 }
1256 return MethList.Method;
1257}
1258
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001259void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001260 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1261 = FactoryMethodPool.find(Method->getSelector());
1262 if (Pos == FactoryMethodPool.end()) {
1263 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1264 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1265 else
1266 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1267 ObjCMethodList())).first;
1268 }
1269
1270 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001271 if (!FirstMethod.Method) {
1272 // Haven't seen a method with this selector name yet - add it.
1273 FirstMethod.Method = Method;
1274 FirstMethod.Next = 0;
1275 } else {
1276 // We've seen a method with this name, now check the type signature(s).
1277 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1278
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001279 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001280 Next = Next->Next)
1281 match = MatchTwoMethodDeclarations(Method, Next->Method);
1282
1283 if (!match) {
1284 // We have a new signature for an existing method - add it.
1285 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001286 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001287 FirstMethod.Next = OMI;
1288 }
1289 }
1290}
1291
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001292ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
1293 SourceRange R) {
1294 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1295 = FactoryMethodPool.find(Sel);
1296 if (Pos == FactoryMethodPool.end()) {
1297 if (ExternalSource && !InstanceMethodPool.count(Sel))
1298 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1299 else
1300 return 0;
1301 }
1302
1303 ObjCMethodList &MethList = Pos->second;
1304 bool issueWarning = false;
1305
1306 if (MethList.Method && MethList.Next) {
1307 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1308 // This checks if the methods differ by size & alignment.
1309 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1310 issueWarning = true;
1311 }
1312 if (issueWarning && (MethList.Method && MethList.Next)) {
1313 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1314 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1315 << MethList.Method->getSourceRange();
1316 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1317 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1318 << Next->Method->getSourceRange();
1319 }
1320 return MethList.Method;
1321}
1322
Steve Naroff0701bbb2009-01-08 17:28:14 +00001323/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1324/// have the property type and issue diagnostics if they don't.
1325/// Also synthesize a getter/setter method if none exist (and update the
1326/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1327/// methods is the "right" thing to do.
1328void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1329 ObjCContainerDecl *CD) {
1330 ObjCMethodDecl *GetterMethod, *SetterMethod;
1331
Douglas Gregor6ab35242009-04-09 21:40:53 +00001332 GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());
1333 SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001334 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1335 property->getLocation());
1336
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001337 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001338 if (Context.getCanonicalType(SetterMethod->getResultType())
1339 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001340 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001341 if (SetterMethod->param_size() != 1 ||
1342 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001343 Diag(property->getLocation(),
Fariborz Jahanian4c2743f2009-05-08 19:36:34 +00001344 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001345 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001346 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001347 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1348 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001349 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001350
1351 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001352 // Find the default getter and if one not found, add one.
Steve Naroff4fb78c62009-01-08 20:17:34 +00001353 // FIXME: The synthesized property we set here is misleading. We
1354 // almost always synthesize these methods unless the user explicitly
1355 // provided prototypes (which is odd, but allowed). Sema should be
1356 // typechecking that the declarations jive in that situation (which
1357 // it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001358 if (!GetterMethod) {
1359 // No instance method of same name as property getter name was found.
1360 // Declare a getter method and add it to the list of methods
1361 // for this class.
1362 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1363 property->getLocation(), property->getGetterName(),
1364 property->getType(), CD, true, false, true,
1365 (property->getPropertyImplementation() ==
1366 ObjCPropertyDecl::Optional) ?
1367 ObjCMethodDecl::Optional :
1368 ObjCMethodDecl::Required);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001369 CD->addDecl(Context, GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001370 } else
1371 // A user declared getter will be synthesize when @synthesize of
1372 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001373 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001374 property->setGetterMethodDecl(GetterMethod);
1375
1376 // Skip setter if property is read-only.
1377 if (!property->isReadOnly()) {
1378 // Find the default setter and if one not found, add one.
1379 if (!SetterMethod) {
1380 // No instance method of same name as property setter name was found.
1381 // Declare a setter method and add it to the list of methods
1382 // for this class.
1383 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1384 property->getLocation(),
1385 property->getSetterName(),
1386 Context.VoidTy, CD, true, false, true,
1387 (property->getPropertyImplementation() ==
1388 ObjCPropertyDecl::Optional) ?
1389 ObjCMethodDecl::Optional :
1390 ObjCMethodDecl::Required);
1391 // Invent the arguments for the setter. We don't bother making a
1392 // nice name for the argument.
1393 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001394 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001395 property->getIdentifier(),
1396 property->getType(),
1397 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001398 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001399 SetterMethod->setMethodParams(Context, &Argument, 1);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001400 CD->addDecl(Context, SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001401 } else
1402 // A user declared setter will be synthesize when @synthesize of
1403 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001404 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001405 property->setSetterMethodDecl(SetterMethod);
1406 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001407 // Add any synthesized methods to the global pool. This allows us to
1408 // handle the following, which is supported by GCC (and part of the design).
1409 //
1410 // @interface Foo
1411 // @property double bar;
1412 // @end
1413 //
1414 // void thisIsUnfortunate() {
1415 // id foo;
1416 // double bar = [foo bar];
1417 // }
1418 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001419 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001420 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001421 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001422 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001423}
1424
Steve Naroffa56f6162007-12-18 01:30:32 +00001425// Note: For class/category implemenations, allMethods/allProperties is
1426// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001427void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1428 DeclPtrTy *allMethods, unsigned allNum,
1429 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001430 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001431 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001432
Steve Naroffa56f6162007-12-18 01:30:32 +00001433 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1434 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001435 // should be true.
1436 if (!ClassDecl)
1437 return;
1438
Chris Lattner4d391482007-12-12 07:09:47 +00001439 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001440 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1441 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001442 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001443
Steve Naroff0701bbb2009-01-08 17:28:14 +00001444 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001445
1446 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1447 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1448 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1449
Chris Lattner4d391482007-12-12 07:09:47 +00001450 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001451 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001452 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001453
1454 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001455 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001456 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001457 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001458 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1459 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001460 if ((isInterfaceDeclKind && PrevMethod && !match)
1461 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001462 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001463 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001464 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001465 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001466 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001467 InsMap[Method->getSelector()] = Method;
1468 /// The following allows us to typecheck messages to "id".
1469 AddInstanceMethodToGlobalPool(Method);
1470 }
1471 }
1472 else {
1473 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001474 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001475 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1476 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001477 if ((isInterfaceDeclKind && PrevMethod && !match)
1478 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001479 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001480 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001481 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001482 } else {
Douglas Gregor6ab35242009-04-09 21:40:53 +00001483 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001484 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001485 /// The following allows us to typecheck messages to "Class".
1486 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001487 }
1488 }
1489 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001490 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001491 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001492 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001493 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001494 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001495 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001496 // Categories are used to extend the class by declaring new methods.
1497 // By the same token, they are also used to add new properties. No
1498 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001499
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001500 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001501 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001502 if (C->getIdentifier() == 0)
1503 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001504 }
Steve Naroff09c47192009-01-09 15:36:25 +00001505 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1506 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1507 // user-defined setter/getter. It also synthesizes setter/getter methods
1508 // and adds them to the DeclContext and global method pools.
Douglas Gregor6ab35242009-04-09 21:40:53 +00001509 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
1510 E = CDecl->prop_end(Context);
1511 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001512 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001513 CDecl->setAtEndLoc(AtEndLoc);
1514 }
1515 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001516 IC->setLocEnd(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001517 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001518 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001519 } else if (ObjCCategoryImplDecl* CatImplClass =
1520 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001521 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001522
Chris Lattner4d391482007-12-12 07:09:47 +00001523 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001524 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001525 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001526 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001527 Categories; Categories = Categories->getNextClassCategory()) {
1528 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001529 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001530 break;
1531 }
1532 }
1533 }
1534 }
Chris Lattner682bf922009-03-29 16:50:03 +00001535 if (isInterfaceDeclKind) {
1536 // Reject invalid vardecls.
1537 for (unsigned i = 0; i != tuvNum; i++) {
1538 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1539 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1540 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001541 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001542 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001543 }
Chris Lattner682bf922009-03-29 16:50:03 +00001544 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001545 }
Chris Lattner4d391482007-12-12 07:09:47 +00001546}
1547
1548
1549/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1550/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001551static Decl::ObjCDeclQualifier
1552CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1553 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1554 if (PQTVal & ObjCDeclSpec::DQ_In)
1555 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1556 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1557 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1558 if (PQTVal & ObjCDeclSpec::DQ_Out)
1559 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1560 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1561 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1562 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1563 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1564 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1565 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001566
1567 return ret;
1568}
1569
Chris Lattnerb28317a2009-03-28 19:18:32 +00001570Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001571 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001572 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001573 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001574 Selector Sel,
1575 // optional arguments. The number of types/arguments is obtained
1576 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001577 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001578 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001579 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1580 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001581 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001582
1583 // Make sure we can establish a context for the method.
1584 if (!ClassDecl) {
1585 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001586 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001587 }
Chris Lattner4d391482007-12-12 07:09:47 +00001588 QualType resultDeclType;
1589
Steve Naroffccef3712009-02-20 22:59:16 +00001590 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001591 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001592
1593 // Methods cannot return interface types. All ObjC objects are
1594 // passed by reference.
1595 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001596 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1597 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001598 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001599 }
1600 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001601 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001602
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001603 ObjCMethodDecl* ObjCMethod =
1604 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner32d3f9c2009-03-29 04:30:19 +00001605 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001606 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001607 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001608 MethodDeclKind == tok::objc_optional ?
1609 ObjCMethodDecl::Optional :
1610 ObjCMethodDecl::Required);
1611
Chris Lattner0ed844b2008-04-04 06:12:32 +00001612 llvm::SmallVector<ParmVarDecl*, 16> Params;
1613
Chris Lattner7db638d2009-04-11 19:42:43 +00001614 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001615 QualType ArgType, UnpromotedArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001616
Chris Lattnere294d3f2009-04-11 18:57:04 +00001617 if (ArgInfo[i].Type == 0) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001618 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001619 } else {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001620 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff6082c622008-12-09 19:36:17 +00001621 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001622 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001623 }
1624
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001625 ParmVarDecl* Param;
Chris Lattner2dd979f2009-04-11 19:08:56 +00001626 if (ArgType == UnpromotedArgType)
Chris Lattner7db638d2009-04-11 19:42:43 +00001627 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001628 ArgInfo[i].Name, ArgType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001629 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001630 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001631 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner7db638d2009-04-11 19:42:43 +00001632 ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001633 ArgInfo[i].Name, ArgType,
1634 UnpromotedArgType,
Douglas Gregor64650af2009-02-02 23:39:07 +00001635 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001636
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001637 if (ArgType->isObjCInterfaceType()) {
1638 Diag(ArgInfo[i].NameLoc,
1639 diag::err_object_cannot_be_passed_returned_by_value)
1640 << 1 << ArgType;
1641 Param->setInvalidDecl();
1642 }
1643
Chris Lattner0ed844b2008-04-04 06:12:32 +00001644 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001645 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001646
1647 // Apply the attributes to the parameter.
1648 ProcessDeclAttributeList(Param, ArgInfo[i].ArgAttrs);
1649
Chris Lattner0ed844b2008-04-04 06:12:32 +00001650 Params.push_back(Param);
1651 }
1652
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001653 ObjCMethod->setMethodParams(Context, &Params[0], Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001654 ObjCMethod->setObjCDeclQualifier(
1655 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1656 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001657
1658 if (AttrList)
1659 ProcessDeclAttributeList(ObjCMethod, AttrList);
Ted Kremenekb27d1172009-04-30 18:41:06 +00001660
Chris Lattner4d391482007-12-12 07:09:47 +00001661 // For implementations (which can be very "coarse grain"), we add the
1662 // method now. This allows the AST to implement lookup methods that work
1663 // incrementally (without waiting until we parse the @end). It also allows
1664 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001665 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001666 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001667 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001668 PrevMethod = ImpDecl->getInstanceMethod(Context, Sel);
1669 ImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001670 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001671 PrevMethod = ImpDecl->getClassMethod(Context, Sel);
1672 ImpDecl->addClassMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001673 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001674 if (AttrList)
1675 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001676 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001677 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001678 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001679 if (MethodType == tok::minus) {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001680 PrevMethod = CatImpDecl->getInstanceMethod(Context, Sel);
1681 CatImpDecl->addInstanceMethod(Context, ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001682 } else {
Douglas Gregor653f1b12009-04-23 01:02:12 +00001683 PrevMethod = CatImpDecl->getClassMethod(Context, Sel);
1684 CatImpDecl->addClassMethod(Context, 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 }
1689 if (PrevMethod) {
1690 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001691 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001692 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001693 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001694 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001695 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001696}
1697
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001698void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1699 SourceLocation Loc,
1700 unsigned &Attributes) {
1701 // FIXME: Improve the reported location.
1702
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001703 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001704 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001705 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1706 ObjCDeclSpec::DQ_PR_assign |
1707 ObjCDeclSpec::DQ_PR_copy |
1708 ObjCDeclSpec::DQ_PR_retain))) {
1709 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1710 "readwrite" :
1711 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1712 "assign" :
1713 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1714 "copy" : "retain";
1715
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001716 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001717 diag::err_objc_property_attr_mutually_exclusive :
1718 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001719 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001720 }
1721
1722 // Check for copy or retain on non-object types.
1723 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1724 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001725 Diag(Loc, diag::err_objc_property_requires_object)
1726 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001727 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1728 }
1729
1730 // Check for more than one of { assign, copy, retain }.
1731 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1732 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001733 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1734 << "assign" << "copy";
1735 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001736 }
1737 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001738 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1739 << "assign" << "retain";
1740 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001741 }
1742 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1743 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001744 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1745 << "copy" << "retain";
1746 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001747 }
1748 }
1749
1750 // Warn if user supplied no assignment attribute, property is
1751 // readwrite, and this is an object type.
1752 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1753 ObjCDeclSpec::DQ_PR_retain)) &&
1754 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1755 Context.isObjCObjectPointerType(PropertyTy)) {
1756 // Skip this warning in gc-only mode.
1757 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1758 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1759
1760 // If non-gc code warn that this is likely inappropriate.
1761 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1762 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1763
1764 // FIXME: Implement warning dependent on NSCopying being
1765 // implemented. See also:
1766 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1767 // (please trim this list while you are at it).
1768 }
Mike Stump046efd92009-05-07 23:06:50 +00001769
1770 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1771 && getLangOptions().getGCMode() == LangOptions::GCOnly
1772 && PropertyTy->isBlockPointerType())
1773 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001774}
1775
Chris Lattnerb28317a2009-03-28 19:18:32 +00001776Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1777 FieldDeclarator &FD,
1778 ObjCDeclSpec &ODS,
1779 Selector GetterSel,
1780 Selector SetterSel,
1781 DeclPtrTy ClassCategory,
1782 bool *isOverridingProperty,
1783 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001784 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001785 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1786 // default is readwrite!
1787 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1788 // property is defaulted to 'assign' if it is readwrite and is
1789 // not retain or copy
1790 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1791 (isReadWrite &&
1792 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1793 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1794 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001795 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001796 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001797 // May modify Attributes.
1798 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001799 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1800 if (!CDecl->getIdentifier()) {
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001801 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001802 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001803 if ((CCPrimary = CDecl->getClassInterface())) {
1804 // Find the property in continuation class's primary class only.
1805 ObjCPropertyDecl *PIDecl = 0;
1806 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001807 for (ObjCInterfaceDecl::prop_iterator
1808 I = CCPrimary->prop_begin(Context),
1809 E = CCPrimary->prop_end(Context);
1810 I != E; ++I)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001811 if ((*I)->getIdentifier() == PropertyId) {
1812 PIDecl = *I;
1813 break;
1814 }
1815
1816 if (PIDecl) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001817 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001818 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001819 unsigned PIkind = PIDecl->getPropertyAttributes();
1820 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001821 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001822 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1823 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001824 PIDecl->makeitReadWriteAttribute();
1825 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1826 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1827 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1828 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1829 PIDecl->setSetterName(SetterSel);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001830 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001831 else
1832 Diag(AtLoc, diag::err_use_continuation_class)
1833 << CCPrimary->getDeclName();
1834 *isOverridingProperty = true;
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001835 // Make sure setter decl is synthesized, and added to primary
1836 // class's list.
1837 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001838 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001839 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001840 // No matching property found in the primary class. Just fall thru
1841 // and add property to continuation class's primary class.
1842 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001843 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001844 Diag(CDecl->getLocation(), diag::err_continuation_class);
1845 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001846 return DeclPtrTy();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001847 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001848 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001849
Steve Naroff93983f82009-01-11 12:47:58 +00001850 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1851 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001852 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1853 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001854 FD.D.getIdentifier(), T);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001855 DC->addDecl(Context, PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001856
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001857 if (T->isArrayType() || T->isFunctionType()) {
1858 Diag(AtLoc, diag::err_property_type) << T;
1859 PDecl->setInvalidDecl();
1860 }
1861
Chris Lattner97a58872009-02-16 18:32:47 +00001862 ProcessDeclAttributes(PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001863
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001864 // Regardless of setter/getter attribute, we save the default getter/setter
1865 // selector names in anticipation of declaration of setter/getter methods.
1866 PDecl->setGetterName(GetterSel);
1867 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001868
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001869 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001870 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001871
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001872 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001873 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001874
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001875 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001876 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001877
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001878 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001879 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001880
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001881 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001882 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001883
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001884 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001885 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001886
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001887 if (isAssign)
1888 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1889
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001890 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001891 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001892
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001893 if (MethodImplKind == tok::objc_required)
1894 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1895 else if (MethodImplKind == tok::objc_optional)
1896 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001897 // A case of continuation class adding a new property in the class. This
1898 // is not what it was meant for. However, gcc supports it and so should we.
1899 // Make sure setter/getters are declared here.
1900 if (CCPrimary)
1901 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001902
Chris Lattnerb28317a2009-03-28 19:18:32 +00001903 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001904}
1905
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001906/// ActOnPropertyImplDecl - This routine performs semantic checks and
1907/// builds the AST node for a property implementation declaration; declared
1908/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001909///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001910Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1911 SourceLocation PropertyLoc,
1912 bool Synthesize,
1913 DeclPtrTy ClassCatImpDecl,
1914 IdentifierInfo *PropertyId,
1915 IdentifierInfo *PropertyIvar) {
1916 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001917 // Make sure we have a context for the property implementation declaration.
1918 if (!ClassImpDecl) {
1919 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001920 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001921 }
1922 ObjCPropertyDecl *property = 0;
1923 ObjCInterfaceDecl* IDecl = 0;
1924 // Find the class or category class where this property must have
1925 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001926 ObjCImplementationDecl *IC = 0;
1927 ObjCCategoryImplDecl* CatImplClass = 0;
1928 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001929 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001930 // We always synthesize an interface for an implementation
1931 // without an interface decl. So, IDecl is always non-zero.
1932 assert(IDecl &&
1933 "ActOnPropertyImplDecl - @implementation without @interface");
1934
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001935 // Look for this property declaration in the @implementation's @interface
Douglas Gregor6ab35242009-04-09 21:40:53 +00001936 property = IDecl->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001937 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001938 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001939 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001940 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001941 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001942 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001943 if (Synthesize) {
1944 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001945 return DeclPtrTy();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001946 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001947 IDecl = CatImplClass->getClassInterface();
1948 if (!IDecl) {
1949 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001950 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001951 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001952 ObjCCategoryDecl *Category =
1953 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1954
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001955 // If category for this implementation not found, it is an error which
1956 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001957 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00001958 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001959 // Look for this property declaration in @implementation's category
Douglas Gregor6ab35242009-04-09 21:40:53 +00001960 property = Category->FindPropertyDeclaration(Context, PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001961 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001962 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001963 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001964 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001965 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001966 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001967 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001968 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001969 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001970 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001971 // Check that we have a valid, previously declared ivar for @synthesize
1972 if (Synthesize) {
1973 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001974 if (!PropertyIvar)
1975 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00001976 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001977 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001978 ObjCInterfaceDecl *ClassDeclared;
1979 Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001980 if (!Ivar) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001981 Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc,
1982 PropertyIvar, PropType,
1983 ObjCIvarDecl::Public,
1984 (Expr *)0);
1985 property->setPropertyIvarDecl(Ivar);
1986 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00001987 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001988 // Note! I deliberately want it to fall thru so, we have a
1989 // a property implementation and to avoid future warnings.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001990 }
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001991 else if (getLangOptions().ObjCNonFragileABI &&
Fariborz Jahanian6e5201b2009-04-29 21:45:02 +00001992 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00001993 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00001994 << property->getDeclName() << Ivar->getDeclName()
1995 << ClassDeclared->getDeclName();
1996 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1997 << Ivar << Ivar->getNameAsCString();
1998 // Note! I deliberately want it to fall thru so more errors are caught.
1999 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002000 QualType IvarType = Context.getCanonicalType(Ivar->getType());
2001
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002002 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002003 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002004 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002005 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002006 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002007 // Note! I deliberately want it to fall thru so, we have a
2008 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002009 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002010
2011 // FIXME! Rules for properties are somewhat different that those
2012 // for assignments. Use a new routine to consolidate all cases;
2013 // specifically for property redeclarations as well as for ivars.
2014 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2015 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
2016 if (lhsType != rhsType &&
2017 lhsType->isArithmeticType()) {
2018 Diag(PropertyLoc, diag::error_property_ivar_type)
2019 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002020 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002021 }
2022 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002023 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2024 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002025 Diag(PropertyLoc, diag::error_weak_property)
2026 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002027 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002028 }
2029 if ((Context.isObjCObjectPointerType(property->getType()) ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002030 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2031 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002032 Diag(PropertyLoc, diag::error_strong_property)
2033 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002034 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002035 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002036 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002037 } else if (PropertyIvar)
2038 // @dynamic
2039 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002040 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002041 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00002042 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2043 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002044 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002045 ObjCPropertyImplDecl::Synthesize
2046 : ObjCPropertyImplDecl::Dynamic),
2047 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002048 if (IC) {
2049 if (Synthesize)
2050 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00002051 IC->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002052 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2053 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2054 << PropertyIvar;
2055 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2056 }
2057
Douglas Gregor653f1b12009-04-23 01:02:12 +00002058 if (ObjCPropertyImplDecl *PPIDecl
2059 = IC->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002060 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2061 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002062 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002063 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00002064 IC->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002065 }
2066 else {
2067 if (Synthesize)
2068 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00002069 CatImplClass->FindPropertyImplIvarDecl(Context, PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002070 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2071 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2072 << PropertyIvar;
2073 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2074 }
2075
2076 if (ObjCPropertyImplDecl *PPIDecl =
Douglas Gregor653f1b12009-04-23 01:02:12 +00002077 CatImplClass->FindPropertyImplDecl(Context, PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002078 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2079 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002080 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002081 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00002082 CatImplClass->addPropertyImplementation(Context, PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002083 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002084
Chris Lattnerb28317a2009-03-28 19:18:32 +00002085 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002086}
Anders Carlsson15281452008-11-04 16:57:32 +00002087
Chris Lattnercc98eac2008-12-17 07:13:27 +00002088bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002089 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002090 return false;
2091
2092 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2093 D->setInvalidDecl();
2094
2095 return true;
2096}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002097
2098/// Collect the instance variables declared in an Objective-C object. Used in
2099/// the creation of structures from objects using the @defs directive.
2100/// FIXME: This should be consolidated with CollectObjCIvars as it is also
2101/// part of the AST generation logic of @defs.
2102static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
2103 ASTContext& Ctx,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002104 llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002105 if (Class->getSuperClass())
2106 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
2107
2108 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002109 for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(),
2110 E = Class->ivar_end(); I != E; ++I) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002111 ObjCIvarDecl* ID = *I;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002112 Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(),
2113 ID->getIdentifier(), ID->getType(),
2114 ID->getBitWidth());
2115 ivars.push_back(Sema::DeclPtrTy::make(FD));
Chris Lattnercc98eac2008-12-17 07:13:27 +00002116 }
2117}
2118
2119/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2120/// instance variables of ClassName into Decls.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002121void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002122 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002123 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002124 // Check that ClassName is a valid class
2125 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2126 if (!Class) {
2127 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2128 return;
2129 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002130 if (LangOpts.ObjCNonFragileABI) {
2131 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2132 return;
2133 }
2134
Chris Lattnercc98eac2008-12-17 07:13:27 +00002135 // Collect the instance variables
Chris Lattnerb28317a2009-03-28 19:18:32 +00002136 CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002137
2138 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002139 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002140 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002141 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002142 if (getLangOptions().CPlusPlus)
2143 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002144 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Douglas Gregor6ab35242009-04-09 21:40:53 +00002145 Record->addDecl(Context, FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002146 }
2147}
2148