blob: 7da37affdc1e6cf709753dec09a0eba8c32abecb [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000015#include "clang/Sema/ExternalSemaSource.h"
Steve Naroffca331292009-03-03 14:49:36 +000016#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000020using namespace clang;
21
Fariborz Jahanianc001e892009-05-08 20:20:55 +000022bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
23 ObjCMethodDecl *GetterMethod,
24 SourceLocation Loc) {
25 if (GetterMethod &&
26 GetterMethod->getResultType() != property->getType()) {
27 AssignConvertType result = Incompatible;
Steve Narofff4954562009-07-16 15:41:00 +000028 if (property->getType()->isObjCObjectPointerType())
Fariborz Jahanian7aaa4092009-05-08 21:10:00 +000029 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
Fariborz Jahanianc001e892009-05-08 20:20:55 +000030 if (result != Compatible) {
Mike Stump1eb44332009-09-09 15:08:12 +000031 Diag(Loc, diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianc001e892009-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 Naroffebf64432009-02-28 16:59:13 +000041/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000042/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000043void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000044 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000045 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump1eb44332009-09-09 15:08:12 +000046
Steve Naroff394f3f42008-07-25 17:57:26 +000047 // If we don't have a valid method decl, simply return.
48 if (!MDecl)
49 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000050
Chris Lattner38c5ebd2009-04-19 05:21:20 +000051 CurFunctionNeedsScopeChecking = false;
52
Steve Naroffa56f6162007-12-18 01:30:32 +000053 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000054 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000055 AddInstanceMethodToGlobalPool(MDecl);
56 else
57 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000058
Chris Lattner4d391482007-12-12 07:09:47 +000059 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000060 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000061
62 // Create Decl objects for each parameter, entrring them in the scope for
63 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000064
65 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000066 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +000067
Daniel Dunbar451318c2008-08-26 06:07:48 +000068 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
69 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000070
Chris Lattner8123a952008-04-10 02:22:51 +000071 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000072 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
73 E = MDecl->param_end(); PI != E; ++PI)
74 if ((*PI)->getIdentifier())
75 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000076}
77
Chris Lattnerb28317a2009-03-28 19:18:32 +000078Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000079ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
80 IdentifierInfo *ClassName, SourceLocation ClassLoc,
81 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000082 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000083 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000084 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000085
Chris Lattner4d391482007-12-12 07:09:47 +000086 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +000087 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000088 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000089 // Maybe we will complain about the shadowed template parameter.
90 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
91 // Just pretend that we didn't see the previous declaration.
92 PrevDecl = 0;
93 }
94
Ted Kremeneka526c5c2008-01-07 19:49:32 +000095 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000096 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000097 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000098 }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000100 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000101 if (IDecl) {
102 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000103 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000104 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000105 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000106 Diag(IDecl->getLocation(), diag::note_previous_definition);
107
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000108 // Return the previous class interface.
109 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000110 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000111 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000112 IDecl->setLocation(AtInterfaceLoc);
113 IDecl->setForwardDecl(false);
Steve Naroff8b26cbd2009-09-11 00:12:01 +0000114 IDecl->setClassLoc(ClassLoc);
Ted Kremenekc32b1d82009-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 Jahanian5f8f8572009-11-17 19:08:08 +0000121 if (AttrList)
122 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000123 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000124 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000125 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000126 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000127 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000128 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Steve Naroffa7503a72009-04-23 15:15:40 +0000130 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000131 }
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Chris Lattner4d391482007-12-12 07:09:47 +0000133 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000134 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000135 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
Fariborz Jahanianfdee0892009-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 Stumpac5fc7c2009-08-04 21:02:39 +0000140 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000141 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000142 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000143
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000144 // Diagnose classes that inherit from deprecated classes.
145 if (SuperClassDecl)
146 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Fariborz Jahanianfdee0892009-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 McCall183700f2009-09-21 23:43:11 +0000154 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000155 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
156 }
157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Fariborz Jahanianfdee0892009-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 Naroff818cb9e2009-02-04 17:14:05 +0000167 }
168 }
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Fariborz Jahanianfdee0892009-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 Naroff818cb9e2009-02-04 17:14:05 +0000178 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000179 IDecl->setSuperClass(SuperClassDecl);
180 IDecl->setSuperClassLoc(SuperLoc);
181 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000182 }
Chris Lattner4d391482007-12-12 07:09:47 +0000183 } else { // we have a root class.
184 IDecl->setLocEnd(ClassLoc);
185 }
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000187 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000188 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000189 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
190 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000191 IDecl->setLocEnd(EndProtoLoc);
192 }
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Anders Carlsson15281452008-11-04 16:57:32 +0000194 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000195 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000196}
197
198/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000199/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000200Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000201 IdentifierInfo *AliasName,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000202 SourceLocation AliasLocation,
203 IdentifierInfo *ClassName,
204 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000205 // Look for previous declaration of alias name
John McCallf36e02d2009-10-09 21:13:30 +0000206 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000207 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000208 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000209 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000210 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000211 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000212 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000213 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000214 }
215 // Check for class declaration
John McCallf36e02d2009-10-09 21:13:30 +0000216 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-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 McCall183700f2009-09-21 23:43:11 +0000220 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000221 ClassName = IDecl->getIdentifier();
John McCallf36e02d2009-10-09 21:13:30 +0000222 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000223 }
224 }
225 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000226 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
227 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000228 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000229 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000230 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000231 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000232 }
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000234 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000235 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000236 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Anders Carlsson15281452008-11-04 16:57:32 +0000238 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000239 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000240
Chris Lattnerb28317a2009-03-28 19:18:32 +0000241 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000242}
243
Steve Naroff61d68522009-03-05 15:22:01 +0000244void Sema::CheckForwardProtocolDeclarationForCircularDependency(
245 IdentifierInfo *PName,
246 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000247 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000248 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
249 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Douglas Gregor6e378de2009-04-23 23:18:26 +0000251 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-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 Stump1eb44332009-09-09 15:08:12 +0000256 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000257 PDecl->getLocation(), PDecl->getReferencedProtocols());
258 }
259 }
260}
261
Chris Lattnerb28317a2009-03-28 19:18:32 +0000262Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000263Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
264 IdentifierInfo *ProtocolName,
265 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000266 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000267 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000268 SourceLocation EndProtoLoc,
269 AttributeList *AttrList) {
270 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000271 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000272 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000273 if (PDecl) {
274 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000275 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000276 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000277 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000278 // Just return the protocol we already had.
279 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000280 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000281 }
Steve Naroff61d68522009-03-05 15:22:01 +0000282 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000283 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000284 CheckForwardProtocolDeclarationForCircularDependency(
285 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
286 PList.Destroy(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Steve Narofff11b5082008-08-13 16:39:22 +0000288 // Make sure the cached decl gets a valid start location.
289 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000290 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000291 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000292 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000293 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000294 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000295 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000296 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000297 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000298 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000299 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000300 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000301 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000302 PDecl->setLocEnd(EndProtoLoc);
303 }
Mike Stump1eb44332009-09-09 15:08:12 +0000304
305 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000306 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000307}
308
309/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-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 Lattner4d391482007-12-12 07:09:47 +0000312void
Chris Lattnere13b9592008-07-26 04:03:38 +0000313Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000314 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000315 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000316 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000317 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000318 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000319 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000320 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000321 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000322 continue;
323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000325 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-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 Lattnerfa25bbb2008-11-19 05:08:23 +0000330 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000331 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000332 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000333 }
334}
335
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000336/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000337/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000338///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000339void
Mike Stump1eb44332009-09-09 15:08:12 +0000340Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000341 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000342 const IdentifierInfo *inheritedName) {
Mike Stump1eb44332009-09-09 15:08:12 +0000343 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000344 Property->getPropertyAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000345 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000346 SuperProperty->getPropertyAttributes();
347 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
348 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000349 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000350 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000351 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
352 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000353 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000354 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000355 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
356 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000357 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000358 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000360 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
361 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000362 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000363 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000364 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000365 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump1eb44332009-09-09 15:08:12 +0000366 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000367 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000368 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000369 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000370
Mike Stump1eb44332009-09-09 15:08:12 +0000371 QualType LHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000372 Context.getCanonicalType(SuperProperty->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000373 QualType RHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000374 Context.getCanonicalType(Property->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Steve Naroff15edf0d2009-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 Naroff4084c302009-07-23 01:01:38 +0000379 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff15edf0d2009-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 Jahanianb5e02242008-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 Lattner70f19542009-02-16 21:26:43 +0000390void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000391 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
392 if (!SDecl)
393 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000394 // FIXME: O(N^2)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000395 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
396 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000397 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000398 // Does property in super class has declaration in current class?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000399 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
400 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000401 ObjCPropertyDecl *PDecl = (*I);
402 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump1eb44332009-09-09 15:08:12 +0000403 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000404 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000405 }
406 }
407}
408
Fariborz Jahanianaebf0cb2008-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 Jahanian1ac2bc42008-12-06 23:03:39 +0000411/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000412void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000413Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000414 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-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 Kyrtzidis17945a02009-06-30 02:36:12 +0000420 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
421 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000422 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000423 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000424 // Is this property already in category's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000425 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000426 if ((*CP)->getIdentifier() == Pr->getIdentifier())
427 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000428 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000429 // Property protocol already exist in class. Diagnose any mismatch.
430 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
431 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000432 return;
433 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000434 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
435 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000436 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000437 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000438 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000439 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000440 if ((*CP)->getIdentifier() == Pr->getIdentifier())
441 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000442 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000443 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000444 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000445 }
Fariborz Jahanianaebf0cb2008-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 Jahanian1ac2bc42008-12-06 23:03:39 +0000450/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000451///
Chris Lattner70f19542009-02-16 21:26:43 +0000452void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000453 DeclPtrTy MergeItsProtocols) {
454 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-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 Stump1eb44332009-09-09 15:08:12 +0000466
Fariborz Jahanian1ac2bc42008-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 Lattnerb28317a2009-03-28 19:18:32 +0000471 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-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 Lattnerb28317a2009-03-28 19:18:32 +0000476 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000477 }
478 return;
479 }
480
Chris Lattnerb752f282008-07-21 07:06:49 +0000481 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000482 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
483 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000484 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000485 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Fariborz Jahanianaebf0cb2008-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 Lattnerb28317a2009-03-28 19:18:32 +0000491 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000492 } else {
Argyrios Kyrtzidise8f0d302008-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 Lattnerb28317a2009-03-28 19:18:32 +0000496 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000497 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000498}
499
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000500/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000501/// a class method in its extension.
502///
Mike Stump1eb44332009-09-09 15:08:12 +0000503void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-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 Kyrtzidis17945a02009-06-30 02:36:12 +0000509 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
510 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000511 ObjCMethodDecl *MD = *i;
512 MethodMap[MD->getSelector()] = MD;
513 }
514
515 if (MethodMap.empty())
516 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000517 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
518 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-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 Lattner58fe03b2009-04-12 08:43:13 +0000529/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000530Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000531Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000532 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000533 unsigned NumElts,
534 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000535 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chris Lattner4d391482007-12-12 07:09:47 +0000537 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000538 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000539 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000540 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000541 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000542 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000543 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000544 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000545 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000546 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000547 Protocols.push_back(PDecl);
548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
550 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000551 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000552 &Protocols[0], Protocols.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000553 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000554 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000555 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000556}
557
Chris Lattnerb28317a2009-03-28 19:18:32 +0000558Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000559ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
560 IdentifierInfo *ClassName, SourceLocation ClassLoc,
561 IdentifierInfo *CategoryName,
562 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000563 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000564 unsigned NumProtoRefs,
565 SourceLocation EndProtoLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000566 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000567 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
568 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000569 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000570
571 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000572 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000573 if (!IDecl || IDecl->isForwardDecl()) {
574 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000575 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000576 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000577 }
Chris Lattner4d391482007-12-12 07:09:47 +0000578
Chris Lattner70f19542009-02-16 21:26:43 +0000579 CDecl->setClassInterface(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Chris Lattner16b34b42009-02-16 21:30:01 +0000581 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000582 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-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 Lattner4d391482007-12-12 07:09:47 +0000598 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000599 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
600 Context);
601 CDecl->setLocEnd(EndProtoLoc);
Fariborz Jahanian339798e2009-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 Lattner4d391482007-12-12 07:09:47 +0000606 }
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Anders Carlsson15281452008-11-04 16:57:32 +0000608 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000609 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000610}
611
612/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000613/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000614/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000615Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000616 SourceLocation AtCatImplLoc,
617 IdentifierInfo *ClassName, SourceLocation ClassLoc,
618 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000619 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Argyrios Kyrtzidis8a1d7222009-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 Stump1eb44332009-09-09 15:08:12 +0000633 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000634 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
635 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000636 /// Check that class of this category is already completely declared.
637 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000638 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000639
Douglas Gregord0434102009-01-09 00:49:46 +0000640 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000641 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000642
Argyrios Kyrtzidis8a1d7222009-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 Stump1eb44332009-09-09 15:08:12 +0000653
Anders Carlsson15281452008-11-04 16:57:32 +0000654 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000655 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000656}
657
Chris Lattnerb28317a2009-03-28 19:18:32 +0000658Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000659 SourceLocation AtClassImplLoc,
660 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000661 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000662 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000663 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000664 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000665 NamedDecl *PrevDecl
666 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000667 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000668 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000669 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000670 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000671 // Is there an interface declaration of this class; if not, warn!
Mike Stump1eb44332009-09-09 15:08:12 +0000672 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000673 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000674 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000675 IDecl = 0;
676 }
Chris Lattner4d391482007-12-12 07:09:47 +0000677 }
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Chris Lattner4d391482007-12-12 07:09:47 +0000679 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000680 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000681 if (SuperClassname) {
682 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000683 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000684 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000685 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
686 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000687 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000688 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000689 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000690 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000691 Diag(SuperClassLoc, diag::err_undef_superclass)
692 << SuperClassname << ClassName;
Chris Lattner4d391482007-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 Lattner3c73c412008-11-19 08:23:25 +0000696 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000697 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000698 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000699 }
700 }
701 }
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Chris Lattner4d391482007-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 Dunbarf6414922008-08-20 18:02:42 +0000706
Mike Stump390b4cc2009-05-16 07:39:55 +0000707 // FIXME: Do we support attributes on the @implementation? If so we should
708 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000709 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000710 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000711 IDecl->setSuperClass(SDecl);
712 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000713
714 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-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 Lattner4d391482007-12-12 07:09:47 +0000720 }
Mike Stump1eb44332009-09-09 15:08:12 +0000721
722 ObjCImplementationDecl* IMPDecl =
723 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000724 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Anders Carlsson15281452008-11-04 16:57:32 +0000726 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000727 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Chris Lattner4d391482007-12-12 07:09:47 +0000729 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000730 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000731 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000732 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000733 Diag(IDecl->getImplementation()->getLocation(),
734 diag::note_previous_definition);
735 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000736 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000737 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000738 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000739 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000740}
741
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000742void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
743 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000744 SourceLocation RBrace) {
745 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000746 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-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 Naroff33feeb02009-04-20 20:09:33 +0000752 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000753 IDecl->setIVarList(ivars, numIvars, Context);
754 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000755 return;
756 }
757 // If implementation has empty ivar list, just return.
758 if (numIvars == 0)
759 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Chris Lattner4d391482007-12-12 07:09:47 +0000761 assert(ivars && "missing @implementation ivars");
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Chris Lattner4d391482007-12-12 07:09:47 +0000763 // Check interface's Ivar list against those in the implementation.
764 // names and types must match.
765 //
Chris Lattner4d391482007-12-12 07:09:47 +0000766 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000767 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000768 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
769 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000770 ObjCIvarDecl* ImplIvar = ivars[j++];
771 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000772 assert (ImplIvar && "missing implementation ivar");
773 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Steve Naroffca331292009-03-03 14:49:36 +0000775 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000776 if (Context.getCanonicalType(ImplIvar->getType()) !=
777 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000778 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000779 << ImplIvar->getIdentifier()
780 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000781 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000782 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
783 Expr *ImplBitWidth = ImplIvar->getBitWidth();
784 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000785 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
786 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-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 Stump1eb44332009-09-09 15:08:12 +0000791 }
Steve Naroffca331292009-03-03 14:49:36 +0000792 // Make sure the names are identical.
793 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000794 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000795 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000796 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000797 }
798 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Chris Lattner609e4c72007-12-12 18:11:49 +0000801 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000802 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000803 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000804 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000805}
806
Steve Naroff3c2eb662008-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 Lattner08631c52008-11-23 21:45:46 +0000813 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000814}
815
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000816void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
817 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000818 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000819 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000820 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
821 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000822 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000823 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
824 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000825 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Chris Lattner3aff9192009-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 Jahanian3393f812009-11-18 18:56:09 +0000831 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
832 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
833 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
834 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000835 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000836
837 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000838 << ImpMethodDecl->getDeclName() << (*IF)->getType()
839 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000840 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000841 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000842}
843
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000844/// isPropertyReadonly - Return true if property is readonly, by searching
845/// for the property in the class and in its categories and implementations
846///
847bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000848 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000849 // by far the most common case.
850 if (!PDecl->isReadOnly())
851 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000852 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000853 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000854 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000855 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000857 // Main class has the property as 'readonly'. Must search
Mike Stump1eb44332009-09-09 15:08:12 +0000858 // through the category list to see if the property's
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000859 // attribute has been over-ridden to 'readwrite'.
860 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
861 Category; Category = Category->getNextClassCategory()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000862 // Even if property is ready only, if a category has a user defined setter,
863 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000864 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000865 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000866 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000867 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000868 if (P && !P->isReadOnly())
869 return false;
870 }
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000872 // Also, check for definition of a setter method in the implementation if
873 // all else failed.
874 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000875 if (ObjCImplementationDecl *IMD =
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000876 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000877 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000878 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000879 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000880 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000881 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000882 return false;
883 }
884 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000885 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000886 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000887 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000888 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000889 // If all fails, look at the super class.
890 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
891 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000892 return true;
893}
894
Mike Stump390b4cc2009-05-16 07:39:55 +0000895/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
896/// improve the efficiency of selector lookups and type checking by associating
897/// with each protocol / interface / category the flattened instance tables. If
898/// we used an immutable set to keep the table then it wouldn't add significant
899/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000900
Steve Naroffefe7f362008-02-08 22:06:17 +0000901/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000902/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000903void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
904 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000905 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000906 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000907 const llvm::DenseSet<Selector> &ClsMap,
908 ObjCInterfaceDecl *IDecl) {
909 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000910 ObjCInterfaceDecl *NSIDecl = 0;
911 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000912 // check to see if class implements forwardInvocation method and objects
913 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000914 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000915 // Under such conditions, which means that every method possible is
916 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000917 // found" warnings.
918 // FIXME: Use a general GetUnarySelector method for this.
919 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
920 Selector fISelector = Context.Selectors.getSelector(1, &II);
921 if (InsMap.count(fISelector))
922 // Is IDecl derived from 'NSProxy'? If so, no instance methods
923 // need be implemented in the implementation.
924 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
925 }
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000927 // If a method lookup fails locally we still need to look and see if
928 // the method was implemented by a base class or an inherited
929 // protocol. This lookup is slow, but occurs rarely in correct code
930 // and otherwise would terminate in a warning.
931
Chris Lattner4d391482007-12-12 07:09:47 +0000932 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000933 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000934 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000935 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000936 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000937 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000938 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000939 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000940 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000941 // Ugly, but necessary. Method declared in protcol might have
942 // have been synthesized due to a property declared in the class which
943 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000944 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000945 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000946 if (!MethodInClass || !MethodInClass->isSynthesized())
947 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
948 }
949 }
Chris Lattner4d391482007-12-12 07:09:47 +0000950 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000951 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000952 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000953 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000954 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000955 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
956 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000957 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000958 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000959 }
Chris Lattner780f3292008-07-21 21:32:27 +0000960 // Check on this protocols's referenced protocols, recursively.
961 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
962 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000963 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000964}
965
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000966/// MatchAllMethodDeclarations - Check methods declaraed in interface or
967/// or protocol against those declared in their implementations.
968///
969void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
970 const llvm::DenseSet<Selector> &ClsMap,
971 llvm::DenseSet<Selector> &InsMapSeen,
972 llvm::DenseSet<Selector> &ClsMapSeen,
973 ObjCImplDecl* IMPDecl,
974 ObjCContainerDecl* CDecl,
975 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +0000976 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000977 // Check and see if instance methods in class interface have been
978 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000979 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
980 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000981 if (InsMapSeen.count((*I)->getSelector()))
982 continue;
983 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000984 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000985 !InsMap.count((*I)->getSelector())) {
986 if (ImmediateClass)
987 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
988 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000989 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000990 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000991 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000992 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000993 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000994 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000995 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
996 // ImpMethodDecl may be null as in a @dynamic property.
997 if (ImpMethodDecl)
998 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
999 }
1000 }
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001002 // Check and see if class methods in class interface have been
1003 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001004 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001005 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001006 if (ClsMapSeen.count((*I)->getSelector()))
1007 continue;
1008 ClsMapSeen.insert((*I)->getSelector());
1009 if (!ClsMap.count((*I)->getSelector())) {
1010 if (ImmediateClass)
1011 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001012 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001013 ObjCMethodDecl *ImpMethodDecl =
1014 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001015 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001016 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001017 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1018 }
1019 }
1020 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1021 // Check for any implementation of a methods declared in protocol.
1022 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1023 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001024 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1025 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001026 (*PI), IncompleteImpl, false);
1027 if (I->getSuperClass())
1028 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001029 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001030 I->getSuperClass(), IncompleteImpl, false);
1031 }
1032}
1033
Mike Stump1eb44332009-09-09 15:08:12 +00001034void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1035 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001036 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001037 llvm::DenseSet<Selector> InsMap;
1038 // Check and see if instance methods in class interface have been
1039 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001040 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001041 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001042 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001044 // Check and see if properties declared in the interface have either 1)
1045 // an implementation or 2) there is a @synthesize/@dynamic implementation
1046 // of the property in the @implementation.
1047 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001048 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1049 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001050 ObjCPropertyDecl *Prop = (*P);
1051 if (Prop->isInvalidDecl())
1052 continue;
1053 ObjCPropertyImplDecl *PI = 0;
1054 // Is there a matching propery synthesize/dynamic?
Mike Stump1eb44332009-09-09 15:08:12 +00001055 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001056 I = IMPDecl->propimpl_begin(),
1057 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001058 if ((*I)->getPropertyDecl() == Prop) {
1059 PI = (*I);
1060 break;
1061 }
1062 if (PI)
1063 continue;
1064 if (!InsMap.count(Prop->getGetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001065 Diag(Prop->getLocation(),
1066 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001067 << Prop->getDeclName() << Prop->getGetterName();
1068 Diag(IMPDecl->getLocation(),
1069 diag::note_property_impl_required);
1070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001072 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001073 Diag(Prop->getLocation(),
1074 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001075 << Prop->getDeclName() << Prop->getSetterName();
1076 Diag(IMPDecl->getLocation(),
1077 diag::note_property_impl_required);
1078 }
1079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Chris Lattner4d391482007-12-12 07:09:47 +00001081 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001082 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001083 I = IMPDecl->classmeth_begin(),
1084 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001085 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001087 // Check for type conflict of methods declared in a class/protocol and
1088 // its implementation; if any.
1089 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001090 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1091 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001092 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Chris Lattner4d391482007-12-12 07:09:47 +00001094 // Check the protocol list for unimplemented methods in the @implementation
1095 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001096 // Check and see if class methods in class interface have been
1097 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Chris Lattnercddc8882009-03-01 00:56:52 +00001099 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001100 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001101 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001102 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001103 InsMap, ClsMap, I);
1104 // Check class extensions (unnamed categories)
1105 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1106 Categories; Categories = Categories->getNextClassCategory()) {
1107 if (!Categories->getIdentifier()) {
1108 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1109 break;
1110 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001111 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001112 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001113 // For extended class, unimplemented methods in its protocols will
1114 // be reported in the primary class.
1115 if (C->getIdentifier()) {
1116 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1117 E = C->protocol_end(); PI != E; ++PI)
1118 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1119 InsMap, ClsMap, C->getClassInterface());
1120 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001121 } else
1122 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001123}
1124
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001125void
1126Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1127 ObjCContainerDecl* IDecl) {
1128 // Rules apply in non-GC mode only
1129 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1130 return;
1131 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1132 E = IDecl->prop_end();
1133 I != E; ++I) {
1134 ObjCPropertyDecl *Property = (*I);
1135 unsigned Attributes = Property->getPropertyAttributes();
1136 // We only care about readwrite atomic property.
1137 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1138 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1139 continue;
1140 if (const ObjCPropertyImplDecl *PIDecl
1141 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1142 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1143 continue;
1144 ObjCMethodDecl *GetterMethod =
1145 IMPDecl->getInstanceMethod(Property->getGetterName());
1146 ObjCMethodDecl *SetterMethod =
1147 IMPDecl->getInstanceMethod(Property->getSetterName());
1148 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1149 SourceLocation MethodLoc =
1150 (GetterMethod ? GetterMethod->getLocation()
1151 : SetterMethod->getLocation());
1152 Diag(MethodLoc, diag::warn_atomic_property_rule)
1153 << Property->getIdentifier();
1154 Diag(Property->getLocation(), diag::note_property_declare);
1155 }
1156 }
1157 }
1158}
1159
Mike Stump1eb44332009-09-09 15:08:12 +00001160/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001161Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001162Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001163 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001164 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001165 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001166 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Chris Lattner4d391482007-12-12 07:09:47 +00001168 for (unsigned i = 0; i != NumElts; ++i) {
1169 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001170 NamedDecl *PrevDecl
1171 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001172 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001173 // Maybe we will complain about the shadowed template parameter.
1174 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1175 // Just pretend that we didn't see the previous declaration.
1176 PrevDecl = 0;
1177 }
1178
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001179 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001180 // GCC apparently allows the following idiom:
1181 //
1182 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1183 // @class XCElementToggler;
1184 //
Mike Stump1eb44332009-09-09 15:08:12 +00001185 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001186 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1187 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001188 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001189 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001190 } else if (TDD) {
1191 // a forward class declaration matching a typedef name of a class refers
1192 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001193 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001194 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1195 PrevDecl = OI->getDecl();
1196 }
Chris Lattner4d391482007-12-12 07:09:47 +00001197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001199 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001200 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001201 IdentList[i], IdentLocs[i], true);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001202
1203 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1204 // the current DeclContext. This prevents clients that walk DeclContext
1205 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1206 // declared later (if at all). We also take care to explicitly make
1207 // sure this declaration is visible for name lookup.
1208 PushOnScopeChains(IDecl, TUScope, false);
1209 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001210 }
1211
1212 Interfaces.push_back(IDecl);
1213 }
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Ted Kremenek321c22f2009-11-18 00:28:11 +00001215 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001216 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001217 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001218 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001219 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001220 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001221 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001222}
1223
1224
1225/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1226/// returns true, or false, accordingly.
1227/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001228bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001229 const ObjCMethodDecl *PrevMethod,
1230 bool matchBasedOnSizeAndAlignment) {
1231 QualType T1 = Context.getCanonicalType(Method->getResultType());
1232 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001234 if (T1 != T2) {
1235 // The result types are different.
1236 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001237 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001238 // Incomplete types don't have a size and alignment.
1239 if (T1->isIncompleteType() || T2->isIncompleteType())
1240 return false;
1241 // Check is based on size and alignment.
1242 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1243 return false;
1244 }
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Chris Lattner89951a82009-02-20 18:43:26 +00001246 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1247 E = Method->param_end();
1248 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Chris Lattner89951a82009-02-20 18:43:26 +00001250 for (; ParamI != E; ++ParamI, ++PrevI) {
1251 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1252 T1 = Context.getCanonicalType((*ParamI)->getType());
1253 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001254 if (T1 != T2) {
1255 // The result types are different.
1256 if (!matchBasedOnSizeAndAlignment)
1257 return false;
1258 // Incomplete types don't have a size and alignment.
1259 if (T1->isIncompleteType() || T2->isIncompleteType())
1260 return false;
1261 // Check is based on size and alignment.
1262 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1263 return false;
1264 }
Chris Lattner4d391482007-12-12 07:09:47 +00001265 }
1266 return true;
1267}
1268
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001269/// \brief Read the contents of the instance and factory method pools
1270/// for a given selector from external storage.
1271///
1272/// This routine should only be called once, when neither the instance
1273/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001274Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001275 bool isInstance) {
1276 assert(ExternalSource && "We need an external AST source");
1277 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1278 "Selector data already loaded into the instance method pool");
1279 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1280 "Selector data already loaded into the factory method pool");
1281
1282 // Read the method list from the external source.
1283 std::pair<ObjCMethodList, ObjCMethodList> Methods
1284 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001286 if (isInstance) {
1287 if (Methods.second.Method)
1288 FactoryMethodPool[Sel] = Methods.second;
1289 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001290 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001291
1292 if (Methods.first.Method)
1293 InstanceMethodPool[Sel] = Methods.first;
1294
1295 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1296}
1297
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001298void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001299 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1300 = InstanceMethodPool.find(Method->getSelector());
1301 if (Pos == InstanceMethodPool.end()) {
1302 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1303 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1304 else
1305 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1306 ObjCMethodList())).first;
1307 }
1308
1309 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001310 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001311 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001312 Entry.Method = Method;
1313 Entry.Next = 0;
1314 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001315 }
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Chris Lattnerb25df352009-03-04 05:16:45 +00001317 // We've seen a method with this name, see if we have already seen this type
1318 // signature.
1319 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1320 if (MatchTwoMethodDeclarations(Method, List->Method))
1321 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Chris Lattnerb25df352009-03-04 05:16:45 +00001323 // We have a new signature for an existing method - add it.
1324 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1325 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001326}
1327
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001328// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001329ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001330 SourceRange R,
1331 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001332 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1333 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001334 if (Pos == InstanceMethodPool.end()) {
1335 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001336 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1337 else
1338 return 0;
1339 }
1340
1341 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001342 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Steve Naroff037cda52008-09-30 14:38:43 +00001344 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001345 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1346 // This checks if the methods differ by size & alignment.
1347 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001348 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001349 }
1350 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001351 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001352 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001353 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001354 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001355 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001356 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001357 }
1358 return MethList.Method;
1359}
1360
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001361void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001362 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1363 = FactoryMethodPool.find(Method->getSelector());
1364 if (Pos == FactoryMethodPool.end()) {
1365 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1366 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1367 else
1368 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1369 ObjCMethodList())).first;
1370 }
1371
1372 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001373 if (!FirstMethod.Method) {
1374 // Haven't seen a method with this selector name yet - add it.
1375 FirstMethod.Method = Method;
1376 FirstMethod.Next = 0;
1377 } else {
1378 // We've seen a method with this name, now check the type signature(s).
1379 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001380
1381 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001382 Next = Next->Next)
1383 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Chris Lattner4d391482007-12-12 07:09:47 +00001385 if (!match) {
1386 // We have a new signature for an existing method - add it.
1387 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001388 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001389 FirstMethod.Next = OMI;
1390 }
1391 }
1392}
1393
Mike Stump1eb44332009-09-09 15:08:12 +00001394ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001395 SourceRange R) {
1396 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1397 = FactoryMethodPool.find(Sel);
1398 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001399 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001400 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1401 else
1402 return 0;
1403 }
1404
1405 ObjCMethodList &MethList = Pos->second;
1406 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001408 if (MethList.Method && MethList.Next) {
1409 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1410 // This checks if the methods differ by size & alignment.
1411 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1412 issueWarning = true;
1413 }
1414 if (issueWarning && (MethList.Method && MethList.Next)) {
1415 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1416 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1417 << MethList.Method->getSourceRange();
1418 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1419 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1420 << Next->Method->getSourceRange();
1421 }
1422 return MethList.Method;
1423}
1424
Mike Stump1eb44332009-09-09 15:08:12 +00001425/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff0701bbb2009-01-08 17:28:14 +00001426/// have the property type and issue diagnostics if they don't.
1427/// Also synthesize a getter/setter method if none exist (and update the
1428/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1429/// methods is the "right" thing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001430void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001431 ObjCContainerDecl *CD) {
1432 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00001433
1434 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001435 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump1eb44332009-09-09 15:08:12 +00001436 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001437 property->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001439 if (SetterMethod) {
Mike Stump1eb44332009-09-09 15:08:12 +00001440 if (Context.getCanonicalType(SetterMethod->getResultType())
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001441 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001442 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001443 if (SetterMethod->param_size() != 1 ||
1444 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001445 Diag(property->getLocation(),
1446 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001447 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001448 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001449 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1450 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001451 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001452
1453 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001454 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001455 // FIXME: The synthesized property we set here is misleading. We almost always
1456 // synthesize these methods unless the user explicitly provided prototypes
1457 // (which is odd, but allowed). Sema should be typechecking that the
1458 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001459 if (!GetterMethod) {
1460 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001461 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001462 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001463 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1464 property->getLocation(), property->getGetterName(),
1465 property->getType(), CD, true, false, true,
1466 (property->getPropertyImplementation() ==
1467 ObjCPropertyDecl::Optional) ?
1468 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001469 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001470 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001471 } else
1472 // A user declared getter will be synthesize when @synthesize of
1473 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001474 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001475 property->setGetterMethodDecl(GetterMethod);
1476
1477 // Skip setter if property is read-only.
1478 if (!property->isReadOnly()) {
1479 // Find the default setter and if one not found, add one.
1480 if (!SetterMethod) {
1481 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001482 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001483 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001484 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1485 property->getLocation(),
1486 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001487 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001488 (property->getPropertyImplementation() ==
1489 ObjCPropertyDecl::Optional) ?
1490 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001491 ObjCMethodDecl::Required);
1492 // Invent the arguments for the setter. We don't bother making a
1493 // nice name for the argument.
1494 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001495 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001496 property->getIdentifier(),
1497 property->getType(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001498 /*DInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001499 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001500 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001501 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001502 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001503 } else
1504 // A user declared setter will be synthesize when @synthesize of
1505 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001506 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001507 property->setSetterMethodDecl(SetterMethod);
1508 }
Mike Stump1eb44332009-09-09 15:08:12 +00001509 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001510 // handle the following, which is supported by GCC (and part of the design).
1511 //
1512 // @interface Foo
1513 // @property double bar;
1514 // @end
1515 //
1516 // void thisIsUnfortunate() {
1517 // id foo;
1518 // double bar = [foo bar];
1519 // }
1520 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001521 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001522 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001523 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001524 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001525}
1526
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001527/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1528/// identical selector names in current and its super classes and issues
1529/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001530void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1531 ObjCMethodDecl *Method,
1532 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001533 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1534 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001536 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001537 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001538 SD->lookupMethod(Method->getSelector(), IsInstance);
1539 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001540 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001541 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001542 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001543 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1544 E = Method->param_end();
1545 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1546 for (; ParamI != E; ++ParamI, ++PrevI) {
1547 // Number of parameters are the same and is guaranteed by selector match.
1548 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1549 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1550 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1551 // If type of arguement of method in this class does not match its
1552 // respective argument type in the super class method, issue warning;
1553 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001554 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001555 << T1 << T2;
1556 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1557 return;
1558 }
1559 }
1560 ID = SD;
1561 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001562}
1563
Steve Naroffa56f6162007-12-18 01:30:32 +00001564// Note: For class/category implemenations, allMethods/allProperties is
1565// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001566void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1567 DeclPtrTy *allMethods, unsigned allNum,
1568 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001569 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001570 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001571
Steve Naroffa56f6162007-12-18 01:30:32 +00001572 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1573 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001574 // should be true.
1575 if (!ClassDecl)
1576 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001577
Mike Stump1eb44332009-09-09 15:08:12 +00001578 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001579 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1580 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001581 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001582
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001583 if (!isInterfaceDeclKind && AtEndLoc.isInvalid()) {
1584 AtEndLoc = ClassDecl->getLocation();
1585 Diag(AtEndLoc, diag::warn_missing_atend);
1586 }
1587
Steve Naroff0701bbb2009-01-08 17:28:14 +00001588 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001589
1590 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1591 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1592 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1593
Chris Lattner4d391482007-12-12 07:09:47 +00001594 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001595 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001596 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001597
1598 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001599 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001600 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001601 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001602 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001603 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001604 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001605 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001606 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001607 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001608 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001609 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001610 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001611 InsMap[Method->getSelector()] = Method;
1612 /// The following allows us to typecheck messages to "id".
1613 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001614 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001615 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001616 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001617 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001618 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001619 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001620 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001621 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001622 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001623 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001624 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001625 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001626 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001627 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001628 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001629 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001630 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001631 /// The following allows us to typecheck messages to "Class".
1632 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001633 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001634 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001635 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001636 }
1637 }
1638 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001639 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001640 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001641 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001642 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001643 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001644 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001645 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001646 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001647 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001648
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001649 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001650 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001651 if (C->getIdentifier() == 0)
1652 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001653 }
Steve Naroff09c47192009-01-09 15:36:25 +00001654 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1655 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1656 // user-defined setter/getter. It also synthesizes setter/getter methods
1657 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001658 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1659 E = CDecl->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001660 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001661 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001662 CDecl->setAtEndLoc(AtEndLoc);
1663 }
1664 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001665 IC->setAtEndLoc(AtEndLoc);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001666 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001667 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001668 AtomicPropertySetterGetterRules(IC, IDecl);
1669 }
Mike Stump1eb44332009-09-09 15:08:12 +00001670 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001671 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001672 CatImplClass->setAtEndLoc(AtEndLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Chris Lattner4d391482007-12-12 07:09:47 +00001674 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001675 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001676 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001677 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001678 Categories; Categories = Categories->getNextClassCategory()) {
1679 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001680 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001681 break;
1682 }
1683 }
1684 }
1685 }
Chris Lattner682bf922009-03-29 16:50:03 +00001686 if (isInterfaceDeclKind) {
1687 // Reject invalid vardecls.
1688 for (unsigned i = 0; i != tuvNum; i++) {
1689 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1690 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1691 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001692 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001693 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001694 }
Chris Lattner682bf922009-03-29 16:50:03 +00001695 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001696 }
Chris Lattner4d391482007-12-12 07:09:47 +00001697}
1698
1699
1700/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1701/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001702static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001703CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1704 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1705 if (PQTVal & ObjCDeclSpec::DQ_In)
1706 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1707 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1708 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1709 if (PQTVal & ObjCDeclSpec::DQ_Out)
1710 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1711 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1712 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1713 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1714 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1715 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1716 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001717
1718 return ret;
1719}
1720
Chris Lattnerb28317a2009-03-28 19:18:32 +00001721Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001722 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001723 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001724 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001725 Selector Sel,
1726 // optional arguments. The number of types/arguments is obtained
1727 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001728 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001729 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001730 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1731 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001732 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001733
1734 // Make sure we can establish a context for the method.
1735 if (!ClassDecl) {
1736 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanian32844b32009-08-28 17:52:37 +00001737 FunctionLabelMap.clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001738 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001739 }
Chris Lattner4d391482007-12-12 07:09:47 +00001740 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Steve Naroffccef3712009-02-20 22:59:16 +00001742 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001743 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Steve Naroffccef3712009-02-20 22:59:16 +00001745 // Methods cannot return interface types. All ObjC objects are
1746 // passed by reference.
1747 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001748 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1749 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001750 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001751 }
1752 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001753 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001754
1755 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001756 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00001757 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001758 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001759 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001760 MethodDeclKind == tok::objc_optional ?
1761 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001762 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Chris Lattner0ed844b2008-04-04 06:12:32 +00001764 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001765
Chris Lattner7db638d2009-04-11 19:42:43 +00001766 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001767 QualType ArgType;
1768 DeclaratorInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Chris Lattnere294d3f2009-04-11 18:57:04 +00001770 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001771 ArgType = Context.getObjCIdType();
1772 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001773 } else {
John McCall58e46772009-10-23 21:48:59 +00001774 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001775 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001776 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001777 }
Mike Stump1eb44332009-09-09 15:08:12 +00001778
John McCall58e46772009-10-23 21:48:59 +00001779 ParmVarDecl* Param
1780 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1781 ArgInfo[i].Name, ArgType, DI,
1782 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001784 if (ArgType->isObjCInterfaceType()) {
1785 Diag(ArgInfo[i].NameLoc,
1786 diag::err_object_cannot_be_passed_returned_by_value)
1787 << 1 << ArgType;
1788 Param->setInvalidDecl();
1789 }
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Chris Lattner0ed844b2008-04-04 06:12:32 +00001791 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001792 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001794 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001795 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Chris Lattner0ed844b2008-04-04 06:12:32 +00001797 Params.push_back(Param);
1798 }
1799
Jay Foadbeaaccd2009-05-21 09:52:38 +00001800 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001801 ObjCMethod->setObjCDeclQualifier(
1802 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1803 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001804
1805 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001806 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001807
John McCall54abf7d2009-11-04 02:18:39 +00001808 const ObjCMethodDecl *InterfaceMD = 0;
1809
Mike Stump1eb44332009-09-09 15:08:12 +00001810 // For implementations (which can be very "coarse grain"), we add the
1811 // method now. This allows the AST to implement lookup methods that work
1812 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001813 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001814 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001815 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001816 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001817 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1818 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001819 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001820 PrevMethod = ImpDecl->getClassMethod(Sel);
1821 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001822 }
John McCall54abf7d2009-11-04 02:18:39 +00001823 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1824 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001825 if (AttrList)
1826 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001827 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001828 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001829 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001830 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1831 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001832 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001833 PrevMethod = CatImpDecl->getClassMethod(Sel);
1834 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001835 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001836 if (AttrList)
1837 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001838 }
1839 if (PrevMethod) {
1840 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001841 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001842 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001843 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001844 }
John McCall54abf7d2009-11-04 02:18:39 +00001845
1846 // If the interface declared this method, and it was deprecated there,
1847 // mark it deprecated here.
1848 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1849 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1850
Chris Lattnerb28317a2009-03-28 19:18:32 +00001851 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001852}
1853
Mike Stump1eb44332009-09-09 15:08:12 +00001854void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001855 SourceLocation Loc,
1856 unsigned &Attributes) {
1857 // FIXME: Improve the reported location.
1858
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001859 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001860 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001861 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1862 ObjCDeclSpec::DQ_PR_assign |
1863 ObjCDeclSpec::DQ_PR_copy |
1864 ObjCDeclSpec::DQ_PR_retain))) {
1865 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1866 "readwrite" :
1867 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1868 "assign" :
1869 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1870 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00001871
1872 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001873 diag::err_objc_property_attr_mutually_exclusive :
1874 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001875 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001876 }
1877
1878 // Check for copy or retain on non-object types.
1879 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001880 !PropertyTy->isObjCObjectPointerType() &&
1881 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00001882 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001883 Diag(Loc, diag::err_objc_property_requires_object)
1884 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001885 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1886 }
1887
1888 // Check for more than one of { assign, copy, retain }.
1889 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1890 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001891 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1892 << "assign" << "copy";
1893 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00001894 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001895 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001896 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1897 << "assign" << "retain";
1898 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001899 }
1900 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1901 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001902 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1903 << "copy" << "retain";
1904 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001905 }
1906 }
1907
1908 // Warn if user supplied no assignment attribute, property is
1909 // readwrite, and this is an object type.
1910 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1911 ObjCDeclSpec::DQ_PR_retain)) &&
1912 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00001913 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001914 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00001915 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001916 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1917
1918 // If non-gc code warn that this is likely inappropriate.
1919 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1920 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00001921
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001922 // FIXME: Implement warning dependent on NSCopying being
1923 // implemented. See also:
1924 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1925 // (please trim this list while you are at it).
1926 }
Mike Stump046efd92009-05-07 23:06:50 +00001927
1928 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1929 && getLangOptions().getGCMode() == LangOptions::GCOnly
1930 && PropertyTy->isBlockPointerType())
1931 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001932}
1933
Mike Stump1eb44332009-09-09 15:08:12 +00001934Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001935 FieldDeclarator &FD,
1936 ObjCDeclSpec &ODS,
1937 Selector GetterSel,
1938 Selector SetterSel,
1939 DeclPtrTy ClassCategory,
1940 bool *isOverridingProperty,
1941 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001942 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001943 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1944 // default is readwrite!
1945 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00001946 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001947 // not retain or copy
1948 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001949 (isReadWrite &&
1950 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001951 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1952 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001953 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001954 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001955 // May modify Attributes.
1956 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001957 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1958 if (!CDecl->getIdentifier()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001959 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001960 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001961 if ((CCPrimary = CDecl->getClassInterface())) {
1962 // Find the property in continuation class's primary class only.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001963 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00001964 if (ObjCPropertyDecl *PIDecl =
1965 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001966 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001967 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001968 unsigned PIkind = PIDecl->getPropertyAttributes();
1969 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00001970 unsigned retainCopyNonatomic =
Fariborz Jahaniand669be52009-11-06 22:59:12 +00001971 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00001972 ObjCPropertyDecl::OBJC_PR_copy |
1973 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00001974 if ((Attributes & retainCopyNonatomic) !=
1975 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001976 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00001977 Diag(PIDecl->getLocation(), diag::note_property_declare);
1978 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001979 PIDecl->makeitReadWriteAttribute();
1980 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1981 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1982 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1983 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1984 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00001985 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001986 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001987 << CCPrimary->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00001988 Diag(PIDecl->getLocation(), diag::note_property_declare);
1989 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001990 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001991 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001992 // class's list.
1993 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001994 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001995 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001996 // No matching property found in the primary class. Just fall thru
1997 // and add property to continuation class's primary class.
1998 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001999 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00002000 Diag(CDecl->getLocation(), diag::err_continuation_class);
2001 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002002 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002003 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002004 }
Mike Stump1eb44332009-09-09 15:08:12 +00002005
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002006 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00002007 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002008 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2009 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2010 if (T->isObjCObjectPointerType()) {
2011 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00002012 if (const ObjCInterfaceType *OIT =
John McCall183700f2009-09-21 23:43:11 +00002013 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002014 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002015 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002016 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002017 LookupProtocol(&Context.Idents.get("NSCopying")))
2018 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00002019 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002020 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002021 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002022 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00002023 if (T->isObjCInterfaceType())
2024 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002025
Steve Naroff93983f82009-01-11 12:47:58 +00002026 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2027 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002028 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00002029 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00002030 FD.D.getIdentifier(), T);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002031 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002032
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002033 if (T->isArrayType() || T->isFunctionType()) {
2034 Diag(AtLoc, diag::err_property_type) << T;
2035 PDecl->setInvalidDecl();
2036 }
Mike Stump1eb44332009-09-09 15:08:12 +00002037
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002038 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00002039
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00002040 // Regardless of setter/getter attribute, we save the default getter/setter
2041 // selector names in anticipation of declaration of setter/getter methods.
2042 PDecl->setGetterName(GetterSel);
2043 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00002044
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002045 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002046 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00002047
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002048 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002049 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00002050
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002051 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002052 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00002053
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002054 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002055 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00002056
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002057 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002058 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00002059
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002060 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002061 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00002062
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002063 if (isAssign)
2064 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002066 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002067 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00002068
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00002069 if (MethodImplKind == tok::objc_required)
2070 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2071 else if (MethodImplKind == tok::objc_optional)
2072 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002073 // A case of continuation class adding a new property in the class. This
2074 // is not what it was meant for. However, gcc supports it and so should we.
2075 // Make sure setter/getters are declared here.
2076 if (CCPrimary)
2077 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002078
Chris Lattnerb28317a2009-03-28 19:18:32 +00002079 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002080}
2081
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002082/// ActOnPropertyImplDecl - This routine performs semantic checks and
2083/// builds the AST node for a property implementation declaration; declared
2084/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002085///
Mike Stump1eb44332009-09-09 15:08:12 +00002086Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002087 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002088 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002089 DeclPtrTy ClassCatImpDecl,
2090 IdentifierInfo *PropertyId,
2091 IdentifierInfo *PropertyIvar) {
2092 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002093 // Make sure we have a context for the property implementation declaration.
2094 if (!ClassImpDecl) {
2095 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002096 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002097 }
2098 ObjCPropertyDecl *property = 0;
2099 ObjCInterfaceDecl* IDecl = 0;
2100 // Find the class or category class where this property must have
2101 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002102 ObjCImplementationDecl *IC = 0;
2103 ObjCCategoryImplDecl* CatImplClass = 0;
2104 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002105 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002106 // We always synthesize an interface for an implementation
2107 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002108 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002109 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002110
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002111 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002112 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002113 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002114 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002115 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002116 }
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002117 if (const ObjCCategoryDecl *CD =
2118 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
2119 if (CD->getIdentifier()) {
2120 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002121 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002122 return DeclPtrTy();
2123 }
2124 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002125 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002126 if (Synthesize) {
2127 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002128 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002129 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002130 IDecl = CatImplClass->getClassInterface();
2131 if (!IDecl) {
2132 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002133 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002134 }
Mike Stump1eb44332009-09-09 15:08:12 +00002135 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002136 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002137
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002138 // If category for this implementation not found, it is an error which
2139 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002140 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002141 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002142 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002143 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002144 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002145 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002146 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002147 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002148 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002149 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002150 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002151 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002152 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002153 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002154 // Check that we have a valid, previously declared ivar for @synthesize
2155 if (Synthesize) {
2156 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002157 if (!PropertyIvar)
2158 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002159 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002160 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002161 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002162 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002163 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002164 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002165 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002166 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002167 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2168 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002169 ObjCIvarDecl::Public,
2170 (Expr *)0);
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002171 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002172 IDecl->addDecl(Ivar);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002173 property->setPropertyIvarDecl(Ivar);
2174 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002175 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002176 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002177 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002178 } else if (getLangOptions().ObjCNonFragileABI &&
2179 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002180 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002181 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002182 << ClassDeclared->getDeclName();
2183 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2184 << Ivar << Ivar->getNameAsCString();
2185 // Note! I deliberately want it to fall thru so more errors are caught.
2186 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002187 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002188
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002189 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002190 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002191 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002192 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002193 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002194 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002195 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002196 }
Mike Stump1eb44332009-09-09 15:08:12 +00002197
Chris Lattnerb28317a2009-03-28 19:18:32 +00002198 // FIXME! Rules for properties are somewhat different that those
2199 // for assignments. Use a new routine to consolidate all cases;
2200 // specifically for property redeclarations as well as for ivars.
2201 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2202 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002203 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002204 lhsType->isArithmeticType()) {
2205 Diag(PropertyLoc, diag::error_property_ivar_type)
2206 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002207 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002208 }
2209 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002210 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2211 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002212 Diag(PropertyLoc, diag::error_weak_property)
2213 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002214 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002215 }
Mike Stump1eb44332009-09-09 15:08:12 +00002216 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002217 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2218 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002219 Diag(PropertyLoc, diag::error_strong_property)
2220 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002221 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002222 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002223 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002224 } else if (PropertyIvar)
2225 // @dynamic
2226 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002227 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002228 ObjCPropertyImplDecl *PIDecl =
2229 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2230 property,
2231 (Synthesize ?
2232 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002233 : ObjCPropertyImplDecl::Dynamic),
2234 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002235 if (IC) {
2236 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002237 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002238 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002239 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2240 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002241 << PropertyIvar;
2242 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2243 }
Mike Stump1eb44332009-09-09 15:08:12 +00002244
2245 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002246 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002247 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2248 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002249 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002250 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002251 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002252 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002253 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002254 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002255 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002256 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2257 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002258 << PropertyIvar;
2259 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2260 }
Mike Stump1eb44332009-09-09 15:08:12 +00002261
2262 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002263 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002264 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2265 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002266 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002267 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002268 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002269 }
Mike Stump1eb44332009-09-09 15:08:12 +00002270
Chris Lattnerb28317a2009-03-28 19:18:32 +00002271 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002272}
Anders Carlsson15281452008-11-04 16:57:32 +00002273
Chris Lattnercc98eac2008-12-17 07:13:27 +00002274bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002275 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002276 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002277
Anders Carlsson15281452008-11-04 16:57:32 +00002278 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2279 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002280
Anders Carlsson15281452008-11-04 16:57:32 +00002281 return true;
2282}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002283
Chris Lattnercc98eac2008-12-17 07:13:27 +00002284/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2285/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002286void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002287 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002288 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002289 // Check that ClassName is a valid class
2290 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2291 if (!Class) {
2292 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2293 return;
2294 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002295 if (LangOpts.ObjCNonFragileABI) {
2296 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2297 return;
2298 }
Mike Stump1eb44332009-09-09 15:08:12 +00002299
Chris Lattnercc98eac2008-12-17 07:13:27 +00002300 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002301 llvm::SmallVector<FieldDecl*, 32> RecFields;
2302 Context.CollectObjCIvars(Class, RecFields);
2303 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2304 for (unsigned i = 0; i < RecFields.size(); i++) {
2305 FieldDecl* ID = RecFields[i];
2306 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2307 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2308 ID->getIdentifier(), ID->getType(),
2309 ID->getBitWidth());
2310 Decls.push_back(Sema::DeclPtrTy::make(FD));
2311 }
Mike Stump1eb44332009-09-09 15:08:12 +00002312
Chris Lattnercc98eac2008-12-17 07:13:27 +00002313 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002314 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002315 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002316 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002317 if (getLangOptions().CPlusPlus)
2318 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002319 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002320 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002321 }
2322}
2323