blob: 8961cf165ac0c67169b5e81c189302c447bdb825 [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 Gregorc78d3462009-04-24 21:10:55 +000015#include "clang/Sema/ExternalSemaSource.h"
Steve Naroff157599f2009-03-03 14:49:36 +000016#include "clang/AST/Expr.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclObjC.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000020using namespace clang;
21
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +000022bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
23 ObjCMethodDecl *GetterMethod,
24 SourceLocation Loc) {
25 if (GetterMethod &&
26 GetterMethod->getResultType() != property->getType()) {
27 AssignConvertType result = Incompatible;
Steve Naroff79d12152009-07-16 15:41:00 +000028 if (property->getType()->isObjCObjectPointerType())
Fariborz Jahanian3e68a1f2009-05-08 21:10:00 +000029 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +000030 if (result != Compatible) {
Mike Stump11289f42009-09-09 15:08:12 +000031 Diag(Loc, diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +000032 << property->getDeclName()
33 << GetterMethod->getSelector();
34 Diag(GetterMethod->getLocation(), diag::note_declared_at);
35 return true;
36 }
37 }
38 return false;
39}
40
Steve Naroff70f41d62009-02-28 16:59:13 +000041/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattnerda463fe2007-12-12 07:09:47 +000042/// and user declared, in the method definition's AST.
Chris Lattner83f095c2009-03-28 19:18:32 +000043void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis853fbea2008-06-28 06:07:14 +000044 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner83f095c2009-03-28 19:18:32 +000045 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump11289f42009-09-09 15:08:12 +000046
Steve Naroff542cd5d2008-07-25 17:57:26 +000047 // If we don't have a valid method decl, simply return.
48 if (!MDecl)
49 return;
Steve Naroff1d2538c2007-12-18 01:30:32 +000050
Chris Lattner9fecd742009-04-19 05:21:20 +000051 CurFunctionNeedsScopeChecking = false;
52
Steve Naroff1d2538c2007-12-18 01:30:32 +000053 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorffca3a22009-01-09 17:18:27 +000054 if (MDecl->isInstanceMethod())
Steve Naroff1d2538c2007-12-18 01:30:32 +000055 AddInstanceMethodToGlobalPool(MDecl);
56 else
57 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump11289f42009-09-09 15:08:12 +000058
Chris Lattnerda463fe2007-12-12 07:09:47 +000059 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor91f84212008-12-11 16:49:14 +000060 PushDeclContext(FnBodyScope, MDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +000061
62 // Create Decl objects for each parameter, entrring them in the scope for
63 // binding to their use.
Chris Lattnerda463fe2007-12-12 07:09:47 +000064
65 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +000066 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump11289f42009-09-09 15:08:12 +000067
Daniel Dunbar279d1cc2008-08-26 06:07:48 +000068 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
69 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +000070
Chris Lattner58258242008-04-10 02:22:51 +000071 // Introduce all of the other parameters into this scope.
Chris Lattnera4997152009-02-20 18:43:26 +000072 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
73 E = MDecl->param_end(); PI != E; ++PI)
74 if ((*PI)->getIdentifier())
75 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattnerda463fe2007-12-12 07:09:47 +000076}
77
Chris Lattner83f095c2009-03-28 19:18:32 +000078Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +000079ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
80 IdentifierInfo *ClassName, SourceLocation ClassLoc,
81 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +000082 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +000083 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattnerda463fe2007-12-12 07:09:47 +000084 assert(ClassName && "Missing class identifier");
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattnerda463fe2007-12-12 07:09:47 +000086 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +000087 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor5daeee22008-12-08 18:40:42 +000088 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-12-05 18:15:24 +000089 // Maybe we will complain about the shadowed template parameter.
90 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
91 // Just pretend that we didn't see the previous declaration.
92 PrevDecl = 0;
93 }
94
Ted Kremenek1b0ea822008-01-07 19:49:32 +000095 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +000096 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +000097 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +000098 }
Mike Stump11289f42009-09-09 15:08:12 +000099
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000100 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000101 if (IDecl) {
102 // Class already seen. Is it a forward declaration?
Steve Naroff119f60e2008-11-18 19:15:30 +0000103 if (!IDecl->isForwardDecl()) {
Chris Lattnerd13b8b52009-02-23 22:00:08 +0000104 IDecl->setInvalidDecl();
Chris Lattnerf3d3fae2008-11-24 05:29:24 +0000105 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnere6447ef2008-11-23 22:46:27 +0000106 Diag(IDecl->getLocation(), diag::note_previous_definition);
107
Steve Naroff119f60e2008-11-18 19:15:30 +0000108 // Return the previous class interface.
109 // FIXME: don't leak the objects passed in!
Chris Lattner83f095c2009-03-28 19:18:32 +0000110 return DeclPtrTy::make(IDecl);
Steve Naroff119f60e2008-11-18 19:15:30 +0000111 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000112 IDecl->setLocation(AtInterfaceLoc);
113 IDecl->setForwardDecl(false);
Steve Naroffe0064d22009-09-11 00:12:01 +0000114 IDecl->setClassLoc(ClassLoc);
Ted Kremenek707ece62009-11-17 22:58:30 +0000115
116 // Since this ObjCInterfaceDecl was created by a forward declaration,
117 // we now add it to the DeclContext since it wasn't added before
118 // (see ActOnForwardClassDeclaration).
119 CurContext->addDecl(IDecl);
120
Fariborz Jahaniand3612392009-11-17 19:08:08 +0000121 if (AttrList)
122 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000123 }
Chris Lattnerca1e8482008-07-21 07:06:49 +0000124 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000125 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff5a4611c2008-04-11 19:35:35 +0000126 ClassName, ClassLoc);
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000127 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000128 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +0000129
Steve Naroff3c301dc2009-04-23 15:15:40 +0000130 PushOnScopeChains(IDecl, TUScope);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000131 }
Mike Stump11289f42009-09-09 15:08:12 +0000132
Chris Lattnerda463fe2007-12-12 07:09:47 +0000133 if (SuperName) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000134 // Check if a different kind of symbol declared in this scope.
John McCall9f3059a2009-10-09 21:13:30 +0000135 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000136 if (PrevDecl == IDecl) {
137 Diag(SuperLoc, diag::err_recursive_superclass)
138 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
139 IDecl->setLocEnd(ClassLoc);
Mike Stump12b8ce12009-08-04 21:02:39 +0000140 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000141 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000142 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000143
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000144 // Diagnose classes that inherit from deprecated classes.
145 if (SuperClassDecl)
146 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000147
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000148 if (PrevDecl && SuperClassDecl == 0) {
149 // The previous declaration was not a class decl. Check if we have a
150 // typedef. If we do, get the underlying class type.
151 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
152 QualType T = TDecl->getUnderlyingType();
153 if (T->isObjCInterfaceType()) {
John McCall9dd450b2009-09-21 23:43:11 +0000154 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000155 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
156 }
157 }
Mike Stump11289f42009-09-09 15:08:12 +0000158
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000159 // This handles the following case:
160 //
161 // typedef int SuperClass;
162 // @interface MyClass : SuperClass {} @end
163 //
164 if (!SuperClassDecl) {
165 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
166 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff189d41f2009-02-04 17:14:05 +0000167 }
168 }
Mike Stump11289f42009-09-09 15:08:12 +0000169
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000170 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
171 if (!SuperClassDecl)
172 Diag(SuperLoc, diag::err_undef_superclass)
173 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
174 else if (SuperClassDecl->isForwardDecl())
175 Diag(SuperLoc, diag::err_undef_superclass)
176 << SuperClassDecl->getDeclName() << ClassName
177 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000178 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000179 IDecl->setSuperClass(SuperClassDecl);
180 IDecl->setSuperClassLoc(SuperLoc);
181 IDecl->setLocEnd(SuperLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000182 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000183 } else { // we have a root class.
184 IDecl->setLocEnd(ClassLoc);
185 }
Mike Stump11289f42009-09-09 15:08:12 +0000186
Steve Naroff119f60e2008-11-18 19:15:30 +0000187 /// Check then save referenced protocols.
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000188 if (NumProtoRefs) {
Chris Lattner22298722009-02-20 21:35:13 +0000189 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
190 Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000191 IDecl->setLocEnd(EndProtoLoc);
192 }
Mike Stump11289f42009-09-09 15:08:12 +0000193
Anders Carlssona6b508a2008-11-04 16:57:32 +0000194 CheckObjCDeclScope(IDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000195 return DeclPtrTy::make(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000196}
197
198/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000199/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner83f095c2009-03-28 19:18:32 +0000200Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000201 IdentifierInfo *AliasName,
Chris Lattner83f095c2009-03-28 19:18:32 +0000202 SourceLocation AliasLocation,
203 IdentifierInfo *ClassName,
204 SourceLocation ClassLocation) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000205 // Look for previous declaration of alias name
John McCall9f3059a2009-10-09 21:13:30 +0000206 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000207 if (ADecl) {
Chris Lattnerd0685032008-11-23 23:20:13 +0000208 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattnerda463fe2007-12-12 07:09:47 +0000209 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerd0685032008-11-23 23:20:13 +0000210 else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000211 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerd0685032008-11-23 23:20:13 +0000212 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000213 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000214 }
215 // Check for class declaration
John McCall9f3059a2009-10-09 21:13:30 +0000216 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000217 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
218 QualType T = TDecl->getUnderlyingType();
219 if (T->isObjCInterfaceType()) {
John McCall9dd450b2009-09-21 23:43:11 +0000220 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000221 ClassName = IDecl->getIdentifier();
John McCall9f3059a2009-10-09 21:13:30 +0000222 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000223 }
224 }
225 }
Chris Lattner219b3e92008-03-16 21:17:37 +0000226 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
227 if (CDecl == 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000228 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner219b3e92008-03-16 21:17:37 +0000229 if (CDeclU)
Chris Lattnerd0685032008-11-23 23:20:13 +0000230 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000231 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000232 }
Mike Stump11289f42009-09-09 15:08:12 +0000233
Chris Lattner219b3e92008-03-16 21:17:37 +0000234 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump11289f42009-09-09 15:08:12 +0000235 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000236 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000237
Anders Carlssona6b508a2008-11-04 16:57:32 +0000238 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor38feed82009-04-24 02:57:34 +0000239 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000240
Chris Lattner83f095c2009-03-28 19:18:32 +0000241 return DeclPtrTy::make(AliasDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000242}
243
Steve Naroff41d09ad2009-03-05 15:22:01 +0000244void Sema::CheckForwardProtocolDeclarationForCircularDependency(
245 IdentifierInfo *PName,
246 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000247 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000248 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
249 E = PList.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000250
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000251 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000252 if (PDecl->getIdentifier() == PName) {
253 Diag(Ploc, diag::err_protocol_has_circular_dependency);
254 Diag(PrevLoc, diag::note_previous_definition);
255 }
Mike Stump11289f42009-09-09 15:08:12 +0000256 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff41d09ad2009-03-05 15:22:01 +0000257 PDecl->getLocation(), PDecl->getReferencedProtocols());
258 }
259 }
260}
261
Chris Lattner83f095c2009-03-28 19:18:32 +0000262Sema::DeclPtrTy
Chris Lattner3bbae002008-07-26 04:03:38 +0000263Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
264 IdentifierInfo *ProtocolName,
265 SourceLocation ProtocolLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000266 const DeclPtrTy *ProtoRefs,
Chris Lattner3bbae002008-07-26 04:03:38 +0000267 unsigned NumProtoRefs,
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000268 SourceLocation EndProtoLoc,
269 AttributeList *AttrList) {
270 // FIXME: Deal with AttrList.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000271 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000272 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000273 if (PDecl) {
274 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner5074f8f2008-03-16 01:25:17 +0000275 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian54d569c2009-04-06 23:43:32 +0000276 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnere6447ef2008-11-23 22:46:27 +0000277 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000278 // Just return the protocol we already had.
279 // FIXME: don't leak the objects passed in!
Chris Lattner83f095c2009-03-28 19:18:32 +0000280 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000281 }
Steve Naroff41d09ad2009-03-05 15:22:01 +0000282 ObjCList<ObjCProtocolDecl> PList;
Mike Stump11289f42009-09-09 15:08:12 +0000283 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff41d09ad2009-03-05 15:22:01 +0000284 CheckForwardProtocolDeclarationForCircularDependency(
285 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
286 PList.Destroy(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000287
Steve Naroffeb03dac2008-08-13 16:39:22 +0000288 // Make sure the cached decl gets a valid start location.
289 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000290 PDecl->setForwardDecl(false);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000291 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000292 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000293 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000294 PushOnScopeChains(PDecl, TUScope);
Chris Lattneracc04a92008-03-16 20:19:15 +0000295 PDecl->setForwardDecl(false);
Chris Lattnerf87ca0a2008-03-16 01:23:04 +0000296 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000297 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000298 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000299 if (NumProtoRefs) {
Chris Lattneracc04a92008-03-16 20:19:15 +0000300 /// Check then save referenced protocols.
Chris Lattner22298722009-02-20 21:35:13 +0000301 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000302 PDecl->setLocEnd(EndProtoLoc);
303 }
Mike Stump11289f42009-09-09 15:08:12 +0000304
305 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000306 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000307}
308
309/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000310/// issues an error if they are not declared. It returns list of
311/// protocol declarations in its 'Protocols' argument.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000312void
Chris Lattner3bbae002008-07-26 04:03:38 +0000313Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000314 const IdentifierLocPair *ProtocolId,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000315 unsigned NumProtocols,
Chris Lattner83f095c2009-03-28 19:18:32 +0000316 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000317 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000318 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000319 if (!PDecl) {
Chris Lattner3b054132008-11-19 05:08:23 +0000320 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000321 << ProtocolId[i].first;
Chris Lattner9c1842b2008-07-26 03:47:43 +0000322 continue;
323 }
Mike Stump11289f42009-09-09 15:08:12 +0000324
Douglas Gregor171c45a2009-02-18 21:56:37 +0000325 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000326
327 // If this is a forward declaration and we are supposed to warn in this
328 // case, do it.
329 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner3b054132008-11-19 05:08:23 +0000330 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000331 << ProtocolId[i].first;
Chris Lattner83f095c2009-03-28 19:18:32 +0000332 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattnerda463fe2007-12-12 07:09:47 +0000333 }
334}
335
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000336/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar4684f372008-08-27 05:40:03 +0000337/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000338///
Fariborz Jahanian7cf18862008-05-01 00:03:38 +0000339void
Mike Stump11289f42009-09-09 15:08:12 +0000340Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian7cf18862008-05-01 00:03:38 +0000341 ObjCPropertyDecl *SuperProperty,
Chris Lattner86d7d912008-11-24 03:54:41 +0000342 const IdentifierInfo *inheritedName) {
Mike Stump11289f42009-09-09 15:08:12 +0000343 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000344 Property->getPropertyAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000345 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000346 SuperProperty->getPropertyAttributes();
347 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
348 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner3b054132008-11-19 05:08:23 +0000349 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner86d7d912008-11-24 03:54:41 +0000350 << Property->getDeclName() << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000351 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
352 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner377d1f82008-11-18 22:52:51 +0000353 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000354 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000355 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
356 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner377d1f82008-11-18 22:52:51 +0000357 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000358 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump11289f42009-09-09 15:08:12 +0000359
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000360 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
361 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner377d1f82008-11-18 22:52:51 +0000362 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000363 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000364 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner377d1f82008-11-18 22:52:51 +0000365 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump11289f42009-09-09 15:08:12 +0000366 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000367 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner377d1f82008-11-18 22:52:51 +0000368 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000369 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff3e1181e2009-03-03 15:43:24 +0000370
Mike Stump11289f42009-09-09 15:08:12 +0000371 QualType LHSType =
Steve Naroff3e1181e2009-03-03 15:43:24 +0000372 Context.getCanonicalType(SuperProperty->getType());
Mike Stump11289f42009-09-09 15:08:12 +0000373 QualType RHSType =
Steve Naroff3e1181e2009-03-03 15:43:24 +0000374 Context.getCanonicalType(Property->getType());
Mike Stump11289f42009-09-09 15:08:12 +0000375
Steve Naroff3e1181e2009-03-03 15:43:24 +0000376 if (!Context.typesAreCompatible(LHSType, RHSType)) {
377 // FIXME: Incorporate this test with typesAreCompatible.
378 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff8e6aee52009-07-23 01:01:38 +0000379 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff3e1181e2009-03-03 15:43:24 +0000380 return;
381 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
382 << Property->getType() << SuperProperty->getType() << inheritedName;
383 }
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000384}
385
386/// ComparePropertiesInBaseAndSuper - This routine compares property
387/// declarations in base and its super class, if any, and issues
388/// diagnostics in a variety of inconsistant situations.
389///
Chris Lattner9018ca82009-02-16 21:26:43 +0000390void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000391 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
392 if (!SDecl)
393 return;
Daniel Dunbar4684f372008-08-27 05:40:03 +0000394 // FIXME: O(N^2)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000395 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
396 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian7cf18862008-05-01 00:03:38 +0000397 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000398 // Does property in super class has declaration in current class?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000399 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
400 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000401 ObjCPropertyDecl *PDecl = (*I);
402 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump11289f42009-09-09 15:08:12 +0000403 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner86d7d912008-11-24 03:54:41 +0000404 SDecl->getIdentifier());
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000405 }
406 }
407}
408
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000409/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
410/// of properties declared in a protocol and adds them to the list
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000411/// of properties for current class/category if it is not there already.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000412void
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000413Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner86d7d912008-11-24 03:54:41 +0000414 ObjCProtocolDecl *PDecl) {
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000415 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
416 if (!IDecl) {
417 // Category
418 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
419 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000420 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
421 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000422 ObjCPropertyDecl *Pr = (*P);
Steve Naroffb3a87982009-01-09 15:36:25 +0000423 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000424 // Is this property already in category's list of properties?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000425 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000426 if ((*CP)->getIdentifier() == Pr->getIdentifier())
427 break;
Fariborz Jahanian519976c2009-01-09 21:04:52 +0000428 if (CP != CE)
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000429 // Property protocol already exist in class. Diagnose any mismatch.
430 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
431 }
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000432 return;
433 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000434 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
435 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000436 ObjCPropertyDecl *Pr = (*P);
Steve Naroffb3a87982009-01-09 15:36:25 +0000437 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000438 // Is this property already in class's list of properties?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000439 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000440 if ((*CP)->getIdentifier() == Pr->getIdentifier())
441 break;
Fariborz Jahanian519976c2009-01-09 21:04:52 +0000442 if (CP != CE)
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000443 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner86d7d912008-11-24 03:54:41 +0000444 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000445 }
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000446}
447
448/// MergeProtocolPropertiesIntoClass - This routine merges properties
449/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000450/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000451///
Chris Lattner9018ca82009-02-16 21:26:43 +0000452void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000453 DeclPtrTy MergeItsProtocols) {
454 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000455 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
456
457 if (!IDecl) {
458 // Category
459 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
460 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
461 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
462 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
463 E = MDecl->protocol_end(); P != E; ++P)
464 // Merge properties of category (*P) into IDECL's
465 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Mike Stump11289f42009-09-09 15:08:12 +0000466
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000467 // Go thru the list of protocols for this category and recursively merge
468 // their properties into this class as well.
469 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
470 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattner83f095c2009-03-28 19:18:32 +0000471 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000472 } else {
473 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
474 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
475 E = MD->protocol_end(); P != E; ++P)
Chris Lattner83f095c2009-03-28 19:18:32 +0000476 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000477 }
478 return;
479 }
480
Chris Lattnerca1e8482008-07-21 07:06:49 +0000481 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000482 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
483 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000484 // Merge properties of class (*P) into IDECL's
Chris Lattnerca1e8482008-07-21 07:06:49 +0000485 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Mike Stump11289f42009-09-09 15:08:12 +0000486
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000487 // Go thru the list of protocols for this class and recursively merge
488 // their properties into this class as well.
489 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
490 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner83f095c2009-03-28 19:18:32 +0000491 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerca1e8482008-07-21 07:06:49 +0000492 } else {
Argyrios Kyrtzidisb3fa8632008-07-21 09:18:38 +0000493 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
494 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
495 E = MD->protocol_end(); P != E; ++P)
Chris Lattner83f095c2009-03-28 19:18:32 +0000496 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerca1e8482008-07-21 07:06:49 +0000497 }
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000498}
499
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000500/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000501/// a class method in its extension.
502///
Mike Stump11289f42009-09-09 15:08:12 +0000503void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000504 ObjCInterfaceDecl *ID) {
505 if (!ID)
506 return; // Possibly due to previous error
507
508 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000509 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
510 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000511 ObjCMethodDecl *MD = *i;
512 MethodMap[MD->getSelector()] = MD;
513 }
514
515 if (MethodMap.empty())
516 return;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000517 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
518 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000519 ObjCMethodDecl *Method = *i;
520 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
521 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
522 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
523 << Method->getDeclName();
524 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
525 }
526 }
527}
528
Chris Lattnerdac168d2009-04-12 08:43:13 +0000529/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner83f095c2009-03-28 19:18:32 +0000530Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000531Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000532 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000533 unsigned NumElts,
534 AttributeList *attrList) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000535 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Mike Stump11289f42009-09-09 15:08:12 +0000536
Chris Lattnerda463fe2007-12-12 07:09:47 +0000537 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000538 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000539 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000540 if (PDecl == 0) { // Not already seen?
Mike Stump11289f42009-09-09 15:08:12 +0000541 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000542 IdentList[i].second, Ident);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000543 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000544 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000545 if (attrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000546 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000547 Protocols.push_back(PDecl);
548 }
Mike Stump11289f42009-09-09 15:08:12 +0000549
550 ObjCForwardProtocolDecl *PDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000551 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlssona6b508a2008-11-04 16:57:32 +0000552 &Protocols[0], Protocols.size());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000553 CurContext->addDecl(PDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +0000554 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000555 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000556}
557
Chris Lattner83f095c2009-03-28 19:18:32 +0000558Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000559ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
560 IdentifierInfo *ClassName, SourceLocation ClassLoc,
561 IdentifierInfo *CategoryName,
562 SourceLocation CategoryLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000563 const DeclPtrTy *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000564 unsigned NumProtoRefs,
565 SourceLocation EndProtoLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000566 ObjCCategoryDecl *CDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000567 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
568 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000569 CurContext->addDecl(CDecl);
Chris Lattner9018ca82009-02-16 21:26:43 +0000570
571 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian9a5124a2008-01-17 20:33:24 +0000572 /// Check that class of this category is already completely declared.
Chris Lattner9018ca82009-02-16 21:26:43 +0000573 if (!IDecl || IDecl->isForwardDecl()) {
574 CDecl->setInvalidDecl();
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000575 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner83f095c2009-03-28 19:18:32 +0000576 return DeclPtrTy::make(CDecl);
Fariborz Jahanian9a5124a2008-01-17 20:33:24 +0000577 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000578
Chris Lattner9018ca82009-02-16 21:26:43 +0000579 CDecl->setClassInterface(IDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000580
Chris Lattnerced903b2009-02-16 21:30:01 +0000581 // If the interface is deprecated, warn about it.
Douglas Gregor171c45a2009-02-18 21:56:37 +0000582 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner9018ca82009-02-16 21:26:43 +0000583
584 /// Check for duplicate interface declaration for this category
585 ObjCCategoryDecl *CDeclChain;
586 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
587 CDeclChain = CDeclChain->getNextClassCategory()) {
588 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
589 Diag(CategoryLoc, diag::warn_dup_category_def)
590 << ClassName << CategoryName;
591 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
592 break;
593 }
594 }
595 if (!CDeclChain)
596 CDecl->insertNextClassCategory();
597
Chris Lattnerda463fe2007-12-12 07:09:47 +0000598 if (NumProtoRefs) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000599 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
600 Context);
601 CDecl->setLocEnd(EndProtoLoc);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000602 // Protocols in the class extension belong to the class.
603 if (!CDecl->getIdentifier())
604 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
605 NumProtoRefs,Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000606 }
Mike Stump11289f42009-09-09 15:08:12 +0000607
Anders Carlssona6b508a2008-11-04 16:57:32 +0000608 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000609 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000610}
611
612/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000613/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000614/// object.
Chris Lattner83f095c2009-03-28 19:18:32 +0000615Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000616 SourceLocation AtCatImplLoc,
617 IdentifierInfo *ClassName, SourceLocation ClassLoc,
618 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000619 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000620 ObjCCategoryDecl *CatIDecl = 0;
621 if (IDecl) {
622 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
623 if (!CatIDecl) {
624 // Category @implementation with no corresponding @interface.
625 // Create and install one.
626 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
627 CatName);
628 CatIDecl->setClassInterface(IDecl);
629 CatIDecl->insertNextClassCategory();
630 }
631 }
632
Mike Stump11289f42009-09-09 15:08:12 +0000633 ObjCCategoryImplDecl *CDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000634 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
635 IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000636 /// Check that class of this category is already completely declared.
637 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000638 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000639
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000640 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000641 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000642
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000643 /// Check that CatName, category name, is not used in another implementation.
644 if (CatIDecl) {
645 if (CatIDecl->getImplementation()) {
646 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
647 << CatName;
648 Diag(CatIDecl->getImplementation()->getLocation(),
649 diag::note_previous_definition);
650 } else
651 CatIDecl->setImplementation(CDecl);
652 }
Mike Stump11289f42009-09-09 15:08:12 +0000653
Anders Carlssona6b508a2008-11-04 16:57:32 +0000654 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000655 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000656}
657
Chris Lattner83f095c2009-03-28 19:18:32 +0000658Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000659 SourceLocation AtClassImplLoc,
660 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000661 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000662 SourceLocation SuperClassLoc) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000663 ObjCInterfaceDecl* IDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000664 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +0000665 NamedDecl *PrevDecl
666 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000667 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000668 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000669 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerd13b8b52009-02-23 22:00:08 +0000670 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000671 // Is there an interface declaration of this class; if not, warn!
Mike Stump11289f42009-09-09 15:08:12 +0000672 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian6f0f25b2009-04-23 21:49:04 +0000673 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000674 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian6f0f25b2009-04-23 21:49:04 +0000675 IDecl = 0;
676 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000677 }
Mike Stump11289f42009-09-09 15:08:12 +0000678
Chris Lattnerda463fe2007-12-12 07:09:47 +0000679 // Check that super class name is valid class name
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000680 ObjCInterfaceDecl* SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000681 if (SuperClassname) {
682 // Check if a different kind of symbol declared in this scope.
John McCall9f3059a2009-10-09 21:13:30 +0000683 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000684 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000685 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
686 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +0000687 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000688 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000689 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000690 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000691 Diag(SuperClassLoc, diag::err_undef_superclass)
692 << SuperClassname << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000693 else if (IDecl && IDecl->getSuperClass() != SDecl) {
694 // This implementation and its interface do not have the same
695 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000696 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000697 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +0000698 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000699 }
700 }
701 }
Mike Stump11289f42009-09-09 15:08:12 +0000702
Chris Lattnerda463fe2007-12-12 07:09:47 +0000703 if (!IDecl) {
704 // Legacy case of @implementation with no corresponding @interface.
705 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000706
Mike Stump87c57ac2009-05-16 07:39:55 +0000707 // FIXME: Do we support attributes on the @implementation? If so we should
708 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +0000709 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000710 ClassName, ClassLoc, false, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000711 IDecl->setSuperClass(SDecl);
712 IDecl->setLocEnd(ClassLoc);
Douglas Gregorac345a32009-04-24 00:16:12 +0000713
714 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbard1148a72009-04-21 21:41:56 +0000715 } else {
716 // Mark the interface as being completed, even if it was just as
717 // @class ....;
718 // declaration; the user cannot reopen it.
719 IDecl->setForwardDecl(false);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000720 }
Mike Stump11289f42009-09-09 15:08:12 +0000721
722 ObjCImplementationDecl* IMPDecl =
723 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000724 IDecl, SDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000725
Anders Carlssona6b508a2008-11-04 16:57:32 +0000726 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner83f095c2009-03-28 19:18:32 +0000727 return DeclPtrTy::make(IMPDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000728
Chris Lattnerda463fe2007-12-12 07:09:47 +0000729 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000730 if (IDecl->getImplementation()) {
Chris Lattner36ac1ca2008-03-16 20:53:07 +0000731 // FIXME: Don't leak everything!
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000732 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000733 Diag(IDecl->getImplementation()->getLocation(),
734 diag::note_previous_definition);
735 } else { // add it to the list.
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000736 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +0000737 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000738 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000739 return DeclPtrTy::make(IMPDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000740}
741
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000742void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
743 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000744 SourceLocation RBrace) {
745 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000746 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000747 if (!IDecl)
748 return;
749 /// Check case of non-existing @interface decl.
750 /// (legacy objective-c @implementation decl without an @interface decl).
751 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +0000752 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner22298722009-02-20 21:35:13 +0000753 IDecl->setIVarList(ivars, numIvars, Context);
754 IDecl->setLocEnd(RBrace);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000755 return;
756 }
757 // If implementation has empty ivar list, just return.
758 if (numIvars == 0)
759 return;
Mike Stump11289f42009-09-09 15:08:12 +0000760
Chris Lattnerda463fe2007-12-12 07:09:47 +0000761 assert(ivars && "missing @implementation ivars");
Mike Stump11289f42009-09-09 15:08:12 +0000762
Chris Lattnerda463fe2007-12-12 07:09:47 +0000763 // Check interface's Ivar list against those in the implementation.
764 // names and types must match.
765 //
Chris Lattnerda463fe2007-12-12 07:09:47 +0000766 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000767 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +0000768 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
769 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000770 ObjCIvarDecl* ImplIvar = ivars[j++];
771 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000772 assert (ImplIvar && "missing implementation ivar");
773 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +0000774
Steve Naroff157599f2009-03-03 14:49:36 +0000775 // First, make sure the types match.
Chris Lattner35ffe332008-07-27 00:05:05 +0000776 if (Context.getCanonicalType(ImplIvar->getType()) !=
777 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +0000778 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000779 << ImplIvar->getIdentifier()
780 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +0000781 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff157599f2009-03-03 14:49:36 +0000782 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
783 Expr *ImplBitWidth = ImplIvar->getBitWidth();
784 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000785 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
786 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff157599f2009-03-03 14:49:36 +0000787 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
788 << ImplIvar->getIdentifier();
789 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
790 }
Mike Stump11289f42009-09-09 15:08:12 +0000791 }
Steve Naroff157599f2009-03-03 14:49:36 +0000792 // Make sure the names are identical.
793 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000794 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000795 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +0000796 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000797 }
798 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000799 }
Mike Stump11289f42009-09-09 15:08:12 +0000800
Chris Lattner0f29d982007-12-12 18:11:49 +0000801 if (numIvars > 0)
Chris Lattner83021e92007-12-12 18:19:52 +0000802 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +0000803 else if (IVI != IVE)
Chris Lattner83021e92007-12-12 18:19:52 +0000804 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000805}
806
Steve Naroff15833ed2008-02-10 21:38:56 +0000807void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
808 bool &IncompleteImpl) {
809 if (!IncompleteImpl) {
810 Diag(ImpLoc, diag::warn_incomplete_impl);
811 IncompleteImpl = true;
812 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000813 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff15833ed2008-02-10 21:38:56 +0000814}
815
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000816void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
817 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnera9d0ffe2009-04-11 18:01:59 +0000818 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian0c20bdd2009-05-14 23:52:54 +0000819 ImpMethodDecl->getResultType()) &&
Steve Naroff8e6aee52009-07-23 01:01:38 +0000820 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
821 ImpMethodDecl->getResultType())) {
Mike Stump11289f42009-09-09 15:08:12 +0000822 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000823 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
824 << ImpMethodDecl->getResultType();
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000825 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
826 }
Mike Stump11289f42009-09-09 15:08:12 +0000827
Chris Lattner67f35b02009-04-11 19:58:42 +0000828 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
829 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
830 IM != EM; ++IM, ++IF) {
Fariborz Jahanian0c20bdd2009-05-14 23:52:54 +0000831 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()) ||
Mike Stump11289f42009-09-09 15:08:12 +0000832 Context.QualifiedIdConformsQualifiedId((*IF)->getType(),
Steve Naroff8e6aee52009-07-23 01:01:38 +0000833 (*IM)->getType()))
Chris Lattner67f35b02009-04-11 19:58:42 +0000834 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000835
836 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000837 << ImpMethodDecl->getDeclName() << (*IF)->getType()
838 << (*IM)->getType();
Chris Lattner5300acc2009-04-11 20:14:49 +0000839 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner67f35b02009-04-11 19:58:42 +0000840 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000841}
842
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000843/// isPropertyReadonly - Return true if property is readonly, by searching
844/// for the property in the class and in its categories and implementations
845///
846bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffde680012009-02-26 19:11:32 +0000847 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000848 // by far the most common case.
849 if (!PDecl->isReadOnly())
850 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000851 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000852 // it is not considered read only.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000853 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000854 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000855
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000856 // Main class has the property as 'readonly'. Must search
Mike Stump11289f42009-09-09 15:08:12 +0000857 // through the category list to see if the property's
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000858 // attribute has been over-ridden to 'readwrite'.
859 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
860 Category; Category = Category->getNextClassCategory()) {
Mike Stump11289f42009-09-09 15:08:12 +0000861 // Even if property is ready only, if a category has a user defined setter,
862 // it is not considered read only.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000863 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000864 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000865 ObjCPropertyDecl *P =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000866 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000867 if (P && !P->isReadOnly())
868 return false;
869 }
Mike Stump11289f42009-09-09 15:08:12 +0000870
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000871 // Also, check for definition of a setter method in the implementation if
872 // all else failed.
873 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump11289f42009-09-09 15:08:12 +0000874 if (ObjCImplementationDecl *IMD =
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000875 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000876 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000877 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000878 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stump12b8ce12009-08-04 21:02:39 +0000879 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000880 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000881 return false;
882 }
883 }
Steve Naroffde680012009-02-26 19:11:32 +0000884 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000885 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000886 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroffde680012009-02-26 19:11:32 +0000887 return false;
Fariborz Jahanian15e3a5c2009-04-06 16:59:10 +0000888 // If all fails, look at the super class.
889 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
890 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000891 return true;
892}
893
Mike Stump87c57ac2009-05-16 07:39:55 +0000894/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
895/// improve the efficiency of selector lookups and type checking by associating
896/// with each protocol / interface / category the flattened instance tables. If
897/// we used an immutable set to keep the table then it wouldn't add significant
898/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +0000899
Steve Naroffa36992242008-02-08 22:06:17 +0000900/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +0000901/// Declared in protocol, and those referenced by it.
Steve Naroffa36992242008-02-08 22:06:17 +0000902void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
903 ObjCProtocolDecl *PDecl,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000904 bool& IncompleteImpl,
Steve Naroffa36992242008-02-08 22:06:17 +0000905 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000906 const llvm::DenseSet<Selector> &ClsMap,
907 ObjCInterfaceDecl *IDecl) {
908 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000909 ObjCInterfaceDecl *NSIDecl = 0;
910 if (getLangOptions().NeXTRuntime) {
Mike Stump11289f42009-09-09 15:08:12 +0000911 // check to see if class implements forwardInvocation method and objects
912 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000913 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +0000914 // Under such conditions, which means that every method possible is
915 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000916 // found" warnings.
917 // FIXME: Use a general GetUnarySelector method for this.
918 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
919 Selector fISelector = Context.Selectors.getSelector(1, &II);
920 if (InsMap.count(fISelector))
921 // Is IDecl derived from 'NSProxy'? If so, no instance methods
922 // need be implemented in the implementation.
923 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
924 }
Mike Stump11289f42009-09-09 15:08:12 +0000925
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000926 // If a method lookup fails locally we still need to look and see if
927 // the method was implemented by a base class or an inherited
928 // protocol. This lookup is slow, but occurs rarely in correct code
929 // and otherwise would terminate in a warning.
930
Chris Lattnerda463fe2007-12-12 07:09:47 +0000931 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000932 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +0000933 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000934 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000935 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000936 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000937 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000938 (!Super ||
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000939 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000940 // Ugly, but necessary. Method declared in protcol might have
941 // have been synthesized due to a property declared in the class which
942 // uses the protocol.
Mike Stump11289f42009-09-09 15:08:12 +0000943 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000944 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000945 if (!MethodInClass || !MethodInClass->isSynthesized())
946 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
947 }
948 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000949 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +0000950 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000951 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000952 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000953 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000954 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
955 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000956 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff15833ed2008-02-10 21:38:56 +0000957 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff3ce37a62007-12-14 23:37:57 +0000958 }
Chris Lattner390d39a2008-07-21 21:32:27 +0000959 // Check on this protocols's referenced protocols, recursively.
960 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
961 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000962 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000963}
964
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000965/// MatchAllMethodDeclarations - Check methods declaraed in interface or
966/// or protocol against those declared in their implementations.
967///
968void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
969 const llvm::DenseSet<Selector> &ClsMap,
970 llvm::DenseSet<Selector> &InsMapSeen,
971 llvm::DenseSet<Selector> &ClsMapSeen,
972 ObjCImplDecl* IMPDecl,
973 ObjCContainerDecl* CDecl,
974 bool &IncompleteImpl,
Mike Stump11289f42009-09-09 15:08:12 +0000975 bool ImmediateClass) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000976 // Check and see if instance methods in class interface have been
977 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000978 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
979 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000980 if (InsMapSeen.count((*I)->getSelector()))
981 continue;
982 InsMapSeen.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000983 if (!(*I)->isSynthesized() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000984 !InsMap.count((*I)->getSelector())) {
985 if (ImmediateClass)
986 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
987 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +0000988 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000989 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000990 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000991 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000992 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +0000993 assert(IntfMethodDecl &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +0000994 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
995 // ImpMethodDecl may be null as in a @dynamic property.
996 if (ImpMethodDecl)
997 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
998 }
999 }
Mike Stump11289f42009-09-09 15:08:12 +00001000
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001001 // Check and see if class methods in class interface have been
1002 // implemented in the implementation class. If so, their types match.
Mike Stump11289f42009-09-09 15:08:12 +00001003 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001004 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001005 if (ClsMapSeen.count((*I)->getSelector()))
1006 continue;
1007 ClsMapSeen.insert((*I)->getSelector());
1008 if (!ClsMap.count((*I)->getSelector())) {
1009 if (ImmediateClass)
1010 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stump12b8ce12009-08-04 21:02:39 +00001011 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001012 ObjCMethodDecl *ImpMethodDecl =
1013 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001014 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001015 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001016 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1017 }
1018 }
1019 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1020 // Check for any implementation of a methods declared in protocol.
1021 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1022 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001023 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1024 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001025 (*PI), IncompleteImpl, false);
1026 if (I->getSuperClass())
1027 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +00001028 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001029 I->getSuperClass(), IncompleteImpl, false);
1030 }
1031}
1032
Mike Stump11289f42009-09-09 15:08:12 +00001033void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1034 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001035 bool IncompleteImpl) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001036 llvm::DenseSet<Selector> InsMap;
1037 // Check and see if instance methods in class interface have been
1038 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001039 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001040 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001041 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001042
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001043 // Check and see if properties declared in the interface have either 1)
1044 // an implementation or 2) there is a @synthesize/@dynamic implementation
1045 // of the property in the @implementation.
1046 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001047 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1048 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001049 ObjCPropertyDecl *Prop = (*P);
1050 if (Prop->isInvalidDecl())
1051 continue;
1052 ObjCPropertyImplDecl *PI = 0;
1053 // Is there a matching propery synthesize/dynamic?
Mike Stump11289f42009-09-09 15:08:12 +00001054 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001055 I = IMPDecl->propimpl_begin(),
1056 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001057 if ((*I)->getPropertyDecl() == Prop) {
1058 PI = (*I);
1059 break;
1060 }
1061 if (PI)
1062 continue;
1063 if (!InsMap.count(Prop->getGetterName())) {
Mike Stump11289f42009-09-09 15:08:12 +00001064 Diag(Prop->getLocation(),
1065 diag::warn_setter_getter_impl_required)
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001066 << Prop->getDeclName() << Prop->getGetterName();
1067 Diag(IMPDecl->getLocation(),
1068 diag::note_property_impl_required);
1069 }
Mike Stump11289f42009-09-09 15:08:12 +00001070
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001071 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Mike Stump11289f42009-09-09 15:08:12 +00001072 Diag(Prop->getLocation(),
1073 diag::warn_setter_getter_impl_required)
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001074 << Prop->getDeclName() << Prop->getSetterName();
1075 Diag(IMPDecl->getLocation(),
1076 diag::note_property_impl_required);
1077 }
1078 }
Mike Stump11289f42009-09-09 15:08:12 +00001079
Chris Lattnerda463fe2007-12-12 07:09:47 +00001080 llvm::DenseSet<Selector> ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +00001081 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001082 I = IMPDecl->classmeth_begin(),
1083 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001084 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001085
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001086 // Check for type conflict of methods declared in a class/protocol and
1087 // its implementation; if any.
1088 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +00001089 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1090 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001091 IncompleteImpl, true);
Mike Stump11289f42009-09-09 15:08:12 +00001092
Chris Lattnerda463fe2007-12-12 07:09:47 +00001093 // Check the protocol list for unimplemented methods in the @implementation
1094 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001095 // Check and see if class methods in class interface have been
1096 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001097
Chris Lattner9ef10f42009-03-01 00:56:52 +00001098 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001099 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner9ef10f42009-03-01 00:56:52 +00001100 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001101 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001102 InsMap, ClsMap, I);
1103 // Check class extensions (unnamed categories)
1104 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1105 Categories; Categories = Categories->getNextClassCategory()) {
1106 if (!Categories->getIdentifier()) {
1107 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1108 break;
1109 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +00001110 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001111 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001112 // For extended class, unimplemented methods in its protocols will
1113 // be reported in the primary class.
1114 if (C->getIdentifier()) {
1115 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1116 E = C->protocol_end(); PI != E; ++PI)
1117 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1118 InsMap, ClsMap, C->getClassInterface());
1119 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001120 } else
1121 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00001122}
1123
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001124void
1125Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1126 ObjCContainerDecl* IDecl) {
1127 // Rules apply in non-GC mode only
1128 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1129 return;
1130 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1131 E = IDecl->prop_end();
1132 I != E; ++I) {
1133 ObjCPropertyDecl *Property = (*I);
1134 unsigned Attributes = Property->getPropertyAttributes();
1135 // We only care about readwrite atomic property.
1136 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1137 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1138 continue;
1139 if (const ObjCPropertyImplDecl *PIDecl
1140 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1141 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1142 continue;
1143 ObjCMethodDecl *GetterMethod =
1144 IMPDecl->getInstanceMethod(Property->getGetterName());
1145 ObjCMethodDecl *SetterMethod =
1146 IMPDecl->getInstanceMethod(Property->getSetterName());
1147 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1148 SourceLocation MethodLoc =
1149 (GetterMethod ? GetterMethod->getLocation()
1150 : SetterMethod->getLocation());
1151 Diag(MethodLoc, diag::warn_atomic_property_rule)
1152 << Property->getIdentifier();
1153 Diag(Property->getLocation(), diag::note_property_declare);
1154 }
1155 }
1156 }
1157}
1158
Mike Stump11289f42009-09-09 15:08:12 +00001159/// ActOnForwardClassDeclaration -
Chris Lattner83f095c2009-03-28 19:18:32 +00001160Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00001161Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00001162 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00001163 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00001164 unsigned NumElts) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001165 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump11289f42009-09-09 15:08:12 +00001166
Chris Lattnerda463fe2007-12-12 07:09:47 +00001167 for (unsigned i = 0; i != NumElts; ++i) {
1168 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001169 NamedDecl *PrevDecl
1170 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor5daeee22008-12-08 18:40:42 +00001171 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001172 // Maybe we will complain about the shadowed template parameter.
1173 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1174 // Just pretend that we didn't see the previous declaration.
1175 PrevDecl = 0;
1176 }
1177
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001178 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00001179 // GCC apparently allows the following idiom:
1180 //
1181 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1182 // @class XCElementToggler;
1183 //
Mike Stump11289f42009-09-09 15:08:12 +00001184 // FIXME: Make an extension?
Steve Naroff946166f2008-06-05 22:57:10 +00001185 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1186 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001187 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00001188 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stump12b8ce12009-08-04 21:02:39 +00001189 } else if (TDD) {
1190 // a forward class declaration matching a typedef name of a class refers
1191 // to the underlying class.
Mike Stump11289f42009-09-09 15:08:12 +00001192 if (ObjCInterfaceType * OI =
Fariborz Jahanian0d451812009-05-07 21:49:26 +00001193 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1194 PrevDecl = OI->getDecl();
1195 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001196 }
Mike Stump11289f42009-09-09 15:08:12 +00001197 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001198 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump11289f42009-09-09 15:08:12 +00001199 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Steve Naroffe0064d22009-09-11 00:12:01 +00001200 IdentList[i],
1201 // FIXME: need to get the 'real'
1202 // identifier loc from the parser.
1203 AtClassLoc, true);
Ted Kremenek707ece62009-11-17 22:58:30 +00001204
1205 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1206 // the current DeclContext. This prevents clients that walk DeclContext
1207 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1208 // declared later (if at all). We also take care to explicitly make
1209 // sure this declaration is visible for name lookup.
1210 PushOnScopeChains(IDecl, TUScope, false);
1211 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001212 }
1213
1214 Interfaces.push_back(IDecl);
1215 }
Mike Stump11289f42009-09-09 15:08:12 +00001216
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001217 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlssona6b508a2008-11-04 16:57:32 +00001218 &Interfaces[0],
1219 Interfaces.size());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001220 CurContext->addDecl(CDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +00001221 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001222 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001223}
1224
1225
1226/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1227/// returns true, or false, accordingly.
1228/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump11289f42009-09-09 15:08:12 +00001229bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffa0ed1652008-10-21 10:37:50 +00001230 const ObjCMethodDecl *PrevMethod,
1231 bool matchBasedOnSizeAndAlignment) {
1232 QualType T1 = Context.getCanonicalType(Method->getResultType());
1233 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump11289f42009-09-09 15:08:12 +00001234
Steve Naroffa0ed1652008-10-21 10:37:50 +00001235 if (T1 != T2) {
1236 // The result types are different.
1237 if (!matchBasedOnSizeAndAlignment)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001238 return false;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001239 // Incomplete types don't have a size and alignment.
1240 if (T1->isIncompleteType() || T2->isIncompleteType())
1241 return false;
1242 // Check is based on size and alignment.
1243 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1244 return false;
1245 }
Mike Stump11289f42009-09-09 15:08:12 +00001246
Chris Lattnera4997152009-02-20 18:43:26 +00001247 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1248 E = Method->param_end();
1249 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001250
Chris Lattnera4997152009-02-20 18:43:26 +00001251 for (; ParamI != E; ++ParamI, ++PrevI) {
1252 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1253 T1 = Context.getCanonicalType((*ParamI)->getType());
1254 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffa0ed1652008-10-21 10:37:50 +00001255 if (T1 != T2) {
1256 // The result types are different.
1257 if (!matchBasedOnSizeAndAlignment)
1258 return false;
1259 // Incomplete types don't have a size and alignment.
1260 if (T1->isIncompleteType() || T2->isIncompleteType())
1261 return false;
1262 // Check is based on size and alignment.
1263 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1264 return false;
1265 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001266 }
1267 return true;
1268}
1269
Douglas Gregorc78d3462009-04-24 21:10:55 +00001270/// \brief Read the contents of the instance and factory method pools
1271/// for a given selector from external storage.
1272///
1273/// This routine should only be called once, when neither the instance
1274/// nor the factory method pool has an entry for this selector.
Mike Stump11289f42009-09-09 15:08:12 +00001275Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001276 bool isInstance) {
1277 assert(ExternalSource && "We need an external AST source");
1278 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1279 "Selector data already loaded into the instance method pool");
1280 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1281 "Selector data already loaded into the factory method pool");
1282
1283 // Read the method list from the external source.
1284 std::pair<ObjCMethodList, ObjCMethodList> Methods
1285 = ExternalSource->ReadMethodPool(Sel);
Mike Stump11289f42009-09-09 15:08:12 +00001286
Douglas Gregorc78d3462009-04-24 21:10:55 +00001287 if (isInstance) {
1288 if (Methods.second.Method)
1289 FactoryMethodPool[Sel] = Methods.second;
1290 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump11289f42009-09-09 15:08:12 +00001291 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001292
1293 if (Methods.first.Method)
1294 InstanceMethodPool[Sel] = Methods.first;
1295
1296 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1297}
1298
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001299void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001300 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1301 = InstanceMethodPool.find(Method->getSelector());
1302 if (Pos == InstanceMethodPool.end()) {
1303 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1304 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1305 else
1306 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1307 ObjCMethodList())).first;
1308 }
1309
1310 ObjCMethodList &Entry = Pos->second;
Chris Lattner7b26b292009-03-04 05:16:45 +00001311 if (Entry.Method == 0) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001312 // Haven't seen a method with this selector name yet - add it.
Chris Lattner7b26b292009-03-04 05:16:45 +00001313 Entry.Method = Method;
1314 Entry.Next = 0;
1315 return;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001316 }
Mike Stump11289f42009-09-09 15:08:12 +00001317
Chris Lattner7b26b292009-03-04 05:16:45 +00001318 // We've seen a method with this name, see if we have already seen this type
1319 // signature.
1320 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1321 if (MatchTwoMethodDeclarations(Method, List->Method))
1322 return;
Mike Stump11289f42009-09-09 15:08:12 +00001323
Chris Lattner7b26b292009-03-04 05:16:45 +00001324 // We have a new signature for an existing method - add it.
1325 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1326 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001327}
1328
Steve Naroff9ebc0502008-10-21 10:50:19 +00001329// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump11289f42009-09-09 15:08:12 +00001330ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001331 SourceRange R,
1332 bool warn) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001333 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1334 = InstanceMethodPool.find(Sel);
Douglas Gregor9a1899b2009-04-24 22:23:41 +00001335 if (Pos == InstanceMethodPool.end()) {
1336 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001337 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1338 else
1339 return 0;
1340 }
1341
1342 ObjCMethodList &MethList = Pos->second;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001343 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001344
Steve Naroff4a82d812008-09-30 14:38:43 +00001345 if (MethList.Method && MethList.Next) {
Steve Naroffa0ed1652008-10-21 10:37:50 +00001346 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1347 // This checks if the methods differ by size & alignment.
1348 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001349 issueWarning = warn;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001350 }
1351 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattnere4b95692008-11-24 03:33:13 +00001352 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner12f7c5a2008-11-23 23:26:13 +00001353 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerf490e152008-11-19 05:27:50 +00001354 << MethList.Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001355 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner12f7c5a2008-11-23 23:26:13 +00001356 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerf490e152008-11-19 05:27:50 +00001357 << Next->Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001358 }
1359 return MethList.Method;
1360}
1361
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001362void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001363 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1364 = FactoryMethodPool.find(Method->getSelector());
1365 if (Pos == FactoryMethodPool.end()) {
1366 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1367 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1368 else
1369 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1370 ObjCMethodList())).first;
1371 }
1372
1373 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001374 if (!FirstMethod.Method) {
1375 // Haven't seen a method with this selector name yet - add it.
1376 FirstMethod.Method = Method;
1377 FirstMethod.Next = 0;
1378 } else {
1379 // We've seen a method with this name, now check the type signature(s).
1380 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump11289f42009-09-09 15:08:12 +00001381
1382 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001383 Next = Next->Next)
1384 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump11289f42009-09-09 15:08:12 +00001385
Chris Lattnerda463fe2007-12-12 07:09:47 +00001386 if (!match) {
1387 // We have a new signature for an existing method - add it.
1388 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001389 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001390 FirstMethod.Next = OMI;
1391 }
1392 }
1393}
1394
Mike Stump11289f42009-09-09 15:08:12 +00001395ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001396 SourceRange R) {
1397 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1398 = FactoryMethodPool.find(Sel);
1399 if (Pos == FactoryMethodPool.end()) {
Mike Stump11289f42009-09-09 15:08:12 +00001400 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001401 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1402 else
1403 return 0;
1404 }
1405
1406 ObjCMethodList &MethList = Pos->second;
1407 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001408
Douglas Gregorc78d3462009-04-24 21:10:55 +00001409 if (MethList.Method && MethList.Next) {
1410 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1411 // This checks if the methods differ by size & alignment.
1412 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1413 issueWarning = true;
1414 }
1415 if (issueWarning && (MethList.Method && MethList.Next)) {
1416 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1417 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1418 << MethList.Method->getSourceRange();
1419 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1420 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1421 << Next->Method->getSourceRange();
1422 }
1423 return MethList.Method;
1424}
1425
Mike Stump11289f42009-09-09 15:08:12 +00001426/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff35c62ae2009-01-08 17:28:14 +00001427/// have the property type and issue diagnostics if they don't.
1428/// Also synthesize a getter/setter method if none exist (and update the
1429/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1430/// methods is the "right" thing to do.
Mike Stump11289f42009-09-09 15:08:12 +00001431void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff35c62ae2009-01-08 17:28:14 +00001432 ObjCContainerDecl *CD) {
1433 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump11289f42009-09-09 15:08:12 +00001434
1435 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001436 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump11289f42009-09-09 15:08:12 +00001437 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +00001438 property->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001439
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001440 if (SetterMethod) {
Mike Stump11289f42009-09-09 15:08:12 +00001441 if (Context.getCanonicalType(SetterMethod->getResultType())
Fariborz Jahanian06d0dd62008-12-06 23:12:49 +00001442 != Context.VoidTy)
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001443 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattnera4997152009-02-20 18:43:26 +00001444 if (SetterMethod->param_size() != 1 ||
1445 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump11289f42009-09-09 15:08:12 +00001446 Diag(property->getLocation(),
1447 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001448 << property->getDeclName()
Ted Kremenek9a468042009-03-14 00:20:08 +00001449 << SetterMethod->getSelector();
Fariborz Jahanianff839982008-12-06 21:48:16 +00001450 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1451 }
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001452 }
Steve Naroff35c62ae2009-01-08 17:28:14 +00001453
1454 // Synthesize getter/setter methods if none exist.
Steve Naroffe538c5f2009-01-08 20:15:03 +00001455 // Find the default getter and if one not found, add one.
Mike Stump87c57ac2009-05-16 07:39:55 +00001456 // FIXME: The synthesized property we set here is misleading. We almost always
1457 // synthesize these methods unless the user explicitly provided prototypes
1458 // (which is odd, but allowed). Sema should be typechecking that the
1459 // declarations jive in that situation (which it is not currently).
Steve Naroffe538c5f2009-01-08 20:15:03 +00001460 if (!GetterMethod) {
1461 // No instance method of same name as property getter name was found.
Mike Stump11289f42009-09-09 15:08:12 +00001462 // Declare a getter method and add it to the list of methods
Steve Naroffe538c5f2009-01-08 20:15:03 +00001463 // for this class.
Mike Stump11289f42009-09-09 15:08:12 +00001464 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1465 property->getLocation(), property->getGetterName(),
1466 property->getType(), CD, true, false, true,
1467 (property->getPropertyImplementation() ==
1468 ObjCPropertyDecl::Optional) ?
1469 ObjCMethodDecl::Optional :
Steve Naroffe538c5f2009-01-08 20:15:03 +00001470 ObjCMethodDecl::Required);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001471 CD->addDecl(GetterMethod);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001472 } else
1473 // A user declared getter will be synthesize when @synthesize of
1474 // the property with the same name is seen in the @implementation
Steve Naroff04f2d142009-04-20 15:06:07 +00001475 GetterMethod->setSynthesized(true);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001476 property->setGetterMethodDecl(GetterMethod);
1477
1478 // Skip setter if property is read-only.
1479 if (!property->isReadOnly()) {
1480 // Find the default setter and if one not found, add one.
1481 if (!SetterMethod) {
1482 // No instance method of same name as property setter name was found.
Mike Stump11289f42009-09-09 15:08:12 +00001483 // Declare a setter 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 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1486 property->getLocation(),
1487 property->getSetterName(),
Steve Naroffe538c5f2009-01-08 20:15:03 +00001488 Context.VoidTy, CD, true, false, true,
Mike Stump11289f42009-09-09 15:08:12 +00001489 (property->getPropertyImplementation() ==
1490 ObjCPropertyDecl::Optional) ?
1491 ObjCMethodDecl::Optional :
Steve Naroffe538c5f2009-01-08 20:15:03 +00001492 ObjCMethodDecl::Required);
1493 // Invent the arguments for the setter. We don't bother making a
1494 // nice name for the argument.
1495 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump11289f42009-09-09 15:08:12 +00001496 property->getLocation(),
Steve Naroffe538c5f2009-01-08 20:15:03 +00001497 property->getIdentifier(),
1498 property->getType(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001499 /*DInfo=*/0,
Steve Naroffe538c5f2009-01-08 20:15:03 +00001500 VarDecl::None,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001501 0);
Steve Naroff04f2d142009-04-20 15:06:07 +00001502 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001503 CD->addDecl(SetterMethod);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001504 } else
1505 // A user declared setter will be synthesize when @synthesize of
1506 // the property with the same name is seen in the @implementation
Steve Naroff04f2d142009-04-20 15:06:07 +00001507 SetterMethod->setSynthesized(true);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001508 property->setSetterMethodDecl(SetterMethod);
1509 }
Mike Stump11289f42009-09-09 15:08:12 +00001510 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff35c62ae2009-01-08 17:28:14 +00001511 // handle the following, which is supported by GCC (and part of the design).
1512 //
1513 // @interface Foo
1514 // @property double bar;
1515 // @end
1516 //
1517 // void thisIsUnfortunate() {
1518 // id foo;
1519 // double bar = [foo bar];
1520 // }
1521 //
Douglas Gregor020713e2009-01-09 19:42:16 +00001522 if (GetterMethod)
Mike Stump11289f42009-09-09 15:08:12 +00001523 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor020713e2009-01-09 19:42:16 +00001524 if (SetterMethod)
Mike Stump11289f42009-09-09 15:08:12 +00001525 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001526}
1527
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001528/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1529/// identical selector names in current and its super classes and issues
1530/// a warning if any of their argument types are incompatible.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001531void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1532 ObjCMethodDecl *Method,
1533 bool IsInstance) {
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001534 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1535 if (ID == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +00001536
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001537 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump11289f42009-09-09 15:08:12 +00001538 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001539 SD->lookupMethod(Method->getSelector(), IsInstance);
1540 if (SuperMethodDecl == 0) {
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001541 ID = SD;
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001542 continue;
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001543 }
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001544 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1545 E = Method->param_end();
1546 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1547 for (; ParamI != E; ++ParamI, ++PrevI) {
1548 // Number of parameters are the same and is guaranteed by selector match.
1549 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1550 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1551 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1552 // If type of arguement of method in this class does not match its
1553 // respective argument type in the super class method, issue warning;
1554 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump11289f42009-09-09 15:08:12 +00001555 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001556 << T1 << T2;
1557 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1558 return;
1559 }
1560 }
1561 ID = SD;
1562 }
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001563}
1564
Steve Naroff1d2538c2007-12-18 01:30:32 +00001565// Note: For class/category implemenations, allMethods/allProperties is
1566// always null.
Chris Lattner83f095c2009-03-28 19:18:32 +00001567void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1568 DeclPtrTy *allMethods, unsigned allNum,
1569 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001570 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001571 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001572
Steve Naroff1d2538c2007-12-18 01:30:32 +00001573 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1574 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattnerda463fe2007-12-12 07:09:47 +00001575 // should be true.
1576 if (!ClassDecl)
1577 return;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001578
Mike Stump11289f42009-09-09 15:08:12 +00001579 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00001580 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1581 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001582 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00001583
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001584 if (!isInterfaceDeclKind && AtEndLoc.isInvalid()) {
1585 AtEndLoc = ClassDecl->getLocation();
1586 Diag(AtEndLoc, diag::warn_missing_atend);
1587 }
1588
Steve Naroff35c62ae2009-01-08 17:28:14 +00001589 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff35c62ae2009-01-08 17:28:14 +00001590
1591 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1592 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1593 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1594
Chris Lattnerda463fe2007-12-12 07:09:47 +00001595 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001596 ObjCMethodDecl *Method =
Chris Lattner83f095c2009-03-28 19:18:32 +00001597 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001598
1599 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00001600 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001601 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001602 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001603 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001604 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001605 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001606 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001607 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001608 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001609 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001610 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001611 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001612 InsMap[Method->getSelector()] = Method;
1613 /// The following allows us to typecheck messages to "id".
1614 AddInstanceMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001615 // verify that the instance method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001616 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001617 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001618 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001619 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001620 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001621 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001622 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001623 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001624 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001625 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001626 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001627 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001628 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001629 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001630 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001631 ClsMap[Method->getSelector()] = Method;
Steve Naroff1d2538c2007-12-18 01:30:32 +00001632 /// The following allows us to typecheck messages to "Class".
1633 AddFactoryMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001634 // verify that the class method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001635 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001636 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001637 }
1638 }
1639 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001640 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump11289f42009-09-09 15:08:12 +00001641 // Compares properties declared in this class to those of its
Fariborz Jahanian7cf18862008-05-01 00:03:38 +00001642 // super class.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +00001643 ComparePropertiesInBaseAndSuper(I);
Chris Lattner83f095c2009-03-28 19:18:32 +00001644 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroffb3a87982009-01-09 15:36:25 +00001645 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001646 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00001647 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001648 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001649
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +00001650 // Merge protocol properties into category
Chris Lattner83f095c2009-03-28 19:18:32 +00001651 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanian33afd772009-03-02 19:05:07 +00001652 if (C->getIdentifier() == 0)
1653 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001654 }
Steve Naroffb3a87982009-01-09 15:36:25 +00001655 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1656 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1657 // user-defined setter/getter. It also synthesizes setter/getter methods
1658 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001659 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1660 E = CDecl->prop_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001661 I != E; ++I)
Chris Lattner41fd42e2009-02-16 18:32:47 +00001662 ProcessPropertyDecl(*I, CDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00001663 CDecl->setAtEndLoc(AtEndLoc);
1664 }
1665 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001666 IC->setAtEndLoc(AtEndLoc);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001667 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001668 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001669 AtomicPropertySetterGetterRules(IC, IDecl);
1670 }
Mike Stump11289f42009-09-09 15:08:12 +00001671 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00001672 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001673 CatImplClass->setAtEndLoc(AtEndLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001674
Chris Lattnerda463fe2007-12-12 07:09:47 +00001675 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00001676 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00001677 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001678 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001679 Categories; Categories = Categories->getNextClassCategory()) {
1680 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner9ef10f42009-03-01 00:56:52 +00001681 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001682 break;
1683 }
1684 }
1685 }
1686 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001687 if (isInterfaceDeclKind) {
1688 // Reject invalid vardecls.
1689 for (unsigned i = 0; i != tuvNum; i++) {
1690 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1691 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1692 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00001693 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00001694 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00001695 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001696 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00001697 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001698}
1699
1700
1701/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1702/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00001703static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001704CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1705 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1706 if (PQTVal & ObjCDeclSpec::DQ_In)
1707 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1708 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1709 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1710 if (PQTVal & ObjCDeclSpec::DQ_Out)
1711 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1712 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1713 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1714 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1715 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1716 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1717 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001718
1719 return ret;
1720}
1721
Chris Lattner83f095c2009-03-28 19:18:32 +00001722Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattnerda463fe2007-12-12 07:09:47 +00001723 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001724 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001725 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001726 Selector Sel,
1727 // optional arguments. The number of types/arguments is obtained
1728 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001729 ObjCArgInfo *ArgInfo,
Fariborz Jahaniane84858c2009-01-09 00:38:19 +00001730 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001731 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1732 bool isVariadic) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001733 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff83777fe2008-02-29 21:48:07 +00001734
1735 // Make sure we can establish a context for the method.
1736 if (!ClassDecl) {
1737 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanianb1771e42009-08-28 17:52:37 +00001738 FunctionLabelMap.clear();
Chris Lattner83f095c2009-03-28 19:18:32 +00001739 return DeclPtrTy();
Steve Naroff83777fe2008-02-29 21:48:07 +00001740 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001741 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00001742
Steve Naroff32606412009-02-20 22:59:16 +00001743 if (ReturnType) {
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +00001744 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump11289f42009-09-09 15:08:12 +00001745
Steve Naroff32606412009-02-20 22:59:16 +00001746 // Methods cannot return interface types. All ObjC objects are
1747 // passed by reference.
1748 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattnerde5a5312009-04-11 19:08:56 +00001749 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1750 << 0 << resultDeclType;
Chris Lattner83f095c2009-03-28 19:18:32 +00001751 return DeclPtrTy();
Steve Naroff32606412009-02-20 22:59:16 +00001752 }
1753 } else // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001754 resultDeclType = Context.getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +00001755
1756 ObjCMethodDecl* ObjCMethod =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001757 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump11289f42009-09-09 15:08:12 +00001758 cast<DeclContext>(ClassDecl),
Chris Lattner8d8829e2008-03-16 00:49:28 +00001759 MethodType == tok::minus, isVariadic,
Fariborz Jahanian8983f172008-05-07 20:53:44 +00001760 false,
Mike Stump11289f42009-09-09 15:08:12 +00001761 MethodDeclKind == tok::objc_optional ?
1762 ObjCMethodDecl::Optional :
Chris Lattner8d8829e2008-03-16 00:49:28 +00001763 ObjCMethodDecl::Required);
Mike Stump11289f42009-09-09 15:08:12 +00001764
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001765 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00001766
Chris Lattner23b0faf2009-04-11 19:42:43 +00001767 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00001768 QualType ArgType;
1769 DeclaratorInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00001770
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001771 if (ArgInfo[i].Type == 0) {
John McCall856bbea2009-10-23 21:48:59 +00001772 ArgType = Context.getObjCIdType();
1773 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001774 } else {
John McCall856bbea2009-10-23 21:48:59 +00001775 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroffb0498ee2008-12-09 19:36:17 +00001776 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner9713a1c2009-04-11 19:34:56 +00001777 ArgType = adjustParameterType(ArgType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001778 }
Mike Stump11289f42009-09-09 15:08:12 +00001779
John McCall856bbea2009-10-23 21:48:59 +00001780 ParmVarDecl* Param
1781 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1782 ArgInfo[i].Name, ArgType, DI,
1783 VarDecl::None, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001784
Chris Lattner9713a1c2009-04-11 19:34:56 +00001785 if (ArgType->isObjCInterfaceType()) {
1786 Diag(ArgInfo[i].NameLoc,
1787 diag::err_object_cannot_be_passed_returned_by_value)
1788 << 1 << ArgType;
1789 Param->setInvalidDecl();
1790 }
Mike Stump11289f42009-09-09 15:08:12 +00001791
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001792 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001793 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00001794
Chris Lattner9713a1c2009-04-11 19:34:56 +00001795 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00001796 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001797
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001798 Params.push_back(Param);
1799 }
1800
Jay Foad7d0479f2009-05-21 09:52:38 +00001801 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001802 ObjCMethod->setObjCDeclQualifier(
1803 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1804 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001805
1806 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00001807 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00001808
John McCall28a6aea2009-11-04 02:18:39 +00001809 const ObjCMethodDecl *InterfaceMD = 0;
1810
Mike Stump11289f42009-09-09 15:08:12 +00001811 // For implementations (which can be very "coarse grain"), we add the
1812 // method now. This allows the AST to implement lookup methods that work
1813 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattnerda463fe2007-12-12 07:09:47 +00001814 // us to flag multiple declaration errors as they occur.
Mike Stump11289f42009-09-09 15:08:12 +00001815 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001816 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001817 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001818 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1819 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001820 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001821 PrevMethod = ImpDecl->getClassMethod(Sel);
1822 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001823 }
John McCall28a6aea2009-11-04 02:18:39 +00001824 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1825 MethodType == tok::minus);
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001826 if (AttrList)
1827 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump11289f42009-09-09 15:08:12 +00001828 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stump12b8ce12009-08-04 21:02:39 +00001829 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001830 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001831 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1832 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001833 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001834 PrevMethod = CatImpDecl->getClassMethod(Sel);
1835 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001836 }
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001837 if (AttrList)
1838 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001839 }
1840 if (PrevMethod) {
1841 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00001842 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001843 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001844 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump11289f42009-09-09 15:08:12 +00001845 }
John McCall28a6aea2009-11-04 02:18:39 +00001846
1847 // If the interface declared this method, and it was deprecated there,
1848 // mark it deprecated here.
1849 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1850 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1851
Chris Lattner83f095c2009-03-28 19:18:32 +00001852 return DeclPtrTy::make(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001853}
1854
Mike Stump11289f42009-09-09 15:08:12 +00001855void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001856 SourceLocation Loc,
1857 unsigned &Attributes) {
1858 // FIXME: Improve the reported location.
1859
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00001860 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001861 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00001862 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1863 ObjCDeclSpec::DQ_PR_assign |
1864 ObjCDeclSpec::DQ_PR_copy |
1865 ObjCDeclSpec::DQ_PR_retain))) {
1866 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1867 "readwrite" :
1868 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1869 "assign" :
1870 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1871 "copy" : "retain";
Mike Stump11289f42009-09-09 15:08:12 +00001872
1873 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner409f5552009-01-29 18:49:48 +00001874 diag::err_objc_property_attr_mutually_exclusive :
1875 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00001876 << "readonly" << which;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001877 }
1878
1879 // Check for copy or retain on non-object types.
1880 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump11289f42009-09-09 15:08:12 +00001881 !PropertyTy->isObjCObjectPointerType() &&
1882 !PropertyTy->isBlockPointerType() &&
Steve Naroff79d12152009-07-16 15:41:00 +00001883 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001884 Diag(Loc, diag::err_objc_property_requires_object)
1885 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001886 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1887 }
1888
1889 // Check for more than one of { assign, copy, retain }.
1890 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1891 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001892 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1893 << "assign" << "copy";
1894 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump11289f42009-09-09 15:08:12 +00001895 }
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001896 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001897 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1898 << "assign" << "retain";
1899 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001900 }
1901 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1902 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001903 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1904 << "copy" << "retain";
1905 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001906 }
1907 }
1908
1909 // Warn if user supplied no assignment attribute, property is
1910 // readwrite, and this is an object type.
1911 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1912 ObjCDeclSpec::DQ_PR_retain)) &&
1913 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Naroff79d12152009-07-16 15:41:00 +00001914 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001915 // Skip this warning in gc-only mode.
Mike Stump11289f42009-09-09 15:08:12 +00001916 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001917 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1918
1919 // If non-gc code warn that this is likely inappropriate.
1920 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1921 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump11289f42009-09-09 15:08:12 +00001922
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001923 // FIXME: Implement warning dependent on NSCopying being
1924 // implemented. See also:
1925 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1926 // (please trim this list while you are at it).
1927 }
Mike Stump5580bdc2009-05-07 23:06:50 +00001928
1929 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1930 && getLangOptions().getGCMode() == LangOptions::GCOnly
1931 && PropertyTy->isBlockPointerType())
1932 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001933}
1934
Mike Stump11289f42009-09-09 15:08:12 +00001935Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001936 FieldDeclarator &FD,
1937 ObjCDeclSpec &ODS,
1938 Selector GetterSel,
1939 Selector SetterSel,
1940 DeclPtrTy ClassCategory,
1941 bool *isOverridingProperty,
1942 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001943 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001944 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1945 // default is readwrite!
1946 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump11289f42009-09-09 15:08:12 +00001947 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001948 // not retain or copy
1949 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump11289f42009-09-09 15:08:12 +00001950 (isReadWrite &&
1951 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001952 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1953 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattner83f095c2009-03-28 19:18:32 +00001954 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001955 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001956 // May modify Attributes.
1957 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001958 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1959 if (!CDecl->getIdentifier()) {
Mike Stump11289f42009-09-09 15:08:12 +00001960 // This is a continuation class. property requires special
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001961 // handling.
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001962 if ((CCPrimary = CDecl->getClassInterface())) {
1963 // Find the property in continuation class's primary class only.
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001964 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniande8db162009-11-02 22:45:15 +00001965 if (ObjCPropertyDecl *PIDecl =
1966 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001967 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahaniane4fd6402009-04-01 23:23:53 +00001968 // with continuation class's readwrite property attribute!
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001969 unsigned PIkind = PIDecl->getPropertyAttributes();
1970 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandc216102009-11-10 19:31:09 +00001971 unsigned retainCopyNonatomic =
Fariborz Jahanian3600f412009-11-06 22:59:12 +00001972 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniana386d952009-11-03 00:01:38 +00001973 ObjCPropertyDecl::OBJC_PR_copy |
1974 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahaniandc216102009-11-10 19:31:09 +00001975 if ((Attributes & retainCopyNonatomic) !=
1976 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001977 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniana386d952009-11-03 00:01:38 +00001978 Diag(PIDecl->getLocation(), diag::note_property_declare);
1979 }
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001980 PIDecl->makeitReadWriteAttribute();
1981 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1982 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1983 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1984 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1985 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniande8db162009-11-02 22:45:15 +00001986 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001987 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001988 << CCPrimary->getDeclName();
Fariborz Jahaniande8db162009-11-02 22:45:15 +00001989 Diag(PIDecl->getLocation(), diag::note_property_declare);
1990 }
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001991 *isOverridingProperty = true;
Mike Stump11289f42009-09-09 15:08:12 +00001992 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian47b21082009-04-15 19:19:03 +00001993 // class's list.
1994 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001995 return DeclPtrTy();
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00001996 }
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00001997 // No matching property found in the primary class. Just fall thru
1998 // and add property to continuation class's primary class.
1999 ClassDecl = CCPrimary;
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002000 } else {
Chris Lattner68d42c72009-02-20 21:38:52 +00002001 Diag(CDecl->getLocation(), diag::err_continuation_class);
2002 *isOverridingProperty = true;
Chris Lattner83f095c2009-03-28 19:18:32 +00002003 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002004 }
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002005 }
Mike Stump11289f42009-09-09 15:08:12 +00002006
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002007 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump11289f42009-09-09 15:08:12 +00002008 // gc'able conforms to NSCopying protocol
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002009 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2010 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2011 if (T->isObjCObjectPointerType()) {
2012 QualType InterfaceTy = T->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +00002013 if (const ObjCInterfaceType *OIT =
John McCall9dd450b2009-09-21 23:43:11 +00002014 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian83b000c2009-08-14 18:06:25 +00002015 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002016 if (IDecl)
Mike Stump11289f42009-09-09 15:08:12 +00002017 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002018 LookupProtocol(&Context.Idents.get("NSCopying")))
2019 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump11289f42009-09-09 15:08:12 +00002020 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002021 << FD.D.getIdentifier();
Fariborz Jahanian83b000c2009-08-14 18:06:25 +00002022 }
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002023 }
Fariborz Jahanian887cd6a2009-08-12 18:17:53 +00002024 if (T->isObjCInterfaceType())
2025 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump11289f42009-09-09 15:08:12 +00002026
Steve Naroffba3dc382009-01-11 12:47:58 +00002027 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2028 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattner5300acc2009-04-11 20:14:49 +00002029 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump11289f42009-09-09 15:08:12 +00002030 FD.D.getIdentifierLoc(),
Fariborz Jahanian0152a1a2008-04-14 23:36:35 +00002031 FD.D.getIdentifier(), T);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002032 DC->addDecl(PDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002033
Chris Lattner5300acc2009-04-11 20:14:49 +00002034 if (T->isArrayType() || T->isFunctionType()) {
2035 Diag(AtLoc, diag::err_property_type) << T;
2036 PDecl->setInvalidDecl();
2037 }
Mike Stump11289f42009-09-09 15:08:12 +00002038
Douglas Gregor758a8692009-06-17 21:51:59 +00002039 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002040
Fariborz Jahanianec6e4c82008-05-07 17:43:59 +00002041 // Regardless of setter/getter attribute, we save the default getter/setter
2042 // selector names in anticipation of declaration of setter/getter methods.
2043 PDecl->setGetterName(GetterSel);
2044 PDecl->setSetterName(SetterSel);
Mike Stump11289f42009-09-09 15:08:12 +00002045
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002046 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002047 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump11289f42009-09-09 15:08:12 +00002048
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002049 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002050 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump11289f42009-09-09 15:08:12 +00002051
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002052 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002053 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump11289f42009-09-09 15:08:12 +00002054
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002055 if (isReadWrite)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002056 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump11289f42009-09-09 15:08:12 +00002057
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002058 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002059 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump11289f42009-09-09 15:08:12 +00002060
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002061 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002062 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump11289f42009-09-09 15:08:12 +00002063
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002064 if (isAssign)
2065 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump11289f42009-09-09 15:08:12 +00002066
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002067 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002068 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump11289f42009-09-09 15:08:12 +00002069
Fariborz Jahanian8d916862008-05-05 18:51:55 +00002070 if (MethodImplKind == tok::objc_required)
2071 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2072 else if (MethodImplKind == tok::objc_optional)
2073 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002074 // A case of continuation class adding a new property in the class. This
2075 // is not what it was meant for. However, gcc supports it and so should we.
2076 // Make sure setter/getters are declared here.
2077 if (CCPrimary)
2078 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump11289f42009-09-09 15:08:12 +00002079
Chris Lattner83f095c2009-03-28 19:18:32 +00002080 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002081}
2082
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002083/// ActOnPropertyImplDecl - This routine performs semantic checks and
2084/// builds the AST node for a property implementation declaration; declared
2085/// as @synthesize or @dynamic.
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002086///
Mike Stump11289f42009-09-09 15:08:12 +00002087Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00002088 SourceLocation PropertyLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002089 bool Synthesize,
Chris Lattner83f095c2009-03-28 19:18:32 +00002090 DeclPtrTy ClassCatImpDecl,
2091 IdentifierInfo *PropertyId,
2092 IdentifierInfo *PropertyIvar) {
2093 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002094 // Make sure we have a context for the property implementation declaration.
2095 if (!ClassImpDecl) {
2096 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner83f095c2009-03-28 19:18:32 +00002097 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002098 }
2099 ObjCPropertyDecl *property = 0;
2100 ObjCInterfaceDecl* IDecl = 0;
2101 // Find the class or category class where this property must have
2102 // a declaration.
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002103 ObjCImplementationDecl *IC = 0;
2104 ObjCCategoryImplDecl* CatImplClass = 0;
2105 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002106 IDecl = IC->getClassInterface();
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002107 // We always synthesize an interface for an implementation
2108 // without an interface decl. So, IDecl is always non-zero.
Mike Stump11289f42009-09-09 15:08:12 +00002109 assert(IDecl &&
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002110 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump11289f42009-09-09 15:08:12 +00002111
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002112 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002113 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002114 if (!property) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002115 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner83f095c2009-03-28 19:18:32 +00002116 return DeclPtrTy();
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002117 }
Fariborz Jahanianec344ed2009-11-02 18:45:36 +00002118 if (const ObjCCategoryDecl *CD =
2119 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
2120 if (CD->getIdentifier()) {
2121 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniande8db162009-11-02 22:45:15 +00002122 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianec344ed2009-11-02 18:45:36 +00002123 return DeclPtrTy();
2124 }
2125 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002126 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002127 if (Synthesize) {
2128 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner83f095c2009-03-28 19:18:32 +00002129 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002130 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002131 IDecl = CatImplClass->getClassInterface();
2132 if (!IDecl) {
2133 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner83f095c2009-03-28 19:18:32 +00002134 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002135 }
Mike Stump11289f42009-09-09 15:08:12 +00002136 ObjCCategoryDecl *Category =
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002137 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump11289f42009-09-09 15:08:12 +00002138
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002139 // If category for this implementation not found, it is an error which
2140 // has already been reported eralier.
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002141 if (!Category)
Chris Lattner83f095c2009-03-28 19:18:32 +00002142 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002143 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002144 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002145 if (!property) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002146 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002147 << Category->getDeclName();
Chris Lattner83f095c2009-03-28 19:18:32 +00002148 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002149 }
Chris Lattner83f095c2009-03-28 19:18:32 +00002150 } else {
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002151 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner83f095c2009-03-28 19:18:32 +00002152 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002153 }
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002154 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002155 // Check that we have a valid, previously declared ivar for @synthesize
2156 if (Synthesize) {
2157 // @synthesize
Fariborz Jahanianc6bec7b2008-04-21 21:57:36 +00002158 if (!PropertyIvar)
2159 PropertyIvar = PropertyId;
Fariborz Jahanianb35b4a92009-03-31 00:06:29 +00002160 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002161 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanianb6d5b542009-04-13 19:30:37 +00002162 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002163 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002164 if (!Ivar) {
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002165 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002166 assert(EnclosingContext &&
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002167 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump11289f42009-09-09 15:08:12 +00002168 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2169 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002170 ObjCIvarDecl::Public,
2171 (Expr *)0);
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002172 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002173 IDecl->addDecl(Ivar);
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002174 property->setPropertyIvarDecl(Ivar);
2175 if (!getLangOptions().ObjCNonFragileABI)
Steve Naroffc03f6b92009-03-03 22:09:41 +00002176 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump11289f42009-09-09 15:08:12 +00002177 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002178 // a property implementation and to avoid future warnings.
Mike Stump12b8ce12009-08-04 21:02:39 +00002179 } else if (getLangOptions().ObjCNonFragileABI &&
2180 ClassDeclared != IDecl) {
Fariborz Jahanian68592fc2009-04-30 21:39:24 +00002181 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump11289f42009-09-09 15:08:12 +00002182 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanianb6d5b542009-04-13 19:30:37 +00002183 << ClassDeclared->getDeclName();
2184 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2185 << Ivar << Ivar->getNameAsCString();
2186 // Note! I deliberately want it to fall thru so more errors are caught.
2187 }
Steve Naroff9f9774c2008-09-30 10:07:56 +00002188 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002189
Steve Naroff506e7172008-09-30 00:24:17 +00002190 // Check that type of property and its ivar are type compatible.
Steve Naroff9f9774c2008-09-30 10:07:56 +00002191 if (PropType != IvarType) {
Steve Naroff9546eee2008-10-16 14:59:30 +00002192 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002193 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002194 << property->getDeclName() << Ivar->getDeclName();
Mike Stump11289f42009-09-09 15:08:12 +00002195 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002196 // a property implementation and to avoid future warnings.
Steve Naroff9f9774c2008-09-30 10:07:56 +00002197 }
Mike Stump11289f42009-09-09 15:08:12 +00002198
Chris Lattner83f095c2009-03-28 19:18:32 +00002199 // FIXME! Rules for properties are somewhat different that those
2200 // for assignments. Use a new routine to consolidate all cases;
2201 // specifically for property redeclarations as well as for ivars.
2202 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2203 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump11289f42009-09-09 15:08:12 +00002204 if (lhsType != rhsType &&
Chris Lattner83f095c2009-03-28 19:18:32 +00002205 lhsType->isArithmeticType()) {
2206 Diag(PropertyLoc, diag::error_property_ivar_type)
2207 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002208 // Fall thru - see previous comment
Chris Lattner83f095c2009-03-28 19:18:32 +00002209 }
2210 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanian9ecb84b2009-04-07 21:25:06 +00002211 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2212 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002213 Diag(PropertyLoc, diag::error_weak_property)
2214 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002215 // Fall thru - see previous comment
Chris Lattner83f095c2009-03-28 19:18:32 +00002216 }
Mike Stump11289f42009-09-09 15:08:12 +00002217 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian60331be2009-04-10 22:42:54 +00002218 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2219 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002220 Diag(PropertyLoc, diag::error_strong_property)
2221 << property->getDeclName() << Ivar->getDeclName();
Mike Stump11289f42009-09-09 15:08:12 +00002222 // Fall thru - see previous comment
Fariborz Jahanian54fa4182009-01-19 20:13:47 +00002223 }
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002224 }
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002225 } else if (PropertyIvar)
2226 // @dynamic
2227 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002228 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump11289f42009-09-09 15:08:12 +00002229 ObjCPropertyImplDecl *PIDecl =
2230 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2231 property,
2232 (Synthesize ?
2233 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002234 : ObjCPropertyImplDecl::Dynamic),
2235 Ivar);
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002236 if (IC) {
2237 if (Synthesize)
Mike Stump11289f42009-09-09 15:08:12 +00002238 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002239 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump11289f42009-09-09 15:08:12 +00002240 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2241 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002242 << PropertyIvar;
2243 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2244 }
Mike Stump11289f42009-09-09 15:08:12 +00002245
2246 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002247 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002248 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2249 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +00002250 return DeclPtrTy();
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002251 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002252 IC->addPropertyImplementation(PIDecl);
Mike Stump12b8ce12009-08-04 21:02:39 +00002253 } else {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002254 if (Synthesize)
Mike Stump11289f42009-09-09 15:08:12 +00002255 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002256 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump11289f42009-09-09 15:08:12 +00002257 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2258 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002259 << PropertyIvar;
2260 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2261 }
Mike Stump11289f42009-09-09 15:08:12 +00002262
2263 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002264 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002265 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2266 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +00002267 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002268 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002269 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002270 }
Mike Stump11289f42009-09-09 15:08:12 +00002271
Chris Lattner83f095c2009-03-28 19:18:32 +00002272 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002273}
Anders Carlssona6b508a2008-11-04 16:57:32 +00002274
Chris Lattner438e5012008-12-17 07:13:27 +00002275bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00002276 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlssona6b508a2008-11-04 16:57:32 +00002277 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002278
Anders Carlssona6b508a2008-11-04 16:57:32 +00002279 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2280 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00002281
Anders Carlssona6b508a2008-11-04 16:57:32 +00002282 return true;
2283}
Chris Lattner438e5012008-12-17 07:13:27 +00002284
Chris Lattner438e5012008-12-17 07:13:27 +00002285/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2286/// instance variables of ClassName into Decls.
Mike Stump11289f42009-09-09 15:08:12 +00002287void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00002288 IdentifierInfo *ClassName,
Chris Lattner83f095c2009-03-28 19:18:32 +00002289 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00002290 // Check that ClassName is a valid class
2291 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2292 if (!Class) {
2293 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2294 return;
2295 }
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00002296 if (LangOpts.ObjCNonFragileABI) {
2297 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2298 return;
2299 }
Mike Stump11289f42009-09-09 15:08:12 +00002300
Chris Lattner438e5012008-12-17 07:13:27 +00002301 // Collect the instance variables
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00002302 llvm::SmallVector<FieldDecl*, 32> RecFields;
2303 Context.CollectObjCIvars(Class, RecFields);
2304 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2305 for (unsigned i = 0; i < RecFields.size(); i++) {
2306 FieldDecl* ID = RecFields[i];
2307 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2308 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2309 ID->getIdentifier(), ID->getType(),
2310 ID->getBitWidth());
2311 Decls.push_back(Sema::DeclPtrTy::make(FD));
2312 }
Mike Stump11289f42009-09-09 15:08:12 +00002313
Chris Lattner438e5012008-12-17 07:13:27 +00002314 // Introduce all of these fields into the appropriate scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002315 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00002316 D != Decls.end(); ++D) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002317 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner438e5012008-12-17 07:13:27 +00002318 if (getLangOptions().CPlusPlus)
2319 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner83f095c2009-03-28 19:18:32 +00002320 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002321 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00002322 }
2323}
2324