blob: 89bde815c0cc4f5b4a668f6475c208fea503e197 [file] [log] [blame]
Chris Lattner855e51f2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner855e51f2007-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 Gregorc3221aa2009-04-24 21:10:55 +000015#include "clang/Sema/ExternalSemaSource.h"
Steve Naroff58d0d6f2009-03-03 14:49:36 +000016#include "clang/AST/Expr.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclObjC.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000020using namespace clang;
21
Fariborz Jahanian80ccaa92009-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 Naroffad75bd22009-07-16 15:41:00 +000028 if (property->getType()->isObjCObjectPointerType())
Fariborz Jahaniand7f00252009-05-08 21:10:00 +000029 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
Fariborz Jahanian80ccaa92009-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 Naroff8df46022009-02-28 16:59:13 +000041/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000042/// and user declared, in the method definition's AST.
Chris Lattner5261d0c2009-03-28 19:18:32 +000043void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000044 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner5261d0c2009-03-28 19:18:32 +000045 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Steve Naroff3ac43f92008-07-25 17:57:26 +000046
47 // If we don't have a valid method decl, simply return.
48 if (!MDecl)
49 return;
Steve Narofffe9eb6a2007-12-18 01:30:32 +000050
Chris Lattnerc92920c2009-04-19 05:21:20 +000051 CurFunctionNeedsScopeChecking = false;
52
Steve Narofffe9eb6a2007-12-18 01:30:32 +000053 // Allow the rest of sema to find private method decl implementations.
Douglas Gregor5d764842009-01-09 17:18:27 +000054 if (MDecl->isInstanceMethod())
Steve Narofffe9eb6a2007-12-18 01:30:32 +000055 AddInstanceMethodToGlobalPool(MDecl);
56 else
57 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000058
59 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor8acb7272008-12-11 16:49:14 +000060 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner855e51f2007-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 Lattner855e51f2007-12-12 07:09:47 +000064
65 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +000066 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +000067
Daniel Dunbareaf91c32008-08-26 06:07:48 +000068 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
69 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000070
Chris Lattner97316c02008-04-10 02:22:51 +000071 // Introduce all of the other parameters into this scope.
Chris Lattner5c6b2c62009-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 Lattner855e51f2007-12-12 07:09:47 +000076}
77
Chris Lattner5261d0c2009-03-28 19:18:32 +000078Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +000079ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
80 IdentifierInfo *ClassName, SourceLocation ClassLoc,
81 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +000082 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000083 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000084 assert(ClassName && "Missing class identifier");
85
86 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +000087 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +000088 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-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 Kremenek42730c52008-01-07 19:49:32 +000095 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000096 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +000097 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +000098 }
99
Ted Kremenek42730c52008-01-07 19:49:32 +0000100 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000101 if (IDecl) {
102 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +0000103 if (!IDecl->isForwardDecl()) {
Chris Lattner9513cfd2009-02-23 22:00:08 +0000104 IDecl->setInvalidDecl();
Chris Lattner271d4c22008-11-24 05:29:24 +0000105 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattner5b250652008-11-23 22:46:27 +0000106 Diag(IDecl->getLocation(), diag::note_previous_definition);
107
Steve Naroff1422a622008-11-18 19:15:30 +0000108 // Return the previous class interface.
109 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +0000110 return DeclPtrTy::make(IDecl);
Steve Naroff1422a622008-11-18 19:15:30 +0000111 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +0000112 IDecl->setLocation(AtInterfaceLoc);
113 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +0000114 }
Chris Lattner5cece462008-07-21 07:06:49 +0000115 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000116 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +0000117 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000118 if (AttrList)
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000119 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000120
Steve Naroff104956f2009-04-23 15:15:40 +0000121 PushOnScopeChains(IDecl, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000122 }
123
124 if (SuperName) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000125 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000126 PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName);
Fariborz Jahanian47325432009-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 Lattner65cae292008-11-19 08:23:25 +0000135
Fariborz Jahanian47325432009-07-09 22:08:26 +0000136 // Diagnose classes that inherit from deprecated classes.
137 if (SuperClassDecl)
138 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Chris Lattner7699a532009-02-16 21:33:09 +0000139
Fariborz Jahanian47325432009-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 Naroffcb3beaf2009-02-04 17:14:05 +0000159 }
160 }
Fariborz Jahanian47325432009-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 Naroffcb3beaf2009-02-04 17:14:05 +0000170 }
Fariborz Jahanian47325432009-07-09 22:08:26 +0000171 IDecl->setSuperClass(SuperClassDecl);
172 IDecl->setSuperClassLoc(SuperLoc);
173 IDecl->setLocEnd(SuperLoc);
Steve Naroffcb3beaf2009-02-04 17:14:05 +0000174 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000175 } else { // we have a root class.
176 IDecl->setLocEnd(ClassLoc);
177 }
178
Steve Naroff1422a622008-11-18 19:15:30 +0000179 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000180 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000181 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
182 Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000183 IDecl->setLocEnd(EndProtoLoc);
184 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000185
186 CheckObjCDeclScope(IDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000187 return DeclPtrTy::make(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000188}
189
190/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000191/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000192Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
193 IdentifierInfo *AliasName,
194 SourceLocation AliasLocation,
195 IdentifierInfo *ClassName,
196 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000197 // Look for previous declaration of alias name
Douglas Gregor09be81b2009-02-04 17:27:36 +0000198 NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000199 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000200 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000201 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000202 else
Chris Lattner65cae292008-11-19 08:23:25 +0000203 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000204 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000205 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000206 }
207 // Check for class declaration
Douglas Gregor09be81b2009-02-04 17:27:36 +0000208 NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-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 Gregor52ae30c2009-01-30 01:04:22 +0000214 CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian2ac4cd32009-01-08 01:10:55 +0000215 }
216 }
217 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000218 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
219 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000220 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000221 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000222 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000223 return DeclPtrTy();
Chris Lattner855e51f2007-12-12 07:09:47 +0000224 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000225
226 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000227 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000228 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Steve Naroffe57c21a2008-04-01 23:04:06 +0000229
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000230 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor54713b62009-04-24 02:57:34 +0000231 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000232
Chris Lattner5261d0c2009-03-28 19:18:32 +0000233 return DeclPtrTy::make(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000234}
235
Steve Narofffe56a632009-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 Gregore57c8cc2009-04-23 23:18:26 +0000244 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Narofffe56a632009-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 Lattner5261d0c2009-03-28 19:18:32 +0000255Sema::DeclPtrTy
Chris Lattner2bdedd62008-07-26 04:03:38 +0000256Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
257 IdentifierInfo *ProtocolName,
258 SourceLocation ProtocolLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000259 const DeclPtrTy *ProtoRefs,
Chris Lattner2bdedd62008-07-26 04:03:38 +0000260 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000261 SourceLocation EndProtoLoc,
262 AttributeList *AttrList) {
263 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000264 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000265 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000266 if (PDecl) {
267 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000268 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian68199552009-04-06 23:43:32 +0000269 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000270 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-03-16 01:25:17 +0000271 // Just return the protocol we already had.
272 // FIXME: don't leak the objects passed in!
Chris Lattner5261d0c2009-03-28 19:18:32 +0000273 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000274 }
Steve Narofffe56a632009-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 Naroff9a9e5212008-08-13 16:39:22 +0000281 // Make sure the cached decl gets a valid start location.
282 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000283 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000284 } else {
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000285 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
286 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000287 PushOnScopeChains(PDecl, TUScope);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000288 PDecl->setForwardDecl(false);
Chris Lattner180f7e22008-03-16 01:23:04 +0000289 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000290 if (AttrList)
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000291 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000292 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000293 /// Check then save referenced protocols.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000294 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner855e51f2007-12-12 07:09:47 +0000295 PDecl->setLocEnd(EndProtoLoc);
296 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000297
298 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000299 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000300}
301
302/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-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 Lattner855e51f2007-12-12 07:09:47 +0000305void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000306Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000307 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000308 unsigned NumProtocols,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000309 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000310 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000311 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattner17d50a92008-07-26 03:47:43 +0000312 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000313 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000314 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000315 continue;
316 }
Chris Lattnerfaaad132009-02-14 08:22:25 +0000317
Douglas Gregoraa57e862009-02-18 21:56:37 +0000318 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner17d50a92008-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 Lattner8ba580c2008-11-19 05:08:23 +0000323 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000324 << ProtocolId[i].first;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000325 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000326 }
327}
328
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000329/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000330/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000331///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000332void
333Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
334 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000335 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-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 Lattner8ba580c2008-11-19 05:08:23 +0000342 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000343 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000344 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
345 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000346 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000347 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000348 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
349 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000350 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000351 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000352
353 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
354 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000355 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000356 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000357 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000358 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000359 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000360 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000361 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000362 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff2567c9e2009-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())
372 if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
373 return;
374 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
375 << Property->getType() << SuperProperty->getType() << inheritedName;
376 }
Fariborz Jahanian1e89de32008-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 Lattner1fa7f622009-02-16 21:26:43 +0000383void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000384 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
385 if (!SDecl)
386 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000387 // FIXME: O(N^2)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000388 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
389 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000390 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000391 // Does property in super class has declaration in current class?
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000392 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
393 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000394 ObjCPropertyDecl *PDecl = (*I);
395 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000396 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000397 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000398 }
399 }
400}
401
Fariborz Jahanian33973a22008-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 Jahanianacad6d12008-12-06 23:03:39 +0000404/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000405void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000406Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000407 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianacad6d12008-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");
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000413 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
414 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000415 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000416 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000417 // Is this property already in category's list of properties?
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000418 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000419 if ((*CP)->getIdentifier() == Pr->getIdentifier())
420 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000421 if (CP != CE)
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000422 // Property protocol already exist in class. Diagnose any mismatch.
423 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
424 }
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000425 return;
426 }
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000427 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
428 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000429 ObjCPropertyDecl *Pr = (*P);
Steve Naroff451f83c2009-01-09 15:36:25 +0000430 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000431 // Is this property already in class's list of properties?
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000432 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000433 if ((*CP)->getIdentifier() == Pr->getIdentifier())
434 break;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000435 if (CP != CE)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000436 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000437 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000438 }
Fariborz Jahanian33973a22008-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 Jahanianacad6d12008-12-06 23:03:39 +0000443/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000444///
Chris Lattner1fa7f622009-02-16 21:26:43 +0000445void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000446 DeclPtrTy MergeItsProtocols) {
447 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanianacad6d12008-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 Lattner5261d0c2009-03-28 19:18:32 +0000464 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanianacad6d12008-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 Lattner5261d0c2009-03-28 19:18:32 +0000469 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000470 }
471 return;
472 }
473
Chris Lattner5cece462008-07-21 07:06:49 +0000474 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000475 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
476 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000477 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000478 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
479
Fariborz Jahanian33973a22008-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 Lattner5261d0c2009-03-28 19:18:32 +0000484 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000485 } else {
Argiris Kirtzidisfc1ea132008-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 Lattner5261d0c2009-03-28 19:18:32 +0000489 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattner5cece462008-07-21 07:06:49 +0000490 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000491}
492
Fariborz Jahanian38a1acc2009-03-02 19:06:08 +0000493/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianffb68822009-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;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000502 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
503 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianffb68822009-03-02 19:05:07 +0000504 ObjCMethodDecl *MD = *i;
505 MethodMap[MD->getSelector()] = MD;
506 }
507
508 if (MethodMap.empty())
509 return;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000510 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
511 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianffb68822009-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 Lattnere6dcfc22009-04-12 08:43:13 +0000522/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000523Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +0000524Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000525 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000526 unsigned NumElts,
527 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000528 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000529
530 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000531 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000532 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000533 if (PDecl == 0) { // Not already seen?
534 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
535 IdentList[i].second, Ident);
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000536 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000537 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000538 if (attrList)
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000539 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000540 Protocols.push_back(PDecl);
541 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000542
543 ObjCForwardProtocolDecl *PDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000544 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000545 &Protocols[0], Protocols.size());
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000546 CurContext->addDecl(PDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000547 CheckObjCDeclScope(PDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000548 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000549}
550
Chris Lattner5261d0c2009-03-28 19:18:32 +0000551Sema::DeclPtrTy Sema::
Chris Lattnere705e5e2008-07-21 22:17:28 +0000552ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
553 IdentifierInfo *ClassName, SourceLocation ClassLoc,
554 IdentifierInfo *CategoryName,
555 SourceLocation CategoryLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000556 const DeclPtrTy *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000557 unsigned NumProtoRefs,
558 SourceLocation EndProtoLoc) {
Chris Lattnere29dc832008-03-16 20:34:23 +0000559 ObjCCategoryDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000560 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
561 // FIXME: PushOnScopeChains?
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000562 CurContext->addDecl(CDecl);
Chris Lattner1fa7f622009-02-16 21:26:43 +0000563
564 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000565 /// Check that class of this category is already completely declared.
Chris Lattner1fa7f622009-02-16 21:26:43 +0000566 if (!IDecl || IDecl->isForwardDecl()) {
567 CDecl->setInvalidDecl();
Chris Lattner65cae292008-11-19 08:23:25 +0000568 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000569 return DeclPtrTy::make(CDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000570 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000571
Chris Lattner1fa7f622009-02-16 21:26:43 +0000572 CDecl->setClassInterface(IDecl);
Chris Lattnerde3fea82009-02-16 21:30:01 +0000573
574 // If the interface is deprecated, warn about it.
Douglas Gregoraa57e862009-02-18 21:56:37 +0000575 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner1fa7f622009-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 Lattner855e51f2007-12-12 07:09:47 +0000591 if (NumProtoRefs) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000592 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner45142b92008-07-26 04:07:02 +0000593 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000594 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000595
596 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000597 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000598}
599
600/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000601/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000602/// object.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000603Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000604 SourceLocation AtCatImplLoc,
605 IdentifierInfo *ClassName, SourceLocation ClassLoc,
606 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000607 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Argiris Kirtzidis3a4d9832009-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 Lattner1b6de332008-03-16 20:53:07 +0000621 ObjCCategoryImplDecl *CDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000622 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
623 IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000624 /// Check that class of this category is already completely declared.
625 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000626 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000627
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000628 // FIXME: PushOnScopeChains?
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000629 CurContext->addDecl(CDecl);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000630
Argiris Kirtzidis3a4d9832009-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 }
641
Steve Naroff4b9846d2008-09-28 14:55:53 +0000642 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000643
644 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000645 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000646}
647
Chris Lattner5261d0c2009-03-28 19:18:32 +0000648Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner855e51f2007-12-12 07:09:47 +0000649 SourceLocation AtClassImplLoc,
650 IdentifierInfo *ClassName, SourceLocation ClassLoc,
651 IdentifierInfo *SuperClassname,
652 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000653 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000654 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +0000655 NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000656 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000657 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000658 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner9513cfd2009-02-23 22:00:08 +0000659 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +0000660 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000661 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanianf7f09ab2009-04-23 21:49:04 +0000662 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000663 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanianf7f09ab2009-04-23 21:49:04 +0000664 IDecl = 0;
665 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000666 }
667
668 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000669 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000670 if (SuperClassname) {
671 // Check if a different kind of symbol declared in this scope.
Douglas Gregor52ae30c2009-01-30 01:04:22 +0000672 PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000673 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000674 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
675 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000676 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000677 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000678 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000679 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000680 Diag(SuperClassLoc, diag::err_undef_superclass)
681 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000682 else if (IDecl && IDecl->getSuperClass() != SDecl) {
683 // This implementation and its interface do not have the same
684 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000685 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000686 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000687 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000688 }
689 }
690 }
691
692 if (!IDecl) {
693 // Legacy case of @implementation with no corresponding @interface.
694 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000695
Mike Stumpe127ae32009-05-16 07:39:55 +0000696 // FIXME: Do we support attributes on the @implementation? If so we should
697 // copy them over.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000698 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
699 ClassName, ClassLoc, false, true);
Chris Lattner855e51f2007-12-12 07:09:47 +0000700 IDecl->setSuperClass(SDecl);
701 IDecl->setLocEnd(ClassLoc);
Douglas Gregor453e11d2009-04-24 00:16:12 +0000702
703 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbardbc8af72009-04-21 21:41:56 +0000704 } else {
705 // Mark the interface as being completed, even if it was just as
706 // @class ....;
707 // declaration; the user cannot reopen it.
708 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +0000709 }
710
Ted Kremenek42730c52008-01-07 19:49:32 +0000711 ObjCImplementationDecl* IMPDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000712 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000713 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000714
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000715 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000716 return DeclPtrTy::make(IMPDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000717
Chris Lattner855e51f2007-12-12 07:09:47 +0000718 // Check that there is no duplicate implementation of this class.
Argiris Kirtzidisb1c4ee52009-07-21 00:06:04 +0000719 if (IDecl->getImplementation()) {
Chris Lattner1b6de332008-03-16 20:53:07 +0000720 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000721 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argiris Kirtzidisb1c4ee52009-07-21 00:06:04 +0000722 Diag(IDecl->getImplementation()->getLocation(),
723 diag::note_previous_definition);
724 } else { // add it to the list.
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000725 IDecl->setImplementation(IMPDecl);
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000726 PushOnScopeChains(IMPDecl, TUScope);
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000727 }
Chris Lattner5261d0c2009-03-28 19:18:32 +0000728 return DeclPtrTy::make(IMPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000729}
730
Ted Kremenek42730c52008-01-07 19:49:32 +0000731void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
732 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000733 SourceLocation RBrace) {
734 assert(ImpDecl && "missing implementation decl");
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000735 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000736 if (!IDecl)
737 return;
738 /// Check case of non-existing @interface decl.
739 /// (legacy objective-c @implementation decl without an @interface decl).
740 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff7333b492009-04-20 20:09:33 +0000741 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000742 IDecl->setIVarList(ivars, numIvars, Context);
743 IDecl->setLocEnd(RBrace);
Chris Lattner855e51f2007-12-12 07:09:47 +0000744 return;
745 }
746 // If implementation has empty ivar list, just return.
747 if (numIvars == 0)
748 return;
749
750 assert(ivars && "missing @implementation ivars");
751
752 // Check interface's Ivar list against those in the implementation.
753 // names and types must match.
754 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000755 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000756 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000757 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
758 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000759 ObjCIvarDecl* ImplIvar = ivars[j++];
760 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000761 assert (ImplIvar && "missing implementation ivar");
762 assert (ClsIvar && "missing class ivar");
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000763
764 // First, make sure the types match.
Chris Lattnerb5457862008-07-27 00:05:05 +0000765 if (Context.getCanonicalType(ImplIvar->getType()) !=
766 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000767 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000768 << ImplIvar->getIdentifier()
769 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000770 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000771 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
772 Expr *ImplBitWidth = ImplIvar->getBitWidth();
773 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman5255e7a2009-04-26 19:19:15 +0000774 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
775 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff58d0d6f2009-03-03 14:49:36 +0000776 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
777 << ImplIvar->getIdentifier();
778 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
779 }
780 }
781 // Make sure the names are identical.
782 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000783 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000784 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000785 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000786 }
787 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000788 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000789
790 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000791 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000792 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000793 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000794}
795
Steve Naroffb4f48512008-02-10 21:38:56 +0000796void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
797 bool &IncompleteImpl) {
798 if (!IncompleteImpl) {
799 Diag(ImpLoc, diag::warn_incomplete_impl);
800 IncompleteImpl = true;
801 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000802 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000803}
804
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000805void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
806 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnerb95aea02009-04-11 18:01:59 +0000807 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian6e83c922009-05-14 23:52:54 +0000808 ImpMethodDecl->getResultType()) &&
809 !QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
810 ImpMethodDecl->getResultType())) {
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000811 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
812 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
813 << ImpMethodDecl->getResultType();
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000814 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
815 }
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000816
817 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
818 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
819 IM != EM; ++IM, ++IF) {
Fariborz Jahanian6e83c922009-05-14 23:52:54 +0000820 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()) ||
821 QualifiedIdConformsQualifiedId((*IF)->getType(), (*IM)->getType()))
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000822 continue;
823
824 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
825 << ImpMethodDecl->getDeclName() << (*IF)->getType()
826 << (*IM)->getType();
Chris Lattner6e48be22009-04-11 20:14:49 +0000827 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner7e4c91b2009-04-11 19:58:42 +0000828 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000829}
830
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000831/// isPropertyReadonly - Return true if property is readonly, by searching
832/// for the property in the class and in its categories and implementations
833///
834bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000835 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000836 // by far the most common case.
837 if (!PDecl->isReadOnly())
838 return false;
839 // Even if property is ready only, if interface has a user defined setter,
840 // it is not considered read only.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000841 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000842 return false;
843
844 // Main class has the property as 'readonly'. Must search
845 // through the category list to see if the property's
846 // attribute has been over-ridden to 'readwrite'.
847 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
848 Category; Category = Category->getNextClassCategory()) {
849 // Even if property is ready only, if a category has a user defined setter,
850 // it is not considered read only.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000851 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000852 return false;
853 ObjCPropertyDecl *P =
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000854 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000855 if (P && !P->isReadOnly())
856 return false;
857 }
858
859 // Also, check for definition of a setter method in the implementation if
860 // all else failed.
861 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
862 if (ObjCImplementationDecl *IMD =
863 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000864 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000865 return false;
866 }
867 else if (ObjCCategoryImplDecl *CIMD =
868 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000869 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000870 return false;
871 }
872 }
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000873 // Lastly, look through the implementation (if one is in scope).
Argiris Kirtzidisb1c4ee52009-07-21 00:06:04 +0000874 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000875 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroffb0bf2c92009-02-26 19:11:32 +0000876 return false;
Fariborz Jahanianbfdf9db2009-04-06 16:59:10 +0000877 // If all fails, look at the super class.
878 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
879 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000880 return true;
881}
882
Mike Stumpe127ae32009-05-16 07:39:55 +0000883/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
884/// improve the efficiency of selector lookups and type checking by associating
885/// with each protocol / interface / category the flattened instance tables. If
886/// we used an immutable set to keep the table then it wouldn't add significant
887/// memory cost and it would be handy for lookups.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000888
Steve Naroffb268d2a2008-02-08 22:06:17 +0000889/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000890/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000891void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
892 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000893 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000894 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000895 const llvm::DenseSet<Selector> &ClsMap,
896 ObjCInterfaceDecl *IDecl) {
897 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahanianc0672e42009-05-22 17:12:32 +0000898 ObjCInterfaceDecl *NSIDecl = 0;
899 if (getLangOptions().NeXTRuntime) {
900 // check to see if class implements forwardInvocation method and objects
901 // of this class are derived from 'NSProxy' so that to forward requests
902 // from one object to another.
903 // Under such conditions, which means that every method possible is
904 // implemented in the class, we should not issue "Method definition not
905 // found" warnings.
906 // FIXME: Use a general GetUnarySelector method for this.
907 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
908 Selector fISelector = Context.Selectors.getSelector(1, &II);
909 if (InsMap.count(fISelector))
910 // Is IDecl derived from 'NSProxy'? If so, no instance methods
911 // need be implemented in the implementation.
912 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
913 }
914
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000915 // If a method lookup fails locally we still need to look and see if
916 // the method was implemented by a base class or an inherited
917 // protocol. This lookup is slow, but occurs rarely in correct code
918 // and otherwise would terminate in a warning.
919
Chris Lattner855e51f2007-12-12 07:09:47 +0000920 // check unimplemented instance methods.
Fariborz Jahanianc0672e42009-05-22 17:12:32 +0000921 if (!NSIDecl)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000922 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
923 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianc0672e42009-05-22 17:12:32 +0000924 ObjCMethodDecl *method = *I;
925 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
926 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
927 (!Super ||
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000928 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianc0672e42009-05-22 17:12:32 +0000929 // Ugly, but necessary. Method declared in protcol might have
930 // have been synthesized due to a property declared in the class which
931 // uses the protocol.
932 ObjCMethodDecl *MethodInClass =
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000933 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanianc0672e42009-05-22 17:12:32 +0000934 if (!MethodInClass || !MethodInClass->isSynthesized())
935 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
936 }
937 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000938 // check unimplemented class methods
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000939 for (ObjCProtocolDecl::classmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000940 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000941 I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000942 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000943 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
944 !ClsMap.count(method->getSelector()) &&
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000945 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000946 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000947 }
Chris Lattner0be08822008-07-21 21:32:27 +0000948 // Check on this protocols's referenced protocols, recursively.
949 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
950 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000951 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000952}
953
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +0000954/// MatchAllMethodDeclarations - Check methods declaraed in interface or
955/// or protocol against those declared in their implementations.
956///
957void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
958 const llvm::DenseSet<Selector> &ClsMap,
959 llvm::DenseSet<Selector> &InsMapSeen,
960 llvm::DenseSet<Selector> &ClsMapSeen,
961 ObjCImplDecl* IMPDecl,
962 ObjCContainerDecl* CDecl,
963 bool &IncompleteImpl,
964 bool ImmediateClass)
965{
966 // Check and see if instance methods in class interface have been
967 // implemented in the implementation class. If so, their types match.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000968 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
969 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +0000970 if (InsMapSeen.count((*I)->getSelector()))
971 continue;
972 InsMapSeen.insert((*I)->getSelector());
973 if (!(*I)->isSynthesized() &&
974 !InsMap.count((*I)->getSelector())) {
975 if (ImmediateClass)
976 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
977 continue;
978 }
979 else {
980 ObjCMethodDecl *ImpMethodDecl =
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000981 IMPDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +0000982 ObjCMethodDecl *IntfMethodDecl =
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000983 CDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +0000984 assert(IntfMethodDecl &&
985 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
986 // ImpMethodDecl may be null as in a @dynamic property.
987 if (ImpMethodDecl)
988 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
989 }
990 }
991
992 // Check and see if class methods in class interface have been
993 // implemented in the implementation class. If so, their types match.
994 for (ObjCInterfaceDecl::classmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000995 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +0000996 if (ClsMapSeen.count((*I)->getSelector()))
997 continue;
998 ClsMapSeen.insert((*I)->getSelector());
999 if (!ClsMap.count((*I)->getSelector())) {
1000 if (ImmediateClass)
1001 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1002 }
1003 else {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001004 ObjCMethodDecl *ImpMethodDecl =
1005 IMPDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001006 ObjCMethodDecl *IntfMethodDecl =
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001007 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001008 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1009 }
1010 }
1011 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1012 // Check for any implementation of a methods declared in protocol.
1013 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1014 E = I->protocol_end(); PI != E; ++PI)
1015 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1016 IMPDecl,
1017 (*PI), IncompleteImpl, false);
1018 if (I->getSuperClass())
1019 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1020 IMPDecl,
1021 I->getSuperClass(), IncompleteImpl, false);
1022 }
1023}
1024
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001025void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1026 ObjCContainerDecl* CDecl,
1027 bool IncompleteImpl) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001028 llvm::DenseSet<Selector> InsMap;
1029 // Check and see if instance methods in class interface have been
1030 // implemented in the implementation class.
Douglas Gregorcd19b572009-04-23 01:02:12 +00001031 for (ObjCImplementationDecl::instmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001032 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +00001033 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +00001034
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001035 // Check and see if properties declared in the interface have either 1)
1036 // an implementation or 2) there is a @synthesize/@dynamic implementation
1037 // of the property in the @implementation.
1038 if (isa<ObjCInterfaceDecl>(CDecl))
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001039 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1040 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001041 ObjCPropertyDecl *Prop = (*P);
1042 if (Prop->isInvalidDecl())
1043 continue;
1044 ObjCPropertyImplDecl *PI = 0;
1045 // Is there a matching propery synthesize/dynamic?
Douglas Gregorcd19b572009-04-23 01:02:12 +00001046 for (ObjCImplDecl::propimpl_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001047 I = IMPDecl->propimpl_begin(),
1048 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00001049 if ((*I)->getPropertyDecl() == Prop) {
1050 PI = (*I);
1051 break;
1052 }
1053 if (PI)
1054 continue;
1055 if (!InsMap.count(Prop->getGetterName())) {
1056 Diag(Prop->getLocation(),
1057 diag::warn_setter_getter_impl_required)
1058 << Prop->getDeclName() << Prop->getGetterName();
1059 Diag(IMPDecl->getLocation(),
1060 diag::note_property_impl_required);
1061 }
1062
1063 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1064 Diag(Prop->getLocation(),
1065 diag::warn_setter_getter_impl_required)
1066 << Prop->getDeclName() << Prop->getSetterName();
1067 Diag(IMPDecl->getLocation(),
1068 diag::note_property_impl_required);
1069 }
1070 }
1071
Chris Lattner855e51f2007-12-12 07:09:47 +00001072 llvm::DenseSet<Selector> ClsMap;
Douglas Gregorcd19b572009-04-23 01:02:12 +00001073 for (ObjCImplementationDecl::classmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001074 I = IMPDecl->classmeth_begin(),
1075 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner9d76c722007-12-12 17:58:05 +00001076 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +00001077
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001078 // Check for type conflict of methods declared in a class/protocol and
1079 // its implementation; if any.
1080 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1081 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1082 IMPDecl, CDecl,
1083 IncompleteImpl, true);
Chris Lattner855e51f2007-12-12 07:09:47 +00001084
1085 // Check the protocol list for unimplemented methods in the @implementation
1086 // class.
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001087 // Check and see if class methods in class interface have been
1088 // implemented in the implementation class.
1089
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001090 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian78eca5d2009-05-01 20:07:12 +00001091 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001092 E = I->protocol_end(); PI != E; ++PI)
1093 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1094 InsMap, ClsMap, I);
1095 // Check class extensions (unnamed categories)
1096 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1097 Categories; Categories = Categories->getNextClassCategory()) {
1098 if (!Categories->getIdentifier()) {
1099 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1100 break;
1101 }
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +00001102 }
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001103 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1104 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1105 E = C->protocol_end(); PI != E; ++PI)
1106 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1107 InsMap, ClsMap, C->getClassInterface());
1108 } else
1109 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner855e51f2007-12-12 07:09:47 +00001110}
1111
1112/// ActOnForwardClassDeclaration -
Chris Lattner5261d0c2009-03-28 19:18:32 +00001113Action::DeclPtrTy
Chris Lattner855e51f2007-12-12 07:09:47 +00001114Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner28400082009-02-16 19:25:52 +00001115 IdentifierInfo **IdentList,
1116 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001117 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +00001118
1119 for (unsigned i = 0; i != NumElts; ++i) {
1120 // Check for another declaration kind with the same name.
Douglas Gregor09be81b2009-02-04 17:27:36 +00001121 NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor2715a1f2008-12-08 18:40:42 +00001122 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +00001123 // Maybe we will complain about the shadowed template parameter.
1124 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1125 // Just pretend that we didn't see the previous declaration.
1126 PrevDecl = 0;
1127 }
1128
Ted Kremenek42730c52008-01-07 19:49:32 +00001129 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +00001130 // GCC apparently allows the following idiom:
1131 //
1132 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1133 // @class XCElementToggler;
1134 //
1135 // FIXME: Make an extension?
1136 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1137 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +00001138 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +00001139 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +00001140 }
Fariborz Jahanian4c16d762009-05-07 21:49:26 +00001141 else if (TDD) {
1142 // a forward class declaration matching a typedef name of a class
1143 // refers to the underlying class.
1144 if (ObjCInterfaceType * OI =
1145 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1146 PrevDecl = OI->getDecl();
1147 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001148 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001149 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001150 if (!IDecl) { // Not already seen? Make a forward decl.
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001151 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1152 IdentList[i], SourceLocation(), true);
Steve Naroff74b916a2009-04-23 16:00:56 +00001153 PushOnScopeChains(IDecl, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +00001154 }
1155
1156 Interfaces.push_back(IDecl);
1157 }
1158
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001159 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001160 &Interfaces[0],
1161 Interfaces.size());
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001162 CurContext->addDecl(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001163 CheckObjCDeclScope(CDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001164 return DeclPtrTy::make(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001165}
1166
1167
1168/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1169/// returns true, or false, accordingly.
1170/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +00001171bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +00001172 const ObjCMethodDecl *PrevMethod,
1173 bool matchBasedOnSizeAndAlignment) {
1174 QualType T1 = Context.getCanonicalType(Method->getResultType());
1175 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
1176
1177 if (T1 != T2) {
1178 // The result types are different.
1179 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +00001180 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +00001181 // Incomplete types don't have a size and alignment.
1182 if (T1->isIncompleteType() || T2->isIncompleteType())
1183 return false;
1184 // Check is based on size and alignment.
1185 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1186 return false;
1187 }
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001188
1189 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1190 E = Method->param_end();
1191 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
1192
1193 for (; ParamI != E; ++ParamI, ++PrevI) {
1194 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1195 T1 = Context.getCanonicalType((*ParamI)->getType());
1196 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffb91afca2008-10-21 10:37:50 +00001197 if (T1 != T2) {
1198 // The result types are different.
1199 if (!matchBasedOnSizeAndAlignment)
1200 return false;
1201 // Incomplete types don't have a size and alignment.
1202 if (T1->isIncompleteType() || T2->isIncompleteType())
1203 return false;
1204 // Check is based on size and alignment.
1205 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1206 return false;
1207 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001208 }
1209 return true;
1210}
1211
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001212/// \brief Read the contents of the instance and factory method pools
1213/// for a given selector from external storage.
1214///
1215/// This routine should only be called once, when neither the instance
1216/// nor the factory method pool has an entry for this selector.
1217Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
1218 bool isInstance) {
1219 assert(ExternalSource && "We need an external AST source");
1220 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1221 "Selector data already loaded into the instance method pool");
1222 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1223 "Selector data already loaded into the factory method pool");
1224
1225 // Read the method list from the external source.
1226 std::pair<ObjCMethodList, ObjCMethodList> Methods
1227 = ExternalSource->ReadMethodPool(Sel);
1228
1229 if (isInstance) {
1230 if (Methods.second.Method)
1231 FactoryMethodPool[Sel] = Methods.second;
1232 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
1233 }
1234
1235 if (Methods.first.Method)
1236 InstanceMethodPool[Sel] = Methods.first;
1237
1238 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1239}
1240
Ted Kremenek42730c52008-01-07 19:49:32 +00001241void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001242 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1243 = InstanceMethodPool.find(Method->getSelector());
1244 if (Pos == InstanceMethodPool.end()) {
1245 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1246 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1247 else
1248 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1249 ObjCMethodList())).first;
1250 }
1251
1252 ObjCMethodList &Entry = Pos->second;
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001253 if (Entry.Method == 0) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001254 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001255 Entry.Method = Method;
1256 Entry.Next = 0;
1257 return;
Chris Lattner855e51f2007-12-12 07:09:47 +00001258 }
Chris Lattnerc13b27f2009-03-04 05:16:45 +00001259
1260 // We've seen a method with this name, see if we have already seen this type
1261 // signature.
1262 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1263 if (MatchTwoMethodDeclarations(Method, List->Method))
1264 return;
1265
1266 // We have a new signature for an existing method - add it.
1267 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1268 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001269}
1270
Steve Naroffec7e0882008-10-21 10:50:19 +00001271// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +00001272ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
1273 SourceRange R) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001274 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1275 = InstanceMethodPool.find(Sel);
Douglas Gregor89f2c8b2009-04-24 22:23:41 +00001276 if (Pos == InstanceMethodPool.end()) {
1277 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001278 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1279 else
1280 return 0;
1281 }
1282
1283 ObjCMethodList &MethList = Pos->second;
Steve Naroffb91afca2008-10-21 10:37:50 +00001284 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +00001285
1286 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +00001287 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1288 // This checks if the methods differ by size & alignment.
1289 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1290 issueWarning = true;
1291 }
1292 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001293 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +00001294 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001295 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001296 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +00001297 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +00001298 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +00001299 }
1300 return MethList.Method;
1301}
1302
Ted Kremenek42730c52008-01-07 19:49:32 +00001303void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001304 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1305 = FactoryMethodPool.find(Method->getSelector());
1306 if (Pos == FactoryMethodPool.end()) {
1307 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1308 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1309 else
1310 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1311 ObjCMethodList())).first;
1312 }
1313
1314 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner855e51f2007-12-12 07:09:47 +00001315 if (!FirstMethod.Method) {
1316 // Haven't seen a method with this selector name yet - add it.
1317 FirstMethod.Method = Method;
1318 FirstMethod.Next = 0;
1319 } else {
1320 // We've seen a method with this name, now check the type signature(s).
1321 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
1322
Ted Kremenek42730c52008-01-07 19:49:32 +00001323 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +00001324 Next = Next->Next)
1325 match = MatchTwoMethodDeclarations(Method, Next->Method);
1326
1327 if (!match) {
1328 // We have a new signature for an existing method - add it.
1329 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +00001330 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +00001331 FirstMethod.Next = OMI;
1332 }
1333 }
1334}
1335
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001336ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
1337 SourceRange R) {
1338 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1339 = FactoryMethodPool.find(Sel);
1340 if (Pos == FactoryMethodPool.end()) {
1341 if (ExternalSource && !InstanceMethodPool.count(Sel))
1342 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1343 else
1344 return 0;
1345 }
1346
1347 ObjCMethodList &MethList = Pos->second;
1348 bool issueWarning = false;
1349
1350 if (MethList.Method && MethList.Next) {
1351 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1352 // This checks if the methods differ by size & alignment.
1353 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1354 issueWarning = true;
1355 }
1356 if (issueWarning && (MethList.Method && MethList.Next)) {
1357 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1358 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1359 << MethList.Method->getSourceRange();
1360 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1361 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1362 << Next->Method->getSourceRange();
1363 }
1364 return MethList.Method;
1365}
1366
Steve Naroffab63fd62009-01-08 17:28:14 +00001367/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1368/// have the property type and issue diagnostics if they don't.
1369/// Also synthesize a getter/setter method if none exist (and update the
1370/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1371/// methods is the "right" thing to do.
1372void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1373 ObjCContainerDecl *CD) {
1374 ObjCMethodDecl *GetterMethod, *SetterMethod;
1375
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001376 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1377 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Fariborz Jahanian80ccaa92009-05-08 20:20:55 +00001378 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1379 property->getLocation());
1380
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001381 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001382 if (Context.getCanonicalType(SetterMethod->getResultType())
1383 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001384 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001385 if (SetterMethod->param_size() != 1 ||
1386 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001387 Diag(property->getLocation(),
Fariborz Jahaniana996bb02009-05-08 19:36:34 +00001388 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001389 << property->getDeclName()
Ted Kremenek7c568a42009-03-14 00:20:08 +00001390 << SetterMethod->getSelector();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001391 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1392 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001393 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001394
1395 // Synthesize getter/setter methods if none exist.
Steve Naroff5492c1b2009-01-08 20:15:03 +00001396 // Find the default getter and if one not found, add one.
Mike Stumpe127ae32009-05-16 07:39:55 +00001397 // FIXME: The synthesized property we set here is misleading. We almost always
1398 // synthesize these methods unless the user explicitly provided prototypes
1399 // (which is odd, but allowed). Sema should be typechecking that the
1400 // declarations jive in that situation (which it is not currently).
Steve Naroff5492c1b2009-01-08 20:15:03 +00001401 if (!GetterMethod) {
1402 // No instance method of same name as property getter name was found.
1403 // Declare a getter method and add it to the list of methods
1404 // for this class.
1405 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1406 property->getLocation(), property->getGetterName(),
1407 property->getType(), CD, true, false, true,
1408 (property->getPropertyImplementation() ==
1409 ObjCPropertyDecl::Optional) ?
1410 ObjCMethodDecl::Optional :
1411 ObjCMethodDecl::Required);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001412 CD->addDecl(GetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001413 } else
1414 // A user declared getter will be synthesize when @synthesize of
1415 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001416 GetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001417 property->setGetterMethodDecl(GetterMethod);
1418
1419 // Skip setter if property is read-only.
1420 if (!property->isReadOnly()) {
1421 // Find the default setter and if one not found, add one.
1422 if (!SetterMethod) {
1423 // No instance method of same name as property setter name was found.
1424 // Declare a setter method and add it to the list of methods
1425 // for this class.
1426 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1427 property->getLocation(),
1428 property->getSetterName(),
1429 Context.VoidTy, CD, true, false, true,
1430 (property->getPropertyImplementation() ==
1431 ObjCPropertyDecl::Optional) ?
1432 ObjCMethodDecl::Optional :
1433 ObjCMethodDecl::Required);
1434 // Invent the arguments for the setter. We don't bother making a
1435 // nice name for the argument.
1436 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Chris Lattner6e48be22009-04-11 20:14:49 +00001437 property->getLocation(),
Steve Naroff5492c1b2009-01-08 20:15:03 +00001438 property->getIdentifier(),
1439 property->getType(),
1440 VarDecl::None,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001441 0);
Steve Naroff79ea0e02009-04-20 15:06:07 +00001442 SetterMethod->setMethodParams(Context, &Argument, 1);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001443 CD->addDecl(SetterMethod);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001444 } else
1445 // A user declared setter will be synthesize when @synthesize of
1446 // the property with the same name is seen in the @implementation
Steve Naroff79ea0e02009-04-20 15:06:07 +00001447 SetterMethod->setSynthesized(true);
Steve Naroff5492c1b2009-01-08 20:15:03 +00001448 property->setSetterMethodDecl(SetterMethod);
1449 }
Steve Naroffab63fd62009-01-08 17:28:14 +00001450 // Add any synthesized methods to the global pool. This allows us to
1451 // handle the following, which is supported by GCC (and part of the design).
1452 //
1453 // @interface Foo
1454 // @property double bar;
1455 // @end
1456 //
1457 // void thisIsUnfortunate() {
1458 // id foo;
1459 // double bar = [foo bar];
1460 // }
1461 //
Douglas Gregord1675382009-01-09 19:42:16 +00001462 if (GetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001463 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregord1675382009-01-09 19:42:16 +00001464 if (SetterMethod)
Steve Naroffab63fd62009-01-08 17:28:14 +00001465 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001466}
1467
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001468// Note: For class/category implemenations, allMethods/allProperties is
1469// always null.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001470void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1471 DeclPtrTy *allMethods, unsigned allNum,
1472 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattnera17991f2009-03-29 16:50:03 +00001473 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001474 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner855e51f2007-12-12 07:09:47 +00001475
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001476 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1477 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001478 // should be true.
1479 if (!ClassDecl)
1480 return;
1481
Chris Lattner855e51f2007-12-12 07:09:47 +00001482 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001483 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1484 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001485 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001486
Steve Naroffab63fd62009-01-08 17:28:14 +00001487 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroffab63fd62009-01-08 17:28:14 +00001488
1489 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1490 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1491 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1492
Chris Lattner855e51f2007-12-12 07:09:47 +00001493 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001494 ObjCMethodDecl *Method =
Chris Lattner5261d0c2009-03-28 19:18:32 +00001495 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner855e51f2007-12-12 07:09:47 +00001496
1497 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregor5d764842009-01-09 17:18:27 +00001498 if (Method->isInstanceMethod()) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001499 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001500 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001501 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1502 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001503 if ((isInterfaceDeclKind && PrevMethod && !match)
1504 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001505 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001506 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001507 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001508 } else {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001509 DC->addDecl(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001510 InsMap[Method->getSelector()] = Method;
1511 /// The following allows us to typecheck messages to "id".
1512 AddInstanceMethodToGlobalPool(Method);
1513 }
1514 }
1515 else {
1516 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001517 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001518 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1519 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001520 if ((isInterfaceDeclKind && PrevMethod && !match)
1521 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001522 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001523 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001524 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001525 } else {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001526 DC->addDecl(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001527 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001528 /// The following allows us to typecheck messages to "Class".
1529 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001530 }
1531 }
1532 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001533 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001534 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001535 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001536 ComparePropertiesInBaseAndSuper(I);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001537 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff451f83c2009-01-09 15:36:25 +00001538 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001539 // Categories are used to extend the class by declaring new methods.
1540 // By the same token, they are also used to add new properties. No
1541 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001542
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001543 // Merge protocol properties into category
Chris Lattner5261d0c2009-03-28 19:18:32 +00001544 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianffb68822009-03-02 19:05:07 +00001545 if (C->getIdentifier() == 0)
1546 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +00001547 }
Steve Naroff451f83c2009-01-09 15:36:25 +00001548 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1549 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1550 // user-defined setter/getter. It also synthesizes setter/getter methods
1551 // and adds them to the DeclContext and global method pools.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001552 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1553 E = CDecl->prop_end();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001554 I != E; ++I)
Chris Lattner22063402009-02-16 18:32:47 +00001555 ProcessPropertyDecl(*I, CDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001556 CDecl->setAtEndLoc(AtEndLoc);
1557 }
1558 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001559 IC->setLocEnd(AtEndLoc);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001560 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Chris Lattner855e51f2007-12-12 07:09:47 +00001561 ImplMethodsVsClassMethods(IC, IDecl);
Steve Naroff451f83c2009-01-09 15:36:25 +00001562 } else if (ObjCCategoryImplDecl* CatImplClass =
1563 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001564 CatImplClass->setLocEnd(AtEndLoc);
Chris Lattner22063402009-02-16 18:32:47 +00001565
Chris Lattner855e51f2007-12-12 07:09:47 +00001566 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001567 // in this interface are implemented in the category @implementation.
Chris Lattner22063402009-02-16 18:32:47 +00001568 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001569 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001570 Categories; Categories = Categories->getNextClassCategory()) {
1571 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner7cc13bf2009-03-01 00:56:52 +00001572 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner855e51f2007-12-12 07:09:47 +00001573 break;
1574 }
1575 }
1576 }
1577 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001578 if (isInterfaceDeclKind) {
1579 // Reject invalid vardecls.
1580 for (unsigned i = 0; i != tuvNum; i++) {
1581 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1582 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1583 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar644c15e2009-04-14 02:25:56 +00001584 if (!VDecl->hasExternalStorage())
Steve Naroff03d55572009-04-13 17:58:46 +00001585 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianca277322009-03-21 18:06:45 +00001586 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001587 }
Fariborz Jahanian2c7de6d2009-03-18 22:33:24 +00001588 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001589}
1590
1591
1592/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1593/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001594static Decl::ObjCDeclQualifier
1595CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1596 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1597 if (PQTVal & ObjCDeclSpec::DQ_In)
1598 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1599 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1600 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1601 if (PQTVal & ObjCDeclSpec::DQ_Out)
1602 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1603 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1604 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1605 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1606 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1607 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1608 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001609
1610 return ret;
1611}
1612
Chris Lattner5261d0c2009-03-28 19:18:32 +00001613Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner855e51f2007-12-12 07:09:47 +00001614 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001615 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001616 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001617 Selector Sel,
1618 // optional arguments. The number of types/arguments is obtained
1619 // from the Sel.getNumArgs().
Chris Lattner612a2f72009-04-11 18:57:04 +00001620 ObjCArgInfo *ArgInfo,
Fariborz Jahanian89d53042009-01-09 00:38:19 +00001621 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner855e51f2007-12-12 07:09:47 +00001622 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1623 bool isVariadic) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001624 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff70f16242008-02-29 21:48:07 +00001625
1626 // Make sure we can establish a context for the method.
1627 if (!ClassDecl) {
1628 Diag(MethodLoc, diag::error_missing_method_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001629 return DeclPtrTy();
Steve Naroff70f16242008-02-29 21:48:07 +00001630 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001631 QualType resultDeclType;
1632
Steve Naroffa442ad92009-02-20 22:59:16 +00001633 if (ReturnType) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001634 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
Steve Naroffa442ad92009-02-20 22:59:16 +00001635
1636 // Methods cannot return interface types. All ObjC objects are
1637 // passed by reference.
1638 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner76210732009-04-11 19:08:56 +00001639 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1640 << 0 << resultDeclType;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001641 return DeclPtrTy();
Steve Naroffa442ad92009-02-20 22:59:16 +00001642 }
1643 } else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001644 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001645
Chris Lattner114add62008-03-16 00:49:28 +00001646 ObjCMethodDecl* ObjCMethod =
1647 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattner3afb2df2009-03-29 04:30:19 +00001648 cast<DeclContext>(ClassDecl),
Chris Lattner114add62008-03-16 00:49:28 +00001649 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001650 false,
Chris Lattner114add62008-03-16 00:49:28 +00001651 MethodDeclKind == tok::objc_optional ?
1652 ObjCMethodDecl::Optional :
1653 ObjCMethodDecl::Required);
1654
Chris Lattnereee57c02008-04-04 06:12:32 +00001655 llvm::SmallVector<ParmVarDecl*, 16> Params;
1656
Chris Lattner4f0496b2009-04-11 19:42:43 +00001657 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
Chris Lattner76210732009-04-11 19:08:56 +00001658 QualType ArgType, UnpromotedArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001659
Chris Lattner612a2f72009-04-11 18:57:04 +00001660 if (ArgInfo[i].Type == 0) {
Chris Lattner76210732009-04-11 19:08:56 +00001661 UnpromotedArgType = ArgType = Context.getObjCIdType();
Chris Lattner612a2f72009-04-11 18:57:04 +00001662 } else {
Chris Lattner76210732009-04-11 19:08:56 +00001663 UnpromotedArgType = ArgType = QualType::getFromOpaquePtr(ArgInfo[i].Type);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001664 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner761b72f2009-04-11 19:34:56 +00001665 ArgType = adjustParameterType(ArgType);
Chris Lattner612a2f72009-04-11 18:57:04 +00001666 }
1667
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001668 ParmVarDecl* Param;
Chris Lattner76210732009-04-11 19:08:56 +00001669 if (ArgType == UnpromotedArgType)
Chris Lattner4f0496b2009-04-11 19:42:43 +00001670 Param = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001671 ArgInfo[i].Name, ArgType,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001672 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001673 else
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001674 Param = OriginalParmVarDecl::Create(Context, ObjCMethod,
Chris Lattner4f0496b2009-04-11 19:42:43 +00001675 ArgInfo[i].NameLoc,
Chris Lattner76210732009-04-11 19:08:56 +00001676 ArgInfo[i].Name, ArgType,
1677 UnpromotedArgType,
Douglas Gregor469fc9a2009-02-02 23:39:07 +00001678 VarDecl::None, 0);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001679
Chris Lattner761b72f2009-04-11 19:34:56 +00001680 if (ArgType->isObjCInterfaceType()) {
1681 Diag(ArgInfo[i].NameLoc,
1682 diag::err_object_cannot_be_passed_returned_by_value)
1683 << 1 << ArgType;
1684 Param->setInvalidDecl();
1685 }
1686
Chris Lattnereee57c02008-04-04 06:12:32 +00001687 Param->setObjCDeclQualifier(
Chris Lattner612a2f72009-04-11 18:57:04 +00001688 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Chris Lattner761b72f2009-04-11 19:34:56 +00001689
1690 // Apply the attributes to the parameter.
Douglas Gregor2a2e0402009-06-17 21:51:59 +00001691 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Chris Lattner761b72f2009-04-11 19:34:56 +00001692
Chris Lattnereee57c02008-04-04 06:12:32 +00001693 Params.push_back(Param);
1694 }
1695
Jay Foad9e6bef42009-05-21 09:52:38 +00001696 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremenek42730c52008-01-07 19:49:32 +00001697 ObjCMethod->setObjCDeclQualifier(
1698 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1699 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001700
1701 if (AttrList)
Douglas Gregor2a2e0402009-06-17 21:51:59 +00001702 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Ted Kremenek2b0e6da2009-04-30 18:41:06 +00001703
Chris Lattner855e51f2007-12-12 07:09:47 +00001704 // For implementations (which can be very "coarse grain"), we add the
1705 // method now. This allows the AST to implement lookup methods that work
1706 // incrementally (without waiting until we parse the @end). It also allows
1707 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001708 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001709 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001710 if (MethodType == tok::minus) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001711 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1712 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001713 } else {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001714 PrevMethod = ImpDecl->getClassMethod(Sel);
1715 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001716 }
Fariborz Jahanian060f7072009-05-12 21:36:23 +00001717 if (AttrList)
1718 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner855e51f2007-12-12 07:09:47 +00001719 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001720 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001721 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001722 if (MethodType == tok::minus) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001723 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1724 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001725 } else {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001726 PrevMethod = CatImpDecl->getClassMethod(Sel);
1727 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001728 }
Fariborz Jahanian060f7072009-05-12 21:36:23 +00001729 if (AttrList)
1730 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner855e51f2007-12-12 07:09:47 +00001731 }
1732 if (PrevMethod) {
1733 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001734 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001735 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001736 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001737 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001738 return DeclPtrTy::make(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001739}
1740
Daniel Dunbar540ff472008-09-23 21:53:23 +00001741void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1742 SourceLocation Loc,
1743 unsigned &Attributes) {
1744 // FIXME: Improve the reported location.
1745
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001746 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001747 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001748 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1749 ObjCDeclSpec::DQ_PR_assign |
1750 ObjCDeclSpec::DQ_PR_copy |
1751 ObjCDeclSpec::DQ_PR_retain))) {
1752 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1753 "readwrite" :
1754 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1755 "assign" :
1756 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1757 "copy" : "retain";
1758
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001759 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner0eab08e2009-01-29 18:49:48 +00001760 diag::err_objc_property_attr_mutually_exclusive :
1761 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001762 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001763 }
1764
1765 // Check for copy or retain on non-object types.
1766 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Steve Naroffad75bd22009-07-16 15:41:00 +00001767 !PropertyTy->isObjCObjectPointerType() &&
1768 !PropertyTy->isBlockPointerType() &&
1769 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001770 Diag(Loc, diag::err_objc_property_requires_object)
1771 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001772 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1773 }
1774
1775 // Check for more than one of { assign, copy, retain }.
1776 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1777 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001778 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1779 << "assign" << "copy";
1780 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001781 }
1782 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001783 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1784 << "assign" << "retain";
1785 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001786 }
1787 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1788 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001789 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1790 << "copy" << "retain";
1791 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001792 }
1793 }
1794
1795 // Warn if user supplied no assignment attribute, property is
1796 // readwrite, and this is an object type.
1797 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1798 ObjCDeclSpec::DQ_PR_retain)) &&
1799 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Naroffad75bd22009-07-16 15:41:00 +00001800 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001801 // Skip this warning in gc-only mode.
1802 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1803 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1804
1805 // If non-gc code warn that this is likely inappropriate.
1806 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1807 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1808
1809 // FIXME: Implement warning dependent on NSCopying being
1810 // implemented. See also:
1811 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1812 // (please trim this list while you are at it).
1813 }
Mike Stump2f567c62009-05-07 23:06:50 +00001814
1815 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1816 && getLangOptions().getGCMode() == LangOptions::GCOnly
1817 && PropertyTy->isBlockPointerType())
1818 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001819}
1820
Chris Lattner5261d0c2009-03-28 19:18:32 +00001821Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1822 FieldDeclarator &FD,
1823 ObjCDeclSpec &ODS,
1824 Selector GetterSel,
1825 Selector SetterSel,
1826 DeclPtrTy ClassCategory,
1827 bool *isOverridingProperty,
1828 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001829 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001830 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1831 // default is readwrite!
1832 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1833 // property is defaulted to 'assign' if it is readwrite and is
1834 // not retain or copy
1835 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1836 (isReadWrite &&
1837 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1838 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1839 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001840 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001841 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar540ff472008-09-23 21:53:23 +00001842 // May modify Attributes.
1843 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001844 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1845 if (!CDecl->getIdentifier()) {
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001846 // This is a continuation class. property requires special
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001847 // handling.
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001848 if ((CCPrimary = CDecl->getClassInterface())) {
1849 // Find the property in continuation class's primary class only.
1850 ObjCPropertyDecl *PIDecl = 0;
1851 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001852 for (ObjCInterfaceDecl::prop_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001853 I = CCPrimary->prop_begin(), E = CCPrimary->prop_end();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001854 I != E; ++I)
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001855 if ((*I)->getIdentifier() == PropertyId) {
1856 PIDecl = *I;
1857 break;
1858 }
1859
1860 if (PIDecl) {
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001861 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian89aa1e02009-04-01 23:23:53 +00001862 // with continuation class's readwrite property attribute!
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001863 unsigned PIkind = PIDecl->getPropertyAttributes();
1864 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001865 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001866 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1867 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001868 PIDecl->makeitReadWriteAttribute();
1869 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1870 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1871 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1872 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1873 PIDecl->setSetterName(SetterSel);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001874 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001875 else
1876 Diag(AtLoc, diag::err_use_continuation_class)
1877 << CCPrimary->getDeclName();
1878 *isOverridingProperty = true;
Fariborz Jahaniane6faa992009-04-15 19:19:03 +00001879 // Make sure setter decl is synthesized, and added to primary
1880 // class's list.
1881 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001882 return DeclPtrTy();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001883 }
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001884 // No matching property found in the primary class. Just fall thru
1885 // and add property to continuation class's primary class.
1886 ClassDecl = CCPrimary;
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001887 } else {
Chris Lattnera5d47712009-02-20 21:38:52 +00001888 Diag(CDecl->getLocation(), diag::err_continuation_class);
1889 *isOverridingProperty = true;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001890 return DeclPtrTy();
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001891 }
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001892 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001893
Steve Naroffdcf1e842009-01-11 12:47:58 +00001894 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1895 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattner6e48be22009-04-11 20:14:49 +00001896 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
1897 FD.D.getIdentifierLoc(),
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001898 FD.D.getIdentifier(), T);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001899 DC->addDecl(PDecl);
Chris Lattner22063402009-02-16 18:32:47 +00001900
Chris Lattner6e48be22009-04-11 20:14:49 +00001901 if (T->isArrayType() || T->isFunctionType()) {
1902 Diag(AtLoc, diag::err_property_type) << T;
1903 PDecl->setInvalidDecl();
1904 }
1905
Douglas Gregor2a2e0402009-06-17 21:51:59 +00001906 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00001907
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001908 // Regardless of setter/getter attribute, we save the default getter/setter
1909 // selector names in anticipation of declaration of setter/getter methods.
1910 PDecl->setGetterName(GetterSel);
1911 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001912
Daniel Dunbar540ff472008-09-23 21:53:23 +00001913 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001914 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001915
Daniel Dunbar540ff472008-09-23 21:53:23 +00001916 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001917 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001918
Daniel Dunbar540ff472008-09-23 21:53:23 +00001919 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001920 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001921
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001922 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001923 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001924
Daniel Dunbar540ff472008-09-23 21:53:23 +00001925 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001926 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001927
Daniel Dunbar540ff472008-09-23 21:53:23 +00001928 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001929 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001930
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001931 if (isAssign)
1932 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1933
Daniel Dunbar540ff472008-09-23 21:53:23 +00001934 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001935 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001936
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001937 if (MethodImplKind == tok::objc_required)
1938 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1939 else if (MethodImplKind == tok::objc_optional)
1940 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian304f7c72009-04-02 18:44:20 +00001941 // A case of continuation class adding a new property in the class. This
1942 // is not what it was meant for. However, gcc supports it and so should we.
1943 // Make sure setter/getters are declared here.
1944 if (CCPrimary)
1945 ProcessPropertyDecl(PDecl, CCPrimary);
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001946
Chris Lattner5261d0c2009-03-28 19:18:32 +00001947 return DeclPtrTy::make(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001948}
1949
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001950/// ActOnPropertyImplDecl - This routine performs semantic checks and
1951/// builds the AST node for a property implementation declaration; declared
1952/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001953///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001954Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1955 SourceLocation PropertyLoc,
1956 bool Synthesize,
1957 DeclPtrTy ClassCatImpDecl,
1958 IdentifierInfo *PropertyId,
1959 IdentifierInfo *PropertyIvar) {
1960 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001961 // Make sure we have a context for the property implementation declaration.
1962 if (!ClassImpDecl) {
1963 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001964 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001965 }
1966 ObjCPropertyDecl *property = 0;
1967 ObjCInterfaceDecl* IDecl = 0;
1968 // Find the class or category class where this property must have
1969 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001970 ObjCImplementationDecl *IC = 0;
1971 ObjCCategoryImplDecl* CatImplClass = 0;
1972 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001973 IDecl = IC->getClassInterface();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001974 // We always synthesize an interface for an implementation
1975 // without an interface decl. So, IDecl is always non-zero.
1976 assert(IDecl &&
1977 "ActOnPropertyImplDecl - @implementation without @interface");
1978
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001979 // Look for this property declaration in the @implementation's @interface
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001980 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001981 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001982 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001983 return DeclPtrTy();
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001984 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001985 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001986 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001987 if (Synthesize) {
1988 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001989 return DeclPtrTy();
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001990 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001991 IDecl = CatImplClass->getClassInterface();
1992 if (!IDecl) {
1993 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001994 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001995 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001996 ObjCCategoryDecl *Category =
1997 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1998
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001999 // If category for this implementation not found, it is an error which
2000 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00002001 if (!Category)
Chris Lattner5261d0c2009-03-28 19:18:32 +00002002 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002003 // Look for this property declaration in @implementation's category
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002004 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00002005 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00002006 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00002007 << Category->getDeclName();
Chris Lattner5261d0c2009-03-28 19:18:32 +00002008 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002009 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00002010 } else {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002011 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002012 return DeclPtrTy();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002013 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002014 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002015 // Check that we have a valid, previously declared ivar for @synthesize
2016 if (Synthesize) {
2017 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00002018 if (!PropertyIvar)
2019 PropertyIvar = PropertyId;
Fariborz Jahanianc625fa92009-03-31 00:06:29 +00002020 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002021 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00002022 ObjCInterfaceDecl *ClassDeclared;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002023 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00002024 if (!Ivar) {
Fariborz Jahanian9e8d7ca2009-06-06 16:36:41 +00002025 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
2026 assert(EnclosingContext &&
2027 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
2028 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002029 PropertyIvar, PropType,
2030 ObjCIvarDecl::Public,
2031 (Expr *)0);
Fariborz Jahanian9e8d7ca2009-06-06 16:36:41 +00002032 Ivar->setLexicalDeclContext(IDecl);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002033 IDecl->addDecl(Ivar);
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002034 property->setPropertyIvarDecl(Ivar);
2035 if (!getLangOptions().ObjCNonFragileABI)
Steve Naroff92aeef32009-03-03 22:09:41 +00002036 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002037 // Note! I deliberately want it to fall thru so, we have a
2038 // a property implementation and to avoid future warnings.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002039 }
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00002040 else if (getLangOptions().ObjCNonFragileABI &&
Fariborz Jahanian76389e42009-04-29 21:45:02 +00002041 ClassDeclared != IDecl) {
Fariborz Jahanianc181f5a2009-04-30 21:39:24 +00002042 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Fariborz Jahanian1b85c502009-04-13 19:30:37 +00002043 << property->getDeclName() << Ivar->getDeclName()
2044 << ClassDeclared->getDeclName();
2045 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2046 << Ivar << Ivar->getNameAsCString();
2047 // Note! I deliberately want it to fall thru so more errors are caught.
2048 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00002049 QualType IvarType = Context.getCanonicalType(Ivar->getType());
2050
Steve Naroff631e3922008-09-30 00:24:17 +00002051 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00002052 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00002053 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00002054 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00002055 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002056 // Note! I deliberately want it to fall thru so, we have a
2057 // a property implementation and to avoid future warnings.
Steve Naroffc32e45c2008-09-30 10:07:56 +00002058 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00002059
2060 // FIXME! Rules for properties are somewhat different that those
2061 // for assignments. Use a new routine to consolidate all cases;
2062 // specifically for property redeclarations as well as for ivars.
2063 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2064 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
2065 if (lhsType != rhsType &&
2066 lhsType->isArithmeticType()) {
2067 Diag(PropertyLoc, diag::error_property_ivar_type)
2068 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002069 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00002070 }
2071 // __weak is explicit. So it works on Canonical type.
Fariborz Jahaniandc150fb2009-04-07 21:25:06 +00002072 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2073 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002074 Diag(PropertyLoc, diag::error_weak_property)
2075 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002076 // Fall thru - see previous comment
Chris Lattner5261d0c2009-03-28 19:18:32 +00002077 }
Steve Naroff329ec222009-07-10 23:34:53 +00002078 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian18411912009-04-10 22:42:54 +00002079 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2080 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002081 Diag(PropertyLoc, diag::error_strong_property)
2082 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002083 // Fall thru - see previous comment
Fariborz Jahanian6ec89552009-01-19 20:13:47 +00002084 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00002085 }
Fariborz Jahanian0a4ea0e2009-04-14 23:15:21 +00002086 } else if (PropertyIvar)
2087 // @dynamic
2088 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002089 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002090 ObjCPropertyImplDecl *PIDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +00002091 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2092 property,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002093 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00002094 ObjCPropertyImplDecl::Synthesize
2095 : ObjCPropertyImplDecl::Dynamic),
2096 Ivar);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002097 if (IC) {
2098 if (Synthesize)
2099 if (ObjCPropertyImplDecl *PPIDecl =
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002100 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002101 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2102 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2103 << PropertyIvar;
2104 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2105 }
2106
Douglas Gregorcd19b572009-04-23 01:02:12 +00002107 if (ObjCPropertyImplDecl *PPIDecl
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002108 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002109 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2110 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002111 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00002112 }
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002113 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002114 }
2115 else {
2116 if (Synthesize)
2117 if (ObjCPropertyImplDecl *PPIDecl =
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002118 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002119 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2120 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
2121 << PropertyIvar;
2122 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2123 }
2124
2125 if (ObjCPropertyImplDecl *PPIDecl =
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002126 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanian68342282008-12-05 22:32:48 +00002127 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2128 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002129 return DeclPtrTy();
Fariborz Jahanian68342282008-12-05 22:32:48 +00002130 }
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002131 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00002132 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00002133
Chris Lattner5261d0c2009-03-28 19:18:32 +00002134 return DeclPtrTy::make(PIDecl);
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00002135}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00002136
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002137bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor69e781f2009-01-06 23:51:29 +00002138 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson0a6ab172008-11-04 16:57:32 +00002139 return false;
2140
2141 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2142 D->setInvalidDecl();
2143
2144 return true;
2145}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002146
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002147/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2148/// instance variables of ClassName into Decls.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002149void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002150 IdentifierInfo *ClassName,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002151 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002152 // Check that ClassName is a valid class
2153 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2154 if (!Class) {
2155 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2156 return;
2157 }
Fariborz Jahanian51ba3b82009-04-21 20:28:41 +00002158 if (LangOpts.ObjCNonFragileABI) {
2159 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2160 return;
2161 }
2162
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002163 // Collect the instance variables
Fariborz Jahanian3974a522009-06-04 17:08:55 +00002164 llvm::SmallVector<FieldDecl*, 32> RecFields;
2165 Context.CollectObjCIvars(Class, RecFields);
2166 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2167 for (unsigned i = 0; i < RecFields.size(); i++) {
2168 FieldDecl* ID = RecFields[i];
2169 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2170 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2171 ID->getIdentifier(), ID->getType(),
2172 ID->getBitWidth());
2173 Decls.push_back(Sema::DeclPtrTy::make(FD));
2174 }
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002175
2176 // Introduce all of these fields into the appropriate scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002177 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002178 D != Decls.end(); ++D) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00002179 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002180 if (getLangOptions().CPlusPlus)
2181 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner5261d0c2009-03-28 19:18:32 +00002182 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002183 Record->addDecl(FD);
Chris Lattner9bb9abf2008-12-17 07:13:27 +00002184 }
2185}
2186