blob: 0ee0ad730f38baa617595fcf47d73f138b651170 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000015#include "clang/Sema/ExternalSemaSource.h"
Steve Naroffca331292009-03-03 14:49:36 +000016#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000020using namespace clang;
21
Fariborz Jahanianc001e892009-05-08 20:20:55 +000022bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
23 ObjCMethodDecl *GetterMethod,
24 SourceLocation Loc) {
25 if (GetterMethod &&
26 GetterMethod->getResultType() != property->getType()) {
27 AssignConvertType result = Incompatible;
Steve Narofff4954562009-07-16 15:41:00 +000028 if (property->getType()->isObjCObjectPointerType())
Fariborz Jahanian7aaa4092009-05-08 21:10:00 +000029 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
Fariborz Jahanianc001e892009-05-08 20:20:55 +000030 if (result != Compatible) {
31 Diag(Loc, diag::warn_accessor_property_type_mismatch)
32 << property->getDeclName()
33 << GetterMethod->getSelector();
34 Diag(GetterMethod->getLocation(), diag::note_declared_at);
35 return true;
36 }
37 }
38 return false;
39}
40
Steve Naroffebf64432009-02-28 16:59:13 +000041/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000042/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000043void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000044 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000045 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff394f3f42008-07-25 17:57:26 +000046
47 // If we don't have a valid method decl, simply return.
48 if (!MDecl)
49 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000050
Chris Lattner38c5ebd2009-04-19 05:21:20 +000051 CurFunctionNeedsScopeChecking = false;
52
Steve Naroffa56f6162007-12-18 01:30:32 +000053 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000054 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000055 AddInstanceMethodToGlobalPool(MDecl);
56 else
57 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000058
59 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000060 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000061
62 // Create Decl objects for each parameter, entrring them in the scope for
63 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000064
65 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000066 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +000067
Daniel Dunbar451318c2008-08-26 06:07:48 +000068 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
69 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000070
Chris Lattner8123a952008-04-10 02:22:51 +000071 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000072 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
73 E = MDecl->param_end(); PI != E; ++PI)
74 if ((*PI)->getIdentifier())
75 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000076}
77
Chris Lattnerb28317a2009-03-28 19:18:32 +000078Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000079ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
80 IdentifierInfo *ClassName, SourceLocation ClassLoc,
81 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000082 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000083 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000084 assert(ClassName && "Missing class identifier");
85
86 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000087 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000088 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000089 // Maybe we will complain about the shadowed template parameter.
90 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
91 // Just pretend that we didn't see the previous declaration.
92 PrevDecl = 0;
93 }
94
Ted Kremeneka526c5c2008-01-07 19:49:32 +000095 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000096 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000097 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000098 }
99
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000100 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000101 if (IDecl) {
102 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000103 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000104 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000105 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000106 Diag(IDecl->getLocation(), diag::note_previous_definition);
107
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000108 // Return the previous class interface.
109 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000110 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000111 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000112 IDecl->setLocation(AtInterfaceLoc);
113 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000114 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000115 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000116 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000117 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000118 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000119 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000120
Steve Naroffa7503a72009-04-23 15:15:40 +0000121 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000122 }
123
124 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000125 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000126 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000127 if (PrevDecl == IDecl) {
128 Diag(SuperLoc, diag::err_recursive_superclass)
129 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
130 IDecl->setLocEnd(ClassLoc);
131 }
132 else {
133 ObjCInterfaceDecl *SuperClassDecl =
134 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000135
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000136 // Diagnose classes that inherit from deprecated classes.
137 if (SuperClassDecl)
138 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattnerc7984dd2009-02-16 21:33:09 +0000139
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000140 if (PrevDecl && SuperClassDecl == 0) {
141 // The previous declaration was not a class decl. Check if we have a
142 // typedef. If we do, get the underlying class type.
143 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
144 QualType T = TDecl->getUnderlyingType();
145 if (T->isObjCInterfaceType()) {
146 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl())
147 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
148 }
149 }
150
151 // This handles the following case:
152 //
153 // typedef int SuperClass;
154 // @interface MyClass : SuperClass {} @end
155 //
156 if (!SuperClassDecl) {
157 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
158 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000159 }
160 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000161
162 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
163 if (!SuperClassDecl)
164 Diag(SuperLoc, diag::err_undef_superclass)
165 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
166 else if (SuperClassDecl->isForwardDecl())
167 Diag(SuperLoc, diag::err_undef_superclass)
168 << SuperClassDecl->getDeclName() << ClassName
169 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000170 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000171 IDecl->setSuperClass(SuperClassDecl);
172 IDecl->setSuperClassLoc(SuperLoc);
173 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000174 }
Chris Lattner4d391482007-12-12 07:09:47 +0000175 } else { // we have a root class.
176 IDecl->setLocEnd(ClassLoc);
177 }
178
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000179 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000180 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000181 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
182 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000183 IDecl->setLocEnd(EndProtoLoc);
184 }
Anders Carlsson15281452008-11-04 16:57:32 +0000185
186 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000187 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000188}
189
190/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000191/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000192Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
193 IdentifierInfo *AliasName,
194 SourceLocation AliasLocation,
195 IdentifierInfo *ClassName,
196 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000197 // Look for previous declaration of alias name
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000198 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000199 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000200 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000201 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000202 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000203 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000204 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000205 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000206 }
207 // Check for class declaration
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000208 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000209 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
210 QualType T = TDecl->getUnderlyingType();
211 if (T->isObjCInterfaceType()) {
212 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
213 ClassName = IDecl->getIdentifier();
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000214 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000215 }
216 }
217 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000218 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
219 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000220 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000221 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000222 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000223 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000224 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000225
226 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000227 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000228 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe8043c32008-04-01 23:04:06 +0000229
Anders Carlsson15281452008-11-04 16:57:32 +0000230 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000231 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000232
Chris Lattnerb28317a2009-03-28 19:18:32 +0000233 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000234}
235
Steve Naroff61d68522009-03-05 15:22:01 +0000236void Sema::CheckForwardProtocolDeclarationForCircularDependency(
237 IdentifierInfo *PName,
238 SourceLocation &Ploc, SourceLocation PrevLoc,
239 const ObjCList<ObjCProtocolDecl> &PList)
240{
241 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
242 E = PList.end(); I != E; ++I) {
243
Douglas Gregor6e378de2009-04-23 23:18:26 +0000244 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000245 if (PDecl->getIdentifier() == PName) {
246 Diag(Ploc, diag::err_protocol_has_circular_dependency);
247 Diag(PrevLoc, diag::note_previous_definition);
248 }
249 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
250 PDecl->getLocation(), PDecl->getReferencedProtocols());
251 }
252 }
253}
254
Chris Lattnerb28317a2009-03-28 19:18:32 +0000255Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000256Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
257 IdentifierInfo *ProtocolName,
258 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000259 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000260 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000261 SourceLocation EndProtoLoc,
262 AttributeList *AttrList) {
263 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000264 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000265 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000266 if (PDecl) {
267 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000268 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000269 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000270 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000271 // Just return the protocol we already had.
272 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000273 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000274 }
Steve Naroff61d68522009-03-05 15:22:01 +0000275 ObjCList<ObjCProtocolDecl> PList;
276 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
277 CheckForwardProtocolDeclarationForCircularDependency(
278 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
279 PList.Destroy(Context);
280
Steve Narofff11b5082008-08-13 16:39:22 +0000281 // Make sure the cached decl gets a valid start location.
282 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000283 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000284 } else {
Douglas Gregord0434102009-01-09 00:49:46 +0000285 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
286 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000287 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000288 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000289 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000290 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000291 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000292 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000293 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000294 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000295 PDecl->setLocEnd(EndProtoLoc);
296 }
Anders Carlsson15281452008-11-04 16:57:32 +0000297
298 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000299 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000300}
301
302/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000303/// issues an error if they are not declared. It returns list of
304/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000305void
Chris Lattnere13b9592008-07-26 04:03:38 +0000306Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000307 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000308 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000309 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000310 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000311 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000312 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000313 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000314 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000315 continue;
316 }
Chris Lattner45ce5c32009-02-14 08:22:25 +0000317
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000318 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000319
320 // If this is a forward declaration and we are supposed to warn in this
321 // case, do it.
322 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000323 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000324 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000325 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000326 }
327}
328
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000329/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000330/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000331///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000332void
333Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
334 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000335 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000336 ObjCPropertyDecl::PropertyAttributeKind CAttr =
337 Property->getPropertyAttributes();
338 ObjCPropertyDecl::PropertyAttributeKind SAttr =
339 SuperProperty->getPropertyAttributes();
340 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
341 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000342 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000343 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000344 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
345 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000346 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000347 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000348 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
349 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000350 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000351 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000352
353 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
354 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
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() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000357 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000358 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000359 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000360 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000361 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000362 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000363
364 QualType LHSType =
365 Context.getCanonicalType(SuperProperty->getType());
366 QualType RHSType =
367 Context.getCanonicalType(Property->getType());
368
369 if (!Context.typesAreCompatible(LHSType, RHSType)) {
370 // FIXME: Incorporate this test with typesAreCompatible.
371 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff4084c302009-07-23 01:01:38 +0000372 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff15edf0d2009-03-03 15:43:24 +0000373 return;
374 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
375 << Property->getType() << SuperProperty->getType() << inheritedName;
376 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000377}
378
379/// ComparePropertiesInBaseAndSuper - This routine compares property
380/// declarations in base and its super class, if any, and issues
381/// diagnostics in a variety of inconsistant situations.
382///
Chris Lattner70f19542009-02-16 21:26:43 +0000383void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000384 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
385 if (!SDecl)
386 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000387 // FIXME: O(N^2)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000388 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
389 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000390 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000391 // Does property in super class has declaration in current class?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000392 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
393 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000394 ObjCPropertyDecl *PDecl = (*I);
395 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000396 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000397 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000398 }
399 }
400}
401
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000402/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
403/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000404/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000405void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000406Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000407 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000408 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
409 if (!IDecl) {
410 // Category
411 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
412 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000413 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
414 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000415 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000416 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000417 // Is this property already in category's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000418 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000419 if ((*CP)->getIdentifier() == Pr->getIdentifier())
420 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000421 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000422 // Property protocol already exist in class. Diagnose any mismatch.
423 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
424 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000425 return;
426 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000427 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
428 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000429 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000430 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000431 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000432 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000433 if ((*CP)->getIdentifier() == Pr->getIdentifier())
434 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000435 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000436 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000437 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000438 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000439}
440
441/// MergeProtocolPropertiesIntoClass - This routine merges properties
442/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000443/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000444///
Chris Lattner70f19542009-02-16 21:26:43 +0000445void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000446 DeclPtrTy MergeItsProtocols) {
447 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000448 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
449
450 if (!IDecl) {
451 // Category
452 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
453 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
454 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
455 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
456 E = MDecl->protocol_end(); P != E; ++P)
457 // Merge properties of category (*P) into IDECL's
458 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
459
460 // Go thru the list of protocols for this category and recursively merge
461 // their properties into this class as well.
462 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
463 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000464 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000465 } else {
466 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
467 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
468 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000469 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000470 }
471 return;
472 }
473
Chris Lattnerb752f282008-07-21 07:06:49 +0000474 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000475 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
476 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000477 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000478 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
479
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000480 // Go thru the list of protocols for this class and recursively merge
481 // their properties into this class as well.
482 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
483 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000484 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000485 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000486 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
487 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
488 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000489 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000490 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000491}
492
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000493/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000494/// a class method in its extension.
495///
496void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
497 ObjCInterfaceDecl *ID) {
498 if (!ID)
499 return; // Possibly due to previous error
500
501 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000502 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
503 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000504 ObjCMethodDecl *MD = *i;
505 MethodMap[MD->getSelector()] = MD;
506 }
507
508 if (MethodMap.empty())
509 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000510 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
511 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000512 ObjCMethodDecl *Method = *i;
513 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
514 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
515 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
516 << Method->getDeclName();
517 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
518 }
519 }
520}
521
Chris Lattner58fe03b2009-04-12 08:43:13 +0000522/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000523Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000524Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000525 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000526 unsigned NumElts,
527 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000528 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000529
530 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000531 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000532 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000533 if (PDecl == 0) { // Not already seen?
534 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
535 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000536 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000537 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000538 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000539 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000540 Protocols.push_back(PDecl);
541 }
Anders Carlsson15281452008-11-04 16:57:32 +0000542
543 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000544 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000545 &Protocols[0], Protocols.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000546 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000547 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000548 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000549}
550
Chris Lattnerb28317a2009-03-28 19:18:32 +0000551Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000552ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
553 IdentifierInfo *ClassName, SourceLocation ClassLoc,
554 IdentifierInfo *CategoryName,
555 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000556 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000557 unsigned NumProtoRefs,
558 SourceLocation EndProtoLoc) {
Chris Lattner61f9d412008-03-16 20:34:23 +0000559 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000560 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
561 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000562 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000563
564 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000565 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000566 if (!IDecl || IDecl->isForwardDecl()) {
567 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000568 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000569 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000570 }
Chris Lattner4d391482007-12-12 07:09:47 +0000571
Chris Lattner70f19542009-02-16 21:26:43 +0000572 CDecl->setClassInterface(IDecl);
Chris Lattner16b34b42009-02-16 21:30:01 +0000573
574 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000575 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000576
577 /// Check for duplicate interface declaration for this category
578 ObjCCategoryDecl *CDeclChain;
579 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
580 CDeclChain = CDeclChain->getNextClassCategory()) {
581 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
582 Diag(CategoryLoc, diag::warn_dup_category_def)
583 << ClassName << CategoryName;
584 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
585 break;
586 }
587 }
588 if (!CDeclChain)
589 CDecl->insertNextClassCategory();
590
Chris Lattner4d391482007-12-12 07:09:47 +0000591 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000592 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000593 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000594 }
Anders Carlsson15281452008-11-04 16:57:32 +0000595
596 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000597 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000598}
599
600/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000601/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000602/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000603Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000604 SourceLocation AtCatImplLoc,
605 IdentifierInfo *ClassName, SourceLocation ClassLoc,
606 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000607 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000608 ObjCCategoryDecl *CatIDecl = 0;
609 if (IDecl) {
610 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
611 if (!CatIDecl) {
612 // Category @implementation with no corresponding @interface.
613 // Create and install one.
614 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
615 CatName);
616 CatIDecl->setClassInterface(IDecl);
617 CatIDecl->insertNextClassCategory();
618 }
619 }
620
Chris Lattner75c9cae2008-03-16 20:53:07 +0000621 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000622 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
623 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000624 /// Check that class of this category is already completely declared.
625 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000626 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000627
Douglas Gregord0434102009-01-09 00:49:46 +0000628 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000629 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000630
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000631 /// Check that CatName, category name, is not used in another implementation.
632 if (CatIDecl) {
633 if (CatIDecl->getImplementation()) {
634 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
635 << CatName;
636 Diag(CatIDecl->getImplementation()->getLocation(),
637 diag::note_previous_definition);
638 } else
639 CatIDecl->setImplementation(CDecl);
640 }
Anders Carlsson15281452008-11-04 16:57:32 +0000641
642 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000643 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000644}
645
Chris Lattnerb28317a2009-03-28 19:18:32 +0000646Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000647 SourceLocation AtClassImplLoc,
648 IdentifierInfo *ClassName, SourceLocation ClassLoc,
649 IdentifierInfo *SuperClassname,
650 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000651 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000652 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000653 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000654 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000655 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000656 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000657 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000658 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000659 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000660 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000661 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000662 IDecl = 0;
663 }
Chris Lattner4d391482007-12-12 07:09:47 +0000664 }
665
666 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000667 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000668 if (SuperClassname) {
669 // Check if a different kind of symbol declared in this scope.
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000670 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000671 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000672 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
673 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000674 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000675 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000676 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000677 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000678 Diag(SuperClassLoc, diag::err_undef_superclass)
679 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000680 else if (IDecl && IDecl->getSuperClass() != SDecl) {
681 // This implementation and its interface do not have the same
682 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000683 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000684 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000685 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000686 }
687 }
688 }
689
690 if (!IDecl) {
691 // Legacy case of @implementation with no corresponding @interface.
692 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000693
Mike Stump390b4cc2009-05-16 07:39:55 +0000694 // FIXME: Do we support attributes on the @implementation? If so we should
695 // copy them over.
Douglas Gregord0434102009-01-09 00:49:46 +0000696 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
697 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000698 IDecl->setSuperClass(SDecl);
699 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000700
701 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000702 } else {
703 // Mark the interface as being completed, even if it was just as
704 // @class ....;
705 // declaration; the user cannot reopen it.
706 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000707 }
708
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000709 ObjCImplementationDecl* IMPDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000710 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000711 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000712
Anders Carlsson15281452008-11-04 16:57:32 +0000713 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000714 return DeclPtrTy::make(IMPDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000715
Chris Lattner4d391482007-12-12 07:09:47 +0000716 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000717 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000718 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000719 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000720 Diag(IDecl->getImplementation()->getLocation(),
721 diag::note_previous_definition);
722 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000723 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000724 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000725 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000726 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000727}
728
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000729void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
730 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000731 SourceLocation RBrace) {
732 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000733 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000734 if (!IDecl)
735 return;
736 /// Check case of non-existing @interface decl.
737 /// (legacy objective-c @implementation decl without an @interface decl).
738 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000739 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000740 IDecl->setIVarList(ivars, numIvars, Context);
741 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000742 return;
743 }
744 // If implementation has empty ivar list, just return.
745 if (numIvars == 0)
746 return;
747
748 assert(ivars && "missing @implementation ivars");
749
750 // Check interface's Ivar list against those in the implementation.
751 // names and types must match.
752 //
Chris Lattner4d391482007-12-12 07:09:47 +0000753 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000754 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000755 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
756 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000757 ObjCIvarDecl* ImplIvar = ivars[j++];
758 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000759 assert (ImplIvar && "missing implementation ivar");
760 assert (ClsIvar && "missing class ivar");
Steve Naroffca331292009-03-03 14:49:36 +0000761
762 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000763 if (Context.getCanonicalType(ImplIvar->getType()) !=
764 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000765 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000766 << ImplIvar->getIdentifier()
767 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000768 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000769 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
770 Expr *ImplBitWidth = ImplIvar->getBitWidth();
771 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000772 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
773 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000774 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
775 << ImplIvar->getIdentifier();
776 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
777 }
778 }
779 // Make sure the names are identical.
780 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000781 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000782 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000783 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000784 }
785 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000786 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000787
788 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000789 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000790 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000791 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000792}
793
Steve Naroff3c2eb662008-02-10 21:38:56 +0000794void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
795 bool &IncompleteImpl) {
796 if (!IncompleteImpl) {
797 Diag(ImpLoc, diag::warn_incomplete_impl);
798 IncompleteImpl = true;
799 }
Chris Lattner08631c52008-11-23 21:45:46 +0000800 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000801}
802
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000803void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
804 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000805 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000806 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000807 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
808 ImpMethodDecl->getResultType())) {
Chris Lattner3aff9192009-04-11 19:58:42 +0000809 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
810 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
811 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000812 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
813 }
Chris Lattner3aff9192009-04-11 19:58:42 +0000814
815 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
816 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
817 IM != EM; ++IM, ++IF) {
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000818 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()) ||
Steve Naroff4084c302009-07-23 01:01:38 +0000819 Context.QualifiedIdConformsQualifiedId((*IF)->getType(),
820 (*IM)->getType()))
Chris Lattner3aff9192009-04-11 19:58:42 +0000821 continue;
822
823 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
824 << ImpMethodDecl->getDeclName() << (*IF)->getType()
825 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000826 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000827 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000828}
829
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000830/// isPropertyReadonly - Return true if property is readonly, by searching
831/// for the property in the class and in its categories and implementations
832///
833bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000834 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000835 // by far the most common case.
836 if (!PDecl->isReadOnly())
837 return false;
838 // Even if property is ready only, if interface has a user defined setter,
839 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000840 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000841 return false;
842
843 // Main class has the property as 'readonly'. Must search
844 // through the category list to see if the property's
845 // attribute has been over-ridden to 'readwrite'.
846 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
847 Category; Category = Category->getNextClassCategory()) {
848 // Even if property is ready only, if a category has a user defined setter,
849 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000850 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000851 return false;
852 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000853 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000854 if (P && !P->isReadOnly())
855 return false;
856 }
857
858 // Also, check for definition of a setter method in the implementation if
859 // all else failed.
860 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
861 if (ObjCImplementationDecl *IMD =
862 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000863 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000864 return false;
865 }
866 else if (ObjCCategoryImplDecl *CIMD =
867 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000868 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000869 return false;
870 }
871 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000872 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000873 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000874 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000875 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000876 // If all fails, look at the super class.
877 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
878 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000879 return true;
880}
881
Mike Stump390b4cc2009-05-16 07:39:55 +0000882/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
883/// improve the efficiency of selector lookups and type checking by associating
884/// with each protocol / interface / category the flattened instance tables. If
885/// we used an immutable set to keep the table then it wouldn't add significant
886/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000887
Steve Naroffefe7f362008-02-08 22:06:17 +0000888/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000889/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000890void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
891 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000892 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000893 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000894 const llvm::DenseSet<Selector> &ClsMap,
895 ObjCInterfaceDecl *IDecl) {
896 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000897 ObjCInterfaceDecl *NSIDecl = 0;
898 if (getLangOptions().NeXTRuntime) {
899 // check to see if class implements forwardInvocation method and objects
900 // of this class are derived from 'NSProxy' so that to forward requests
901 // from one object to another.
902 // Under such conditions, which means that every method possible is
903 // implemented in the class, we should not issue "Method definition not
904 // found" warnings.
905 // FIXME: Use a general GetUnarySelector method for this.
906 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
907 Selector fISelector = Context.Selectors.getSelector(1, &II);
908 if (InsMap.count(fISelector))
909 // Is IDecl derived from 'NSProxy'? If so, no instance methods
910 // need be implemented in the implementation.
911 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
912 }
913
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000914 // If a method lookup fails locally we still need to look and see if
915 // the method was implemented by a base class or an inherited
916 // protocol. This lookup is slow, but occurs rarely in correct code
917 // and otherwise would terminate in a warning.
918
Chris Lattner4d391482007-12-12 07:09:47 +0000919 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000920 if (!NSIDecl)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000921 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
922 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000923 ObjCMethodDecl *method = *I;
924 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
925 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
926 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000927 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000928 // Ugly, but necessary. Method declared in protcol might have
929 // have been synthesized due to a property declared in the class which
930 // uses the protocol.
931 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000932 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000933 if (!MethodInClass || !MethodInClass->isSynthesized())
934 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
935 }
936 }
Chris Lattner4d391482007-12-12 07:09:47 +0000937 // check unimplemented class methods
Douglas Gregor6ab35242009-04-09 21:40:53 +0000938 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000939 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000940 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000941 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000942 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
943 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000944 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000945 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000946 }
Chris Lattner780f3292008-07-21 21:32:27 +0000947 // Check on this protocols's referenced protocols, recursively.
948 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
949 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000950 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000951}
952
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000953/// MatchAllMethodDeclarations - Check methods declaraed in interface or
954/// or protocol against those declared in their implementations.
955///
956void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
957 const llvm::DenseSet<Selector> &ClsMap,
958 llvm::DenseSet<Selector> &InsMapSeen,
959 llvm::DenseSet<Selector> &ClsMapSeen,
960 ObjCImplDecl* IMPDecl,
961 ObjCContainerDecl* CDecl,
962 bool &IncompleteImpl,
963 bool ImmediateClass)
964{
965 // Check and see if instance methods in class interface have been
966 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000967 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
968 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000969 if (InsMapSeen.count((*I)->getSelector()))
970 continue;
971 InsMapSeen.insert((*I)->getSelector());
972 if (!(*I)->isSynthesized() &&
973 !InsMap.count((*I)->getSelector())) {
974 if (ImmediateClass)
975 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
976 continue;
977 }
978 else {
979 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000980 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000981 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000982 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000983 assert(IntfMethodDecl &&
984 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
985 // ImpMethodDecl may be null as in a @dynamic property.
986 if (ImpMethodDecl)
987 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
988 }
989 }
990
991 // Check and see if class methods in class interface have been
992 // implemented in the implementation class. If so, their types match.
993 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000994 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000995 if (ClsMapSeen.count((*I)->getSelector()))
996 continue;
997 ClsMapSeen.insert((*I)->getSelector());
998 if (!ClsMap.count((*I)->getSelector())) {
999 if (ImmediateClass)
1000 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1001 }
1002 else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001003 ObjCMethodDecl *ImpMethodDecl =
1004 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001005 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001006 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001007 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1008 }
1009 }
1010 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1011 // Check for any implementation of a methods declared in protocol.
1012 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1013 E = I->protocol_end(); PI != E; ++PI)
1014 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1015 IMPDecl,
1016 (*PI), IncompleteImpl, false);
1017 if (I->getSuperClass())
1018 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1019 IMPDecl,
1020 I->getSuperClass(), IncompleteImpl, false);
1021 }
1022}
1023
Chris Lattnercddc8882009-03-01 00:56:52 +00001024void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1025 ObjCContainerDecl* CDecl,
1026 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001027 llvm::DenseSet<Selector> InsMap;
1028 // Check and see if instance methods in class interface have been
1029 // implemented in the implementation class.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001030 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001031 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001032 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +00001033
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001034 // Check and see if properties declared in the interface have either 1)
1035 // an implementation or 2) there is a @synthesize/@dynamic implementation
1036 // of the property in the @implementation.
1037 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001038 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1039 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001040 ObjCPropertyDecl *Prop = (*P);
1041 if (Prop->isInvalidDecl())
1042 continue;
1043 ObjCPropertyImplDecl *PI = 0;
1044 // Is there a matching propery synthesize/dynamic?
Douglas Gregor653f1b12009-04-23 01:02:12 +00001045 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001046 I = IMPDecl->propimpl_begin(),
1047 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001048 if ((*I)->getPropertyDecl() == Prop) {
1049 PI = (*I);
1050 break;
1051 }
1052 if (PI)
1053 continue;
1054 if (!InsMap.count(Prop->getGetterName())) {
1055 Diag(Prop->getLocation(),
1056 diag::warn_setter_getter_impl_required)
1057 << Prop->getDeclName() << Prop->getGetterName();
1058 Diag(IMPDecl->getLocation(),
1059 diag::note_property_impl_required);
1060 }
1061
1062 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1063 Diag(Prop->getLocation(),
1064 diag::warn_setter_getter_impl_required)
1065 << Prop->getDeclName() << Prop->getSetterName();
1066 Diag(IMPDecl->getLocation(),
1067 diag::note_property_impl_required);
1068 }
1069 }
1070
Chris Lattner4d391482007-12-12 07:09:47 +00001071 llvm::DenseSet<Selector> ClsMap;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001072 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001073 I = IMPDecl->classmeth_begin(),
1074 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001075 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +00001076
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001077 // Check for type conflict of methods declared in a class/protocol and
1078 // its implementation; if any.
1079 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1080 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1081 IMPDecl, CDecl,
1082 IncompleteImpl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001083
1084 // Check the protocol list for unimplemented methods in the @implementation
1085 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001086 // Check and see if class methods in class interface have been
1087 // implemented in the implementation class.
1088
Chris Lattnercddc8882009-03-01 00:56:52 +00001089 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001090 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001091 E = I->protocol_end(); PI != E; ++PI)
1092 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1093 InsMap, ClsMap, I);
1094 // Check class extensions (unnamed categories)
1095 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1096 Categories; Categories = Categories->getNextClassCategory()) {
1097 if (!Categories->getIdentifier()) {
1098 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1099 break;
1100 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001101 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001102 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1103 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1104 E = C->protocol_end(); PI != E; ++PI)
1105 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1106 InsMap, ClsMap, C->getClassInterface());
1107 } else
1108 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001109}
1110
1111/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001112Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001113Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001114 IdentifierInfo **IdentList,
1115 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001116 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +00001117
1118 for (unsigned i = 0; i != NumElts; ++i) {
1119 // Check for another declaration kind with the same name.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001120 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001121 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001122 // Maybe we will complain about the shadowed template parameter.
1123 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1124 // Just pretend that we didn't see the previous declaration.
1125 PrevDecl = 0;
1126 }
1127
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001128 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001129 // GCC apparently allows the following idiom:
1130 //
1131 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1132 // @class XCElementToggler;
1133 //
1134 // FIXME: Make an extension?
1135 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1136 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001137 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001138 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +00001139 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001140 else if (TDD) {
1141 // a forward class declaration matching a typedef name of a class
1142 // refers to the underlying class.
1143 if (ObjCInterfaceType * OI =
1144 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1145 PrevDecl = OI->getDecl();
1146 }
Chris Lattner4d391482007-12-12 07:09:47 +00001147 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001148 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001149 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregord0434102009-01-09 00:49:46 +00001150 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1151 IdentList[i], SourceLocation(), true);
Steve Naroff8f06f842009-04-23 16:00:56 +00001152 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +00001153 }
1154
1155 Interfaces.push_back(IDecl);
1156 }
1157
Douglas Gregord0434102009-01-09 00:49:46 +00001158 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001159 &Interfaces[0],
1160 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001161 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001162 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001163 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001164}
1165
1166
1167/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1168/// returns true, or false, accordingly.
1169/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001170bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001171 const ObjCMethodDecl *PrevMethod,
1172 bool matchBasedOnSizeAndAlignment) {
1173 QualType T1 = Context.getCanonicalType(Method->getResultType());
1174 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1175
1176 if (T1 != T2) {
1177 // The result types are different.
1178 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001179 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001180 // Incomplete types don't have a size and alignment.
1181 if (T1->isIncompleteType() || T2->isIncompleteType())
1182 return false;
1183 // Check is based on size and alignment.
1184 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1185 return false;
1186 }
Chris Lattner89951a82009-02-20 18:43:26 +00001187
1188 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1189 E = Method->param_end();
1190 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1191
1192 for (; ParamI != E; ++ParamI, ++PrevI) {
1193 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1194 T1 = Context.getCanonicalType((*ParamI)->getType());
1195 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001196 if (T1 != T2) {
1197 // The result types are different.
1198 if (!matchBasedOnSizeAndAlignment)
1199 return false;
1200 // Incomplete types don't have a size and alignment.
1201 if (T1->isIncompleteType() || T2->isIncompleteType())
1202 return false;
1203 // Check is based on size and alignment.
1204 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1205 return false;
1206 }
Chris Lattner4d391482007-12-12 07:09:47 +00001207 }
1208 return true;
1209}
1210
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001211/// \brief Read the contents of the instance and factory method pools
1212/// for a given selector from external storage.
1213///
1214/// This routine should only be called once, when neither the instance
1215/// nor the factory method pool has an entry for this selector.
1216Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
1217 bool isInstance) {
1218 assert(ExternalSource && "We need an external AST source");
1219 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1220 "Selector data already loaded into the instance method pool");
1221 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1222 "Selector data already loaded into the factory method pool");
1223
1224 // Read the method list from the external source.
1225 std::pair<ObjCMethodList, ObjCMethodList> Methods
1226 = ExternalSource->ReadMethodPool(Sel);
1227
1228 if (isInstance) {
1229 if (Methods.second.Method)
1230 FactoryMethodPool[Sel] = Methods.second;
1231 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
1232 }
1233
1234 if (Methods.first.Method)
1235 InstanceMethodPool[Sel] = Methods.first;
1236
1237 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1238}
1239
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001240void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001241 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1242 = InstanceMethodPool.find(Method->getSelector());
1243 if (Pos == InstanceMethodPool.end()) {
1244 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1245 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1246 else
1247 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1248 ObjCMethodList())).first;
1249 }
1250
1251 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001252 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001253 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001254 Entry.Method = Method;
1255 Entry.Next = 0;
1256 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001257 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001258
1259 // We've seen a method with this name, see if we have already seen this type
1260 // signature.
1261 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1262 if (MatchTwoMethodDeclarations(Method, List->Method))
1263 return;
1264
1265 // We have a new signature for an existing method - add it.
1266 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1267 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001268}
1269
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001270// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +00001271ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1272 SourceRange R) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001273 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1274 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001275 if (Pos == InstanceMethodPool.end()) {
1276 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001277 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1278 else
1279 return 0;
1280 }
1281
1282 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001283 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +00001284
1285 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001286 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1287 // This checks if the methods differ by size & alignment.
1288 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1289 issueWarning = true;
1290 }
1291 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001292 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001293 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001294 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001295 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001296 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001297 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001298 }
1299 return MethList.Method;
1300}
1301
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001302void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001303 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1304 = FactoryMethodPool.find(Method->getSelector());
1305 if (Pos == FactoryMethodPool.end()) {
1306 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1307 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1308 else
1309 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1310 ObjCMethodList())).first;
1311 }
1312
1313 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001314 if (!FirstMethod.Method) {
1315 // Haven't seen a method with this selector name yet - add it.
1316 FirstMethod.Method = Method;
1317 FirstMethod.Next = 0;
1318 } else {
1319 // We've seen a method with this name, now check the type signature(s).
1320 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1321
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001322 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001323 Next = Next->Next)
1324 match = MatchTwoMethodDeclarations(Method, Next->Method);
1325
1326 if (!match) {
1327 // We have a new signature for an existing method - add it.
1328 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001329 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001330 FirstMethod.Next = OMI;
1331 }
1332 }
1333}
1334
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001335ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
1336 SourceRange R) {
1337 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1338 = FactoryMethodPool.find(Sel);
1339 if (Pos == FactoryMethodPool.end()) {
1340 if (ExternalSource && !InstanceMethodPool.count(Sel))
1341 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1342 else
1343 return 0;
1344 }
1345
1346 ObjCMethodList &MethList = Pos->second;
1347 bool issueWarning = false;
1348
1349 if (MethList.Method && MethList.Next) {
1350 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1351 // This checks if the methods differ by size & alignment.
1352 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1353 issueWarning = true;
1354 }
1355 if (issueWarning && (MethList.Method && MethList.Next)) {
1356 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1357 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1358 << MethList.Method->getSourceRange();
1359 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1360 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1361 << Next->Method->getSourceRange();
1362 }
1363 return MethList.Method;
1364}
1365
Steve Naroff0701bbb2009-01-08 17:28:14 +00001366/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1367/// have the property type and issue diagnostics if they don't.
1368/// Also synthesize a getter/setter method if none exist (and update the
1369/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1370/// methods is the "right" thing to do.
1371void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1372 ObjCContainerDecl *CD) {
1373 ObjCMethodDecl *GetterMethod, *SetterMethod;
1374
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001375 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1376 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001377 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1378 property->getLocation());
1379
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001380 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001381 if (Context.getCanonicalType(SetterMethod->getResultType())
1382 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001383 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001384 if (SetterMethod->param_size() != 1 ||
1385 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001386 Diag(property->getLocation(),
Fariborz Jahanian4c2743f2009-05-08 19:36:34 +00001387 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001388 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001389 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001390 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1391 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001392 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001393
1394 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001395 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001396 // FIXME: The synthesized property we set here is misleading. We almost always
1397 // synthesize these methods unless the user explicitly provided prototypes
1398 // (which is odd, but allowed). Sema should be typechecking that the
1399 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001400 if (!GetterMethod) {
1401 // No instance method of same name as property getter name was found.
1402 // Declare a getter method and add it to the list of methods
1403 // for this class.
1404 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1405 property->getLocation(), property->getGetterName(),
1406 property->getType(), CD, true, false, true,
1407 (property->getPropertyImplementation() ==
1408 ObjCPropertyDecl::Optional) ?
1409 ObjCMethodDecl::Optional :
1410 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001411 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001412 } else
1413 // A user declared getter will be synthesize when @synthesize of
1414 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001415 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001416 property->setGetterMethodDecl(GetterMethod);
1417
1418 // Skip setter if property is read-only.
1419 if (!property->isReadOnly()) {
1420 // Find the default setter and if one not found, add one.
1421 if (!SetterMethod) {
1422 // No instance method of same name as property setter name was found.
1423 // Declare a setter method and add it to the list of methods
1424 // for this class.
1425 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1426 property->getLocation(),
1427 property->getSetterName(),
1428 Context.VoidTy, CD, true, false, true,
1429 (property->getPropertyImplementation() ==
1430 ObjCPropertyDecl::Optional) ?
1431 ObjCMethodDecl::Optional :
1432 ObjCMethodDecl::Required);
1433 // Invent the arguments for the setter. We don't bother making a
1434 // nice name for the argument.
1435 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001436 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001437 property->getIdentifier(),
1438 property->getType(),
1439 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001440 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001441 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001442 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001443 } else
1444 // A user declared setter will be synthesize when @synthesize of
1445 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001446 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001447 property->setSetterMethodDecl(SetterMethod);
1448 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001449 // Add any synthesized methods to the global pool. This allows us to
1450 // handle the following, which is supported by GCC (and part of the design).
1451 //
1452 // @interface Foo
1453 // @property double bar;
1454 // @end
1455 //
1456 // void thisIsUnfortunate() {
1457 // id foo;
1458 // double bar = [foo bar];
1459 // }
1460 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001461 if (GetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001462 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001463 if (SetterMethod)
Steve Naroff0701bbb2009-01-08 17:28:14 +00001464 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001465}
1466
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001467void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1468 ObjCMethodDecl *Method,
1469 bool IsInstance) {
1470 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
1471 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
1472 if (ObjCMethodDecl *SuperMethodDecl =
1473 SD->lookupMethod(Method->getSelector(), IsInstance)) {
1474 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1475 E = Method->param_end();
1476 ObjCMethodDecl::param_iterator PrevI =
1477 SuperMethodDecl->param_begin();
1478 for (; ParamI != E; ++ParamI, ++PrevI) {
1479 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1480 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1481 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1482 if (T1 != T2) {
1483 AssignConvertType ConvTy = CheckAssignmentConstraints(T1, T2);
1484 if (ConvTy == Incompatible || ConvTy == IncompatiblePointer) {
1485 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
1486 << T1 << T2;
1487 Diag(SuperMethodDecl->getLocation(),
1488 diag::note_previous_declaration);
1489 return;
1490 }
1491 }
1492 }
1493 }
1494 ID = SD;
1495 }
1496}
1497
Steve Naroffa56f6162007-12-18 01:30:32 +00001498// Note: For class/category implemenations, allMethods/allProperties is
1499// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001500void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1501 DeclPtrTy *allMethods, unsigned allNum,
1502 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001503 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001504 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001505
Steve Naroffa56f6162007-12-18 01:30:32 +00001506 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1507 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001508 // should be true.
1509 if (!ClassDecl)
1510 return;
1511
Chris Lattner4d391482007-12-12 07:09:47 +00001512 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001513 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1514 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001515 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001516
Steve Naroff0701bbb2009-01-08 17:28:14 +00001517 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001518
1519 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1520 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1521 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1522
Chris Lattner4d391482007-12-12 07:09:47 +00001523 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001524 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001525 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001526
1527 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001528 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001529 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001530 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001531 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1532 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001533 if ((isInterfaceDeclKind && PrevMethod && !match)
1534 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001535 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001536 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001537 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001538 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001539 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001540 InsMap[Method->getSelector()] = Method;
1541 /// The following allows us to typecheck messages to "id".
1542 AddInstanceMethodToGlobalPool(Method);
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001543 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001544 }
1545 }
1546 else {
1547 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001548 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001549 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1550 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001551 if ((isInterfaceDeclKind && PrevMethod && !match)
1552 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001553 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001554 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001555 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001556 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001557 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001558 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001559 /// The following allows us to typecheck messages to "Class".
1560 AddFactoryMethodToGlobalPool(Method);
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001561 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001562 }
1563 }
1564 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001565 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001566 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001567 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001568 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001569 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001570 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001571 // Categories are used to extend the class by declaring new methods.
1572 // By the same token, they are also used to add new properties. No
1573 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001574
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001575 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001576 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001577 if (C->getIdentifier() == 0)
1578 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001579 }
Steve Naroff09c47192009-01-09 15:36:25 +00001580 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1581 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1582 // user-defined setter/getter. It also synthesizes setter/getter methods
1583 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001584 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1585 E = CDecl->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001586 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001587 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001588 CDecl->setAtEndLoc(AtEndLoc);
1589 }
1590 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001591 IC->setAtEndLoc(AtEndLoc);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001592 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner4d391482007-12-12 07:09:47 +00001593 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001594 } else if (ObjCCategoryImplDecl* CatImplClass =
1595 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001596 CatImplClass->setAtEndLoc(AtEndLoc);
Chris Lattner97a58872009-02-16 18:32:47 +00001597
Chris Lattner4d391482007-12-12 07:09:47 +00001598 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001599 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001600 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001601 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001602 Categories; Categories = Categories->getNextClassCategory()) {
1603 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001604 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001605 break;
1606 }
1607 }
1608 }
1609 }
Chris Lattner682bf922009-03-29 16:50:03 +00001610 if (isInterfaceDeclKind) {
1611 // Reject invalid vardecls.
1612 for (unsigned i = 0; i != tuvNum; i++) {
1613 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1614 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1615 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001616 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001617 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001618 }
Chris Lattner682bf922009-03-29 16:50:03 +00001619 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001620 }
Chris Lattner4d391482007-12-12 07:09:47 +00001621}
1622
1623
1624/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1625/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001626static Decl::ObjCDeclQualifier
1627CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1628 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1629 if (PQTVal & ObjCDeclSpec::DQ_In)
1630 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1631 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1632 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1633 if (PQTVal & ObjCDeclSpec::DQ_Out)
1634 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1635 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1636 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1637 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1638 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1639 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1640 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001641
1642 return ret;
1643}
1644
Chris Lattnerb28317a2009-03-28 19:18:32 +00001645Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001646 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001647 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001648 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001649 Selector Sel,
1650 // optional arguments. The number of types/arguments is obtained
1651 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001652 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001653 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001654 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1655 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001656 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001657
1658 // Make sure we can establish a context for the method.
1659 if (!ClassDecl) {
1660 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001661 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001662 }
Chris Lattner4d391482007-12-12 07:09:47 +00001663 QualType resultDeclType;
1664
Steve Naroffccef3712009-02-20 22:59:16 +00001665 if (ReturnType) {
Chris Lattner4d391482007-12-12 07:09:47 +00001666 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffccef3712009-02-20 22:59:16 +00001667
1668 // Methods cannot return interface types. All ObjC objects are
1669 // passed by reference.
1670 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001671 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1672 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001673 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001674 }
1675 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001676 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001677
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001678 ObjCMethodDecl* ObjCMethod =
1679 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner32d3f9c2009-03-29 04:30:19 +00001680 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001681 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001682 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001683 MethodDeclKind == tok::objc_optional ?
1684 ObjCMethodDecl::Optional :
1685 ObjCMethodDecl::Required);
1686
Chris Lattner0ed844b2008-04-04 06:12:32 +00001687 llvm::SmallVector<ParmVarDecl*, 16> Params;
1688
Chris Lattner7db638d2009-04-11 19:42:43 +00001689 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001690 QualType ArgType, UnpromotedArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001691
Chris Lattnere294d3f2009-04-11 18:57:04 +00001692 if (ArgInfo[i].Type == 0) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001693 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001694 } else {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001695 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff6082c622008-12-09 19:36:17 +00001696 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001697 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001698 }
1699
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001700 ParmVarDecl* Param;
Chris Lattner2dd979f2009-04-11 19:08:56 +00001701 if (ArgType == UnpromotedArgType)
Chris Lattner7db638d2009-04-11 19:42:43 +00001702 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001703 ArgInfo[i].Name, ArgType,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001704 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001705 else
Douglas Gregor64650af2009-02-02 23:39:07 +00001706 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner7db638d2009-04-11 19:42:43 +00001707 ArgInfo[i].NameLoc,
Chris Lattner2dd979f2009-04-11 19:08:56 +00001708 ArgInfo[i].Name, ArgType,
1709 UnpromotedArgType,
Douglas Gregor64650af2009-02-02 23:39:07 +00001710 VarDecl::None, 0);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001711
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001712 if (ArgType->isObjCInterfaceType()) {
1713 Diag(ArgInfo[i].NameLoc,
1714 diag::err_object_cannot_be_passed_returned_by_value)
1715 << 1 << ArgType;
1716 Param->setInvalidDecl();
1717 }
1718
Chris Lattner0ed844b2008-04-04 06:12:32 +00001719 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001720 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001721
1722 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001723 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001724
Chris Lattner0ed844b2008-04-04 06:12:32 +00001725 Params.push_back(Param);
1726 }
1727
Jay Foadbeaaccd2009-05-21 09:52:38 +00001728 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001729 ObjCMethod->setObjCDeclQualifier(
1730 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1731 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001732
1733 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001734 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Ted Kremenekb27d1172009-04-30 18:41:06 +00001735
Chris Lattner4d391482007-12-12 07:09:47 +00001736 // For implementations (which can be very "coarse grain"), we add the
1737 // method now. This allows the AST to implement lookup methods that work
1738 // incrementally (without waiting until we parse the @end). It also allows
1739 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001740 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001741 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001742 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001743 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1744 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001745 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001746 PrevMethod = ImpDecl->getClassMethod(Sel);
1747 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001748 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001749 if (AttrList)
1750 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001751 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001752 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001753 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001754 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001755 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1756 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001757 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001758 PrevMethod = CatImpDecl->getClassMethod(Sel);
1759 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001760 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001761 if (AttrList)
1762 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001763 }
1764 if (PrevMethod) {
1765 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001766 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001767 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001768 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001769 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001770 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001771}
1772
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001773void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1774 SourceLocation Loc,
1775 unsigned &Attributes) {
1776 // FIXME: Improve the reported location.
1777
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001778 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001779 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001780 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1781 ObjCDeclSpec::DQ_PR_assign |
1782 ObjCDeclSpec::DQ_PR_copy |
1783 ObjCDeclSpec::DQ_PR_retain))) {
1784 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1785 "readwrite" :
1786 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1787 "assign" :
1788 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1789 "copy" : "retain";
1790
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001791 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001792 diag::err_objc_property_attr_mutually_exclusive :
1793 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001794 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001795 }
1796
1797 // Check for copy or retain on non-object types.
1798 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Steve Narofff4954562009-07-16 15:41:00 +00001799 !PropertyTy->isObjCObjectPointerType() &&
1800 !PropertyTy->isBlockPointerType() &&
1801 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001802 Diag(Loc, diag::err_objc_property_requires_object)
1803 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001804 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1805 }
1806
1807 // Check for more than one of { assign, copy, retain }.
1808 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1809 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001810 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1811 << "assign" << "copy";
1812 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001813 }
1814 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001815 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1816 << "assign" << "retain";
1817 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001818 }
1819 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1820 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001821 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1822 << "copy" << "retain";
1823 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001824 }
1825 }
1826
1827 // Warn if user supplied no assignment attribute, property is
1828 // readwrite, and this is an object type.
1829 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1830 ObjCDeclSpec::DQ_PR_retain)) &&
1831 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00001832 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001833 // Skip this warning in gc-only mode.
1834 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1835 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1836
1837 // If non-gc code warn that this is likely inappropriate.
1838 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1839 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1840
1841 // FIXME: Implement warning dependent on NSCopying being
1842 // implemented. See also:
1843 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1844 // (please trim this list while you are at it).
1845 }
Mike Stump046efd92009-05-07 23:06:50 +00001846
1847 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1848 && getLangOptions().getGCMode() == LangOptions::GCOnly
1849 && PropertyTy->isBlockPointerType())
1850 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001851}
1852
Chris Lattnerb28317a2009-03-28 19:18:32 +00001853Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1854 FieldDeclarator &FD,
1855 ObjCDeclSpec &ODS,
1856 Selector GetterSel,
1857 Selector SetterSel,
1858 DeclPtrTy ClassCategory,
1859 bool *isOverridingProperty,
1860 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001861 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001862 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1863 // default is readwrite!
1864 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1865 // property is defaulted to 'assign' if it is readwrite and is
1866 // not retain or copy
1867 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1868 (isReadWrite &&
1869 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1870 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1871 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001872 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001873 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001874 // May modify Attributes.
1875 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001876 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1877 if (!CDecl->getIdentifier()) {
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001878 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001879 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001880 if ((CCPrimary = CDecl->getClassInterface())) {
1881 // Find the property in continuation class's primary class only.
1882 ObjCPropertyDecl *PIDecl = 0;
1883 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001884 for (ObjCInterfaceDecl::prop_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001885 I = CCPrimary->prop_begin(), E = CCPrimary->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001886 I != E; ++I)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001887 if ((*I)->getIdentifier() == PropertyId) {
1888 PIDecl = *I;
1889 break;
1890 }
1891
1892 if (PIDecl) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001893 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001894 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001895 unsigned PIkind = PIDecl->getPropertyAttributes();
1896 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001897 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001898 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1899 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001900 PIDecl->makeitReadWriteAttribute();
1901 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1902 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1903 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1904 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1905 PIDecl->setSetterName(SetterSel);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001906 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001907 else
1908 Diag(AtLoc, diag::err_use_continuation_class)
1909 << CCPrimary->getDeclName();
1910 *isOverridingProperty = true;
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001911 // Make sure setter decl is synthesized, and added to primary
1912 // class's list.
1913 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001914 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001915 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001916 // No matching property found in the primary class. Just fall thru
1917 // and add property to continuation class's primary class.
1918 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001919 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001920 Diag(CDecl->getLocation(), diag::err_continuation_class);
1921 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001922 return DeclPtrTy();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001923 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001924 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001925
Steve Naroff93983f82009-01-11 12:47:58 +00001926 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1927 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001928 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1929 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001930 FD.D.getIdentifier(), T);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001931 DC->addDecl(PDecl);
Chris Lattner97a58872009-02-16 18:32:47 +00001932
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00001933 if (T->isArrayType() || T->isFunctionType()) {
1934 Diag(AtLoc, diag::err_property_type) << T;
1935 PDecl->setInvalidDecl();
1936 }
1937
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001938 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00001939
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001940 // Regardless of setter/getter attribute, we save the default getter/setter
1941 // selector names in anticipation of declaration of setter/getter methods.
1942 PDecl->setGetterName(GetterSel);
1943 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001944
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001945 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001946 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001947
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001948 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001949 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001950
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001951 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001952 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001953
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001954 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001955 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001956
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001957 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001958 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001959
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001960 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001961 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001962
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001963 if (isAssign)
1964 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1965
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001966 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001967 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001968
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001969 if (MethodImplKind == tok::objc_required)
1970 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1971 else if (MethodImplKind == tok::objc_optional)
1972 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001973 // A case of continuation class adding a new property in the class. This
1974 // is not what it was meant for. However, gcc supports it and so should we.
1975 // Make sure setter/getters are declared here.
1976 if (CCPrimary)
1977 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001978
Chris Lattnerb28317a2009-03-28 19:18:32 +00001979 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001980}
1981
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001982/// ActOnPropertyImplDecl - This routine performs semantic checks and
1983/// builds the AST node for a property implementation declaration; declared
1984/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001985///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001986Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1987 SourceLocation PropertyLoc,
1988 bool Synthesize,
1989 DeclPtrTy ClassCatImpDecl,
1990 IdentifierInfo *PropertyId,
1991 IdentifierInfo *PropertyIvar) {
1992 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001993 // Make sure we have a context for the property implementation declaration.
1994 if (!ClassImpDecl) {
1995 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001996 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001997 }
1998 ObjCPropertyDecl *property = 0;
1999 ObjCInterfaceDecl* IDecl = 0;
2000 // Find the class or category class where this property must have
2001 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002002 ObjCImplementationDecl *IC = 0;
2003 ObjCCategoryImplDecl* CatImplClass = 0;
2004 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002005 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002006 // We always synthesize an interface for an implementation
2007 // without an interface decl. So, IDecl is always non-zero.
2008 assert(IDecl &&
2009 "ActOnPropertyImplDecl - @implementation without @interface");
2010
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002011 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002012 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002013 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002014 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002015 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002016 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002017 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002018 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002019 if (Synthesize) {
2020 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002021 return DeclPtrTy();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002022 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002023 IDecl = CatImplClass->getClassInterface();
2024 if (!IDecl) {
2025 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002026 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002027 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002028 ObjCCategoryDecl *Category =
2029 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
2030
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002031 // If category for this implementation not found, it is an error which
2032 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002033 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002034 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002035 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002036 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002037 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002038 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002039 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002040 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002041 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002042 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002043 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002044 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002045 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002046 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002047 // Check that we have a valid, previously declared ivar for @synthesize
2048 if (Synthesize) {
2049 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002050 if (!PropertyIvar)
2051 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002052 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002053 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002054 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002055 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002056 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002057 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
2058 assert(EnclosingContext &&
2059 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
2060 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002061 PropertyIvar, PropType,
2062 ObjCIvarDecl::Public,
2063 (Expr *)0);
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002064 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002065 IDecl->addDecl(Ivar);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002066 property->setPropertyIvarDecl(Ivar);
2067 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002068 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002069 // Note! I deliberately want it to fall thru so, we have a
2070 // a property implementation and to avoid future warnings.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002071 }
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002072 else if (getLangOptions().ObjCNonFragileABI &&
Fariborz Jahanian6e5201b2009-04-29 21:45:02 +00002073 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002074 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002075 << property->getDeclName() << Ivar->getDeclName()
2076 << ClassDeclared->getDeclName();
2077 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2078 << Ivar << Ivar->getNameAsCString();
2079 // Note! I deliberately want it to fall thru so more errors are caught.
2080 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002081 QualType IvarType = Context.getCanonicalType(Ivar->getType());
2082
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002083 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002084 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002085 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002086 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002087 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002088 // Note! I deliberately want it to fall thru so, we have a
2089 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002090 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002091
2092 // FIXME! Rules for properties are somewhat different that those
2093 // for assignments. Use a new routine to consolidate all cases;
2094 // specifically for property redeclarations as well as for ivars.
2095 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2096 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
2097 if (lhsType != rhsType &&
2098 lhsType->isArithmeticType()) {
2099 Diag(PropertyLoc, diag::error_property_ivar_type)
2100 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002101 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002102 }
2103 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002104 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2105 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002106 Diag(PropertyLoc, diag::error_weak_property)
2107 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002108 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002109 }
Steve Naroff14108da2009-07-10 23:34:53 +00002110 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002111 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2112 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002113 Diag(PropertyLoc, diag::error_strong_property)
2114 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002115 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002116 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002117 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002118 } else if (PropertyIvar)
2119 // @dynamic
2120 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002121 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002122 ObjCPropertyImplDecl *PIDecl =
Douglas Gregord0434102009-01-09 00:49:46 +00002123 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2124 property,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002125 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002126 ObjCPropertyImplDecl::Synthesize
2127 : ObjCPropertyImplDecl::Dynamic),
2128 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002129 if (IC) {
2130 if (Synthesize)
2131 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002132 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002133 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2134 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2135 << PropertyIvar;
2136 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2137 }
2138
Douglas Gregor653f1b12009-04-23 01:02:12 +00002139 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002140 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002141 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2142 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002143 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002144 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002145 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002146 }
2147 else {
2148 if (Synthesize)
2149 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002150 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002151 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2152 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2153 << PropertyIvar;
2154 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2155 }
2156
2157 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002158 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002159 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2160 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002161 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002162 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002163 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002164 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002165
Chris Lattnerb28317a2009-03-28 19:18:32 +00002166 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002167}
Anders Carlsson15281452008-11-04 16:57:32 +00002168
Chris Lattnercc98eac2008-12-17 07:13:27 +00002169bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002170 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002171 return false;
2172
2173 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2174 D->setInvalidDecl();
2175
2176 return true;
2177}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002178
Chris Lattnercc98eac2008-12-17 07:13:27 +00002179/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2180/// instance variables of ClassName into Decls.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002181void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002182 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002183 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002184 // Check that ClassName is a valid class
2185 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2186 if (!Class) {
2187 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2188 return;
2189 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002190 if (LangOpts.ObjCNonFragileABI) {
2191 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2192 return;
2193 }
2194
Chris Lattnercc98eac2008-12-17 07:13:27 +00002195 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002196 llvm::SmallVector<FieldDecl*, 32> RecFields;
2197 Context.CollectObjCIvars(Class, RecFields);
2198 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2199 for (unsigned i = 0; i < RecFields.size(); i++) {
2200 FieldDecl* ID = RecFields[i];
2201 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2202 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2203 ID->getIdentifier(), ID->getType(),
2204 ID->getBitWidth());
2205 Decls.push_back(Sema::DeclPtrTy::make(FD));
2206 }
Chris Lattnercc98eac2008-12-17 07:13:27 +00002207
2208 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002209 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002210 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002211 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002212 if (getLangOptions().CPlusPlus)
2213 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002214 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002215 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002216 }
2217}
2218