blob: 6ff898970af0219646d71627e2b44f0785442047 [file] [log] [blame]
Chris Lattnerda463fe2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerda463fe2007-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 Gregor35b0bac2010-01-03 18:01:57 +000015#include "Lookup.h"
Douglas Gregorc78d3462009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
Steve Naroff157599f2009-03-03 14:49:36 +000017#include "clang/AST/Expr.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000021using namespace clang;
22
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +000023bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
24 ObjCMethodDecl *GetterMethod,
25 SourceLocation Loc) {
26 if (GetterMethod &&
27 GetterMethod->getResultType() != property->getType()) {
28 AssignConvertType result = Incompatible;
Steve Naroff79d12152009-07-16 15:41:00 +000029 if (property->getType()->isObjCObjectPointerType())
Fariborz Jahanian3e68a1f2009-05-08 21:10:00 +000030 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +000031 if (result != Compatible) {
Mike Stump11289f42009-09-09 15:08:12 +000032 Diag(Loc, diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +000033 << property->getDeclName()
34 << GetterMethod->getSelector();
35 Diag(GetterMethod->getLocation(), diag::note_declared_at);
36 return true;
37 }
38 }
39 return false;
40}
41
Steve Naroff70f41d62009-02-28 16:59:13 +000042/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattnerda463fe2007-12-12 07:09:47 +000043/// and user declared, in the method definition's AST.
Chris Lattner83f095c2009-03-28 19:18:32 +000044void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis853fbea2008-06-28 06:07:14 +000045 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner83f095c2009-03-28 19:18:32 +000046 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump11289f42009-09-09 15:08:12 +000047
Steve Naroff542cd5d2008-07-25 17:57:26 +000048 // If we don't have a valid method decl, simply return.
49 if (!MDecl)
50 return;
Steve Naroff1d2538c2007-12-18 01:30:32 +000051
Chris Lattner9fecd742009-04-19 05:21:20 +000052 CurFunctionNeedsScopeChecking = false;
53
Steve Naroff1d2538c2007-12-18 01:30:32 +000054 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorffca3a22009-01-09 17:18:27 +000055 if (MDecl->isInstanceMethod())
Steve Naroff1d2538c2007-12-18 01:30:32 +000056 AddInstanceMethodToGlobalPool(MDecl);
57 else
58 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump11289f42009-09-09 15:08:12 +000059
Chris Lattnerda463fe2007-12-12 07:09:47 +000060 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor91f84212008-12-11 16:49:14 +000061 PushDeclContext(FnBodyScope, MDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +000062
63 // Create Decl objects for each parameter, entrring them in the scope for
64 // binding to their use.
Chris Lattnerda463fe2007-12-12 07:09:47 +000065
66 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +000067 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump11289f42009-09-09 15:08:12 +000068
Daniel Dunbar279d1cc2008-08-26 06:07:48 +000069 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
70 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +000071
Chris Lattner58258242008-04-10 02:22:51 +000072 // Introduce all of the other parameters into this scope.
Chris Lattnera4997152009-02-20 18:43:26 +000073 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
74 E = MDecl->param_end(); PI != E; ++PI)
75 if ((*PI)->getIdentifier())
76 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattnerda463fe2007-12-12 07:09:47 +000077}
78
Chris Lattner83f095c2009-03-28 19:18:32 +000079Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +000080ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
81 IdentifierInfo *ClassName, SourceLocation ClassLoc,
82 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +000083 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +000084 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattnerda463fe2007-12-12 07:09:47 +000085 assert(ClassName && "Missing class identifier");
Mike Stump11289f42009-09-09 15:08:12 +000086
Chris Lattnerda463fe2007-12-12 07:09:47 +000087 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +000088 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor5daeee22008-12-08 18:40:42 +000089 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-12-05 18:15:24 +000090 // Maybe we will complain about the shadowed template parameter.
91 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
92 // Just pretend that we didn't see the previous declaration.
93 PrevDecl = 0;
94 }
95
Ted Kremenek1b0ea822008-01-07 19:49:32 +000096 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +000097 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +000098 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +000099 }
Mike Stump11289f42009-09-09 15:08:12 +0000100
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000101 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000102 if (IDecl) {
103 // Class already seen. Is it a forward declaration?
Steve Naroff119f60e2008-11-18 19:15:30 +0000104 if (!IDecl->isForwardDecl()) {
Chris Lattnerd13b8b52009-02-23 22:00:08 +0000105 IDecl->setInvalidDecl();
Chris Lattnerf3d3fae2008-11-24 05:29:24 +0000106 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnere6447ef2008-11-23 22:46:27 +0000107 Diag(IDecl->getLocation(), diag::note_previous_definition);
108
Steve Naroff119f60e2008-11-18 19:15:30 +0000109 // Return the previous class interface.
110 // FIXME: don't leak the objects passed in!
Chris Lattner83f095c2009-03-28 19:18:32 +0000111 return DeclPtrTy::make(IDecl);
Steve Naroff119f60e2008-11-18 19:15:30 +0000112 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000113 IDecl->setLocation(AtInterfaceLoc);
114 IDecl->setForwardDecl(false);
Steve Naroffe0064d22009-09-11 00:12:01 +0000115 IDecl->setClassLoc(ClassLoc);
Ted Kremenek707ece62009-11-17 22:58:30 +0000116
117 // Since this ObjCInterfaceDecl was created by a forward declaration,
118 // we now add it to the DeclContext since it wasn't added before
119 // (see ActOnForwardClassDeclaration).
120 CurContext->addDecl(IDecl);
121
Fariborz Jahaniand3612392009-11-17 19:08:08 +0000122 if (AttrList)
123 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000124 }
Chris Lattnerca1e8482008-07-21 07:06:49 +0000125 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000126 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff5a4611c2008-04-11 19:35:35 +0000127 ClassName, ClassLoc);
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000128 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000129 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +0000130
Steve Naroff3c301dc2009-04-23 15:15:40 +0000131 PushOnScopeChains(IDecl, TUScope);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000132 }
Mike Stump11289f42009-09-09 15:08:12 +0000133
Chris Lattnerda463fe2007-12-12 07:09:47 +0000134 if (SuperName) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000135 // Check if a different kind of symbol declared in this scope.
John McCall9f3059a2009-10-09 21:13:30 +0000136 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000137
138 if (!PrevDecl) {
139 // Try to correct for a typo in the superclass name.
140 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
141 if (CorrectTypo(R, TUScope, 0) &&
142 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
143 Diag(SuperLoc, diag::err_undef_superclass_suggest)
144 << SuperName << ClassName << PrevDecl->getDeclName();
145 }
146 }
147
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000148 if (PrevDecl == IDecl) {
149 Diag(SuperLoc, diag::err_recursive_superclass)
150 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
151 IDecl->setLocEnd(ClassLoc);
Mike Stump12b8ce12009-08-04 21:02:39 +0000152 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000153 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000154 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000155
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000156 // Diagnose classes that inherit from deprecated classes.
157 if (SuperClassDecl)
158 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000159
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000160 if (PrevDecl && SuperClassDecl == 0) {
161 // The previous declaration was not a class decl. Check if we have a
162 // typedef. If we do, get the underlying class type.
163 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
164 QualType T = TDecl->getUnderlyingType();
165 if (T->isObjCInterfaceType()) {
John McCall9dd450b2009-09-21 23:43:11 +0000166 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000167 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
168 }
169 }
Mike Stump11289f42009-09-09 15:08:12 +0000170
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000171 // This handles the following case:
172 //
173 // typedef int SuperClass;
174 // @interface MyClass : SuperClass {} @end
175 //
176 if (!SuperClassDecl) {
177 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
178 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff189d41f2009-02-04 17:14:05 +0000179 }
180 }
Mike Stump11289f42009-09-09 15:08:12 +0000181
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000182 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
183 if (!SuperClassDecl)
184 Diag(SuperLoc, diag::err_undef_superclass)
185 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
186 else if (SuperClassDecl->isForwardDecl())
187 Diag(SuperLoc, diag::err_undef_superclass)
188 << SuperClassDecl->getDeclName() << ClassName
189 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000190 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000191 IDecl->setSuperClass(SuperClassDecl);
192 IDecl->setSuperClassLoc(SuperLoc);
193 IDecl->setLocEnd(SuperLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000194 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000195 } else { // we have a root class.
196 IDecl->setLocEnd(ClassLoc);
197 }
Mike Stump11289f42009-09-09 15:08:12 +0000198
Steve Naroff119f60e2008-11-18 19:15:30 +0000199 /// Check then save referenced protocols.
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000200 if (NumProtoRefs) {
Chris Lattner22298722009-02-20 21:35:13 +0000201 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
202 Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000203 IDecl->setLocEnd(EndProtoLoc);
204 }
Mike Stump11289f42009-09-09 15:08:12 +0000205
Anders Carlssona6b508a2008-11-04 16:57:32 +0000206 CheckObjCDeclScope(IDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000207 return DeclPtrTy::make(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000208}
209
210/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000211/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner83f095c2009-03-28 19:18:32 +0000212Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000213 IdentifierInfo *AliasName,
Chris Lattner83f095c2009-03-28 19:18:32 +0000214 SourceLocation AliasLocation,
215 IdentifierInfo *ClassName,
216 SourceLocation ClassLocation) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000217 // Look for previous declaration of alias name
John McCall9f3059a2009-10-09 21:13:30 +0000218 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000219 if (ADecl) {
Chris Lattnerd0685032008-11-23 23:20:13 +0000220 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattnerda463fe2007-12-12 07:09:47 +0000221 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerd0685032008-11-23 23:20:13 +0000222 else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000223 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerd0685032008-11-23 23:20:13 +0000224 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000225 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000226 }
227 // Check for class declaration
John McCall9f3059a2009-10-09 21:13:30 +0000228 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000229 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
230 QualType T = TDecl->getUnderlyingType();
231 if (T->isObjCInterfaceType()) {
John McCall9dd450b2009-09-21 23:43:11 +0000232 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000233 ClassName = IDecl->getIdentifier();
John McCall9f3059a2009-10-09 21:13:30 +0000234 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000235 }
236 }
237 }
Chris Lattner219b3e92008-03-16 21:17:37 +0000238 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
239 if (CDecl == 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000240 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner219b3e92008-03-16 21:17:37 +0000241 if (CDeclU)
Chris Lattnerd0685032008-11-23 23:20:13 +0000242 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000243 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000244 }
Mike Stump11289f42009-09-09 15:08:12 +0000245
Chris Lattner219b3e92008-03-16 21:17:37 +0000246 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump11289f42009-09-09 15:08:12 +0000247 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000248 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000249
Anders Carlssona6b508a2008-11-04 16:57:32 +0000250 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor38feed82009-04-24 02:57:34 +0000251 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000252
Chris Lattner83f095c2009-03-28 19:18:32 +0000253 return DeclPtrTy::make(AliasDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000254}
255
Steve Naroff41d09ad2009-03-05 15:22:01 +0000256void Sema::CheckForwardProtocolDeclarationForCircularDependency(
257 IdentifierInfo *PName,
258 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000259 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000260 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
261 E = PList.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000262
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000263 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000264 if (PDecl->getIdentifier() == PName) {
265 Diag(Ploc, diag::err_protocol_has_circular_dependency);
266 Diag(PrevLoc, diag::note_previous_definition);
267 }
Mike Stump11289f42009-09-09 15:08:12 +0000268 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff41d09ad2009-03-05 15:22:01 +0000269 PDecl->getLocation(), PDecl->getReferencedProtocols());
270 }
271 }
272}
273
Chris Lattner83f095c2009-03-28 19:18:32 +0000274Sema::DeclPtrTy
Chris Lattner3bbae002008-07-26 04:03:38 +0000275Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
276 IdentifierInfo *ProtocolName,
277 SourceLocation ProtocolLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000278 const DeclPtrTy *ProtoRefs,
Chris Lattner3bbae002008-07-26 04:03:38 +0000279 unsigned NumProtoRefs,
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000280 SourceLocation EndProtoLoc,
281 AttributeList *AttrList) {
282 // FIXME: Deal with AttrList.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000283 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000284 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000285 if (PDecl) {
286 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner5074f8f2008-03-16 01:25:17 +0000287 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian54d569c2009-04-06 23:43:32 +0000288 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnere6447ef2008-11-23 22:46:27 +0000289 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000290 // Just return the protocol we already had.
291 // FIXME: don't leak the objects passed in!
Chris Lattner83f095c2009-03-28 19:18:32 +0000292 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000293 }
Steve Naroff41d09ad2009-03-05 15:22:01 +0000294 ObjCList<ObjCProtocolDecl> PList;
Mike Stump11289f42009-09-09 15:08:12 +0000295 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff41d09ad2009-03-05 15:22:01 +0000296 CheckForwardProtocolDeclarationForCircularDependency(
297 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
298 PList.Destroy(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000299
Steve Naroffeb03dac2008-08-13 16:39:22 +0000300 // Make sure the cached decl gets a valid start location.
301 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000302 PDecl->setForwardDecl(false);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000303 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000304 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000305 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000306 PushOnScopeChains(PDecl, TUScope);
Chris Lattneracc04a92008-03-16 20:19:15 +0000307 PDecl->setForwardDecl(false);
Chris Lattnerf87ca0a2008-03-16 01:23:04 +0000308 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000309 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000310 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000311 if (NumProtoRefs) {
Chris Lattneracc04a92008-03-16 20:19:15 +0000312 /// Check then save referenced protocols.
Chris Lattner22298722009-02-20 21:35:13 +0000313 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000314 PDecl->setLocEnd(EndProtoLoc);
315 }
Mike Stump11289f42009-09-09 15:08:12 +0000316
317 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000318 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000319}
320
321/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000322/// issues an error if they are not declared. It returns list of
323/// protocol declarations in its 'Protocols' argument.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000324void
Chris Lattner3bbae002008-07-26 04:03:38 +0000325Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000326 const IdentifierLocPair *ProtocolId,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000327 unsigned NumProtocols,
Chris Lattner83f095c2009-03-28 19:18:32 +0000328 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000329 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000330 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000331 if (!PDecl) {
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000332 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
333 LookupObjCProtocolName);
334 if (CorrectTypo(R, TUScope, 0) &&
335 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
336 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
337 << ProtocolId[i].first << R.getLookupName();
338 }
339 }
340
341 if (!PDecl) {
Chris Lattner3b054132008-11-19 05:08:23 +0000342 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000343 << ProtocolId[i].first;
Chris Lattner9c1842b2008-07-26 03:47:43 +0000344 continue;
345 }
Mike Stump11289f42009-09-09 15:08:12 +0000346
Douglas Gregor171c45a2009-02-18 21:56:37 +0000347 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000348
349 // If this is a forward declaration and we are supposed to warn in this
350 // case, do it.
351 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner3b054132008-11-19 05:08:23 +0000352 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000353 << ProtocolId[i].first;
Chris Lattner83f095c2009-03-28 19:18:32 +0000354 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattnerda463fe2007-12-12 07:09:47 +0000355 }
356}
357
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000358/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar4684f372008-08-27 05:40:03 +0000359/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000360///
Fariborz Jahanian7cf18862008-05-01 00:03:38 +0000361void
Mike Stump11289f42009-09-09 15:08:12 +0000362Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian7cf18862008-05-01 00:03:38 +0000363 ObjCPropertyDecl *SuperProperty,
Chris Lattner86d7d912008-11-24 03:54:41 +0000364 const IdentifierInfo *inheritedName) {
Mike Stump11289f42009-09-09 15:08:12 +0000365 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000366 Property->getPropertyAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000367 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000368 SuperProperty->getPropertyAttributes();
369 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
370 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner3b054132008-11-19 05:08:23 +0000371 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner86d7d912008-11-24 03:54:41 +0000372 << Property->getDeclName() << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000373 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
374 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner377d1f82008-11-18 22:52:51 +0000375 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000376 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000377 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
378 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner377d1f82008-11-18 22:52:51 +0000379 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000380 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump11289f42009-09-09 15:08:12 +0000381
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000382 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
383 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner377d1f82008-11-18 22:52:51 +0000384 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000385 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000386 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner377d1f82008-11-18 22:52:51 +0000387 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump11289f42009-09-09 15:08:12 +0000388 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000389 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner377d1f82008-11-18 22:52:51 +0000390 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000391 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff3e1181e2009-03-03 15:43:24 +0000392
Mike Stump11289f42009-09-09 15:08:12 +0000393 QualType LHSType =
Steve Naroff3e1181e2009-03-03 15:43:24 +0000394 Context.getCanonicalType(SuperProperty->getType());
Mike Stump11289f42009-09-09 15:08:12 +0000395 QualType RHSType =
Steve Naroff3e1181e2009-03-03 15:43:24 +0000396 Context.getCanonicalType(Property->getType());
Mike Stump11289f42009-09-09 15:08:12 +0000397
Steve Naroff3e1181e2009-03-03 15:43:24 +0000398 if (!Context.typesAreCompatible(LHSType, RHSType)) {
399 // FIXME: Incorporate this test with typesAreCompatible.
400 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff8e6aee52009-07-23 01:01:38 +0000401 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff3e1181e2009-03-03 15:43:24 +0000402 return;
403 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
404 << Property->getType() << SuperProperty->getType() << inheritedName;
405 }
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000406}
407
408/// ComparePropertiesInBaseAndSuper - This routine compares property
409/// declarations in base and its super class, if any, and issues
410/// diagnostics in a variety of inconsistant situations.
411///
Chris Lattner9018ca82009-02-16 21:26:43 +0000412void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000413 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
414 if (!SDecl)
415 return;
Daniel Dunbar4684f372008-08-27 05:40:03 +0000416 // FIXME: O(N^2)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000417 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
418 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian7cf18862008-05-01 00:03:38 +0000419 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000420 // Does property in super class has declaration in current class?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000421 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
422 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000423 ObjCPropertyDecl *PDecl = (*I);
424 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump11289f42009-09-09 15:08:12 +0000425 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner86d7d912008-11-24 03:54:41 +0000426 SDecl->getIdentifier());
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000427 }
428 }
429}
430
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000431/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
432/// of properties declared in a protocol and adds them to the list
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000433/// of properties for current class/category if it is not there already.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000434void
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000435Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner86d7d912008-11-24 03:54:41 +0000436 ObjCProtocolDecl *PDecl) {
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000437 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
438 if (!IDecl) {
439 // Category
440 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
441 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000442 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
443 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000444 ObjCPropertyDecl *Pr = (*P);
Steve Naroffb3a87982009-01-09 15:36:25 +0000445 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000446 // Is this property already in category's list of properties?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000447 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000448 if ((*CP)->getIdentifier() == Pr->getIdentifier())
449 break;
Fariborz Jahanian519976c2009-01-09 21:04:52 +0000450 if (CP != CE)
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000451 // Property protocol already exist in class. Diagnose any mismatch.
452 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
453 }
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000454 return;
455 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000456 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
457 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000458 ObjCPropertyDecl *Pr = (*P);
Steve Naroffb3a87982009-01-09 15:36:25 +0000459 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000460 // Is this property already in class's list of properties?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000461 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000462 if ((*CP)->getIdentifier() == Pr->getIdentifier())
463 break;
Fariborz Jahanian519976c2009-01-09 21:04:52 +0000464 if (CP != CE)
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000465 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner86d7d912008-11-24 03:54:41 +0000466 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000467 }
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000468}
469
470/// MergeProtocolPropertiesIntoClass - This routine merges properties
471/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000472/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000473///
Chris Lattner9018ca82009-02-16 21:26:43 +0000474void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000475 DeclPtrTy MergeItsProtocols) {
476 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000477 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
478
479 if (!IDecl) {
480 // Category
481 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
482 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
483 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
484 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
485 E = MDecl->protocol_end(); P != E; ++P)
486 // Merge properties of category (*P) into IDECL's
487 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Mike Stump11289f42009-09-09 15:08:12 +0000488
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000489 // Go thru the list of protocols for this category and recursively merge
490 // their properties into this class as well.
491 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
492 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattner83f095c2009-03-28 19:18:32 +0000493 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000494 } else {
495 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
496 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
497 E = MD->protocol_end(); P != E; ++P)
Chris Lattner83f095c2009-03-28 19:18:32 +0000498 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000499 }
500 return;
501 }
502
Chris Lattnerca1e8482008-07-21 07:06:49 +0000503 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000504 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
505 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000506 // Merge properties of class (*P) into IDECL's
Chris Lattnerca1e8482008-07-21 07:06:49 +0000507 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Mike Stump11289f42009-09-09 15:08:12 +0000508
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000509 // Go thru the list of protocols for this class and recursively merge
510 // their properties into this class as well.
511 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
512 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner83f095c2009-03-28 19:18:32 +0000513 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerca1e8482008-07-21 07:06:49 +0000514 } else {
Argyrios Kyrtzidisb3fa8632008-07-21 09:18:38 +0000515 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
516 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
517 E = MD->protocol_end(); P != E; ++P)
Chris Lattner83f095c2009-03-28 19:18:32 +0000518 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerca1e8482008-07-21 07:06:49 +0000519 }
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000520}
521
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000522/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000523/// a class method in its extension.
524///
Mike Stump11289f42009-09-09 15:08:12 +0000525void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000526 ObjCInterfaceDecl *ID) {
527 if (!ID)
528 return; // Possibly due to previous error
529
530 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000531 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
532 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000533 ObjCMethodDecl *MD = *i;
534 MethodMap[MD->getSelector()] = MD;
535 }
536
537 if (MethodMap.empty())
538 return;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000539 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
540 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000541 ObjCMethodDecl *Method = *i;
542 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
543 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
544 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
545 << Method->getDeclName();
546 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
547 }
548 }
549}
550
Chris Lattnerdac168d2009-04-12 08:43:13 +0000551/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner83f095c2009-03-28 19:18:32 +0000552Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000553Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000554 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000555 unsigned NumElts,
556 AttributeList *attrList) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000557 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Mike Stump11289f42009-09-09 15:08:12 +0000558
Chris Lattnerda463fe2007-12-12 07:09:47 +0000559 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000560 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000561 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000562 if (PDecl == 0) { // Not already seen?
Mike Stump11289f42009-09-09 15:08:12 +0000563 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000564 IdentList[i].second, Ident);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000565 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000566 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000567 if (attrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000568 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000569 Protocols.push_back(PDecl);
570 }
Mike Stump11289f42009-09-09 15:08:12 +0000571
572 ObjCForwardProtocolDecl *PDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000573 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlssona6b508a2008-11-04 16:57:32 +0000574 &Protocols[0], Protocols.size());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000575 CurContext->addDecl(PDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +0000576 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000577 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000578}
579
Chris Lattner83f095c2009-03-28 19:18:32 +0000580Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000581ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
582 IdentifierInfo *ClassName, SourceLocation ClassLoc,
583 IdentifierInfo *CategoryName,
584 SourceLocation CategoryLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000585 const DeclPtrTy *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000586 unsigned NumProtoRefs,
587 SourceLocation EndProtoLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000588 ObjCCategoryDecl *CDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000589 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
590 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000591 CurContext->addDecl(CDecl);
Chris Lattner9018ca82009-02-16 21:26:43 +0000592
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000593 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Fariborz Jahanian9a5124a2008-01-17 20:33:24 +0000594 /// Check that class of this category is already completely declared.
Chris Lattner9018ca82009-02-16 21:26:43 +0000595 if (!IDecl || IDecl->isForwardDecl()) {
596 CDecl->setInvalidDecl();
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000597 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner83f095c2009-03-28 19:18:32 +0000598 return DeclPtrTy::make(CDecl);
Fariborz Jahanian9a5124a2008-01-17 20:33:24 +0000599 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000600
Chris Lattner9018ca82009-02-16 21:26:43 +0000601 CDecl->setClassInterface(IDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000602
Chris Lattnerced903b2009-02-16 21:30:01 +0000603 // If the interface is deprecated, warn about it.
Douglas Gregor171c45a2009-02-18 21:56:37 +0000604 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner9018ca82009-02-16 21:26:43 +0000605
606 /// Check for duplicate interface declaration for this category
607 ObjCCategoryDecl *CDeclChain;
608 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
609 CDeclChain = CDeclChain->getNextClassCategory()) {
610 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
611 Diag(CategoryLoc, diag::warn_dup_category_def)
612 << ClassName << CategoryName;
613 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
614 break;
615 }
616 }
617 if (!CDeclChain)
618 CDecl->insertNextClassCategory();
619
Chris Lattnerda463fe2007-12-12 07:09:47 +0000620 if (NumProtoRefs) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000621 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
622 Context);
623 CDecl->setLocEnd(EndProtoLoc);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000624 // Protocols in the class extension belong to the class.
625 if (!CDecl->getIdentifier())
626 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
627 NumProtoRefs,Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000628 }
Mike Stump11289f42009-09-09 15:08:12 +0000629
Anders Carlssona6b508a2008-11-04 16:57:32 +0000630 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000631 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000632}
633
634/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000635/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000636/// object.
Chris Lattner83f095c2009-03-28 19:18:32 +0000637Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000638 SourceLocation AtCatImplLoc,
639 IdentifierInfo *ClassName, SourceLocation ClassLoc,
640 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000641 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000642 ObjCCategoryDecl *CatIDecl = 0;
643 if (IDecl) {
644 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
645 if (!CatIDecl) {
646 // Category @implementation with no corresponding @interface.
647 // Create and install one.
648 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
649 CatName);
650 CatIDecl->setClassInterface(IDecl);
651 CatIDecl->insertNextClassCategory();
652 }
653 }
654
Mike Stump11289f42009-09-09 15:08:12 +0000655 ObjCCategoryImplDecl *CDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000656 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
657 IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000658 /// Check that class of this category is already completely declared.
659 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000660 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000661
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000662 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000663 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000664
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000665 /// Check that CatName, category name, is not used in another implementation.
666 if (CatIDecl) {
667 if (CatIDecl->getImplementation()) {
668 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
669 << CatName;
670 Diag(CatIDecl->getImplementation()->getLocation(),
671 diag::note_previous_definition);
672 } else
673 CatIDecl->setImplementation(CDecl);
674 }
Mike Stump11289f42009-09-09 15:08:12 +0000675
Anders Carlssona6b508a2008-11-04 16:57:32 +0000676 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000677 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000678}
679
Chris Lattner83f095c2009-03-28 19:18:32 +0000680Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000681 SourceLocation AtClassImplLoc,
682 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000683 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000684 SourceLocation SuperClassLoc) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000685 ObjCInterfaceDecl* IDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000686 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +0000687 NamedDecl *PrevDecl
688 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000689 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000690 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000691 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerd13b8b52009-02-23 22:00:08 +0000692 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000693 // Is there an interface declaration of this class; if not, warn!
Mike Stump11289f42009-09-09 15:08:12 +0000694 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian6f0f25b2009-04-23 21:49:04 +0000695 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000696 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian6f0f25b2009-04-23 21:49:04 +0000697 IDecl = 0;
698 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000699 }
Mike Stump11289f42009-09-09 15:08:12 +0000700
Chris Lattnerda463fe2007-12-12 07:09:47 +0000701 // Check that super class name is valid class name
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000702 ObjCInterfaceDecl* SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000703 if (SuperClassname) {
704 // Check if a different kind of symbol declared in this scope.
John McCall9f3059a2009-10-09 21:13:30 +0000705 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000706 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000707 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
708 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +0000709 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000710 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000711 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000712 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000713 Diag(SuperClassLoc, diag::err_undef_superclass)
714 << SuperClassname << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000715 else if (IDecl && IDecl->getSuperClass() != SDecl) {
716 // This implementation and its interface do not have the same
717 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000718 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000719 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +0000720 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000721 }
722 }
723 }
Mike Stump11289f42009-09-09 15:08:12 +0000724
Chris Lattnerda463fe2007-12-12 07:09:47 +0000725 if (!IDecl) {
726 // Legacy case of @implementation with no corresponding @interface.
727 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000728
Mike Stump87c57ac2009-05-16 07:39:55 +0000729 // FIXME: Do we support attributes on the @implementation? If so we should
730 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +0000731 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000732 ClassName, ClassLoc, false, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000733 IDecl->setSuperClass(SDecl);
734 IDecl->setLocEnd(ClassLoc);
Douglas Gregorac345a32009-04-24 00:16:12 +0000735
736 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbard1148a72009-04-21 21:41:56 +0000737 } else {
738 // Mark the interface as being completed, even if it was just as
739 // @class ....;
740 // declaration; the user cannot reopen it.
741 IDecl->setForwardDecl(false);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000742 }
Mike Stump11289f42009-09-09 15:08:12 +0000743
744 ObjCImplementationDecl* IMPDecl =
745 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000746 IDecl, SDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000747
Anders Carlssona6b508a2008-11-04 16:57:32 +0000748 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner83f095c2009-03-28 19:18:32 +0000749 return DeclPtrTy::make(IMPDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000750
Chris Lattnerda463fe2007-12-12 07:09:47 +0000751 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000752 if (IDecl->getImplementation()) {
Chris Lattner36ac1ca2008-03-16 20:53:07 +0000753 // FIXME: Don't leak everything!
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000754 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000755 Diag(IDecl->getImplementation()->getLocation(),
756 diag::note_previous_definition);
757 } else { // add it to the list.
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000758 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +0000759 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000760 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000761 return DeclPtrTy::make(IMPDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000762}
763
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000764void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
765 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000766 SourceLocation RBrace) {
767 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000768 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000769 if (!IDecl)
770 return;
771 /// Check case of non-existing @interface decl.
772 /// (legacy objective-c @implementation decl without an @interface decl).
773 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +0000774 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner22298722009-02-20 21:35:13 +0000775 IDecl->setIVarList(ivars, numIvars, Context);
776 IDecl->setLocEnd(RBrace);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000777 return;
778 }
779 // If implementation has empty ivar list, just return.
780 if (numIvars == 0)
781 return;
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattnerda463fe2007-12-12 07:09:47 +0000783 assert(ivars && "missing @implementation ivars");
Mike Stump11289f42009-09-09 15:08:12 +0000784
Chris Lattnerda463fe2007-12-12 07:09:47 +0000785 // Check interface's Ivar list against those in the implementation.
786 // names and types must match.
787 //
Chris Lattnerda463fe2007-12-12 07:09:47 +0000788 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000789 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +0000790 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
791 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000792 ObjCIvarDecl* ImplIvar = ivars[j++];
793 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000794 assert (ImplIvar && "missing implementation ivar");
795 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +0000796
Steve Naroff157599f2009-03-03 14:49:36 +0000797 // First, make sure the types match.
Chris Lattner35ffe332008-07-27 00:05:05 +0000798 if (Context.getCanonicalType(ImplIvar->getType()) !=
799 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +0000800 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000801 << ImplIvar->getIdentifier()
802 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +0000803 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff157599f2009-03-03 14:49:36 +0000804 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
805 Expr *ImplBitWidth = ImplIvar->getBitWidth();
806 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000807 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
808 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff157599f2009-03-03 14:49:36 +0000809 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
810 << ImplIvar->getIdentifier();
811 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
812 }
Mike Stump11289f42009-09-09 15:08:12 +0000813 }
Steve Naroff157599f2009-03-03 14:49:36 +0000814 // Make sure the names are identical.
815 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000816 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000817 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +0000818 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000819 }
820 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000821 }
Mike Stump11289f42009-09-09 15:08:12 +0000822
Chris Lattner0f29d982007-12-12 18:11:49 +0000823 if (numIvars > 0)
Chris Lattner83021e92007-12-12 18:19:52 +0000824 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +0000825 else if (IVI != IVE)
Chris Lattner83021e92007-12-12 18:19:52 +0000826 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000827}
828
Steve Naroff15833ed2008-02-10 21:38:56 +0000829void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
830 bool &IncompleteImpl) {
831 if (!IncompleteImpl) {
832 Diag(ImpLoc, diag::warn_incomplete_impl);
833 IncompleteImpl = true;
834 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000835 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff15833ed2008-02-10 21:38:56 +0000836}
837
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000838void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
839 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnera9d0ffe2009-04-11 18:01:59 +0000840 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian0c20bdd2009-05-14 23:52:54 +0000841 ImpMethodDecl->getResultType()) &&
Steve Naroff8e6aee52009-07-23 01:01:38 +0000842 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
843 ImpMethodDecl->getResultType())) {
Mike Stump11289f42009-09-09 15:08:12 +0000844 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000845 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
846 << ImpMethodDecl->getResultType();
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000847 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
848 }
Mike Stump11289f42009-09-09 15:08:12 +0000849
Chris Lattner67f35b02009-04-11 19:58:42 +0000850 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
851 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
852 IM != EM; ++IM, ++IF) {
Fariborz Jahanian41e803d2009-11-18 18:56:09 +0000853 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
854 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
855 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
856 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner67f35b02009-04-11 19:58:42 +0000857 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000858
859 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000860 << ImpMethodDecl->getDeclName() << (*IF)->getType()
861 << (*IM)->getType();
Chris Lattner5300acc2009-04-11 20:14:49 +0000862 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner67f35b02009-04-11 19:58:42 +0000863 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000864}
865
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000866/// isPropertyReadonly - Return true if property is readonly, by searching
867/// for the property in the class and in its categories and implementations
868///
869bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffde680012009-02-26 19:11:32 +0000870 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000871 // by far the most common case.
872 if (!PDecl->isReadOnly())
873 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000874 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000875 // it is not considered read only.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000876 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000877 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000878
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000879 // Main class has the property as 'readonly'. Must search
Mike Stump11289f42009-09-09 15:08:12 +0000880 // through the category list to see if the property's
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000881 // attribute has been over-ridden to 'readwrite'.
882 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
883 Category; Category = Category->getNextClassCategory()) {
Mike Stump11289f42009-09-09 15:08:12 +0000884 // Even if property is ready only, if a category has a user defined setter,
885 // it is not considered read only.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000886 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000887 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000888 ObjCPropertyDecl *P =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000889 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000890 if (P && !P->isReadOnly())
891 return false;
892 }
Mike Stump11289f42009-09-09 15:08:12 +0000893
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000894 // Also, check for definition of a setter method in the implementation if
895 // all else failed.
896 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump11289f42009-09-09 15:08:12 +0000897 if (ObjCImplementationDecl *IMD =
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000898 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000899 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000900 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000901 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stump12b8ce12009-08-04 21:02:39 +0000902 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000903 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000904 return false;
905 }
906 }
Steve Naroffde680012009-02-26 19:11:32 +0000907 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000908 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000909 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroffde680012009-02-26 19:11:32 +0000910 return false;
Fariborz Jahanian15e3a5c2009-04-06 16:59:10 +0000911 // If all fails, look at the super class.
912 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
913 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000914 return true;
915}
916
Mike Stump87c57ac2009-05-16 07:39:55 +0000917/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
918/// improve the efficiency of selector lookups and type checking by associating
919/// with each protocol / interface / category the flattened instance tables. If
920/// we used an immutable set to keep the table then it wouldn't add significant
921/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +0000922
Steve Naroffa36992242008-02-08 22:06:17 +0000923/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +0000924/// Declared in protocol, and those referenced by it.
Steve Naroffa36992242008-02-08 22:06:17 +0000925void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
926 ObjCProtocolDecl *PDecl,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000927 bool& IncompleteImpl,
Steve Naroffa36992242008-02-08 22:06:17 +0000928 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000929 const llvm::DenseSet<Selector> &ClsMap,
930 ObjCInterfaceDecl *IDecl) {
931 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000932 ObjCInterfaceDecl *NSIDecl = 0;
933 if (getLangOptions().NeXTRuntime) {
Mike Stump11289f42009-09-09 15:08:12 +0000934 // check to see if class implements forwardInvocation method and objects
935 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000936 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +0000937 // Under such conditions, which means that every method possible is
938 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000939 // found" warnings.
940 // FIXME: Use a general GetUnarySelector method for this.
941 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
942 Selector fISelector = Context.Selectors.getSelector(1, &II);
943 if (InsMap.count(fISelector))
944 // Is IDecl derived from 'NSProxy'? If so, no instance methods
945 // need be implemented in the implementation.
946 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
947 }
Mike Stump11289f42009-09-09 15:08:12 +0000948
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000949 // If a method lookup fails locally we still need to look and see if
950 // the method was implemented by a base class or an inherited
951 // protocol. This lookup is slow, but occurs rarely in correct code
952 // and otherwise would terminate in a warning.
953
Chris Lattnerda463fe2007-12-12 07:09:47 +0000954 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000955 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +0000956 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000957 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000958 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000959 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000960 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000961 (!Super ||
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000962 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000963 // Ugly, but necessary. Method declared in protcol might have
964 // have been synthesized due to a property declared in the class which
965 // uses the protocol.
Mike Stump11289f42009-09-09 15:08:12 +0000966 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000967 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000968 if (!MethodInClass || !MethodInClass->isSynthesized())
969 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
970 }
971 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000972 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +0000973 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000974 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000975 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000976 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000977 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
978 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000979 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff15833ed2008-02-10 21:38:56 +0000980 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff3ce37a62007-12-14 23:37:57 +0000981 }
Chris Lattner390d39a2008-07-21 21:32:27 +0000982 // Check on this protocols's referenced protocols, recursively.
983 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
984 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000985 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000986}
987
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000988/// MatchAllMethodDeclarations - Check methods declaraed in interface or
989/// or protocol against those declared in their implementations.
990///
991void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
992 const llvm::DenseSet<Selector> &ClsMap,
993 llvm::DenseSet<Selector> &InsMapSeen,
994 llvm::DenseSet<Selector> &ClsMapSeen,
995 ObjCImplDecl* IMPDecl,
996 ObjCContainerDecl* CDecl,
997 bool &IncompleteImpl,
Mike Stump11289f42009-09-09 15:08:12 +0000998 bool ImmediateClass) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000999 // Check and see if instance methods in class interface have been
1000 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001001 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1002 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001003 if (InsMapSeen.count((*I)->getSelector()))
1004 continue;
1005 InsMapSeen.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001006 if (!(*I)->isSynthesized() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001007 !InsMap.count((*I)->getSelector())) {
1008 if (ImmediateClass)
1009 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1010 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +00001011 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001012 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001013 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001014 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001015 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001016 assert(IntfMethodDecl &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001017 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1018 // ImpMethodDecl may be null as in a @dynamic property.
1019 if (ImpMethodDecl)
1020 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1021 }
1022 }
Mike Stump11289f42009-09-09 15:08:12 +00001023
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001024 // Check and see if class methods in class interface have been
1025 // implemented in the implementation class. If so, their types match.
Mike Stump11289f42009-09-09 15:08:12 +00001026 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001027 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001028 if (ClsMapSeen.count((*I)->getSelector()))
1029 continue;
1030 ClsMapSeen.insert((*I)->getSelector());
1031 if (!ClsMap.count((*I)->getSelector())) {
1032 if (ImmediateClass)
1033 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stump12b8ce12009-08-04 21:02:39 +00001034 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001035 ObjCMethodDecl *ImpMethodDecl =
1036 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001037 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001038 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001039 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1040 }
1041 }
1042 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1043 // Check for any implementation of a methods declared in protocol.
1044 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1045 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001046 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1047 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001048 (*PI), IncompleteImpl, false);
1049 if (I->getSuperClass())
1050 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +00001051 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001052 I->getSuperClass(), IncompleteImpl, false);
1053 }
1054}
1055
Mike Stump11289f42009-09-09 15:08:12 +00001056void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1057 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001058 bool IncompleteImpl) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001059 llvm::DenseSet<Selector> InsMap;
1060 // Check and see if instance methods in class interface have been
1061 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001062 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001063 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001064 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001065
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001066 // Check and see if properties declared in the interface have either 1)
1067 // an implementation or 2) there is a @synthesize/@dynamic implementation
1068 // of the property in the @implementation.
1069 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001070 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1071 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001072 ObjCPropertyDecl *Prop = (*P);
1073 if (Prop->isInvalidDecl())
1074 continue;
1075 ObjCPropertyImplDecl *PI = 0;
1076 // Is there a matching propery synthesize/dynamic?
Mike Stump11289f42009-09-09 15:08:12 +00001077 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001078 I = IMPDecl->propimpl_begin(),
1079 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001080 if ((*I)->getPropertyDecl() == Prop) {
1081 PI = (*I);
1082 break;
1083 }
1084 if (PI)
1085 continue;
1086 if (!InsMap.count(Prop->getGetterName())) {
Mike Stump11289f42009-09-09 15:08:12 +00001087 Diag(Prop->getLocation(),
1088 diag::warn_setter_getter_impl_required)
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001089 << Prop->getDeclName() << Prop->getGetterName();
1090 Diag(IMPDecl->getLocation(),
1091 diag::note_property_impl_required);
1092 }
Mike Stump11289f42009-09-09 15:08:12 +00001093
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001094 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Mike Stump11289f42009-09-09 15:08:12 +00001095 Diag(Prop->getLocation(),
1096 diag::warn_setter_getter_impl_required)
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001097 << Prop->getDeclName() << Prop->getSetterName();
1098 Diag(IMPDecl->getLocation(),
1099 diag::note_property_impl_required);
1100 }
1101 }
Mike Stump11289f42009-09-09 15:08:12 +00001102
Chris Lattnerda463fe2007-12-12 07:09:47 +00001103 llvm::DenseSet<Selector> ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +00001104 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001105 I = IMPDecl->classmeth_begin(),
1106 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001107 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001108
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001109 // Check for type conflict of methods declared in a class/protocol and
1110 // its implementation; if any.
1111 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +00001112 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1113 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001114 IncompleteImpl, true);
Mike Stump11289f42009-09-09 15:08:12 +00001115
Chris Lattnerda463fe2007-12-12 07:09:47 +00001116 // Check the protocol list for unimplemented methods in the @implementation
1117 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001118 // Check and see if class methods in class interface have been
1119 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001120
Chris Lattner9ef10f42009-03-01 00:56:52 +00001121 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001122 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner9ef10f42009-03-01 00:56:52 +00001123 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001124 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001125 InsMap, ClsMap, I);
1126 // Check class extensions (unnamed categories)
1127 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1128 Categories; Categories = Categories->getNextClassCategory()) {
1129 if (!Categories->getIdentifier()) {
1130 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1131 break;
1132 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +00001133 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001134 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001135 // For extended class, unimplemented methods in its protocols will
1136 // be reported in the primary class.
1137 if (C->getIdentifier()) {
1138 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1139 E = C->protocol_end(); PI != E; ++PI)
1140 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1141 InsMap, ClsMap, C->getClassInterface());
1142 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001143 } else
1144 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00001145}
1146
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001147void
1148Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1149 ObjCContainerDecl* IDecl) {
1150 // Rules apply in non-GC mode only
1151 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1152 return;
1153 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1154 E = IDecl->prop_end();
1155 I != E; ++I) {
1156 ObjCPropertyDecl *Property = (*I);
1157 unsigned Attributes = Property->getPropertyAttributes();
1158 // We only care about readwrite atomic property.
1159 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1160 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1161 continue;
1162 if (const ObjCPropertyImplDecl *PIDecl
1163 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1164 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1165 continue;
1166 ObjCMethodDecl *GetterMethod =
1167 IMPDecl->getInstanceMethod(Property->getGetterName());
1168 ObjCMethodDecl *SetterMethod =
1169 IMPDecl->getInstanceMethod(Property->getSetterName());
1170 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1171 SourceLocation MethodLoc =
1172 (GetterMethod ? GetterMethod->getLocation()
1173 : SetterMethod->getLocation());
1174 Diag(MethodLoc, diag::warn_atomic_property_rule)
1175 << Property->getIdentifier();
1176 Diag(Property->getLocation(), diag::note_property_declare);
1177 }
1178 }
1179 }
1180}
1181
Mike Stump11289f42009-09-09 15:08:12 +00001182/// ActOnForwardClassDeclaration -
Chris Lattner83f095c2009-03-28 19:18:32 +00001183Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00001184Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00001185 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00001186 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00001187 unsigned NumElts) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001188 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump11289f42009-09-09 15:08:12 +00001189
Chris Lattnerda463fe2007-12-12 07:09:47 +00001190 for (unsigned i = 0; i != NumElts; ++i) {
1191 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001192 NamedDecl *PrevDecl
1193 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor5daeee22008-12-08 18:40:42 +00001194 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001195 // Maybe we will complain about the shadowed template parameter.
1196 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1197 // Just pretend that we didn't see the previous declaration.
1198 PrevDecl = 0;
1199 }
1200
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001201 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00001202 // GCC apparently allows the following idiom:
1203 //
1204 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1205 // @class XCElementToggler;
1206 //
Mike Stump11289f42009-09-09 15:08:12 +00001207 // FIXME: Make an extension?
Steve Naroff946166f2008-06-05 22:57:10 +00001208 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1209 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001210 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00001211 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stump12b8ce12009-08-04 21:02:39 +00001212 } else if (TDD) {
1213 // a forward class declaration matching a typedef name of a class refers
1214 // to the underlying class.
Mike Stump11289f42009-09-09 15:08:12 +00001215 if (ObjCInterfaceType * OI =
Fariborz Jahanian0d451812009-05-07 21:49:26 +00001216 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1217 PrevDecl = OI->getDecl();
1218 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001219 }
Mike Stump11289f42009-09-09 15:08:12 +00001220 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001221 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump11289f42009-09-09 15:08:12 +00001222 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001223 IdentList[i], IdentLocs[i], true);
Ted Kremenek707ece62009-11-17 22:58:30 +00001224
1225 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1226 // the current DeclContext. This prevents clients that walk DeclContext
1227 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1228 // declared later (if at all). We also take care to explicitly make
1229 // sure this declaration is visible for name lookup.
1230 PushOnScopeChains(IDecl, TUScope, false);
1231 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001232 }
1233
1234 Interfaces.push_back(IDecl);
1235 }
Mike Stump11289f42009-09-09 15:08:12 +00001236
Ted Kremenek9b124e12009-11-18 00:28:11 +00001237 assert(Interfaces.size() == NumElts);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001238 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001239 Interfaces.data(), IdentLocs,
Anders Carlssona6b508a2008-11-04 16:57:32 +00001240 Interfaces.size());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001241 CurContext->addDecl(CDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +00001242 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001243 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001244}
1245
1246
1247/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1248/// returns true, or false, accordingly.
1249/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump11289f42009-09-09 15:08:12 +00001250bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffa0ed1652008-10-21 10:37:50 +00001251 const ObjCMethodDecl *PrevMethod,
1252 bool matchBasedOnSizeAndAlignment) {
1253 QualType T1 = Context.getCanonicalType(Method->getResultType());
1254 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump11289f42009-09-09 15:08:12 +00001255
Steve Naroffa0ed1652008-10-21 10:37:50 +00001256 if (T1 != T2) {
1257 // The result types are different.
1258 if (!matchBasedOnSizeAndAlignment)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001259 return false;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001260 // Incomplete types don't have a size and alignment.
1261 if (T1->isIncompleteType() || T2->isIncompleteType())
1262 return false;
1263 // Check is based on size and alignment.
1264 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1265 return false;
1266 }
Mike Stump11289f42009-09-09 15:08:12 +00001267
Chris Lattnera4997152009-02-20 18:43:26 +00001268 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1269 E = Method->param_end();
1270 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001271
Chris Lattnera4997152009-02-20 18:43:26 +00001272 for (; ParamI != E; ++ParamI, ++PrevI) {
1273 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1274 T1 = Context.getCanonicalType((*ParamI)->getType());
1275 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffa0ed1652008-10-21 10:37:50 +00001276 if (T1 != T2) {
1277 // The result types are different.
1278 if (!matchBasedOnSizeAndAlignment)
1279 return false;
1280 // Incomplete types don't have a size and alignment.
1281 if (T1->isIncompleteType() || T2->isIncompleteType())
1282 return false;
1283 // Check is based on size and alignment.
1284 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1285 return false;
1286 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001287 }
1288 return true;
1289}
1290
Douglas Gregorc78d3462009-04-24 21:10:55 +00001291/// \brief Read the contents of the instance and factory method pools
1292/// for a given selector from external storage.
1293///
1294/// This routine should only be called once, when neither the instance
1295/// nor the factory method pool has an entry for this selector.
Mike Stump11289f42009-09-09 15:08:12 +00001296Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001297 bool isInstance) {
1298 assert(ExternalSource && "We need an external AST source");
1299 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1300 "Selector data already loaded into the instance method pool");
1301 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1302 "Selector data already loaded into the factory method pool");
1303
1304 // Read the method list from the external source.
1305 std::pair<ObjCMethodList, ObjCMethodList> Methods
1306 = ExternalSource->ReadMethodPool(Sel);
Mike Stump11289f42009-09-09 15:08:12 +00001307
Douglas Gregorc78d3462009-04-24 21:10:55 +00001308 if (isInstance) {
1309 if (Methods.second.Method)
1310 FactoryMethodPool[Sel] = Methods.second;
1311 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump11289f42009-09-09 15:08:12 +00001312 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001313
1314 if (Methods.first.Method)
1315 InstanceMethodPool[Sel] = Methods.first;
1316
1317 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1318}
1319
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001320void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001321 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1322 = InstanceMethodPool.find(Method->getSelector());
1323 if (Pos == InstanceMethodPool.end()) {
1324 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1325 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1326 else
1327 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1328 ObjCMethodList())).first;
1329 }
1330
1331 ObjCMethodList &Entry = Pos->second;
Chris Lattner7b26b292009-03-04 05:16:45 +00001332 if (Entry.Method == 0) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001333 // Haven't seen a method with this selector name yet - add it.
Chris Lattner7b26b292009-03-04 05:16:45 +00001334 Entry.Method = Method;
1335 Entry.Next = 0;
1336 return;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001337 }
Mike Stump11289f42009-09-09 15:08:12 +00001338
Chris Lattner7b26b292009-03-04 05:16:45 +00001339 // We've seen a method with this name, see if we have already seen this type
1340 // signature.
1341 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1342 if (MatchTwoMethodDeclarations(Method, List->Method))
1343 return;
Mike Stump11289f42009-09-09 15:08:12 +00001344
Chris Lattner7b26b292009-03-04 05:16:45 +00001345 // We have a new signature for an existing method - add it.
1346 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1347 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001348}
1349
Steve Naroff9ebc0502008-10-21 10:50:19 +00001350// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump11289f42009-09-09 15:08:12 +00001351ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001352 SourceRange R,
1353 bool warn) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001354 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1355 = InstanceMethodPool.find(Sel);
Douglas Gregor9a1899b2009-04-24 22:23:41 +00001356 if (Pos == InstanceMethodPool.end()) {
1357 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001358 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1359 else
1360 return 0;
1361 }
1362
1363 ObjCMethodList &MethList = Pos->second;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001364 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001365
Steve Naroff4a82d812008-09-30 14:38:43 +00001366 if (MethList.Method && MethList.Next) {
Steve Naroffa0ed1652008-10-21 10:37:50 +00001367 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1368 // This checks if the methods differ by size & alignment.
1369 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001370 issueWarning = warn;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001371 }
1372 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattnere4b95692008-11-24 03:33:13 +00001373 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCalle29c5cd2009-12-10 19:51:03 +00001374 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerf490e152008-11-19 05:27:50 +00001375 << MethList.Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001376 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCalle29c5cd2009-12-10 19:51:03 +00001377 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerf490e152008-11-19 05:27:50 +00001378 << Next->Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001379 }
1380 return MethList.Method;
1381}
1382
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001383void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001384 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1385 = FactoryMethodPool.find(Method->getSelector());
1386 if (Pos == FactoryMethodPool.end()) {
1387 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1388 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1389 else
1390 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1391 ObjCMethodList())).first;
1392 }
1393
1394 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001395 if (!FirstMethod.Method) {
1396 // Haven't seen a method with this selector name yet - add it.
1397 FirstMethod.Method = Method;
1398 FirstMethod.Next = 0;
1399 } else {
1400 // We've seen a method with this name, now check the type signature(s).
1401 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump11289f42009-09-09 15:08:12 +00001402
1403 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001404 Next = Next->Next)
1405 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump11289f42009-09-09 15:08:12 +00001406
Chris Lattnerda463fe2007-12-12 07:09:47 +00001407 if (!match) {
1408 // We have a new signature for an existing method - add it.
1409 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001410 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001411 FirstMethod.Next = OMI;
1412 }
1413 }
1414}
1415
Mike Stump11289f42009-09-09 15:08:12 +00001416ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001417 SourceRange R) {
1418 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1419 = FactoryMethodPool.find(Sel);
1420 if (Pos == FactoryMethodPool.end()) {
Mike Stump11289f42009-09-09 15:08:12 +00001421 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001422 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1423 else
1424 return 0;
1425 }
1426
1427 ObjCMethodList &MethList = Pos->second;
1428 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001429
Douglas Gregorc78d3462009-04-24 21:10:55 +00001430 if (MethList.Method && MethList.Next) {
1431 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1432 // This checks if the methods differ by size & alignment.
1433 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1434 issueWarning = true;
1435 }
1436 if (issueWarning && (MethList.Method && MethList.Next)) {
1437 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCalle29c5cd2009-12-10 19:51:03 +00001438 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorc78d3462009-04-24 21:10:55 +00001439 << MethList.Method->getSourceRange();
1440 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCalle29c5cd2009-12-10 19:51:03 +00001441 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorc78d3462009-04-24 21:10:55 +00001442 << Next->Method->getSourceRange();
1443 }
1444 return MethList.Method;
1445}
1446
Mike Stump11289f42009-09-09 15:08:12 +00001447/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff35c62ae2009-01-08 17:28:14 +00001448/// have the property type and issue diagnostics if they don't.
1449/// Also synthesize a getter/setter method if none exist (and update the
1450/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1451/// methods is the "right" thing to do.
Mike Stump11289f42009-09-09 15:08:12 +00001452void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff35c62ae2009-01-08 17:28:14 +00001453 ObjCContainerDecl *CD) {
1454 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump11289f42009-09-09 15:08:12 +00001455
1456 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001457 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump11289f42009-09-09 15:08:12 +00001458 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +00001459 property->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001460
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001461 if (SetterMethod) {
Mike Stump11289f42009-09-09 15:08:12 +00001462 if (Context.getCanonicalType(SetterMethod->getResultType())
Fariborz Jahanian06d0dd62008-12-06 23:12:49 +00001463 != Context.VoidTy)
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001464 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattnera4997152009-02-20 18:43:26 +00001465 if (SetterMethod->param_size() != 1 ||
1466 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump11289f42009-09-09 15:08:12 +00001467 Diag(property->getLocation(),
1468 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001469 << property->getDeclName()
Ted Kremenek9a468042009-03-14 00:20:08 +00001470 << SetterMethod->getSelector();
Fariborz Jahanianff839982008-12-06 21:48:16 +00001471 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1472 }
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001473 }
Steve Naroff35c62ae2009-01-08 17:28:14 +00001474
1475 // Synthesize getter/setter methods if none exist.
Steve Naroffe538c5f2009-01-08 20:15:03 +00001476 // Find the default getter and if one not found, add one.
Mike Stump87c57ac2009-05-16 07:39:55 +00001477 // FIXME: The synthesized property we set here is misleading. We almost always
1478 // synthesize these methods unless the user explicitly provided prototypes
1479 // (which is odd, but allowed). Sema should be typechecking that the
1480 // declarations jive in that situation (which it is not currently).
Steve Naroffe538c5f2009-01-08 20:15:03 +00001481 if (!GetterMethod) {
1482 // No instance method of same name as property getter name was found.
Mike Stump11289f42009-09-09 15:08:12 +00001483 // Declare a getter method and add it to the list of methods
Steve Naroffe538c5f2009-01-08 20:15:03 +00001484 // for this class.
Mike Stump11289f42009-09-09 15:08:12 +00001485 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1486 property->getLocation(), property->getGetterName(),
1487 property->getType(), CD, true, false, true,
1488 (property->getPropertyImplementation() ==
1489 ObjCPropertyDecl::Optional) ?
1490 ObjCMethodDecl::Optional :
Steve Naroffe538c5f2009-01-08 20:15:03 +00001491 ObjCMethodDecl::Required);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001492 CD->addDecl(GetterMethod);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001493 } else
1494 // A user declared getter will be synthesize when @synthesize of
1495 // the property with the same name is seen in the @implementation
Steve Naroff04f2d142009-04-20 15:06:07 +00001496 GetterMethod->setSynthesized(true);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001497 property->setGetterMethodDecl(GetterMethod);
1498
1499 // Skip setter if property is read-only.
1500 if (!property->isReadOnly()) {
1501 // Find the default setter and if one not found, add one.
1502 if (!SetterMethod) {
1503 // No instance method of same name as property setter name was found.
Mike Stump11289f42009-09-09 15:08:12 +00001504 // Declare a setter method and add it to the list of methods
Steve Naroffe538c5f2009-01-08 20:15:03 +00001505 // for this class.
Mike Stump11289f42009-09-09 15:08:12 +00001506 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1507 property->getLocation(),
1508 property->getSetterName(),
Steve Naroffe538c5f2009-01-08 20:15:03 +00001509 Context.VoidTy, CD, true, false, true,
Mike Stump11289f42009-09-09 15:08:12 +00001510 (property->getPropertyImplementation() ==
1511 ObjCPropertyDecl::Optional) ?
1512 ObjCMethodDecl::Optional :
Steve Naroffe538c5f2009-01-08 20:15:03 +00001513 ObjCMethodDecl::Required);
1514 // Invent the arguments for the setter. We don't bother making a
1515 // nice name for the argument.
1516 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump11289f42009-09-09 15:08:12 +00001517 property->getLocation(),
Steve Naroffe538c5f2009-01-08 20:15:03 +00001518 property->getIdentifier(),
1519 property->getType(),
John McCallbcd03502009-12-07 02:54:59 +00001520 /*TInfo=*/0,
Steve Naroffe538c5f2009-01-08 20:15:03 +00001521 VarDecl::None,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001522 0);
Steve Naroff04f2d142009-04-20 15:06:07 +00001523 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001524 CD->addDecl(SetterMethod);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001525 } else
1526 // A user declared setter will be synthesize when @synthesize of
1527 // the property with the same name is seen in the @implementation
Steve Naroff04f2d142009-04-20 15:06:07 +00001528 SetterMethod->setSynthesized(true);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001529 property->setSetterMethodDecl(SetterMethod);
1530 }
Mike Stump11289f42009-09-09 15:08:12 +00001531 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff35c62ae2009-01-08 17:28:14 +00001532 // handle the following, which is supported by GCC (and part of the design).
1533 //
1534 // @interface Foo
1535 // @property double bar;
1536 // @end
1537 //
1538 // void thisIsUnfortunate() {
1539 // id foo;
1540 // double bar = [foo bar];
1541 // }
1542 //
Douglas Gregor020713e2009-01-09 19:42:16 +00001543 if (GetterMethod)
Mike Stump11289f42009-09-09 15:08:12 +00001544 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor020713e2009-01-09 19:42:16 +00001545 if (SetterMethod)
Mike Stump11289f42009-09-09 15:08:12 +00001546 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001547}
1548
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001549/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1550/// identical selector names in current and its super classes and issues
1551/// a warning if any of their argument types are incompatible.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001552void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1553 ObjCMethodDecl *Method,
1554 bool IsInstance) {
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001555 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1556 if (ID == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +00001557
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001558 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump11289f42009-09-09 15:08:12 +00001559 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001560 SD->lookupMethod(Method->getSelector(), IsInstance);
1561 if (SuperMethodDecl == 0) {
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001562 ID = SD;
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001563 continue;
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001564 }
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001565 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1566 E = Method->param_end();
1567 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1568 for (; ParamI != E; ++ParamI, ++PrevI) {
1569 // Number of parameters are the same and is guaranteed by selector match.
1570 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1571 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1572 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1573 // If type of arguement of method in this class does not match its
1574 // respective argument type in the super class method, issue warning;
1575 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump11289f42009-09-09 15:08:12 +00001576 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001577 << T1 << T2;
1578 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1579 return;
1580 }
1581 }
1582 ID = SD;
1583 }
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001584}
1585
Steve Naroff1d2538c2007-12-18 01:30:32 +00001586// Note: For class/category implemenations, allMethods/allProperties is
1587// always null.
Chris Lattner83f095c2009-03-28 19:18:32 +00001588void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1589 DeclPtrTy *allMethods, unsigned allNum,
1590 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001591 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001592 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001593
Steve Naroff1d2538c2007-12-18 01:30:32 +00001594 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1595 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattnerda463fe2007-12-12 07:09:47 +00001596 // should be true.
1597 if (!ClassDecl)
1598 return;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001599
Mike Stump11289f42009-09-09 15:08:12 +00001600 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00001601 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1602 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001603 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00001604
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001605 if (!isInterfaceDeclKind && AtEndLoc.isInvalid()) {
1606 AtEndLoc = ClassDecl->getLocation();
1607 Diag(AtEndLoc, diag::warn_missing_atend);
1608 }
1609
Steve Naroff35c62ae2009-01-08 17:28:14 +00001610 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff35c62ae2009-01-08 17:28:14 +00001611
1612 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1613 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1614 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1615
Chris Lattnerda463fe2007-12-12 07:09:47 +00001616 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001617 ObjCMethodDecl *Method =
Chris Lattner83f095c2009-03-28 19:18:32 +00001618 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001619
1620 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00001621 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001622 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001623 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001624 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001625 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001626 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001627 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001628 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001629 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001630 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001631 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001632 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001633 InsMap[Method->getSelector()] = Method;
1634 /// The following allows us to typecheck messages to "id".
1635 AddInstanceMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001636 // verify that the instance method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001637 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001638 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001639 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001640 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001641 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001642 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001643 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001644 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001645 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001646 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001647 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001648 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001649 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001650 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001651 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001652 ClsMap[Method->getSelector()] = Method;
Steve Naroff1d2538c2007-12-18 01:30:32 +00001653 /// The following allows us to typecheck messages to "Class".
1654 AddFactoryMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001655 // verify that the class method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001656 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001657 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001658 }
1659 }
1660 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001661 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump11289f42009-09-09 15:08:12 +00001662 // Compares properties declared in this class to those of its
Fariborz Jahanian7cf18862008-05-01 00:03:38 +00001663 // super class.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +00001664 ComparePropertiesInBaseAndSuper(I);
Chris Lattner83f095c2009-03-28 19:18:32 +00001665 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroffb3a87982009-01-09 15:36:25 +00001666 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001667 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00001668 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001669 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001670
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +00001671 // Merge protocol properties into category
Chris Lattner83f095c2009-03-28 19:18:32 +00001672 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanian33afd772009-03-02 19:05:07 +00001673 if (C->getIdentifier() == 0)
1674 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001675 }
Steve Naroffb3a87982009-01-09 15:36:25 +00001676 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1677 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1678 // user-defined setter/getter. It also synthesizes setter/getter methods
1679 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001680 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1681 E = CDecl->prop_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001682 I != E; ++I)
Chris Lattner41fd42e2009-02-16 18:32:47 +00001683 ProcessPropertyDecl(*I, CDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00001684 CDecl->setAtEndLoc(AtEndLoc);
1685 }
1686 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001687 IC->setAtEndLoc(AtEndLoc);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001688 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001689 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001690 AtomicPropertySetterGetterRules(IC, IDecl);
1691 }
Mike Stump11289f42009-09-09 15:08:12 +00001692 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00001693 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001694 CatImplClass->setAtEndLoc(AtEndLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001695
Chris Lattnerda463fe2007-12-12 07:09:47 +00001696 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00001697 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00001698 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001699 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001700 Categories; Categories = Categories->getNextClassCategory()) {
1701 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner9ef10f42009-03-01 00:56:52 +00001702 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001703 break;
1704 }
1705 }
1706 }
1707 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001708 if (isInterfaceDeclKind) {
1709 // Reject invalid vardecls.
1710 for (unsigned i = 0; i != tuvNum; i++) {
1711 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1712 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1713 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00001714 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00001715 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00001716 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001717 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00001718 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001719}
1720
1721
1722/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1723/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00001724static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001725CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1726 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1727 if (PQTVal & ObjCDeclSpec::DQ_In)
1728 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1729 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1730 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1731 if (PQTVal & ObjCDeclSpec::DQ_Out)
1732 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1733 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1734 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1735 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1736 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1737 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1738 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001739
1740 return ret;
1741}
1742
Chris Lattner83f095c2009-03-28 19:18:32 +00001743Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattnerda463fe2007-12-12 07:09:47 +00001744 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001745 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001746 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001747 Selector Sel,
1748 // optional arguments. The number of types/arguments is obtained
1749 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001750 ObjCArgInfo *ArgInfo,
Fariborz Jahaniane84858c2009-01-09 00:38:19 +00001751 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001752 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1753 bool isVariadic) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001754 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff83777fe2008-02-29 21:48:07 +00001755
1756 // Make sure we can establish a context for the method.
1757 if (!ClassDecl) {
1758 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanianb1771e42009-08-28 17:52:37 +00001759 FunctionLabelMap.clear();
Chris Lattner83f095c2009-03-28 19:18:32 +00001760 return DeclPtrTy();
Steve Naroff83777fe2008-02-29 21:48:07 +00001761 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001762 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00001763
Steve Naroff32606412009-02-20 22:59:16 +00001764 if (ReturnType) {
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +00001765 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump11289f42009-09-09 15:08:12 +00001766
Steve Naroff32606412009-02-20 22:59:16 +00001767 // Methods cannot return interface types. All ObjC objects are
1768 // passed by reference.
1769 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattnerde5a5312009-04-11 19:08:56 +00001770 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1771 << 0 << resultDeclType;
Chris Lattner83f095c2009-03-28 19:18:32 +00001772 return DeclPtrTy();
Steve Naroff32606412009-02-20 22:59:16 +00001773 }
1774 } else // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001775 resultDeclType = Context.getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +00001776
1777 ObjCMethodDecl* ObjCMethod =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001778 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump11289f42009-09-09 15:08:12 +00001779 cast<DeclContext>(ClassDecl),
Chris Lattner8d8829e2008-03-16 00:49:28 +00001780 MethodType == tok::minus, isVariadic,
Fariborz Jahanian8983f172008-05-07 20:53:44 +00001781 false,
Mike Stump11289f42009-09-09 15:08:12 +00001782 MethodDeclKind == tok::objc_optional ?
1783 ObjCMethodDecl::Optional :
Chris Lattner8d8829e2008-03-16 00:49:28 +00001784 ObjCMethodDecl::Required);
Mike Stump11289f42009-09-09 15:08:12 +00001785
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001786 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00001787
Chris Lattner23b0faf2009-04-11 19:42:43 +00001788 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00001789 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00001790 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00001791
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001792 if (ArgInfo[i].Type == 0) {
John McCall856bbea2009-10-23 21:48:59 +00001793 ArgType = Context.getObjCIdType();
1794 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001795 } else {
John McCall856bbea2009-10-23 21:48:59 +00001796 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroffb0498ee2008-12-09 19:36:17 +00001797 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner9713a1c2009-04-11 19:34:56 +00001798 ArgType = adjustParameterType(ArgType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001799 }
Mike Stump11289f42009-09-09 15:08:12 +00001800
John McCall856bbea2009-10-23 21:48:59 +00001801 ParmVarDecl* Param
1802 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1803 ArgInfo[i].Name, ArgType, DI,
1804 VarDecl::None, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001805
Chris Lattner9713a1c2009-04-11 19:34:56 +00001806 if (ArgType->isObjCInterfaceType()) {
1807 Diag(ArgInfo[i].NameLoc,
1808 diag::err_object_cannot_be_passed_returned_by_value)
1809 << 1 << ArgType;
1810 Param->setInvalidDecl();
1811 }
Mike Stump11289f42009-09-09 15:08:12 +00001812
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001813 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001814 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00001815
Chris Lattner9713a1c2009-04-11 19:34:56 +00001816 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00001817 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001818
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001819 Params.push_back(Param);
1820 }
1821
Jay Foad7d0479f2009-05-21 09:52:38 +00001822 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001823 ObjCMethod->setObjCDeclQualifier(
1824 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1825 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001826
1827 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00001828 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00001829
John McCall28a6aea2009-11-04 02:18:39 +00001830 const ObjCMethodDecl *InterfaceMD = 0;
1831
Mike Stump11289f42009-09-09 15:08:12 +00001832 // For implementations (which can be very "coarse grain"), we add the
1833 // method now. This allows the AST to implement lookup methods that work
1834 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattnerda463fe2007-12-12 07:09:47 +00001835 // us to flag multiple declaration errors as they occur.
Mike Stump11289f42009-09-09 15:08:12 +00001836 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001837 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001838 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001839 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1840 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001841 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001842 PrevMethod = ImpDecl->getClassMethod(Sel);
1843 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001844 }
John McCall28a6aea2009-11-04 02:18:39 +00001845 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1846 MethodType == tok::minus);
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001847 if (AttrList)
1848 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump11289f42009-09-09 15:08:12 +00001849 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stump12b8ce12009-08-04 21:02:39 +00001850 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001851 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001852 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1853 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001854 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001855 PrevMethod = CatImpDecl->getClassMethod(Sel);
1856 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001857 }
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001858 if (AttrList)
1859 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001860 }
1861 if (PrevMethod) {
1862 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00001863 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001864 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001865 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump11289f42009-09-09 15:08:12 +00001866 }
John McCall28a6aea2009-11-04 02:18:39 +00001867
1868 // If the interface declared this method, and it was deprecated there,
1869 // mark it deprecated here.
1870 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1871 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1872
Chris Lattner83f095c2009-03-28 19:18:32 +00001873 return DeclPtrTy::make(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001874}
1875
Mike Stump11289f42009-09-09 15:08:12 +00001876void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001877 SourceLocation Loc,
1878 unsigned &Attributes) {
1879 // FIXME: Improve the reported location.
1880
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00001881 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001882 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00001883 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1884 ObjCDeclSpec::DQ_PR_assign |
1885 ObjCDeclSpec::DQ_PR_copy |
1886 ObjCDeclSpec::DQ_PR_retain))) {
1887 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1888 "readwrite" :
1889 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1890 "assign" :
1891 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1892 "copy" : "retain";
Mike Stump11289f42009-09-09 15:08:12 +00001893
1894 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner409f5552009-01-29 18:49:48 +00001895 diag::err_objc_property_attr_mutually_exclusive :
1896 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00001897 << "readonly" << which;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001898 }
1899
1900 // Check for copy or retain on non-object types.
1901 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump11289f42009-09-09 15:08:12 +00001902 !PropertyTy->isObjCObjectPointerType() &&
1903 !PropertyTy->isBlockPointerType() &&
Steve Naroff79d12152009-07-16 15:41:00 +00001904 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001905 Diag(Loc, diag::err_objc_property_requires_object)
1906 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001907 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1908 }
1909
1910 // Check for more than one of { assign, copy, retain }.
1911 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1912 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001913 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1914 << "assign" << "copy";
1915 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump11289f42009-09-09 15:08:12 +00001916 }
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001917 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001918 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1919 << "assign" << "retain";
1920 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001921 }
1922 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1923 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001924 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1925 << "copy" << "retain";
1926 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001927 }
1928 }
1929
1930 // Warn if user supplied no assignment attribute, property is
1931 // readwrite, and this is an object type.
1932 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1933 ObjCDeclSpec::DQ_PR_retain)) &&
1934 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Naroff79d12152009-07-16 15:41:00 +00001935 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001936 // Skip this warning in gc-only mode.
Mike Stump11289f42009-09-09 15:08:12 +00001937 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001938 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1939
1940 // If non-gc code warn that this is likely inappropriate.
1941 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1942 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump11289f42009-09-09 15:08:12 +00001943
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001944 // FIXME: Implement warning dependent on NSCopying being
1945 // implemented. See also:
1946 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1947 // (please trim this list while you are at it).
1948 }
Mike Stump5580bdc2009-05-07 23:06:50 +00001949
1950 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1951 && getLangOptions().getGCMode() == LangOptions::GCOnly
1952 && PropertyTy->isBlockPointerType())
1953 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001954}
1955
Mike Stump11289f42009-09-09 15:08:12 +00001956Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001957 FieldDeclarator &FD,
1958 ObjCDeclSpec &ODS,
1959 Selector GetterSel,
1960 Selector SetterSel,
1961 DeclPtrTy ClassCategory,
1962 bool *isOverridingProperty,
1963 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001964 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001965 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1966 // default is readwrite!
1967 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump11289f42009-09-09 15:08:12 +00001968 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001969 // not retain or copy
1970 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump11289f42009-09-09 15:08:12 +00001971 (isReadWrite &&
1972 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001973 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1974 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian00857fc2009-12-16 18:03:30 +00001975 if (T->isReferenceType()) {
1976 Diag(AtLoc, diag::error_reference_property);
1977 return DeclPtrTy();
1978 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001979 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001980 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001981 // May modify Attributes.
1982 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001983 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1984 if (!CDecl->getIdentifier()) {
Mike Stump11289f42009-09-09 15:08:12 +00001985 // This is a continuation class. property requires special
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001986 // handling.
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001987 if ((CCPrimary = CDecl->getClassInterface())) {
1988 // Find the property in continuation class's primary class only.
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001989 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniande8db162009-11-02 22:45:15 +00001990 if (ObjCPropertyDecl *PIDecl =
1991 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001992 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahaniane4fd6402009-04-01 23:23:53 +00001993 // with continuation class's readwrite property attribute!
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001994 unsigned PIkind = PIDecl->getPropertyAttributes();
1995 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandc216102009-11-10 19:31:09 +00001996 unsigned retainCopyNonatomic =
Fariborz Jahanian3600f412009-11-06 22:59:12 +00001997 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniana386d952009-11-03 00:01:38 +00001998 ObjCPropertyDecl::OBJC_PR_copy |
1999 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahaniandc216102009-11-10 19:31:09 +00002000 if ((Attributes & retainCopyNonatomic) !=
2001 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002002 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniana386d952009-11-03 00:01:38 +00002003 Diag(PIDecl->getLocation(), diag::note_property_declare);
2004 }
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002005 PIDecl->makeitReadWriteAttribute();
2006 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2007 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2008 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2009 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2010 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniande8db162009-11-02 22:45:15 +00002011 } else {
Mike Stump11289f42009-09-09 15:08:12 +00002012 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002013 << CCPrimary->getDeclName();
Fariborz Jahaniande8db162009-11-02 22:45:15 +00002014 Diag(PIDecl->getLocation(), diag::note_property_declare);
2015 }
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002016 *isOverridingProperty = true;
Mike Stump11289f42009-09-09 15:08:12 +00002017 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian47b21082009-04-15 19:19:03 +00002018 // class's list.
2019 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002020 return DeclPtrTy();
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002021 }
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002022 // No matching property found in the primary class. Just fall thru
2023 // and add property to continuation class's primary class.
2024 ClassDecl = CCPrimary;
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002025 } else {
Chris Lattner68d42c72009-02-20 21:38:52 +00002026 Diag(CDecl->getLocation(), diag::err_continuation_class);
2027 *isOverridingProperty = true;
Chris Lattner83f095c2009-03-28 19:18:32 +00002028 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002029 }
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002030 }
Mike Stump11289f42009-09-09 15:08:12 +00002031
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002032 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump11289f42009-09-09 15:08:12 +00002033 // gc'able conforms to NSCopying protocol
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002034 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2035 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2036 if (T->isObjCObjectPointerType()) {
2037 QualType InterfaceTy = T->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +00002038 if (const ObjCInterfaceType *OIT =
John McCall9dd450b2009-09-21 23:43:11 +00002039 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian83b000c2009-08-14 18:06:25 +00002040 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002041 if (IDecl)
Mike Stump11289f42009-09-09 15:08:12 +00002042 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002043 LookupProtocol(&Context.Idents.get("NSCopying")))
2044 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump11289f42009-09-09 15:08:12 +00002045 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002046 << FD.D.getIdentifier();
Fariborz Jahanian83b000c2009-08-14 18:06:25 +00002047 }
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002048 }
Fariborz Jahanian887cd6a2009-08-12 18:17:53 +00002049 if (T->isObjCInterfaceType())
2050 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump11289f42009-09-09 15:08:12 +00002051
Steve Naroffba3dc382009-01-11 12:47:58 +00002052 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2053 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattner5300acc2009-04-11 20:14:49 +00002054 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump11289f42009-09-09 15:08:12 +00002055 FD.D.getIdentifierLoc(),
Fariborz Jahanian0152a1a2008-04-14 23:36:35 +00002056 FD.D.getIdentifier(), T);
Fariborz Jahanian057a17e2009-12-17 00:49:09 +00002057 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
2058 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2059 Diag(PDecl->getLocation(), diag::err_duplicate_property);
2060 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2061 PDecl->setInvalidDecl();
2062 }
2063 else
2064 DC->addDecl(PDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002065
Chris Lattner5300acc2009-04-11 20:14:49 +00002066 if (T->isArrayType() || T->isFunctionType()) {
2067 Diag(AtLoc, diag::err_property_type) << T;
2068 PDecl->setInvalidDecl();
2069 }
Mike Stump11289f42009-09-09 15:08:12 +00002070
Douglas Gregor758a8692009-06-17 21:51:59 +00002071 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002072
Fariborz Jahanianec6e4c82008-05-07 17:43:59 +00002073 // Regardless of setter/getter attribute, we save the default getter/setter
2074 // selector names in anticipation of declaration of setter/getter methods.
2075 PDecl->setGetterName(GetterSel);
2076 PDecl->setSetterName(SetterSel);
Mike Stump11289f42009-09-09 15:08:12 +00002077
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002078 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002079 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump11289f42009-09-09 15:08:12 +00002080
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002081 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002082 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump11289f42009-09-09 15:08:12 +00002083
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002084 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002085 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump11289f42009-09-09 15:08:12 +00002086
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002087 if (isReadWrite)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002088 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump11289f42009-09-09 15:08:12 +00002089
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002090 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002091 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump11289f42009-09-09 15:08:12 +00002092
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002093 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002094 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump11289f42009-09-09 15:08:12 +00002095
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002096 if (isAssign)
2097 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump11289f42009-09-09 15:08:12 +00002098
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002099 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002100 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump11289f42009-09-09 15:08:12 +00002101
Fariborz Jahanian8d916862008-05-05 18:51:55 +00002102 if (MethodImplKind == tok::objc_required)
2103 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2104 else if (MethodImplKind == tok::objc_optional)
2105 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002106 // A case of continuation class adding a new property in the class. This
2107 // is not what it was meant for. However, gcc supports it and so should we.
2108 // Make sure setter/getters are declared here.
2109 if (CCPrimary)
2110 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump11289f42009-09-09 15:08:12 +00002111
Chris Lattner83f095c2009-03-28 19:18:32 +00002112 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002113}
2114
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002115/// ActOnPropertyImplDecl - This routine performs semantic checks and
2116/// builds the AST node for a property implementation declaration; declared
2117/// as @synthesize or @dynamic.
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002118///
Mike Stump11289f42009-09-09 15:08:12 +00002119Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00002120 SourceLocation PropertyLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002121 bool Synthesize,
Chris Lattner83f095c2009-03-28 19:18:32 +00002122 DeclPtrTy ClassCatImpDecl,
2123 IdentifierInfo *PropertyId,
2124 IdentifierInfo *PropertyIvar) {
2125 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002126 // Make sure we have a context for the property implementation declaration.
2127 if (!ClassImpDecl) {
2128 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner83f095c2009-03-28 19:18:32 +00002129 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002130 }
2131 ObjCPropertyDecl *property = 0;
2132 ObjCInterfaceDecl* IDecl = 0;
2133 // Find the class or category class where this property must have
2134 // a declaration.
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002135 ObjCImplementationDecl *IC = 0;
2136 ObjCCategoryImplDecl* CatImplClass = 0;
2137 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002138 IDecl = IC->getClassInterface();
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002139 // We always synthesize an interface for an implementation
2140 // without an interface decl. So, IDecl is always non-zero.
Mike Stump11289f42009-09-09 15:08:12 +00002141 assert(IDecl &&
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002142 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump11289f42009-09-09 15:08:12 +00002143
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002144 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002145 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002146 if (!property) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002147 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner83f095c2009-03-28 19:18:32 +00002148 return DeclPtrTy();
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002149 }
Fariborz Jahanianec344ed2009-11-02 18:45:36 +00002150 if (const ObjCCategoryDecl *CD =
2151 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
2152 if (CD->getIdentifier()) {
2153 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniande8db162009-11-02 22:45:15 +00002154 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianec344ed2009-11-02 18:45:36 +00002155 return DeclPtrTy();
2156 }
2157 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002158 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002159 if (Synthesize) {
2160 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner83f095c2009-03-28 19:18:32 +00002161 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002162 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002163 IDecl = CatImplClass->getClassInterface();
2164 if (!IDecl) {
2165 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner83f095c2009-03-28 19:18:32 +00002166 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002167 }
Mike Stump11289f42009-09-09 15:08:12 +00002168 ObjCCategoryDecl *Category =
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002169 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump11289f42009-09-09 15:08:12 +00002170
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002171 // If category for this implementation not found, it is an error which
2172 // has already been reported eralier.
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002173 if (!Category)
Chris Lattner83f095c2009-03-28 19:18:32 +00002174 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002175 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002176 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002177 if (!property) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002178 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002179 << Category->getDeclName();
Chris Lattner83f095c2009-03-28 19:18:32 +00002180 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002181 }
Chris Lattner83f095c2009-03-28 19:18:32 +00002182 } else {
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002183 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner83f095c2009-03-28 19:18:32 +00002184 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002185 }
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002186 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002187 // Check that we have a valid, previously declared ivar for @synthesize
2188 if (Synthesize) {
2189 // @synthesize
Fariborz Jahanianc6bec7b2008-04-21 21:57:36 +00002190 if (!PropertyIvar)
2191 PropertyIvar = PropertyId;
Fariborz Jahanianb35b4a92009-03-31 00:06:29 +00002192 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002193 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanianb6d5b542009-04-13 19:30:37 +00002194 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002195 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002196 if (!Ivar) {
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002197 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002198 assert(EnclosingContext &&
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002199 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump11289f42009-09-09 15:08:12 +00002200 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2201 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002202 ObjCIvarDecl::Public,
2203 (Expr *)0);
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002204 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002205 IDecl->addDecl(Ivar);
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002206 property->setPropertyIvarDecl(Ivar);
2207 if (!getLangOptions().ObjCNonFragileABI)
Steve Naroffc03f6b92009-03-03 22:09:41 +00002208 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump11289f42009-09-09 15:08:12 +00002209 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002210 // a property implementation and to avoid future warnings.
Mike Stump12b8ce12009-08-04 21:02:39 +00002211 } else if (getLangOptions().ObjCNonFragileABI &&
2212 ClassDeclared != IDecl) {
Fariborz Jahanian68592fc2009-04-30 21:39:24 +00002213 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump11289f42009-09-09 15:08:12 +00002214 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanianb6d5b542009-04-13 19:30:37 +00002215 << ClassDeclared->getDeclName();
2216 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2217 << Ivar << Ivar->getNameAsCString();
2218 // Note! I deliberately want it to fall thru so more errors are caught.
2219 }
Steve Naroff9f9774c2008-09-30 10:07:56 +00002220 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002221
Steve Naroff506e7172008-09-30 00:24:17 +00002222 // Check that type of property and its ivar are type compatible.
Steve Naroff9f9774c2008-09-30 10:07:56 +00002223 if (PropType != IvarType) {
Steve Naroff9546eee2008-10-16 14:59:30 +00002224 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002225 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002226 << property->getDeclName() << Ivar->getDeclName();
Mike Stump11289f42009-09-09 15:08:12 +00002227 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002228 // a property implementation and to avoid future warnings.
Steve Naroff9f9774c2008-09-30 10:07:56 +00002229 }
Mike Stump11289f42009-09-09 15:08:12 +00002230
Chris Lattner83f095c2009-03-28 19:18:32 +00002231 // FIXME! Rules for properties are somewhat different that those
2232 // for assignments. Use a new routine to consolidate all cases;
2233 // specifically for property redeclarations as well as for ivars.
2234 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2235 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump11289f42009-09-09 15:08:12 +00002236 if (lhsType != rhsType &&
Chris Lattner83f095c2009-03-28 19:18:32 +00002237 lhsType->isArithmeticType()) {
2238 Diag(PropertyLoc, diag::error_property_ivar_type)
2239 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002240 // Fall thru - see previous comment
Chris Lattner83f095c2009-03-28 19:18:32 +00002241 }
2242 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanian9ecb84b2009-04-07 21:25:06 +00002243 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2244 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002245 Diag(PropertyLoc, diag::error_weak_property)
2246 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002247 // Fall thru - see previous comment
Chris Lattner83f095c2009-03-28 19:18:32 +00002248 }
Mike Stump11289f42009-09-09 15:08:12 +00002249 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian60331be2009-04-10 22:42:54 +00002250 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2251 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002252 Diag(PropertyLoc, diag::error_strong_property)
2253 << property->getDeclName() << Ivar->getDeclName();
Mike Stump11289f42009-09-09 15:08:12 +00002254 // Fall thru - see previous comment
Fariborz Jahanian54fa4182009-01-19 20:13:47 +00002255 }
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002256 }
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002257 } else if (PropertyIvar)
2258 // @dynamic
2259 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002260 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump11289f42009-09-09 15:08:12 +00002261 ObjCPropertyImplDecl *PIDecl =
2262 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2263 property,
2264 (Synthesize ?
2265 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002266 : ObjCPropertyImplDecl::Dynamic),
2267 Ivar);
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002268 if (IC) {
2269 if (Synthesize)
Mike Stump11289f42009-09-09 15:08:12 +00002270 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002271 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump11289f42009-09-09 15:08:12 +00002272 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2273 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002274 << PropertyIvar;
2275 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2276 }
Mike Stump11289f42009-09-09 15:08:12 +00002277
2278 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002279 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002280 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2281 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +00002282 return DeclPtrTy();
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002283 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002284 IC->addPropertyImplementation(PIDecl);
Mike Stump12b8ce12009-08-04 21:02:39 +00002285 } else {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002286 if (Synthesize)
Mike Stump11289f42009-09-09 15:08:12 +00002287 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002288 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump11289f42009-09-09 15:08:12 +00002289 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2290 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002291 << PropertyIvar;
2292 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2293 }
Mike Stump11289f42009-09-09 15:08:12 +00002294
2295 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002296 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002297 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2298 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +00002299 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002300 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002301 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002302 }
Mike Stump11289f42009-09-09 15:08:12 +00002303
Chris Lattner83f095c2009-03-28 19:18:32 +00002304 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002305}
Anders Carlssona6b508a2008-11-04 16:57:32 +00002306
Chris Lattner438e5012008-12-17 07:13:27 +00002307bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00002308 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlssona6b508a2008-11-04 16:57:32 +00002309 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002310
Anders Carlssona6b508a2008-11-04 16:57:32 +00002311 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2312 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00002313
Anders Carlssona6b508a2008-11-04 16:57:32 +00002314 return true;
2315}
Chris Lattner438e5012008-12-17 07:13:27 +00002316
Chris Lattner438e5012008-12-17 07:13:27 +00002317/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2318/// instance variables of ClassName into Decls.
Mike Stump11289f42009-09-09 15:08:12 +00002319void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00002320 IdentifierInfo *ClassName,
Chris Lattner83f095c2009-03-28 19:18:32 +00002321 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00002322 // Check that ClassName is a valid class
2323 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2324 if (!Class) {
2325 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2326 return;
2327 }
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00002328 if (LangOpts.ObjCNonFragileABI) {
2329 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2330 return;
2331 }
Mike Stump11289f42009-09-09 15:08:12 +00002332
Chris Lattner438e5012008-12-17 07:13:27 +00002333 // Collect the instance variables
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00002334 llvm::SmallVector<FieldDecl*, 32> RecFields;
2335 Context.CollectObjCIvars(Class, RecFields);
2336 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2337 for (unsigned i = 0; i < RecFields.size(); i++) {
2338 FieldDecl* ID = RecFields[i];
2339 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2340 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2341 ID->getIdentifier(), ID->getType(),
2342 ID->getBitWidth());
2343 Decls.push_back(Sema::DeclPtrTy::make(FD));
2344 }
Mike Stump11289f42009-09-09 15:08:12 +00002345
Chris Lattner438e5012008-12-17 07:13:27 +00002346 // Introduce all of these fields into the appropriate scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002347 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00002348 D != Decls.end(); ++D) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002349 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner438e5012008-12-17 07:13:27 +00002350 if (getLangOptions().CPlusPlus)
2351 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner83f095c2009-03-28 19:18:32 +00002352 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002353 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00002354 }
2355}
2356