blob: d1b40bed0fd8ae71f2ef1f97d4dbb6b204c00060 [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);
Chris Lattner4d391482007-12-12 07:09:47 +0000115 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000116 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000117 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000118 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000119 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000120 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Steve Naroffa7503a72009-04-23 15:15:40 +0000122 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000123 }
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner4d391482007-12-12 07:09:47 +0000125 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000126 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000127 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000128 if (PrevDecl == IDecl) {
129 Diag(SuperLoc, diag::err_recursive_superclass)
130 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
131 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000132 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000133 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000134 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000135
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000136 // Diagnose classes that inherit from deprecated classes.
137 if (SuperClassDecl)
138 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000140 if (PrevDecl && SuperClassDecl == 0) {
141 // The previous declaration was not a class decl. Check if we have a
142 // typedef. If we do, get the underlying class type.
143 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
144 QualType T = TDecl->getUnderlyingType();
145 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000146 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000147 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
148 }
149 }
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000151 // This handles the following case:
152 //
153 // typedef int SuperClass;
154 // @interface MyClass : SuperClass {} @end
155 //
156 if (!SuperClassDecl) {
157 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
158 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000159 }
160 }
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000162 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
163 if (!SuperClassDecl)
164 Diag(SuperLoc, diag::err_undef_superclass)
165 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
166 else if (SuperClassDecl->isForwardDecl())
167 Diag(SuperLoc, diag::err_undef_superclass)
168 << SuperClassDecl->getDeclName() << ClassName
169 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000170 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000171 IDecl->setSuperClass(SuperClassDecl);
172 IDecl->setSuperClassLoc(SuperLoc);
173 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000174 }
Chris Lattner4d391482007-12-12 07:09:47 +0000175 } else { // we have a root class.
176 IDecl->setLocEnd(ClassLoc);
177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000179 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000180 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000181 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
182 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000183 IDecl->setLocEnd(EndProtoLoc);
184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Anders Carlsson15281452008-11-04 16:57:32 +0000186 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000187 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000188}
189
190/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000191/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000192Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000193 IdentifierInfo *AliasName,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000194 SourceLocation AliasLocation,
195 IdentifierInfo *ClassName,
196 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000197 // Look for previous declaration of alias name
John McCallf36e02d2009-10-09 21:13:30 +0000198 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000199 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000200 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000201 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000202 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000203 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000204 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000205 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000206 }
207 // Check for class declaration
John McCallf36e02d2009-10-09 21:13:30 +0000208 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000209 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
210 QualType T = TDecl->getUnderlyingType();
211 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000212 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000213 ClassName = IDecl->getIdentifier();
John McCallf36e02d2009-10-09 21:13:30 +0000214 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000215 }
216 }
217 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000218 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
219 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000220 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000221 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000222 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000223 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000224 }
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000226 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000227 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000228 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Anders Carlsson15281452008-11-04 16:57:32 +0000230 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000231 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000232
Chris Lattnerb28317a2009-03-28 19:18:32 +0000233 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000234}
235
Steve Naroff61d68522009-03-05 15:22:01 +0000236void Sema::CheckForwardProtocolDeclarationForCircularDependency(
237 IdentifierInfo *PName,
238 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000239 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000240 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
241 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Douglas Gregor6e378de2009-04-23 23:18:26 +0000243 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000244 if (PDecl->getIdentifier() == PName) {
245 Diag(Ploc, diag::err_protocol_has_circular_dependency);
246 Diag(PrevLoc, diag::note_previous_definition);
247 }
Mike Stump1eb44332009-09-09 15:08:12 +0000248 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000249 PDecl->getLocation(), PDecl->getReferencedProtocols());
250 }
251 }
252}
253
Chris Lattnerb28317a2009-03-28 19:18:32 +0000254Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000255Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
256 IdentifierInfo *ProtocolName,
257 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000258 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000259 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000260 SourceLocation EndProtoLoc,
261 AttributeList *AttrList) {
262 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000263 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000264 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000265 if (PDecl) {
266 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000267 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000268 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000269 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000270 // Just return the protocol we already had.
271 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000272 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000273 }
Steve Naroff61d68522009-03-05 15:22:01 +0000274 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000275 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000276 CheckForwardProtocolDeclarationForCircularDependency(
277 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
278 PList.Destroy(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Steve Narofff11b5082008-08-13 16:39:22 +0000280 // Make sure the cached decl gets a valid start location.
281 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000282 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000283 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000284 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000285 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000286 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000287 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000288 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000289 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000290 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000291 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000292 /// Check then save referenced protocols.
Chris Lattner38af2de2009-02-20 21:35:13 +0000293 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000294 PDecl->setLocEnd(EndProtoLoc);
295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296
297 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000298 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000299}
300
301/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000302/// issues an error if they are not declared. It returns list of
303/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000304void
Chris Lattnere13b9592008-07-26 04:03:38 +0000305Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000306 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000307 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000308 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000309 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000310 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000311 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000312 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000313 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000314 continue;
315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000317 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000318
319 // If this is a forward declaration and we are supposed to warn in this
320 // case, do it.
321 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000322 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000323 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000324 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000325 }
326}
327
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000328/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000329/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000330///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000331void
Mike Stump1eb44332009-09-09 15:08:12 +0000332Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000333 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000334 const IdentifierInfo *inheritedName) {
Mike Stump1eb44332009-09-09 15:08:12 +0000335 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000336 Property->getPropertyAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000337 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000338 SuperProperty->getPropertyAttributes();
339 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
340 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000341 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000342 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000343 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
344 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000345 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000346 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000347 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
348 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000349 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000350 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000352 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
353 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000354 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000355 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000356 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000357 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump1eb44332009-09-09 15:08:12 +0000358 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000359 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000360 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000361 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000362
Mike Stump1eb44332009-09-09 15:08:12 +0000363 QualType LHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000364 Context.getCanonicalType(SuperProperty->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000365 QualType RHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000366 Context.getCanonicalType(Property->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Steve Naroff15edf0d2009-03-03 15:43:24 +0000368 if (!Context.typesAreCompatible(LHSType, RHSType)) {
369 // FIXME: Incorporate this test with typesAreCompatible.
370 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff4084c302009-07-23 01:01:38 +0000371 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff15edf0d2009-03-03 15:43:24 +0000372 return;
373 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
374 << Property->getType() << SuperProperty->getType() << inheritedName;
375 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000376}
377
378/// ComparePropertiesInBaseAndSuper - This routine compares property
379/// declarations in base and its super class, if any, and issues
380/// diagnostics in a variety of inconsistant situations.
381///
Chris Lattner70f19542009-02-16 21:26:43 +0000382void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000383 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
384 if (!SDecl)
385 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000386 // FIXME: O(N^2)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000387 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
388 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000389 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000390 // Does property in super class has declaration in current class?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000391 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
392 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000393 ObjCPropertyDecl *PDecl = (*I);
394 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump1eb44332009-09-09 15:08:12 +0000395 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000396 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000397 }
398 }
399}
400
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000401/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
402/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000403/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000404void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000405Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000406 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000407 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
408 if (!IDecl) {
409 // Category
410 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
411 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000412 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
413 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000414 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000415 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000416 // Is this property already in category's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000417 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000418 if ((*CP)->getIdentifier() == Pr->getIdentifier())
419 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000420 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000421 // Property protocol already exist in class. Diagnose any mismatch.
422 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
423 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000424 return;
425 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000426 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
427 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000428 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000429 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000430 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000431 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000432 if ((*CP)->getIdentifier() == Pr->getIdentifier())
433 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000434 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000435 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000436 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000437 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000438}
439
440/// MergeProtocolPropertiesIntoClass - This routine merges properties
441/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000442/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000443///
Chris Lattner70f19542009-02-16 21:26:43 +0000444void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000445 DeclPtrTy MergeItsProtocols) {
446 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000447 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
448
449 if (!IDecl) {
450 // Category
451 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
452 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
453 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
454 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
455 E = MDecl->protocol_end(); P != E; ++P)
456 // Merge properties of category (*P) into IDECL's
457 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000459 // Go thru the list of protocols for this category and recursively merge
460 // their properties into this class as well.
461 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
462 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000463 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000464 } else {
465 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
466 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
467 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000468 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000469 }
470 return;
471 }
472
Chris Lattnerb752f282008-07-21 07:06:49 +0000473 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000474 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
475 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000476 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000477 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000479 // Go thru the list of protocols for this class and recursively merge
480 // their properties into this class as well.
481 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
482 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000483 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000484 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000485 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
486 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
487 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000488 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000489 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000490}
491
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000492/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000493/// a class method in its extension.
494///
Mike Stump1eb44332009-09-09 15:08:12 +0000495void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000496 ObjCInterfaceDecl *ID) {
497 if (!ID)
498 return; // Possibly due to previous error
499
500 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000501 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
502 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000503 ObjCMethodDecl *MD = *i;
504 MethodMap[MD->getSelector()] = MD;
505 }
506
507 if (MethodMap.empty())
508 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000509 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
510 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000511 ObjCMethodDecl *Method = *i;
512 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
513 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
514 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
515 << Method->getDeclName();
516 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
517 }
518 }
519}
520
Chris Lattner58fe03b2009-04-12 08:43:13 +0000521/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000522Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000523Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000524 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000525 unsigned NumElts,
526 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000527 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Chris Lattner4d391482007-12-12 07:09:47 +0000529 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000530 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000531 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000532 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000533 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000534 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000535 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000536 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000537 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000538 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000539 Protocols.push_back(PDecl);
540 }
Mike Stump1eb44332009-09-09 15:08:12 +0000541
542 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000543 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Anders Carlsson15281452008-11-04 16:57:32 +0000544 &Protocols[0], Protocols.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000545 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000546 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000547 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000548}
549
Chris Lattnerb28317a2009-03-28 19:18:32 +0000550Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000551ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
552 IdentifierInfo *ClassName, SourceLocation ClassLoc,
553 IdentifierInfo *CategoryName,
554 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000555 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000556 unsigned NumProtoRefs,
557 SourceLocation EndProtoLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000558 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000559 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
560 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000561 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000562
563 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000564 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000565 if (!IDecl || IDecl->isForwardDecl()) {
566 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000567 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000568 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000569 }
Chris Lattner4d391482007-12-12 07:09:47 +0000570
Chris Lattner70f19542009-02-16 21:26:43 +0000571 CDecl->setClassInterface(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Chris Lattner16b34b42009-02-16 21:30:01 +0000573 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000574 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000575
576 /// Check for duplicate interface declaration for this category
577 ObjCCategoryDecl *CDeclChain;
578 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
579 CDeclChain = CDeclChain->getNextClassCategory()) {
580 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
581 Diag(CategoryLoc, diag::warn_dup_category_def)
582 << ClassName << CategoryName;
583 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
584 break;
585 }
586 }
587 if (!CDeclChain)
588 CDecl->insertNextClassCategory();
589
Chris Lattner4d391482007-12-12 07:09:47 +0000590 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000591 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
592 Context);
593 CDecl->setLocEnd(EndProtoLoc);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000594 // Protocols in the class extension belong to the class.
595 if (!CDecl->getIdentifier())
596 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
597 NumProtoRefs,Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000598 }
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Anders Carlsson15281452008-11-04 16:57:32 +0000600 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000601 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000602}
603
604/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000605/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000606/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000607Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000608 SourceLocation AtCatImplLoc,
609 IdentifierInfo *ClassName, SourceLocation ClassLoc,
610 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000611 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000612 ObjCCategoryDecl *CatIDecl = 0;
613 if (IDecl) {
614 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
615 if (!CatIDecl) {
616 // Category @implementation with no corresponding @interface.
617 // Create and install one.
618 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
619 CatName);
620 CatIDecl->setClassInterface(IDecl);
621 CatIDecl->insertNextClassCategory();
622 }
623 }
624
Mike Stump1eb44332009-09-09 15:08:12 +0000625 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000626 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
627 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000628 /// Check that class of this category is already completely declared.
629 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000630 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000631
Douglas Gregord0434102009-01-09 00:49:46 +0000632 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000633 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000634
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000635 /// Check that CatName, category name, is not used in another implementation.
636 if (CatIDecl) {
637 if (CatIDecl->getImplementation()) {
638 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
639 << CatName;
640 Diag(CatIDecl->getImplementation()->getLocation(),
641 diag::note_previous_definition);
642 } else
643 CatIDecl->setImplementation(CDecl);
644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Anders Carlsson15281452008-11-04 16:57:32 +0000646 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000647 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000648}
649
Chris Lattnerb28317a2009-03-28 19:18:32 +0000650Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000651 SourceLocation AtClassImplLoc,
652 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000653 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000654 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000655 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000656 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000657 NamedDecl *PrevDecl
658 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000659 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000660 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000661 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner1829a6d2009-02-23 22:00:08 +0000662 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000663 // Is there an interface declaration of this class; if not, warn!
Mike Stump1eb44332009-09-09 15:08:12 +0000664 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000665 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000666 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000667 IDecl = 0;
668 }
Chris Lattner4d391482007-12-12 07:09:47 +0000669 }
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Chris Lattner4d391482007-12-12 07:09:47 +0000671 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000672 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000673 if (SuperClassname) {
674 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000675 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000676 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000677 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
678 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000679 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000680 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000681 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000682 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000683 Diag(SuperClassLoc, diag::err_undef_superclass)
684 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000685 else if (IDecl && IDecl->getSuperClass() != SDecl) {
686 // This implementation and its interface do not have the same
687 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000688 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000689 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000690 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000691 }
692 }
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Chris Lattner4d391482007-12-12 07:09:47 +0000695 if (!IDecl) {
696 // Legacy case of @implementation with no corresponding @interface.
697 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000698
Mike Stump390b4cc2009-05-16 07:39:55 +0000699 // FIXME: Do we support attributes on the @implementation? If so we should
700 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000701 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000702 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000703 IDecl->setSuperClass(SDecl);
704 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000705
706 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000707 } else {
708 // Mark the interface as being completed, even if it was just as
709 // @class ....;
710 // declaration; the user cannot reopen it.
711 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
714 ObjCImplementationDecl* IMPDecl =
715 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000716 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Anders Carlsson15281452008-11-04 16:57:32 +0000718 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000719 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Chris Lattner4d391482007-12-12 07:09:47 +0000721 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000722 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000723 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000724 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000725 Diag(IDecl->getImplementation()->getLocation(),
726 diag::note_previous_definition);
727 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000728 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000729 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000730 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000731 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000732}
733
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000734void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
735 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000736 SourceLocation RBrace) {
737 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000738 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000739 if (!IDecl)
740 return;
741 /// Check case of non-existing @interface decl.
742 /// (legacy objective-c @implementation decl without an @interface decl).
743 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000744 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000745 IDecl->setIVarList(ivars, numIvars, Context);
746 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000747 return;
748 }
749 // If implementation has empty ivar list, just return.
750 if (numIvars == 0)
751 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Chris Lattner4d391482007-12-12 07:09:47 +0000753 assert(ivars && "missing @implementation ivars");
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Chris Lattner4d391482007-12-12 07:09:47 +0000755 // Check interface's Ivar list against those in the implementation.
756 // names and types must match.
757 //
Chris Lattner4d391482007-12-12 07:09:47 +0000758 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000759 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000760 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
761 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000762 ObjCIvarDecl* ImplIvar = ivars[j++];
763 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000764 assert (ImplIvar && "missing implementation ivar");
765 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Steve Naroffca331292009-03-03 14:49:36 +0000767 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000768 if (Context.getCanonicalType(ImplIvar->getType()) !=
769 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000770 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000771 << ImplIvar->getIdentifier()
772 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000773 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000774 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
775 Expr *ImplBitWidth = ImplIvar->getBitWidth();
776 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000777 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
778 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000779 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
780 << ImplIvar->getIdentifier();
781 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
782 }
Mike Stump1eb44332009-09-09 15:08:12 +0000783 }
Steve Naroffca331292009-03-03 14:49:36 +0000784 // Make sure the names are identical.
785 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000786 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000787 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000788 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000789 }
790 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000791 }
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Chris Lattner609e4c72007-12-12 18:11:49 +0000793 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000794 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000795 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000796 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000797}
798
Steve Naroff3c2eb662008-02-10 21:38:56 +0000799void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
800 bool &IncompleteImpl) {
801 if (!IncompleteImpl) {
802 Diag(ImpLoc, diag::warn_incomplete_impl);
803 IncompleteImpl = true;
804 }
Chris Lattner08631c52008-11-23 21:45:46 +0000805 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000806}
807
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000808void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
809 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000810 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000811 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000812 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
813 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000814 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000815 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
816 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000817 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
818 }
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Chris Lattner3aff9192009-04-11 19:58:42 +0000820 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
821 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
822 IM != EM; ++IM, ++IF) {
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000823 if (Context.typesAreCompatible((*IF)->getType(), (*IM)->getType()) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000824 Context.QualifiedIdConformsQualifiedId((*IF)->getType(),
Steve Naroff4084c302009-07-23 01:01:38 +0000825 (*IM)->getType()))
Chris Lattner3aff9192009-04-11 19:58:42 +0000826 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000827
828 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000829 << ImpMethodDecl->getDeclName() << (*IF)->getType()
830 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000831 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000832 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000833}
834
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000835/// isPropertyReadonly - Return true if property is readonly, by searching
836/// for the property in the class and in its categories and implementations
837///
838bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000839 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000840 // by far the most common case.
841 if (!PDecl->isReadOnly())
842 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000843 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000844 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000845 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000846 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000848 // Main class has the property as 'readonly'. Must search
Mike Stump1eb44332009-09-09 15:08:12 +0000849 // through the category list to see if the property's
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000850 // attribute has been over-ridden to 'readwrite'.
851 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
852 Category; Category = Category->getNextClassCategory()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000853 // Even if property is ready only, if a category has a user defined setter,
854 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000855 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000856 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000857 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000858 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000859 if (P && !P->isReadOnly())
860 return false;
861 }
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000863 // Also, check for definition of a setter method in the implementation if
864 // all else failed.
865 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000866 if (ObjCImplementationDecl *IMD =
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000867 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000868 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000869 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000870 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000871 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000872 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000873 return false;
874 }
875 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000876 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000877 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000878 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000879 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000880 // If all fails, look at the super class.
881 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
882 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000883 return true;
884}
885
Mike Stump390b4cc2009-05-16 07:39:55 +0000886/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
887/// improve the efficiency of selector lookups and type checking by associating
888/// with each protocol / interface / category the flattened instance tables. If
889/// we used an immutable set to keep the table then it wouldn't add significant
890/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000891
Steve Naroffefe7f362008-02-08 22:06:17 +0000892/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000893/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000894void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
895 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000896 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000897 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000898 const llvm::DenseSet<Selector> &ClsMap,
899 ObjCInterfaceDecl *IDecl) {
900 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000901 ObjCInterfaceDecl *NSIDecl = 0;
902 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000903 // check to see if class implements forwardInvocation method and objects
904 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000905 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000906 // Under such conditions, which means that every method possible is
907 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000908 // found" warnings.
909 // FIXME: Use a general GetUnarySelector method for this.
910 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
911 Selector fISelector = Context.Selectors.getSelector(1, &II);
912 if (InsMap.count(fISelector))
913 // Is IDecl derived from 'NSProxy'? If so, no instance methods
914 // need be implemented in the implementation.
915 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
916 }
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000918 // If a method lookup fails locally we still need to look and see if
919 // the method was implemented by a base class or an inherited
920 // protocol. This lookup is slow, but occurs rarely in correct code
921 // and otherwise would terminate in a warning.
922
Chris Lattner4d391482007-12-12 07:09:47 +0000923 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000924 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000925 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000926 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000927 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000928 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000929 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000930 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000931 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000932 // Ugly, but necessary. Method declared in protcol might have
933 // have been synthesized due to a property declared in the class which
934 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000935 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000936 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000937 if (!MethodInClass || !MethodInClass->isSynthesized())
938 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
939 }
940 }
Chris Lattner4d391482007-12-12 07:09:47 +0000941 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000942 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000943 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000944 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000945 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000946 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
947 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000948 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000949 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000950 }
Chris Lattner780f3292008-07-21 21:32:27 +0000951 // Check on this protocols's referenced protocols, recursively.
952 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
953 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000954 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000955}
956
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000957/// MatchAllMethodDeclarations - Check methods declaraed in interface or
958/// or protocol against those declared in their implementations.
959///
960void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
961 const llvm::DenseSet<Selector> &ClsMap,
962 llvm::DenseSet<Selector> &InsMapSeen,
963 llvm::DenseSet<Selector> &ClsMapSeen,
964 ObjCImplDecl* IMPDecl,
965 ObjCContainerDecl* CDecl,
966 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +0000967 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000968 // Check and see if instance methods in class interface have been
969 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000970 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
971 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000972 if (InsMapSeen.count((*I)->getSelector()))
973 continue;
974 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000975 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000976 !InsMap.count((*I)->getSelector())) {
977 if (ImmediateClass)
978 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
979 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000980 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000981 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000982 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000983 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000984 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000985 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000986 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
987 // ImpMethodDecl may be null as in a @dynamic property.
988 if (ImpMethodDecl)
989 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
990 }
991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000993 // Check and see if class methods in class interface have been
994 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +0000995 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000996 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000997 if (ClsMapSeen.count((*I)->getSelector()))
998 continue;
999 ClsMapSeen.insert((*I)->getSelector());
1000 if (!ClsMap.count((*I)->getSelector())) {
1001 if (ImmediateClass)
1002 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001003 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001004 ObjCMethodDecl *ImpMethodDecl =
1005 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001006 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001007 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001008 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1009 }
1010 }
1011 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1012 // Check for any implementation of a methods declared in protocol.
1013 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1014 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001015 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1016 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001017 (*PI), IncompleteImpl, false);
1018 if (I->getSuperClass())
1019 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001020 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001021 I->getSuperClass(), IncompleteImpl, false);
1022 }
1023}
1024
Mike Stump1eb44332009-09-09 15:08:12 +00001025void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1026 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001027 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001028 llvm::DenseSet<Selector> InsMap;
1029 // Check and see if instance methods in class interface have been
1030 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001031 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001032 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001033 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001035 // Check and see if properties declared in the interface have either 1)
1036 // an implementation or 2) there is a @synthesize/@dynamic implementation
1037 // of the property in the @implementation.
1038 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001039 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1040 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001041 ObjCPropertyDecl *Prop = (*P);
1042 if (Prop->isInvalidDecl())
1043 continue;
1044 ObjCPropertyImplDecl *PI = 0;
1045 // Is there a matching propery synthesize/dynamic?
Mike Stump1eb44332009-09-09 15:08:12 +00001046 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001047 I = IMPDecl->propimpl_begin(),
1048 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001049 if ((*I)->getPropertyDecl() == Prop) {
1050 PI = (*I);
1051 break;
1052 }
1053 if (PI)
1054 continue;
1055 if (!InsMap.count(Prop->getGetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001056 Diag(Prop->getLocation(),
1057 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001058 << Prop->getDeclName() << Prop->getGetterName();
1059 Diag(IMPDecl->getLocation(),
1060 diag::note_property_impl_required);
1061 }
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001063 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001064 Diag(Prop->getLocation(),
1065 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001066 << Prop->getDeclName() << Prop->getSetterName();
1067 Diag(IMPDecl->getLocation(),
1068 diag::note_property_impl_required);
1069 }
1070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Chris Lattner4d391482007-12-12 07:09:47 +00001072 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001073 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001074 I = IMPDecl->classmeth_begin(),
1075 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001076 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001078 // Check for type conflict of methods declared in a class/protocol and
1079 // its implementation; if any.
1080 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001081 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1082 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001083 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Chris Lattner4d391482007-12-12 07:09:47 +00001085 // Check the protocol list for unimplemented methods in the @implementation
1086 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001087 // Check and see if class methods in class interface have been
1088 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Chris Lattnercddc8882009-03-01 00:56:52 +00001090 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001091 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001092 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001093 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001094 InsMap, ClsMap, I);
1095 // Check class extensions (unnamed categories)
1096 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1097 Categories; Categories = Categories->getNextClassCategory()) {
1098 if (!Categories->getIdentifier()) {
1099 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1100 break;
1101 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001102 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001103 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001104 // For extended class, unimplemented methods in its protocols will
1105 // be reported in the primary class.
1106 if (C->getIdentifier()) {
1107 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1108 E = C->protocol_end(); PI != E; ++PI)
1109 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1110 InsMap, ClsMap, C->getClassInterface());
1111 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001112 } else
1113 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001114}
1115
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001116void
1117Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1118 ObjCContainerDecl* IDecl) {
1119 // Rules apply in non-GC mode only
1120 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1121 return;
1122 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1123 E = IDecl->prop_end();
1124 I != E; ++I) {
1125 ObjCPropertyDecl *Property = (*I);
1126 unsigned Attributes = Property->getPropertyAttributes();
1127 // We only care about readwrite atomic property.
1128 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1129 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1130 continue;
1131 if (const ObjCPropertyImplDecl *PIDecl
1132 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1133 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1134 continue;
1135 ObjCMethodDecl *GetterMethod =
1136 IMPDecl->getInstanceMethod(Property->getGetterName());
1137 ObjCMethodDecl *SetterMethod =
1138 IMPDecl->getInstanceMethod(Property->getSetterName());
1139 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1140 SourceLocation MethodLoc =
1141 (GetterMethod ? GetterMethod->getLocation()
1142 : SetterMethod->getLocation());
1143 Diag(MethodLoc, diag::warn_atomic_property_rule)
1144 << Property->getIdentifier();
1145 Diag(Property->getLocation(), diag::note_property_declare);
1146 }
1147 }
1148 }
1149}
1150
Mike Stump1eb44332009-09-09 15:08:12 +00001151/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001152Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001153Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001154 IdentifierInfo **IdentList,
1155 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001156 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Chris Lattner4d391482007-12-12 07:09:47 +00001158 for (unsigned i = 0; i != NumElts; ++i) {
1159 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001160 NamedDecl *PrevDecl
1161 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001162 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001163 // Maybe we will complain about the shadowed template parameter.
1164 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1165 // Just pretend that we didn't see the previous declaration.
1166 PrevDecl = 0;
1167 }
1168
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001169 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001170 // GCC apparently allows the following idiom:
1171 //
1172 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1173 // @class XCElementToggler;
1174 //
Mike Stump1eb44332009-09-09 15:08:12 +00001175 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001176 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1177 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001178 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001179 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001180 } else if (TDD) {
1181 // a forward class declaration matching a typedef name of a class refers
1182 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001183 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001184 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1185 PrevDecl = OI->getDecl();
1186 }
Chris Lattner4d391482007-12-12 07:09:47 +00001187 }
Mike Stump1eb44332009-09-09 15:08:12 +00001188 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001189 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001190 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Steve Naroff8b26cbd2009-09-11 00:12:01 +00001191 IdentList[i],
1192 // FIXME: need to get the 'real'
1193 // identifier loc from the parser.
1194 AtClassLoc, true);
Steve Naroff8f06f842009-04-23 16:00:56 +00001195 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +00001196 }
1197
1198 Interfaces.push_back(IDecl);
1199 }
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Douglas Gregord0434102009-01-09 00:49:46 +00001201 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Anders Carlsson15281452008-11-04 16:57:32 +00001202 &Interfaces[0],
1203 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001204 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001205 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001206 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001207}
1208
1209
1210/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1211/// returns true, or false, accordingly.
1212/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001213bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001214 const ObjCMethodDecl *PrevMethod,
1215 bool matchBasedOnSizeAndAlignment) {
1216 QualType T1 = Context.getCanonicalType(Method->getResultType());
1217 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001219 if (T1 != T2) {
1220 // The result types are different.
1221 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001222 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001223 // Incomplete types don't have a size and alignment.
1224 if (T1->isIncompleteType() || T2->isIncompleteType())
1225 return false;
1226 // Check is based on size and alignment.
1227 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1228 return false;
1229 }
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Chris Lattner89951a82009-02-20 18:43:26 +00001231 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1232 E = Method->param_end();
1233 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Chris Lattner89951a82009-02-20 18:43:26 +00001235 for (; ParamI != E; ++ParamI, ++PrevI) {
1236 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1237 T1 = Context.getCanonicalType((*ParamI)->getType());
1238 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001239 if (T1 != T2) {
1240 // The result types are different.
1241 if (!matchBasedOnSizeAndAlignment)
1242 return false;
1243 // Incomplete types don't have a size and alignment.
1244 if (T1->isIncompleteType() || T2->isIncompleteType())
1245 return false;
1246 // Check is based on size and alignment.
1247 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1248 return false;
1249 }
Chris Lattner4d391482007-12-12 07:09:47 +00001250 }
1251 return true;
1252}
1253
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001254/// \brief Read the contents of the instance and factory method pools
1255/// for a given selector from external storage.
1256///
1257/// This routine should only be called once, when neither the instance
1258/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001259Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001260 bool isInstance) {
1261 assert(ExternalSource && "We need an external AST source");
1262 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1263 "Selector data already loaded into the instance method pool");
1264 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1265 "Selector data already loaded into the factory method pool");
1266
1267 // Read the method list from the external source.
1268 std::pair<ObjCMethodList, ObjCMethodList> Methods
1269 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001271 if (isInstance) {
1272 if (Methods.second.Method)
1273 FactoryMethodPool[Sel] = Methods.second;
1274 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001275 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001276
1277 if (Methods.first.Method)
1278 InstanceMethodPool[Sel] = Methods.first;
1279
1280 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1281}
1282
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001283void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001284 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1285 = InstanceMethodPool.find(Method->getSelector());
1286 if (Pos == InstanceMethodPool.end()) {
1287 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1288 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1289 else
1290 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1291 ObjCMethodList())).first;
1292 }
1293
1294 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001295 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001296 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001297 Entry.Method = Method;
1298 Entry.Next = 0;
1299 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001300 }
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Chris Lattnerb25df352009-03-04 05:16:45 +00001302 // We've seen a method with this name, see if we have already seen this type
1303 // signature.
1304 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1305 if (MatchTwoMethodDeclarations(Method, List->Method))
1306 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Chris Lattnerb25df352009-03-04 05:16:45 +00001308 // We have a new signature for an existing method - add it.
1309 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1310 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001311}
1312
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001313// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001314ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001315 SourceRange R,
1316 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001317 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1318 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001319 if (Pos == InstanceMethodPool.end()) {
1320 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001321 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1322 else
1323 return 0;
1324 }
1325
1326 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001327 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Steve Naroff037cda52008-09-30 14:38:43 +00001329 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001330 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1331 // This checks if the methods differ by size & alignment.
1332 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001333 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001334 }
1335 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001336 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +00001337 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001338 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001339 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +00001340 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001341 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001342 }
1343 return MethList.Method;
1344}
1345
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001346void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001347 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1348 = FactoryMethodPool.find(Method->getSelector());
1349 if (Pos == FactoryMethodPool.end()) {
1350 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1351 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1352 else
1353 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1354 ObjCMethodList())).first;
1355 }
1356
1357 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001358 if (!FirstMethod.Method) {
1359 // Haven't seen a method with this selector name yet - add it.
1360 FirstMethod.Method = Method;
1361 FirstMethod.Next = 0;
1362 } else {
1363 // We've seen a method with this name, now check the type signature(s).
1364 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001365
1366 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001367 Next = Next->Next)
1368 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Chris Lattner4d391482007-12-12 07:09:47 +00001370 if (!match) {
1371 // We have a new signature for an existing method - add it.
1372 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001373 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001374 FirstMethod.Next = OMI;
1375 }
1376 }
1377}
1378
Mike Stump1eb44332009-09-09 15:08:12 +00001379ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001380 SourceRange R) {
1381 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1382 = FactoryMethodPool.find(Sel);
1383 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001384 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001385 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1386 else
1387 return 0;
1388 }
1389
1390 ObjCMethodList &MethList = Pos->second;
1391 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001393 if (MethList.Method && MethList.Next) {
1394 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1395 // This checks if the methods differ by size & alignment.
1396 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1397 issueWarning = true;
1398 }
1399 if (issueWarning && (MethList.Method && MethList.Next)) {
1400 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
1401 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
1402 << MethList.Method->getSourceRange();
1403 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1404 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
1405 << Next->Method->getSourceRange();
1406 }
1407 return MethList.Method;
1408}
1409
Mike Stump1eb44332009-09-09 15:08:12 +00001410/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff0701bbb2009-01-08 17:28:14 +00001411/// have the property type and issue diagnostics if they don't.
1412/// Also synthesize a getter/setter method if none exist (and update the
1413/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1414/// methods is the "right" thing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001415void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001416 ObjCContainerDecl *CD) {
1417 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00001418
1419 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001420 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump1eb44332009-09-09 15:08:12 +00001421 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001422 property->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001424 if (SetterMethod) {
Mike Stump1eb44332009-09-09 15:08:12 +00001425 if (Context.getCanonicalType(SetterMethod->getResultType())
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001426 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001427 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001428 if (SetterMethod->param_size() != 1 ||
1429 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001430 Diag(property->getLocation(),
1431 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001432 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001433 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001434 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1435 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001436 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001437
1438 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001439 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001440 // FIXME: The synthesized property we set here is misleading. We almost always
1441 // synthesize these methods unless the user explicitly provided prototypes
1442 // (which is odd, but allowed). Sema should be typechecking that the
1443 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001444 if (!GetterMethod) {
1445 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001446 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001447 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001448 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1449 property->getLocation(), property->getGetterName(),
1450 property->getType(), CD, true, false, true,
1451 (property->getPropertyImplementation() ==
1452 ObjCPropertyDecl::Optional) ?
1453 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001454 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001455 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001456 } else
1457 // A user declared getter will be synthesize when @synthesize of
1458 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001459 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001460 property->setGetterMethodDecl(GetterMethod);
1461
1462 // Skip setter if property is read-only.
1463 if (!property->isReadOnly()) {
1464 // Find the default setter and if one not found, add one.
1465 if (!SetterMethod) {
1466 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001467 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001468 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001469 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1470 property->getLocation(),
1471 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001472 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001473 (property->getPropertyImplementation() ==
1474 ObjCPropertyDecl::Optional) ?
1475 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001476 ObjCMethodDecl::Required);
1477 // Invent the arguments for the setter. We don't bother making a
1478 // nice name for the argument.
1479 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001480 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001481 property->getIdentifier(),
1482 property->getType(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001483 /*DInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001484 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001485 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001486 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001487 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001488 } else
1489 // A user declared setter will be synthesize when @synthesize of
1490 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001491 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001492 property->setSetterMethodDecl(SetterMethod);
1493 }
Mike Stump1eb44332009-09-09 15:08:12 +00001494 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001495 // handle the following, which is supported by GCC (and part of the design).
1496 //
1497 // @interface Foo
1498 // @property double bar;
1499 // @end
1500 //
1501 // void thisIsUnfortunate() {
1502 // id foo;
1503 // double bar = [foo bar];
1504 // }
1505 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001506 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001507 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001508 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001509 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001510}
1511
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001512/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1513/// identical selector names in current and its super classes and issues
1514/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001515void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1516 ObjCMethodDecl *Method,
1517 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001518 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1519 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001521 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001522 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001523 SD->lookupMethod(Method->getSelector(), IsInstance);
1524 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001525 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001526 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001527 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001528 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1529 E = Method->param_end();
1530 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1531 for (; ParamI != E; ++ParamI, ++PrevI) {
1532 // Number of parameters are the same and is guaranteed by selector match.
1533 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1534 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1535 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1536 // If type of arguement of method in this class does not match its
1537 // respective argument type in the super class method, issue warning;
1538 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001539 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001540 << T1 << T2;
1541 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1542 return;
1543 }
1544 }
1545 ID = SD;
1546 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001547}
1548
Steve Naroffa56f6162007-12-18 01:30:32 +00001549// Note: For class/category implemenations, allMethods/allProperties is
1550// always null.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001551void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl,
1552 DeclPtrTy *allMethods, unsigned allNum,
1553 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001554 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001555 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001556
Steve Naroffa56f6162007-12-18 01:30:32 +00001557 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1558 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001559 // should be true.
1560 if (!ClassDecl)
1561 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001562
Mike Stump1eb44332009-09-09 15:08:12 +00001563 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001564 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1565 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001566 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001567
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001568 if (!isInterfaceDeclKind && AtEndLoc.isInvalid()) {
1569 AtEndLoc = ClassDecl->getLocation();
1570 Diag(AtEndLoc, diag::warn_missing_atend);
1571 }
1572
Steve Naroff0701bbb2009-01-08 17:28:14 +00001573 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001574
1575 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1576 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1577 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1578
Chris Lattner4d391482007-12-12 07:09:47 +00001579 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001580 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001581 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001582
1583 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001584 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001585 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001586 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001587 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001588 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001589 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001590 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001591 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001592 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001593 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001594 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001595 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001596 InsMap[Method->getSelector()] = Method;
1597 /// The following allows us to typecheck messages to "id".
1598 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001599 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001600 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001601 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001602 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001603 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001604 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001605 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001606 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001607 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001608 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001609 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001610 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001611 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001612 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001613 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001614 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001615 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001616 /// The following allows us to typecheck messages to "Class".
1617 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001618 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001619 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001620 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001621 }
1622 }
1623 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001624 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001625 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001626 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001627 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001628 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001629 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001630 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001631 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001632 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001633
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001634 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001635 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001636 if (C->getIdentifier() == 0)
1637 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001638 }
Steve Naroff09c47192009-01-09 15:36:25 +00001639 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1640 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1641 // user-defined setter/getter. It also synthesizes setter/getter methods
1642 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001643 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1644 E = CDecl->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001645 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001646 ProcessPropertyDecl(*I, CDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001647 CDecl->setAtEndLoc(AtEndLoc);
1648 }
1649 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001650 IC->setAtEndLoc(AtEndLoc);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001651 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001652 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001653 AtomicPropertySetterGetterRules(IC, IDecl);
1654 }
Mike Stump1eb44332009-09-09 15:08:12 +00001655 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001656 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +00001657 CatImplClass->setAtEndLoc(AtEndLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Chris Lattner4d391482007-12-12 07:09:47 +00001659 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001660 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001661 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001662 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001663 Categories; Categories = Categories->getNextClassCategory()) {
1664 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001665 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001666 break;
1667 }
1668 }
1669 }
1670 }
Chris Lattner682bf922009-03-29 16:50:03 +00001671 if (isInterfaceDeclKind) {
1672 // Reject invalid vardecls.
1673 for (unsigned i = 0; i != tuvNum; i++) {
1674 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1675 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1676 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001677 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001678 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001679 }
Chris Lattner682bf922009-03-29 16:50:03 +00001680 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001681 }
Chris Lattner4d391482007-12-12 07:09:47 +00001682}
1683
1684
1685/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1686/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001687static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001688CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1689 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1690 if (PQTVal & ObjCDeclSpec::DQ_In)
1691 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1692 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1693 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1694 if (PQTVal & ObjCDeclSpec::DQ_Out)
1695 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1696 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1697 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1698 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1699 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1700 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1701 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001702
1703 return ret;
1704}
1705
Chris Lattnerb28317a2009-03-28 19:18:32 +00001706Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001707 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001708 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001709 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001710 Selector Sel,
1711 // optional arguments. The number of types/arguments is obtained
1712 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001713 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001714 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001715 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1716 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001717 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001718
1719 // Make sure we can establish a context for the method.
1720 if (!ClassDecl) {
1721 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanian32844b32009-08-28 17:52:37 +00001722 FunctionLabelMap.clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001723 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001724 }
Chris Lattner4d391482007-12-12 07:09:47 +00001725 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Steve Naroffccef3712009-02-20 22:59:16 +00001727 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001728 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Steve Naroffccef3712009-02-20 22:59:16 +00001730 // Methods cannot return interface types. All ObjC objects are
1731 // passed by reference.
1732 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001733 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1734 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001735 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001736 }
1737 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001738 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001739
1740 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001741 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00001742 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001743 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001744 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001745 MethodDeclKind == tok::objc_optional ?
1746 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001747 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001748
Chris Lattner0ed844b2008-04-04 06:12:32 +00001749 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Chris Lattner7db638d2009-04-11 19:42:43 +00001751 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001752 QualType ArgType;
1753 DeclaratorInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Chris Lattnere294d3f2009-04-11 18:57:04 +00001755 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001756 ArgType = Context.getObjCIdType();
1757 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001758 } else {
John McCall58e46772009-10-23 21:48:59 +00001759 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001760 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001761 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001762 }
Mike Stump1eb44332009-09-09 15:08:12 +00001763
John McCall58e46772009-10-23 21:48:59 +00001764 ParmVarDecl* Param
1765 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1766 ArgInfo[i].Name, ArgType, DI,
1767 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001769 if (ArgType->isObjCInterfaceType()) {
1770 Diag(ArgInfo[i].NameLoc,
1771 diag::err_object_cannot_be_passed_returned_by_value)
1772 << 1 << ArgType;
1773 Param->setInvalidDecl();
1774 }
Mike Stump1eb44332009-09-09 15:08:12 +00001775
Chris Lattner0ed844b2008-04-04 06:12:32 +00001776 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001777 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001779 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001780 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Chris Lattner0ed844b2008-04-04 06:12:32 +00001782 Params.push_back(Param);
1783 }
1784
Jay Foadbeaaccd2009-05-21 09:52:38 +00001785 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001786 ObjCMethod->setObjCDeclQualifier(
1787 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1788 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001789
1790 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001791 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001792
John McCall54abf7d2009-11-04 02:18:39 +00001793 const ObjCMethodDecl *InterfaceMD = 0;
1794
Mike Stump1eb44332009-09-09 15:08:12 +00001795 // For implementations (which can be very "coarse grain"), we add the
1796 // method now. This allows the AST to implement lookup methods that work
1797 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001798 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001799 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001800 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001801 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001802 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1803 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001804 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001805 PrevMethod = ImpDecl->getClassMethod(Sel);
1806 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001807 }
John McCall54abf7d2009-11-04 02:18:39 +00001808 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1809 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001810 if (AttrList)
1811 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001812 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001813 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001814 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001815 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1816 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001817 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001818 PrevMethod = CatImpDecl->getClassMethod(Sel);
1819 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001820 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001821 if (AttrList)
1822 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001823 }
1824 if (PrevMethod) {
1825 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001826 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001827 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001828 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001829 }
John McCall54abf7d2009-11-04 02:18:39 +00001830
1831 // If the interface declared this method, and it was deprecated there,
1832 // mark it deprecated here.
1833 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1834 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1835
Chris Lattnerb28317a2009-03-28 19:18:32 +00001836 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001837}
1838
Mike Stump1eb44332009-09-09 15:08:12 +00001839void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001840 SourceLocation Loc,
1841 unsigned &Attributes) {
1842 // FIXME: Improve the reported location.
1843
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001844 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001845 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001846 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1847 ObjCDeclSpec::DQ_PR_assign |
1848 ObjCDeclSpec::DQ_PR_copy |
1849 ObjCDeclSpec::DQ_PR_retain))) {
1850 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1851 "readwrite" :
1852 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1853 "assign" :
1854 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1855 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00001856
1857 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001858 diag::err_objc_property_attr_mutually_exclusive :
1859 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001860 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001861 }
1862
1863 // Check for copy or retain on non-object types.
1864 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001865 !PropertyTy->isObjCObjectPointerType() &&
1866 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00001867 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001868 Diag(Loc, diag::err_objc_property_requires_object)
1869 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001870 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1871 }
1872
1873 // Check for more than one of { assign, copy, retain }.
1874 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1875 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001876 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1877 << "assign" << "copy";
1878 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00001879 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001880 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001881 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1882 << "assign" << "retain";
1883 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001884 }
1885 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1886 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001887 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1888 << "copy" << "retain";
1889 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001890 }
1891 }
1892
1893 // Warn if user supplied no assignment attribute, property is
1894 // readwrite, and this is an object type.
1895 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1896 ObjCDeclSpec::DQ_PR_retain)) &&
1897 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00001898 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001899 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00001900 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001901 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1902
1903 // If non-gc code warn that this is likely inappropriate.
1904 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1905 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001907 // FIXME: Implement warning dependent on NSCopying being
1908 // implemented. See also:
1909 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1910 // (please trim this list while you are at it).
1911 }
Mike Stump046efd92009-05-07 23:06:50 +00001912
1913 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1914 && getLangOptions().getGCMode() == LangOptions::GCOnly
1915 && PropertyTy->isBlockPointerType())
1916 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001917}
1918
Mike Stump1eb44332009-09-09 15:08:12 +00001919Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001920 FieldDeclarator &FD,
1921 ObjCDeclSpec &ODS,
1922 Selector GetterSel,
1923 Selector SetterSel,
1924 DeclPtrTy ClassCategory,
1925 bool *isOverridingProperty,
1926 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001927 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001928 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1929 // default is readwrite!
1930 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00001931 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001932 // not retain or copy
1933 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001934 (isReadWrite &&
1935 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001936 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1937 QualType T = GetTypeForDeclarator(FD.D, S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001938 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001939 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001940 // May modify Attributes.
1941 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001942 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1943 if (!CDecl->getIdentifier()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001944 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001945 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001946 if ((CCPrimary = CDecl->getClassInterface())) {
1947 // Find the property in continuation class's primary class only.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001948 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00001949 if (ObjCPropertyDecl *PIDecl =
1950 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001951 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00001952 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001953 unsigned PIkind = PIDecl->getPropertyAttributes();
1954 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00001955 unsigned retainCopyNonatomic =
Fariborz Jahaniand669be52009-11-06 22:59:12 +00001956 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00001957 ObjCPropertyDecl::OBJC_PR_copy |
1958 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00001959 if ((Attributes & retainCopyNonatomic) !=
1960 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001961 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00001962 Diag(PIDecl->getLocation(), diag::note_property_declare);
1963 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001964 PIDecl->makeitReadWriteAttribute();
1965 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1966 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1967 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1968 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1969 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00001970 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001971 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001972 << CCPrimary->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00001973 Diag(PIDecl->getLocation(), diag::note_property_declare);
1974 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001975 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001976 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00001977 // class's list.
1978 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001979 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001980 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00001981 // No matching property found in the primary class. Just fall thru
1982 // and add property to continuation class's primary class.
1983 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001984 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00001985 Diag(CDecl->getLocation(), diag::err_continuation_class);
1986 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001987 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001988 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001989 }
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001991 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00001992 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001993 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
1994 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
1995 if (T->isObjCObjectPointerType()) {
1996 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001997 if (const ObjCInterfaceType *OIT =
John McCall183700f2009-09-21 23:43:11 +00001998 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00001999 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002000 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002001 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002002 LookupProtocol(&Context.Idents.get("NSCopying")))
2003 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00002004 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002005 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002006 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002007 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00002008 if (T->isObjCInterfaceType())
2009 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002010
Steve Naroff93983f82009-01-11 12:47:58 +00002011 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2012 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002013 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00002014 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00002015 FD.D.getIdentifier(), T);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002016 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002018 if (T->isArrayType() || T->isFunctionType()) {
2019 Diag(AtLoc, diag::err_property_type) << T;
2020 PDecl->setInvalidDecl();
2021 }
Mike Stump1eb44332009-09-09 15:08:12 +00002022
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002023 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00002024
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00002025 // Regardless of setter/getter attribute, we save the default getter/setter
2026 // selector names in anticipation of declaration of setter/getter methods.
2027 PDecl->setGetterName(GetterSel);
2028 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00002029
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002030 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002031 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00002032
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002033 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002034 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00002035
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002036 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002037 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00002038
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002039 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002040 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00002041
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002042 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002043 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00002044
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002045 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002046 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00002047
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002048 if (isAssign)
2049 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00002050
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002051 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002052 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00002053
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00002054 if (MethodImplKind == tok::objc_required)
2055 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2056 else if (MethodImplKind == tok::objc_optional)
2057 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002058 // A case of continuation class adding a new property in the class. This
2059 // is not what it was meant for. However, gcc supports it and so should we.
2060 // Make sure setter/getters are declared here.
2061 if (CCPrimary)
2062 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002063
Chris Lattnerb28317a2009-03-28 19:18:32 +00002064 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002065}
2066
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002067/// ActOnPropertyImplDecl - This routine performs semantic checks and
2068/// builds the AST node for a property implementation declaration; declared
2069/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002070///
Mike Stump1eb44332009-09-09 15:08:12 +00002071Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002072 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002073 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002074 DeclPtrTy ClassCatImpDecl,
2075 IdentifierInfo *PropertyId,
2076 IdentifierInfo *PropertyIvar) {
2077 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002078 // Make sure we have a context for the property implementation declaration.
2079 if (!ClassImpDecl) {
2080 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002081 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002082 }
2083 ObjCPropertyDecl *property = 0;
2084 ObjCInterfaceDecl* IDecl = 0;
2085 // Find the class or category class where this property must have
2086 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002087 ObjCImplementationDecl *IC = 0;
2088 ObjCCategoryImplDecl* CatImplClass = 0;
2089 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002090 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002091 // We always synthesize an interface for an implementation
2092 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002093 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002094 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002095
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002096 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002097 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002098 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002099 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002100 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002101 }
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002102 if (const ObjCCategoryDecl *CD =
2103 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
2104 if (CD->getIdentifier()) {
2105 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002106 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002107 return DeclPtrTy();
2108 }
2109 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002110 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002111 if (Synthesize) {
2112 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002113 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002114 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002115 IDecl = CatImplClass->getClassInterface();
2116 if (!IDecl) {
2117 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002118 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002119 }
Mike Stump1eb44332009-09-09 15:08:12 +00002120 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002121 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002122
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002123 // If category for this implementation not found, it is an error which
2124 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002125 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002126 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002127 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002128 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002129 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002130 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002131 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002132 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002133 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002134 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002135 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002136 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002137 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002138 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002139 // Check that we have a valid, previously declared ivar for @synthesize
2140 if (Synthesize) {
2141 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002142 if (!PropertyIvar)
2143 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002144 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002145 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002146 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002147 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002148 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002149 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002150 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002151 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002152 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2153 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002154 ObjCIvarDecl::Public,
2155 (Expr *)0);
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002156 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002157 IDecl->addDecl(Ivar);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002158 property->setPropertyIvarDecl(Ivar);
2159 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002160 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002161 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002162 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002163 } else if (getLangOptions().ObjCNonFragileABI &&
2164 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002165 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002166 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002167 << ClassDeclared->getDeclName();
2168 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2169 << Ivar << Ivar->getNameAsCString();
2170 // Note! I deliberately want it to fall thru so more errors are caught.
2171 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002172 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002173
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002174 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002175 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002176 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002177 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002178 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002179 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002180 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002181 }
Mike Stump1eb44332009-09-09 15:08:12 +00002182
Chris Lattnerb28317a2009-03-28 19:18:32 +00002183 // FIXME! Rules for properties are somewhat different that those
2184 // for assignments. Use a new routine to consolidate all cases;
2185 // specifically for property redeclarations as well as for ivars.
2186 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2187 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002188 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002189 lhsType->isArithmeticType()) {
2190 Diag(PropertyLoc, diag::error_property_ivar_type)
2191 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002192 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002193 }
2194 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002195 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2196 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002197 Diag(PropertyLoc, diag::error_weak_property)
2198 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002199 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002200 }
Mike Stump1eb44332009-09-09 15:08:12 +00002201 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002202 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2203 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002204 Diag(PropertyLoc, diag::error_strong_property)
2205 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002206 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002207 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002208 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002209 } else if (PropertyIvar)
2210 // @dynamic
2211 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002212 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002213 ObjCPropertyImplDecl *PIDecl =
2214 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2215 property,
2216 (Synthesize ?
2217 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002218 : ObjCPropertyImplDecl::Dynamic),
2219 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002220 if (IC) {
2221 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002222 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002223 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002224 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2225 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002226 << PropertyIvar;
2227 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2228 }
Mike Stump1eb44332009-09-09 15:08:12 +00002229
2230 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002231 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002232 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2233 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002234 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002235 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002236 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002237 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002238 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002239 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002240 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002241 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2242 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002243 << PropertyIvar;
2244 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2245 }
Mike Stump1eb44332009-09-09 15:08:12 +00002246
2247 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002248 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002249 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2250 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002251 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002252 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002253 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002254 }
Mike Stump1eb44332009-09-09 15:08:12 +00002255
Chris Lattnerb28317a2009-03-28 19:18:32 +00002256 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002257}
Anders Carlsson15281452008-11-04 16:57:32 +00002258
Chris Lattnercc98eac2008-12-17 07:13:27 +00002259bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002260 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002261 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002262
Anders Carlsson15281452008-11-04 16:57:32 +00002263 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2264 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002265
Anders Carlsson15281452008-11-04 16:57:32 +00002266 return true;
2267}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002268
Chris Lattnercc98eac2008-12-17 07:13:27 +00002269/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2270/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002271void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002272 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002273 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002274 // Check that ClassName is a valid class
2275 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2276 if (!Class) {
2277 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2278 return;
2279 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002280 if (LangOpts.ObjCNonFragileABI) {
2281 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2282 return;
2283 }
Mike Stump1eb44332009-09-09 15:08:12 +00002284
Chris Lattnercc98eac2008-12-17 07:13:27 +00002285 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002286 llvm::SmallVector<FieldDecl*, 32> RecFields;
2287 Context.CollectObjCIvars(Class, RecFields);
2288 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2289 for (unsigned i = 0; i < RecFields.size(); i++) {
2290 FieldDecl* ID = RecFields[i];
2291 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2292 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2293 ID->getIdentifier(), ID->getType(),
2294 ID->getBitWidth());
2295 Decls.push_back(Sema::DeclPtrTy::make(FD));
2296 }
Mike Stump1eb44332009-09-09 15:08:12 +00002297
Chris Lattnercc98eac2008-12-17 07:13:27 +00002298 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002299 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002300 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002301 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002302 if (getLangOptions().CPlusPlus)
2303 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002304 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002305 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002306 }
2307}
2308