blob: a5fdb8725c865d6067f6d1f62086c8e57ba82696 [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);
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000115 if (AttrList)
116 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000117 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000118 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000119 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000120 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000121 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000122 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Steve Naroffa7503a72009-04-23 15:15:40 +0000124 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000125 }
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Chris Lattner4d391482007-12-12 07:09:47 +0000127 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000128 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000129 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000130 if (PrevDecl == IDecl) {
131 Diag(SuperLoc, diag::err_recursive_superclass)
132 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
133 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000134 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000135 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000136 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000137
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000138 // Diagnose classes that inherit from deprecated classes.
139 if (SuperClassDecl)
140 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000142 if (PrevDecl && SuperClassDecl == 0) {
143 // The previous declaration was not a class decl. Check if we have a
144 // typedef. If we do, get the underlying class type.
145 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
146 QualType T = TDecl->getUnderlyingType();
147 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000148 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000149 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
150 }
151 }
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000153 // This handles the following case:
154 //
155 // typedef int SuperClass;
156 // @interface MyClass : SuperClass {} @end
157 //
158 if (!SuperClassDecl) {
159 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
160 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000161 }
162 }
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000164 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
165 if (!SuperClassDecl)
166 Diag(SuperLoc, diag::err_undef_superclass)
167 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
168 else if (SuperClassDecl->isForwardDecl())
169 Diag(SuperLoc, diag::err_undef_superclass)
170 << SuperClassDecl->getDeclName() << ClassName
171 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000172 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000173 IDecl->setSuperClass(SuperClassDecl);
174 IDecl->setSuperClassLoc(SuperLoc);
175 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000176 }
Chris Lattner4d391482007-12-12 07:09:47 +0000177 } else { // we have a root class.
178 IDecl->setLocEnd(ClassLoc);
179 }
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000181 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000182 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000183 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
184 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000185 IDecl->setLocEnd(EndProtoLoc);
186 }
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Anders Carlsson15281452008-11-04 16:57:32 +0000188 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000189 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000190}
191
192/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000193/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000194Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000195 IdentifierInfo *AliasName,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000196 SourceLocation AliasLocation,
197 IdentifierInfo *ClassName,
198 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000199 // Look for previous declaration of alias name
John McCallf36e02d2009-10-09 21:13:30 +0000200 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000201 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000202 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000203 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000204 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000205 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000206 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000207 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000208 }
209 // Check for class declaration
John McCallf36e02d2009-10-09 21:13:30 +0000210 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000211 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
212 QualType T = TDecl->getUnderlyingType();
213 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000214 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000215 ClassName = IDecl->getIdentifier();
John McCallf36e02d2009-10-09 21:13:30 +0000216 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000217 }
218 }
219 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000220 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
221 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000222 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000223 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000224 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000225 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000226 }
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000228 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000229 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000230 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Anders Carlsson15281452008-11-04 16:57:32 +0000232 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000233 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000234
Chris Lattnerb28317a2009-03-28 19:18:32 +0000235 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000236}
237
Steve Naroff61d68522009-03-05 15:22:01 +0000238void Sema::CheckForwardProtocolDeclarationForCircularDependency(
239 IdentifierInfo *PName,
240 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000241 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000242 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
243 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Douglas Gregor6e378de2009-04-23 23:18:26 +0000245 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000246 if (PDecl->getIdentifier() == PName) {
247 Diag(Ploc, diag::err_protocol_has_circular_dependency);
248 Diag(PrevLoc, diag::note_previous_definition);
249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000251 PDecl->getLocation(), PDecl->getReferencedProtocols());
252 }
253 }
254}
255
Chris Lattnerb28317a2009-03-28 19:18:32 +0000256Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000257Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
258 IdentifierInfo *ProtocolName,
259 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000260 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000261 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000262 SourceLocation EndProtoLoc,
263 AttributeList *AttrList) {
264 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000265 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000266 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000267 if (PDecl) {
268 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000269 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000270 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000271 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000272 // Just return the protocol we already had.
273 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000274 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000275 }
Steve Naroff61d68522009-03-05 15:22:01 +0000276 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000277 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000278 CheckForwardProtocolDeclarationForCircularDependency(
279 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
280 PList.Destroy(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Steve Narofff11b5082008-08-13 16:39:22 +0000282 // Make sure the cached decl gets a valid start location.
283 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000284 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000285 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000286 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000287 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000288 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000289 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000290 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000291 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000292 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000293 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000294 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000295 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000296 PDecl->setLocEnd(EndProtoLoc);
297 }
Mike Stump1eb44332009-09-09 15:08:12 +0000298
299 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000300 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000301}
302
303/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000304/// issues an error if they are not declared. It returns list of
305/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000306void
Chris Lattnere13b9592008-07-26 04:03:38 +0000307Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000308 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000309 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000310 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000311 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000312 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000313 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000314 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000315 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000316 continue;
317 }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000319 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000320
321 // If this is a forward declaration and we are supposed to warn in this
322 // case, do it.
323 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000324 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000325 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000326 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000327 }
328}
329
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000330/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000331/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000332///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000333void
Mike Stump1eb44332009-09-09 15:08:12 +0000334Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000335 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000336 const IdentifierInfo *inheritedName) {
Mike Stump1eb44332009-09-09 15:08:12 +0000337 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000338 Property->getPropertyAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000339 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000340 SuperProperty->getPropertyAttributes();
341 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
342 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000343 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000344 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000345 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
346 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000347 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000348 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000349 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
350 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000351 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000352 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000354 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
355 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000356 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000357 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000358 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000359 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump1eb44332009-09-09 15:08:12 +0000360 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000361 if (Property->getGetterName() != SuperProperty->getGetterName())
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() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000364
Mike Stump1eb44332009-09-09 15:08:12 +0000365 QualType LHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000366 Context.getCanonicalType(SuperProperty->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000367 QualType RHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000368 Context.getCanonicalType(Property->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Steve Naroff15edf0d2009-03-03 15:43:24 +0000370 if (!Context.typesAreCompatible(LHSType, RHSType)) {
371 // FIXME: Incorporate this test with typesAreCompatible.
372 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff4084c302009-07-23 01:01:38 +0000373 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff15edf0d2009-03-03 15:43:24 +0000374 return;
375 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
376 << Property->getType() << SuperProperty->getType() << inheritedName;
377 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000378}
379
380/// ComparePropertiesInBaseAndSuper - This routine compares property
381/// declarations in base and its super class, if any, and issues
382/// diagnostics in a variety of inconsistant situations.
383///
Chris Lattner70f19542009-02-16 21:26:43 +0000384void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000385 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
386 if (!SDecl)
387 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000388 // FIXME: O(N^2)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000389 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
390 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000391 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000392 // Does property in super class has declaration in current class?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000393 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
394 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000395 ObjCPropertyDecl *PDecl = (*I);
396 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump1eb44332009-09-09 15:08:12 +0000397 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000398 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000399 }
400 }
401}
402
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000403/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
404/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000405/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000406void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000407Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000408 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000409 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
410 if (!IDecl) {
411 // Category
412 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
413 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000414 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
415 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000416 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000417 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000418 // Is this property already in category's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000419 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000420 if ((*CP)->getIdentifier() == Pr->getIdentifier())
421 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000422 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000423 // Property protocol already exist in class. Diagnose any mismatch.
424 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
425 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000426 return;
427 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000428 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
429 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000430 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000431 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000432 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000433 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000434 if ((*CP)->getIdentifier() == Pr->getIdentifier())
435 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000436 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000437 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000438 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000439 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000440}
441
442/// MergeProtocolPropertiesIntoClass - This routine merges properties
443/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000444/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000445///
Chris Lattner70f19542009-02-16 21:26:43 +0000446void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000447 DeclPtrTy MergeItsProtocols) {
448 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000449 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
450
451 if (!IDecl) {
452 // Category
453 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
454 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
455 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
456 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
457 E = MDecl->protocol_end(); P != E; ++P)
458 // Merge properties of category (*P) into IDECL's
459 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000461 // Go thru the list of protocols for this category and recursively merge
462 // their properties into this class as well.
463 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
464 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000465 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000466 } else {
467 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
468 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
469 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000470 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000471 }
472 return;
473 }
474
Chris Lattnerb752f282008-07-21 07:06:49 +0000475 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000476 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
477 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000478 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000479 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000481 // Go thru the list of protocols for this class and recursively merge
482 // their properties into this class as well.
483 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
484 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000485 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000486 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000487 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
488 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
489 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000490 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000491 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000492}
493
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000494/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000495/// a class method in its extension.
496///
Mike Stump1eb44332009-09-09 15:08:12 +0000497void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000498 ObjCInterfaceDecl *ID) {
499 if (!ID)
500 return; // Possibly due to previous error
501
502 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000503 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
504 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000505 ObjCMethodDecl *MD = *i;
506 MethodMap[MD->getSelector()] = MD;
507 }
508
509 if (MethodMap.empty())
510 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000511 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
512 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000513 ObjCMethodDecl *Method = *i;
514 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
515 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
516 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
517 << Method->getDeclName();
518 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
519 }
520 }
521}
522
Chris Lattner58fe03b2009-04-12 08:43:13 +0000523/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000524Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000525Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000526 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000527 unsigned NumElts,
528 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000529 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Chris Lattner4d391482007-12-12 07:09:47 +0000531 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000532 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000533 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000534 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000535 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000536 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000537 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000538 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000539 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000540 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000541 Protocols.push_back(PDecl);
542 }
Mike Stump1eb44332009-09-09 15:08:12 +0000543
544 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000545 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000546 &Protocols[0], Protocols.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000547 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000548 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000549 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000550}
551
Chris Lattnerb28317a2009-03-28 19:18:32 +0000552Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000553ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
554 IdentifierInfo *ClassName, SourceLocation ClassLoc,
555 IdentifierInfo *CategoryName,
556 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000557 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000558 unsigned NumProtoRefs,
559 SourceLocation EndProtoLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000560 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000561 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
562 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000563 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000564
565 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000566 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000567 if (!IDecl || IDecl->isForwardDecl()) {
568 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000569 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000570 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000571 }
Chris Lattner4d391482007-12-12 07:09:47 +0000572
Chris Lattner70f19542009-02-16 21:26:43 +0000573 CDecl->setClassInterface(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Chris Lattner16b34b42009-02-16 21:30:01 +0000575 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000576 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000577
578 /// Check for duplicate interface declaration for this category
579 ObjCCategoryDecl *CDeclChain;
580 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
581 CDeclChain = CDeclChain->getNextClassCategory()) {
582 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
583 Diag(CategoryLoc, diag::warn_dup_category_def)
584 << ClassName << CategoryName;
585 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
586 break;
587 }
588 }
589 if (!CDeclChain)
590 CDecl->insertNextClassCategory();
591
Chris Lattner4d391482007-12-12 07:09:47 +0000592 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000593 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
594 Context);
595 CDecl->setLocEnd(EndProtoLoc);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000596 // Protocols in the class extension belong to the class.
597 if (!CDecl->getIdentifier())
598 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
599 NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000600 }
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Anders Carlsson15281452008-11-04 16:57:32 +0000602 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000603 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000604}
605
606/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000607/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000608/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000609Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000610 SourceLocation AtCatImplLoc,
611 IdentifierInfo *ClassName, SourceLocation ClassLoc,
612 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000613 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000614 ObjCCategoryDecl *CatIDecl = 0;
615 if (IDecl) {
616 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
617 if (!CatIDecl) {
618 // Category @implementation with no corresponding @interface.
619 // Create and install one.
620 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
621 CatName);
622 CatIDecl->setClassInterface(IDecl);
623 CatIDecl->insertNextClassCategory();
624 }
625 }
626
Mike Stump1eb44332009-09-09 15:08:12 +0000627 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000628 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
629 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000630 /// Check that class of this category is already completely declared.
631 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000632 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000633
Douglas Gregord0434102009-01-09 00:49:46 +0000634 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000635 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000636
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000637 /// Check that CatName, category name, is not used in another implementation.
638 if (CatIDecl) {
639 if (CatIDecl->getImplementation()) {
640 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
641 << CatName;
642 Diag(CatIDecl->getImplementation()->getLocation(),
643 diag::note_previous_definition);
644 } else
645 CatIDecl->setImplementation(CDecl);
646 }
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Anders Carlsson15281452008-11-04 16:57:32 +0000648 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000649 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000650}
651
Chris Lattnerb28317a2009-03-28 19:18:32 +0000652Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000653 SourceLocation AtClassImplLoc,
654 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000655 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000656 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000657 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000658 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000659 NamedDecl *PrevDecl
660 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000661 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000662 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000663 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000664 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000665 // Is there an interface declaration of this class; if not, warn!
Mike Stump1eb44332009-09-09 15:08:12 +0000666 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000667 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000668 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000669 IDecl = 0;
670 }
Chris Lattner4d391482007-12-12 07:09:47 +0000671 }
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Chris Lattner4d391482007-12-12 07:09:47 +0000673 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000674 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000675 if (SuperClassname) {
676 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000677 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000678 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000679 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
680 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000681 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000682 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000683 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000684 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000685 Diag(SuperClassLoc, diag::err_undef_superclass)
686 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000687 else if (IDecl && IDecl->getSuperClass() != SDecl) {
688 // This implementation and its interface do not have the same
689 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000690 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000691 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000692 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000693 }
694 }
695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Chris Lattner4d391482007-12-12 07:09:47 +0000697 if (!IDecl) {
698 // Legacy case of @implementation with no corresponding @interface.
699 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000700
Mike Stump390b4cc2009-05-16 07:39:55 +0000701 // FIXME: Do we support attributes on the @implementation? If so we should
702 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000703 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000704 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000705 IDecl->setSuperClass(SDecl);
706 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000707
708 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000709 } else {
710 // Mark the interface as being completed, even if it was just as
711 // @class ....;
712 // declaration; the user cannot reopen it.
713 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000714 }
Mike Stump1eb44332009-09-09 15:08:12 +0000715
716 ObjCImplementationDecl* IMPDecl =
717 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000718 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Anders Carlsson15281452008-11-04 16:57:32 +0000720 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000721 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Chris Lattner4d391482007-12-12 07:09:47 +0000723 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000724 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000725 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000726 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000727 Diag(IDecl->getImplementation()->getLocation(),
728 diag::note_previous_definition);
729 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000730 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000731 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000732 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000733 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000734}
735
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000736void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
737 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000738 SourceLocation RBrace) {
739 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000740 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000741 if (!IDecl)
742 return;
743 /// Check case of non-existing @interface decl.
744 /// (legacy objective-c @implementation decl without an @interface decl).
745 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000746 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000747 IDecl->setIVarList(ivars, numIvars, Context);
748 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000749 return;
750 }
751 // If implementation has empty ivar list, just return.
752 if (numIvars == 0)
753 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Chris Lattner4d391482007-12-12 07:09:47 +0000755 assert(ivars && "missing @implementation ivars");
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Chris Lattner4d391482007-12-12 07:09:47 +0000757 // Check interface's Ivar list against those in the implementation.
758 // names and types must match.
759 //
Chris Lattner4d391482007-12-12 07:09:47 +0000760 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000761 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000762 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
763 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000764 ObjCIvarDecl* ImplIvar = ivars[j++];
765 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000766 assert (ImplIvar && "missing implementation ivar");
767 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Steve Naroffca331292009-03-03 14:49:36 +0000769 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000770 if (Context.getCanonicalType(ImplIvar->getType()) !=
771 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000772 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000773 << ImplIvar->getIdentifier()
774 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000775 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000776 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
777 Expr *ImplBitWidth = ImplIvar->getBitWidth();
778 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000779 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
780 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000781 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
782 << ImplIvar->getIdentifier();
783 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785 }
Steve Naroffca331292009-03-03 14:49:36 +0000786 // Make sure the names are identical.
787 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000788 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000789 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000790 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000791 }
792 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000793 }
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Chris Lattner609e4c72007-12-12 18:11:49 +0000795 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000796 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000797 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000798 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000799}
800
Steve Naroff3c2eb662008-02-10 21:38:56 +0000801void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
802 bool &IncompleteImpl) {
803 if (!IncompleteImpl) {
804 Diag(ImpLoc, diag::warn_incomplete_impl);
805 IncompleteImpl = true;
806 }
Chris Lattner08631c52008-11-23 21:45:46 +0000807 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000808}
809
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000810void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
811 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000812 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000813 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000814 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
815 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000816 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000817 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
818 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000819 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Chris Lattner3aff9192009-04-11 19:58:42 +0000822 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
823 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
824 IM != EM; ++IM, ++IF) {
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000825 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000826 Context.QualifiedIdConformsQualifiedId((*IF)->getType(),
Steve Naroff4084c302009-07-23 01:01:38 +0000827 (*IM)->getType()))
Chris Lattner3aff9192009-04-11 19:58:42 +0000828 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000829
830 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000831 << ImpMethodDecl->getDeclName() << (*IF)->getType()
832 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000833 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000834 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000835}
836
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000837/// isPropertyReadonly - Return true if property is readonly, by searching
838/// for the property in the class and in its categories and implementations
839///
840bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000841 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000842 // by far the most common case.
843 if (!PDecl->isReadOnly())
844 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000845 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000846 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000847 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000848 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000850 // Main class has the property as 'readonly'. Must search
Mike Stump1eb44332009-09-09 15:08:12 +0000851 // through the category list to see if the property's
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000852 // attribute has been over-ridden to 'readwrite'.
853 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
854 Category; Category = Category->getNextClassCategory()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000855 // Even if property is ready only, if a category has a user defined setter,
856 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000857 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000858 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000859 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000860 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000861 if (P && !P->isReadOnly())
862 return false;
863 }
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000865 // Also, check for definition of a setter method in the implementation if
866 // all else failed.
867 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000868 if (ObjCImplementationDecl *IMD =
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000869 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000870 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000871 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000872 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000873 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000874 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000875 return false;
876 }
877 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000878 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000879 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000880 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000881 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000882 // If all fails, look at the super class.
883 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
884 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000885 return true;
886}
887
Mike Stump390b4cc2009-05-16 07:39:55 +0000888/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
889/// improve the efficiency of selector lookups and type checking by associating
890/// with each protocol / interface / category the flattened instance tables. If
891/// we used an immutable set to keep the table then it wouldn't add significant
892/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000893
Steve Naroffefe7f362008-02-08 22:06:17 +0000894/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000895/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000896void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
897 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000898 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000899 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000900 const llvm::DenseSet<Selector> &ClsMap,
901 ObjCInterfaceDecl *IDecl) {
902 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000903 ObjCInterfaceDecl *NSIDecl = 0;
904 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000905 // check to see if class implements forwardInvocation method and objects
906 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000907 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000908 // Under such conditions, which means that every method possible is
909 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000910 // found" warnings.
911 // FIXME: Use a general GetUnarySelector method for this.
912 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
913 Selector fISelector = Context.Selectors.getSelector(1, &II);
914 if (InsMap.count(fISelector))
915 // Is IDecl derived from 'NSProxy'? If so, no instance methods
916 // need be implemented in the implementation.
917 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
918 }
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000920 // If a method lookup fails locally we still need to look and see if
921 // the method was implemented by a base class or an inherited
922 // protocol. This lookup is slow, but occurs rarely in correct code
923 // and otherwise would terminate in a warning.
924
Chris Lattner4d391482007-12-12 07:09:47 +0000925 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000926 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000927 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000928 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000929 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000930 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000931 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000932 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000933 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000934 // Ugly, but necessary. Method declared in protcol might have
935 // have been synthesized due to a property declared in the class which
936 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000937 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000938 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000939 if (!MethodInClass || !MethodInClass->isSynthesized())
940 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
941 }
942 }
Chris Lattner4d391482007-12-12 07:09:47 +0000943 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000944 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000945 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000946 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000947 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000948 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
949 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000950 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000951 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000952 }
Chris Lattner780f3292008-07-21 21:32:27 +0000953 // Check on this protocols's referenced protocols, recursively.
954 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
955 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000956 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000957}
958
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000959/// MatchAllMethodDeclarations - Check methods declaraed in interface or
960/// or protocol against those declared in their implementations.
961///
962void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
963 const llvm::DenseSet<Selector> &ClsMap,
964 llvm::DenseSet<Selector> &InsMapSeen,
965 llvm::DenseSet<Selector> &ClsMapSeen,
966 ObjCImplDecl* IMPDecl,
967 ObjCContainerDecl* CDecl,
968 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +0000969 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000970 // Check and see if instance methods in class interface have been
971 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000972 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
973 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000974 if (InsMapSeen.count((*I)->getSelector()))
975 continue;
976 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000977 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000978 !InsMap.count((*I)->getSelector())) {
979 if (ImmediateClass)
980 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
981 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000982 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000983 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000984 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000985 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000986 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000987 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000988 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
989 // ImpMethodDecl may be null as in a @dynamic property.
990 if (ImpMethodDecl)
991 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
992 }
993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000995 // Check and see if class methods in class interface have been
996 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +0000997 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000998 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000999 if (ClsMapSeen.count((*I)->getSelector()))
1000 continue;
1001 ClsMapSeen.insert((*I)->getSelector());
1002 if (!ClsMap.count((*I)->getSelector())) {
1003 if (ImmediateClass)
1004 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001005 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001006 ObjCMethodDecl *ImpMethodDecl =
1007 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001008 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001009 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001010 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1011 }
1012 }
1013 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1014 // Check for any implementation of a methods declared in protocol.
1015 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1016 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001017 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1018 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001019 (*PI), IncompleteImpl, false);
1020 if (I->getSuperClass())
1021 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001022 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001023 I->getSuperClass(), IncompleteImpl, false);
1024 }
1025}
1026
Mike Stump1eb44332009-09-09 15:08:12 +00001027void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1028 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001029 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001030 llvm::DenseSet<Selector> InsMap;
1031 // Check and see if instance methods in class interface have been
1032 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001033 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001034 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001035 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001037 // Check and see if properties declared in the interface have either 1)
1038 // an implementation or 2) there is a @synthesize/@dynamic implementation
1039 // of the property in the @implementation.
1040 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001041 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1042 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001043 ObjCPropertyDecl *Prop = (*P);
1044 if (Prop->isInvalidDecl())
1045 continue;
1046 ObjCPropertyImplDecl *PI = 0;
1047 // Is there a matching propery synthesize/dynamic?
Mike Stump1eb44332009-09-09 15:08:12 +00001048 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001049 I = IMPDecl->propimpl_begin(),
1050 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001051 if ((*I)->getPropertyDecl() == Prop) {
1052 PI = (*I);
1053 break;
1054 }
1055 if (PI)
1056 continue;
1057 if (!InsMap.count(Prop->getGetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001058 Diag(Prop->getLocation(),
1059 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001060 << Prop->getDeclName() << Prop->getGetterName();
1061 Diag(IMPDecl->getLocation(),
1062 diag::note_property_impl_required);
1063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001065 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001066 Diag(Prop->getLocation(),
1067 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001068 << Prop->getDeclName() << Prop->getSetterName();
1069 Diag(IMPDecl->getLocation(),
1070 diag::note_property_impl_required);
1071 }
1072 }
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Chris Lattner4d391482007-12-12 07:09:47 +00001074 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001075 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001076 I = IMPDecl->classmeth_begin(),
1077 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001078 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001080 // Check for type conflict of methods declared in a class/protocol and
1081 // its implementation; if any.
1082 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001083 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1084 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001085 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Chris Lattner4d391482007-12-12 07:09:47 +00001087 // Check the protocol list for unimplemented methods in the @implementation
1088 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001089 // Check and see if class methods in class interface have been
1090 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Chris Lattnercddc8882009-03-01 00:56:52 +00001092 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001093 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001094 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001095 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001096 InsMap, ClsMap, I);
1097 // Check class extensions (unnamed categories)
1098 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1099 Categories; Categories = Categories->getNextClassCategory()) {
1100 if (!Categories->getIdentifier()) {
1101 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1102 break;
1103 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001104 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001105 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001106 // For extended class, unimplemented methods in its protocols will
1107 // be reported in the primary class.
1108 if (C->getIdentifier()) {
1109 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1110 E = C->protocol_end(); PI != E; ++PI)
1111 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1112 InsMap, ClsMap, C->getClassInterface());
1113 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001114 } else
1115 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001116}
1117
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001118void
1119Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1120 ObjCContainerDecl* IDecl) {
1121 // Rules apply in non-GC mode only
1122 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1123 return;
1124 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1125 E = IDecl->prop_end();
1126 I != E; ++I) {
1127 ObjCPropertyDecl *Property = (*I);
1128 unsigned Attributes = Property->getPropertyAttributes();
1129 // We only care about readwrite atomic property.
1130 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1131 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1132 continue;
1133 if (const ObjCPropertyImplDecl *PIDecl
1134 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1135 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1136 continue;
1137 ObjCMethodDecl *GetterMethod =
1138 IMPDecl->getInstanceMethod(Property->getGetterName());
1139 ObjCMethodDecl *SetterMethod =
1140 IMPDecl->getInstanceMethod(Property->getSetterName());
1141 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1142 SourceLocation MethodLoc =
1143 (GetterMethod ? GetterMethod->getLocation()
1144 : SetterMethod->getLocation());
1145 Diag(MethodLoc, diag::warn_atomic_property_rule)
1146 << Property->getIdentifier();
1147 Diag(Property->getLocation(), diag::note_property_declare);
1148 }
1149 }
1150 }
1151}
1152
Mike Stump1eb44332009-09-09 15:08:12 +00001153/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001154Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001155Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001156 IdentifierInfo **IdentList,
1157 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001158 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Chris Lattner4d391482007-12-12 07:09:47 +00001160 for (unsigned i = 0; i != NumElts; ++i) {
1161 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001162 NamedDecl *PrevDecl
1163 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001164 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001165 // Maybe we will complain about the shadowed template parameter.
1166 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1167 // Just pretend that we didn't see the previous declaration.
1168 PrevDecl = 0;
1169 }
1170
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001171 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001172 // GCC apparently allows the following idiom:
1173 //
1174 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1175 // @class XCElementToggler;
1176 //
Mike Stump1eb44332009-09-09 15:08:12 +00001177 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001178 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1179 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001180 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001181 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001182 } else if (TDD) {
1183 // a forward class declaration matching a typedef name of a class refers
1184 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001185 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001186 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1187 PrevDecl = OI->getDecl();
1188 }
Chris Lattner4d391482007-12-12 07:09:47 +00001189 }
Mike Stump1eb44332009-09-09 15:08:12 +00001190 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001191 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001192 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Steve Naroff8b26cbd2009-09-11 00:12:01 +00001193 IdentList[i],
1194 // FIXME: need to get the 'real'
1195 // identifier loc from the parser.
1196 AtClassLoc, true);
Steve Naroff8f06f842009-04-23 16:00:56 +00001197 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +00001198 }
1199
1200 Interfaces.push_back(IDecl);
1201 }
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Douglas Gregord0434102009-01-09 00:49:46 +00001203 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001204 &Interfaces[0],
1205 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001206 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001207 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001208 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001209}
1210
1211
1212/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1213/// returns true, or false, accordingly.
1214/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001215bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001216 const ObjCMethodDecl *PrevMethod,
1217 bool matchBasedOnSizeAndAlignment) {
1218 QualType T1 = Context.getCanonicalType(Method->getResultType());
1219 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001221 if (T1 != T2) {
1222 // The result types are different.
1223 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001224 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001225 // Incomplete types don't have a size and alignment.
1226 if (T1->isIncompleteType() || T2->isIncompleteType())
1227 return false;
1228 // Check is based on size and alignment.
1229 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1230 return false;
1231 }
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Chris Lattner89951a82009-02-20 18:43:26 +00001233 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1234 E = Method->param_end();
1235 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Chris Lattner89951a82009-02-20 18:43:26 +00001237 for (; ParamI != E; ++ParamI, ++PrevI) {
1238 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1239 T1 = Context.getCanonicalType((*ParamI)->getType());
1240 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001241 if (T1 != T2) {
1242 // The result types are different.
1243 if (!matchBasedOnSizeAndAlignment)
1244 return false;
1245 // Incomplete types don't have a size and alignment.
1246 if (T1->isIncompleteType() || T2->isIncompleteType())
1247 return false;
1248 // Check is based on size and alignment.
1249 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1250 return false;
1251 }
Chris Lattner4d391482007-12-12 07:09:47 +00001252 }
1253 return true;
1254}
1255
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001256/// \brief Read the contents of the instance and factory method pools
1257/// for a given selector from external storage.
1258///
1259/// This routine should only be called once, when neither the instance
1260/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001261Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001262 bool isInstance) {
1263 assert(ExternalSource && "We need an external AST source");
1264 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1265 "Selector data already loaded into the instance method pool");
1266 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1267 "Selector data already loaded into the factory method pool");
1268
1269 // Read the method list from the external source.
1270 std::pair<ObjCMethodList, ObjCMethodList> Methods
1271 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001273 if (isInstance) {
1274 if (Methods.second.Method)
1275 FactoryMethodPool[Sel] = Methods.second;
1276 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001277 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001278
1279 if (Methods.first.Method)
1280 InstanceMethodPool[Sel] = Methods.first;
1281
1282 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1283}
1284
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001285void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001286 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1287 = InstanceMethodPool.find(Method->getSelector());
1288 if (Pos == InstanceMethodPool.end()) {
1289 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1290 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1291 else
1292 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1293 ObjCMethodList())).first;
1294 }
1295
1296 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001297 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001298 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001299 Entry.Method = Method;
1300 Entry.Next = 0;
1301 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001302 }
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Chris Lattnerb25df352009-03-04 05:16:45 +00001304 // We've seen a method with this name, see if we have already seen this type
1305 // signature.
1306 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1307 if (MatchTwoMethodDeclarations(Method, List->Method))
1308 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Chris Lattnerb25df352009-03-04 05:16:45 +00001310 // We have a new signature for an existing method - add it.
1311 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1312 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001313}
1314
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001315// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001316ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001317 SourceRange R,
1318 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001319 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1320 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001321 if (Pos == InstanceMethodPool.end()) {
1322 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001323 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1324 else
1325 return 0;
1326 }
1327
1328 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001329 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Steve Naroff037cda52008-09-30 14:38:43 +00001331 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001332 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1333 // This checks if the methods differ by size & alignment.
1334 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001335 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001336 }
1337 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001338 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001339 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001340 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001341 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001342 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001343 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001344 }
1345 return MethList.Method;
1346}
1347
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001348void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001349 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1350 = FactoryMethodPool.find(Method->getSelector());
1351 if (Pos == FactoryMethodPool.end()) {
1352 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1353 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1354 else
1355 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1356 ObjCMethodList())).first;
1357 }
1358
1359 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001360 if (!FirstMethod.Method) {
1361 // Haven't seen a method with this selector name yet - add it.
1362 FirstMethod.Method = Method;
1363 FirstMethod.Next = 0;
1364 } else {
1365 // We've seen a method with this name, now check the type signature(s).
1366 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001367
1368 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001369 Next = Next->Next)
1370 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Chris Lattner4d391482007-12-12 07:09:47 +00001372 if (!match) {
1373 // We have a new signature for an existing method - add it.
1374 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001375 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001376 FirstMethod.Next = OMI;
1377 }
1378 }
1379}
1380
Mike Stump1eb44332009-09-09 15:08:12 +00001381ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001382 SourceRange R) {
1383 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1384 = FactoryMethodPool.find(Sel);
1385 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001386 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001387 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1388 else
1389 return 0;
1390 }
1391
1392 ObjCMethodList &MethList = Pos->second;
1393 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001395 if (MethList.Method && MethList.Next) {
1396 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1397 // This checks if the methods differ by size & alignment.
1398 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1399 issueWarning = true;
1400 }
1401 if (issueWarning && (MethList.Method && MethList.Next)) {
1402 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1403 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1404 << MethList.Method->getSourceRange();
1405 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1406 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1407 << Next->Method->getSourceRange();
1408 }
1409 return MethList.Method;
1410}
1411
Mike Stump1eb44332009-09-09 15:08:12 +00001412/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff0701bbb2009-01-08 17:28:14 +00001413/// have the property type and issue diagnostics if they don't.
1414/// Also synthesize a getter/setter method if none exist (and update the
1415/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1416/// methods is the "right" thing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001417void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001418 ObjCContainerDecl *CD) {
1419 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00001420
1421 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001422 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump1eb44332009-09-09 15:08:12 +00001423 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001424 property->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001426 if (SetterMethod) {
Mike Stump1eb44332009-09-09 15:08:12 +00001427 if (Context.getCanonicalType(SetterMethod->getResultType())
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001428 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001429 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001430 if (SetterMethod->param_size() != 1 ||
1431 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001432 Diag(property->getLocation(),
1433 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001434 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001435 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001436 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1437 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001438 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001439
1440 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001441 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001442 // FIXME: The synthesized property we set here is misleading. We almost always
1443 // synthesize these methods unless the user explicitly provided prototypes
1444 // (which is odd, but allowed). Sema should be typechecking that the
1445 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001446 if (!GetterMethod) {
1447 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001448 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001449 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001450 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1451 property->getLocation(), property->getGetterName(),
1452 property->getType(), CD, true, false, true,
1453 (property->getPropertyImplementation() ==
1454 ObjCPropertyDecl::Optional) ?
1455 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001456 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001457 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001458 } else
1459 // A user declared getter will be synthesize when @synthesize of
1460 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001461 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001462 property->setGetterMethodDecl(GetterMethod);
1463
1464 // Skip setter if property is read-only.
1465 if (!property->isReadOnly()) {
1466 // Find the default setter and if one not found, add one.
1467 if (!SetterMethod) {
1468 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001469 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001470 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001471 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1472 property->getLocation(),
1473 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001474 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001475 (property->getPropertyImplementation() ==
1476 ObjCPropertyDecl::Optional) ?
1477 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001478 ObjCMethodDecl::Required);
1479 // Invent the arguments for the setter. We don't bother making a
1480 // nice name for the argument.
1481 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001482 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001483 property->getIdentifier(),
1484 property->getType(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001485 /*DInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001486 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001487 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001488 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001489 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001490 } else
1491 // A user declared setter will be synthesize when @synthesize of
1492 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001493 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001494 property->setSetterMethodDecl(SetterMethod);
1495 }
Mike Stump1eb44332009-09-09 15:08:12 +00001496 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001497 // handle the following, which is supported by GCC (and part of the design).
1498 //
1499 // @interface Foo
1500 // @property double bar;
1501 // @end
1502 //
1503 // void thisIsUnfortunate() {
1504 // id foo;
1505 // double bar = [foo bar];
1506 // }
1507 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001508 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001509 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001510 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001511 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001512}
1513
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001514/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1515/// identical selector names in current and its super classes and issues
1516/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001517void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1518 ObjCMethodDecl *Method,
1519 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001520 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1521 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001523 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001524 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001525 SD->lookupMethod(Method->getSelector(), IsInstance);
1526 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001527 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001528 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001529 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001530 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1531 E = Method->param_end();
1532 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1533 for (; ParamI != E; ++ParamI, ++PrevI) {
1534 // Number of parameters are the same and is guaranteed by selector match.
1535 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1536 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1537 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1538 // If type of arguement of method in this class does not match its
1539 // respective argument type in the super class method, issue warning;
1540 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001541 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001542 << T1 << T2;
1543 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1544 return;
1545 }
1546 }
1547 ID = SD;
1548 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001549}
1550
Steve Naroffa56f6162007-12-18 01:30:32 +00001551// Note: For class/category implemenations, allMethods/allProperties is
1552// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001553void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1554 DeclPtrTy *allMethods, unsigned allNum,
1555 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001556 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001557 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001558
Steve Naroffa56f6162007-12-18 01:30:32 +00001559 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1560 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001561 // should be true.
1562 if (!ClassDecl)
1563 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001564
Mike Stump1eb44332009-09-09 15:08:12 +00001565 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001566 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1567 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001568 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001569
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001570 if (!isInterfaceDeclKind && AtEndLoc.isInvalid()) {
1571 AtEndLoc = ClassDecl->getLocation();
1572 Diag(AtEndLoc, diag::warn_missing_atend);
1573 }
1574
Steve Naroff0701bbb2009-01-08 17:28:14 +00001575 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001576
1577 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1578 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1579 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1580
Chris Lattner4d391482007-12-12 07:09:47 +00001581 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001582 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001583 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001584
1585 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001586 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001587 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001588 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001589 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001590 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001591 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001592 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001593 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001594 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001595 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001596 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001597 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001598 InsMap[Method->getSelector()] = Method;
1599 /// The following allows us to typecheck messages to "id".
1600 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001601 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001602 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001603 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001604 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001605 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001606 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001607 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001608 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001609 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001610 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001611 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001612 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001613 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001614 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001615 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001616 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001617 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001618 /// The following allows us to typecheck messages to "Class".
1619 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001620 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001621 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001622 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001623 }
1624 }
1625 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001626 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001627 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001628 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001629 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001630 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001631 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001632 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001633 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001634 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001635
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001636 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001637 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001638 if (C->getIdentifier() == 0)
1639 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001640 }
Steve Naroff09c47192009-01-09 15:36:25 +00001641 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1642 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1643 // user-defined setter/getter. It also synthesizes setter/getter methods
1644 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001645 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1646 E = CDecl->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001647 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001648 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001649 CDecl->setAtEndLoc(AtEndLoc);
1650 }
1651 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001652 IC->setAtEndLoc(AtEndLoc);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001653 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001654 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001655 AtomicPropertySetterGetterRules(IC, IDecl);
1656 }
Mike Stump1eb44332009-09-09 15:08:12 +00001657 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001658 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001659 CatImplClass->setAtEndLoc(AtEndLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Chris Lattner4d391482007-12-12 07:09:47 +00001661 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001662 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001663 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001664 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001665 Categories; Categories = Categories->getNextClassCategory()) {
1666 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001667 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001668 break;
1669 }
1670 }
1671 }
1672 }
Chris Lattner682bf922009-03-29 16:50:03 +00001673 if (isInterfaceDeclKind) {
1674 // Reject invalid vardecls.
1675 for (unsigned i = 0; i != tuvNum; i++) {
1676 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1677 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1678 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001679 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001680 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001681 }
Chris Lattner682bf922009-03-29 16:50:03 +00001682 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001683 }
Chris Lattner4d391482007-12-12 07:09:47 +00001684}
1685
1686
1687/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1688/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001689static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001690CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1691 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1692 if (PQTVal & ObjCDeclSpec::DQ_In)
1693 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1694 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1695 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1696 if (PQTVal & ObjCDeclSpec::DQ_Out)
1697 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1698 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1699 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1700 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1701 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1702 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1703 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001704
1705 return ret;
1706}
1707
Chris Lattnerb28317a2009-03-28 19:18:32 +00001708Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001709 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001710 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001711 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001712 Selector Sel,
1713 // optional arguments. The number of types/arguments is obtained
1714 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001715 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001716 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001717 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1718 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001719 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001720
1721 // Make sure we can establish a context for the method.
1722 if (!ClassDecl) {
1723 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanian32844b32009-08-28 17:52:37 +00001724 FunctionLabelMap.clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001725 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001726 }
Chris Lattner4d391482007-12-12 07:09:47 +00001727 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Steve Naroffccef3712009-02-20 22:59:16 +00001729 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001730 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001731
Steve Naroffccef3712009-02-20 22:59:16 +00001732 // Methods cannot return interface types. All ObjC objects are
1733 // passed by reference.
1734 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001735 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1736 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001737 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001738 }
1739 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001740 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001741
1742 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001743 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00001744 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001745 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001746 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001747 MethodDeclKind == tok::objc_optional ?
1748 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001749 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Chris Lattner0ed844b2008-04-04 06:12:32 +00001751 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Chris Lattner7db638d2009-04-11 19:42:43 +00001753 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001754 QualType ArgType;
1755 DeclaratorInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Chris Lattnere294d3f2009-04-11 18:57:04 +00001757 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001758 ArgType = Context.getObjCIdType();
1759 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001760 } else {
John McCall58e46772009-10-23 21:48:59 +00001761 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001762 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001763 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001764 }
Mike Stump1eb44332009-09-09 15:08:12 +00001765
John McCall58e46772009-10-23 21:48:59 +00001766 ParmVarDecl* Param
1767 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1768 ArgInfo[i].Name, ArgType, DI,
1769 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001771 if (ArgType->isObjCInterfaceType()) {
1772 Diag(ArgInfo[i].NameLoc,
1773 diag::err_object_cannot_be_passed_returned_by_value)
1774 << 1 << ArgType;
1775 Param->setInvalidDecl();
1776 }
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Chris Lattner0ed844b2008-04-04 06:12:32 +00001778 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001779 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001781 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001782 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Chris Lattner0ed844b2008-04-04 06:12:32 +00001784 Params.push_back(Param);
1785 }
1786
Jay Foadbeaaccd2009-05-21 09:52:38 +00001787 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001788 ObjCMethod->setObjCDeclQualifier(
1789 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1790 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001791
1792 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001793 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001794
John McCall54abf7d2009-11-04 02:18:39 +00001795 const ObjCMethodDecl *InterfaceMD = 0;
1796
Mike Stump1eb44332009-09-09 15:08:12 +00001797 // For implementations (which can be very "coarse grain"), we add the
1798 // method now. This allows the AST to implement lookup methods that work
1799 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001800 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001801 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001802 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001803 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001804 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1805 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001806 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001807 PrevMethod = ImpDecl->getClassMethod(Sel);
1808 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001809 }
John McCall54abf7d2009-11-04 02:18:39 +00001810 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1811 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001812 if (AttrList)
1813 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001814 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001815 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001816 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001817 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1818 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001819 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001820 PrevMethod = CatImpDecl->getClassMethod(Sel);
1821 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001822 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001823 if (AttrList)
1824 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001825 }
1826 if (PrevMethod) {
1827 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001828 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001829 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001830 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001831 }
John McCall54abf7d2009-11-04 02:18:39 +00001832
1833 // If the interface declared this method, and it was deprecated there,
1834 // mark it deprecated here.
1835 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1836 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1837
Chris Lattnerb28317a2009-03-28 19:18:32 +00001838 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001839}
1840
Mike Stump1eb44332009-09-09 15:08:12 +00001841void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001842 SourceLocation Loc,
1843 unsigned &Attributes) {
1844 // FIXME: Improve the reported location.
1845
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001846 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001847 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001848 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1849 ObjCDeclSpec::DQ_PR_assign |
1850 ObjCDeclSpec::DQ_PR_copy |
1851 ObjCDeclSpec::DQ_PR_retain))) {
1852 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1853 "readwrite" :
1854 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1855 "assign" :
1856 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1857 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00001858
1859 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001860 diag::err_objc_property_attr_mutually_exclusive :
1861 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001862 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001863 }
1864
1865 // Check for copy or retain on non-object types.
1866 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001867 !PropertyTy->isObjCObjectPointerType() &&
1868 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00001869 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001870 Diag(Loc, diag::err_objc_property_requires_object)
1871 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001872 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1873 }
1874
1875 // Check for more than one of { assign, copy, retain }.
1876 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1877 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001878 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1879 << "assign" << "copy";
1880 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00001881 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001882 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001883 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1884 << "assign" << "retain";
1885 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001886 }
1887 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1888 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001889 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1890 << "copy" << "retain";
1891 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001892 }
1893 }
1894
1895 // Warn if user supplied no assignment attribute, property is
1896 // readwrite, and this is an object type.
1897 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1898 ObjCDeclSpec::DQ_PR_retain)) &&
1899 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00001900 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001901 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00001902 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001903 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1904
1905 // If non-gc code warn that this is likely inappropriate.
1906 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1907 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001909 // FIXME: Implement warning dependent on NSCopying being
1910 // implemented. See also:
1911 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1912 // (please trim this list while you are at it).
1913 }
Mike Stump046efd92009-05-07 23:06:50 +00001914
1915 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1916 && getLangOptions().getGCMode() == LangOptions::GCOnly
1917 && PropertyTy->isBlockPointerType())
1918 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001919}
1920
Mike Stump1eb44332009-09-09 15:08:12 +00001921Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001922 FieldDeclarator &FD,
1923 ObjCDeclSpec &ODS,
1924 Selector GetterSel,
1925 Selector SetterSel,
1926 DeclPtrTy ClassCategory,
1927 bool *isOverridingProperty,
1928 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001929 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001930 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1931 // default is readwrite!
1932 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00001933 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001934 // not retain or copy
1935 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001936 (isReadWrite &&
1937 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001938 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1939 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001940 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001941 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001942 // May modify Attributes.
1943 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001944 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1945 if (!CDecl->getIdentifier()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001946 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001947 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001948 if ((CCPrimary = CDecl->getClassInterface())) {
1949 // Find the property in continuation class's primary class only.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001950 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00001951 if (ObjCPropertyDecl *PIDecl =
1952 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001953 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001954 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001955 unsigned PIkind = PIDecl->getPropertyAttributes();
1956 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00001957 unsigned retainCopyNonatomic =
Fariborz Jahaniand669be52009-11-06 22:59:12 +00001958 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00001959 ObjCPropertyDecl::OBJC_PR_copy |
1960 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00001961 if ((Attributes & retainCopyNonatomic) !=
1962 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001963 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00001964 Diag(PIDecl->getLocation(), diag::note_property_declare);
1965 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001966 PIDecl->makeitReadWriteAttribute();
1967 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1968 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1969 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1970 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1971 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00001972 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001973 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001974 << CCPrimary->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00001975 Diag(PIDecl->getLocation(), diag::note_property_declare);
1976 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001977 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001978 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001979 // class's list.
1980 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001981 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001982 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001983 // No matching property found in the primary class. Just fall thru
1984 // and add property to continuation class's primary class.
1985 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001986 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001987 Diag(CDecl->getLocation(), diag::err_continuation_class);
1988 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001989 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001990 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001991 }
Mike Stump1eb44332009-09-09 15:08:12 +00001992
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001993 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00001994 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001995 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
1996 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
1997 if (T->isObjCObjectPointerType()) {
1998 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001999 if (const ObjCInterfaceType *OIT =
John McCall183700f2009-09-21 23:43:11 +00002000 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002001 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002002 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002003 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002004 LookupProtocol(&Context.Idents.get("NSCopying")))
2005 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00002006 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002007 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002008 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002009 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00002010 if (T->isObjCInterfaceType())
2011 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002012
Steve Naroff93983f82009-01-11 12:47:58 +00002013 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2014 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002015 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00002016 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00002017 FD.D.getIdentifier(), T);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002018 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002019
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002020 if (T->isArrayType() || T->isFunctionType()) {
2021 Diag(AtLoc, diag::err_property_type) << T;
2022 PDecl->setInvalidDecl();
2023 }
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002025 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00002026
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00002027 // Regardless of setter/getter attribute, we save the default getter/setter
2028 // selector names in anticipation of declaration of setter/getter methods.
2029 PDecl->setGetterName(GetterSel);
2030 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00002031
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002032 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002033 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00002034
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002035 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002036 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00002037
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002038 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002039 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00002040
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002041 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002042 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00002043
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002044 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002045 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00002046
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002047 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002048 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00002049
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002050 if (isAssign)
2051 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00002052
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002053 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002054 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00002055
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00002056 if (MethodImplKind == tok::objc_required)
2057 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2058 else if (MethodImplKind == tok::objc_optional)
2059 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002060 // A case of continuation class adding a new property in the class. This
2061 // is not what it was meant for. However, gcc supports it and so should we.
2062 // Make sure setter/getters are declared here.
2063 if (CCPrimary)
2064 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Chris Lattnerb28317a2009-03-28 19:18:32 +00002066 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002067}
2068
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002069/// ActOnPropertyImplDecl - This routine performs semantic checks and
2070/// builds the AST node for a property implementation declaration; declared
2071/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002072///
Mike Stump1eb44332009-09-09 15:08:12 +00002073Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002074 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002075 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002076 DeclPtrTy ClassCatImpDecl,
2077 IdentifierInfo *PropertyId,
2078 IdentifierInfo *PropertyIvar) {
2079 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002080 // Make sure we have a context for the property implementation declaration.
2081 if (!ClassImpDecl) {
2082 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002083 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002084 }
2085 ObjCPropertyDecl *property = 0;
2086 ObjCInterfaceDecl* IDecl = 0;
2087 // Find the class or category class where this property must have
2088 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002089 ObjCImplementationDecl *IC = 0;
2090 ObjCCategoryImplDecl* CatImplClass = 0;
2091 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002092 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002093 // We always synthesize an interface for an implementation
2094 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002095 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002096 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002097
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002098 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002099 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002100 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002101 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002102 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002103 }
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002104 if (const ObjCCategoryDecl *CD =
2105 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
2106 if (CD->getIdentifier()) {
2107 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002108 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002109 return DeclPtrTy();
2110 }
2111 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002112 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002113 if (Synthesize) {
2114 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002115 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002116 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002117 IDecl = CatImplClass->getClassInterface();
2118 if (!IDecl) {
2119 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002120 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002121 }
Mike Stump1eb44332009-09-09 15:08:12 +00002122 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002123 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002124
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002125 // If category for this implementation not found, it is an error which
2126 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002127 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002128 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002129 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002130 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002131 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002132 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002133 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002134 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002135 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002136 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002137 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002138 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002139 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002140 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002141 // Check that we have a valid, previously declared ivar for @synthesize
2142 if (Synthesize) {
2143 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002144 if (!PropertyIvar)
2145 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002146 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002147 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002148 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002149 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002150 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002151 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002152 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002153 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002154 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2155 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002156 ObjCIvarDecl::Public,
2157 (Expr *)0);
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002158 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002159 IDecl->addDecl(Ivar);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002160 property->setPropertyIvarDecl(Ivar);
2161 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002162 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002163 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002164 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002165 } else if (getLangOptions().ObjCNonFragileABI &&
2166 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002167 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002168 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002169 << ClassDeclared->getDeclName();
2170 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2171 << Ivar << Ivar->getNameAsCString();
2172 // Note! I deliberately want it to fall thru so more errors are caught.
2173 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002174 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002175
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002176 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002177 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002178 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002179 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002180 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002181 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002182 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002183 }
Mike Stump1eb44332009-09-09 15:08:12 +00002184
Chris Lattnerb28317a2009-03-28 19:18:32 +00002185 // FIXME! Rules for properties are somewhat different that those
2186 // for assignments. Use a new routine to consolidate all cases;
2187 // specifically for property redeclarations as well as for ivars.
2188 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2189 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002190 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002191 lhsType->isArithmeticType()) {
2192 Diag(PropertyLoc, diag::error_property_ivar_type)
2193 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002194 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002195 }
2196 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002197 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2198 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002199 Diag(PropertyLoc, diag::error_weak_property)
2200 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002201 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002202 }
Mike Stump1eb44332009-09-09 15:08:12 +00002203 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002204 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2205 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002206 Diag(PropertyLoc, diag::error_strong_property)
2207 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002208 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002209 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002210 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002211 } else if (PropertyIvar)
2212 // @dynamic
2213 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002214 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002215 ObjCPropertyImplDecl *PIDecl =
2216 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2217 property,
2218 (Synthesize ?
2219 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002220 : ObjCPropertyImplDecl::Dynamic),
2221 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002222 if (IC) {
2223 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002224 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002225 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002226 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2227 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002228 << PropertyIvar;
2229 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2230 }
Mike Stump1eb44332009-09-09 15:08:12 +00002231
2232 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002233 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002234 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2235 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002236 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002237 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002238 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002239 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002240 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002241 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002242 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002243 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2244 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002245 << PropertyIvar;
2246 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2247 }
Mike Stump1eb44332009-09-09 15:08:12 +00002248
2249 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002250 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002251 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2252 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002253 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002254 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002255 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002256 }
Mike Stump1eb44332009-09-09 15:08:12 +00002257
Chris Lattnerb28317a2009-03-28 19:18:32 +00002258 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002259}
Anders Carlsson15281452008-11-04 16:57:32 +00002260
Chris Lattnercc98eac2008-12-17 07:13:27 +00002261bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002262 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002263 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002264
Anders Carlsson15281452008-11-04 16:57:32 +00002265 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2266 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Anders Carlsson15281452008-11-04 16:57:32 +00002268 return true;
2269}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002270
Chris Lattnercc98eac2008-12-17 07:13:27 +00002271/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2272/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002273void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002274 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002275 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002276 // Check that ClassName is a valid class
2277 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2278 if (!Class) {
2279 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2280 return;
2281 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002282 if (LangOpts.ObjCNonFragileABI) {
2283 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2284 return;
2285 }
Mike Stump1eb44332009-09-09 15:08:12 +00002286
Chris Lattnercc98eac2008-12-17 07:13:27 +00002287 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002288 llvm::SmallVector<FieldDecl*, 32> RecFields;
2289 Context.CollectObjCIvars(Class, RecFields);
2290 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2291 for (unsigned i = 0; i < RecFields.size(); i++) {
2292 FieldDecl* ID = RecFields[i];
2293 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2294 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2295 ID->getIdentifier(), ID->getType(),
2296 ID->getBitWidth());
2297 Decls.push_back(Sema::DeclPtrTy::make(FD));
2298 }
Mike Stump1eb44332009-09-09 15:08:12 +00002299
Chris Lattnercc98eac2008-12-17 07:13:27 +00002300 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002301 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002302 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002303 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002304 if (getLangOptions().CPlusPlus)
2305 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002306 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002307 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002308 }
2309}
2310