blob: cfea87504d45d562d59ac953dfbb80bd4608eb7d [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 Gregorf06cdae2010-01-03 18:01:57 +000015#include "Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
Steve Naroffca331292009-03-03 14:49:36 +000017#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000021using namespace clang;
22
Fariborz Jahanianc001e892009-05-08 20:20:55 +000023bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
24 ObjCMethodDecl *GetterMethod,
25 SourceLocation Loc) {
26 if (GetterMethod &&
27 GetterMethod->getResultType() != property->getType()) {
28 AssignConvertType result = Incompatible;
Steve Narofff4954562009-07-16 15:41:00 +000029 if (property->getType()->isObjCObjectPointerType())
Fariborz Jahanian7aaa4092009-05-08 21:10:00 +000030 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
Fariborz Jahanianc001e892009-05-08 20:20:55 +000031 if (result != Compatible) {
Mike Stump1eb44332009-09-09 15:08:12 +000032 Diag(Loc, diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianc001e892009-05-08 20:20:55 +000033 << property->getDeclName()
34 << GetterMethod->getSelector();
35 Diag(GetterMethod->getLocation(), diag::note_declared_at);
36 return true;
37 }
38 }
39 return false;
40}
41
Steve Naroffebf64432009-02-28 16:59:13 +000042/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000043/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000044void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000045 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000046 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump1eb44332009-09-09 15:08:12 +000047
Steve Naroff394f3f42008-07-25 17:57:26 +000048 // If we don't have a valid method decl, simply return.
49 if (!MDecl)
50 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000051
Chris Lattner38c5ebd2009-04-19 05:21:20 +000052 CurFunctionNeedsScopeChecking = false;
53
Steve Naroffa56f6162007-12-18 01:30:32 +000054 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000055 if (MDecl->isInstanceMethod())
Steve Naroffa56f6162007-12-18 01:30:32 +000056 AddInstanceMethodToGlobalPool(MDecl);
57 else
58 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000059
Chris Lattner4d391482007-12-12 07:09:47 +000060 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000061 PushDeclContext(FnBodyScope, MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000062
63 // Create Decl objects for each parameter, entrring them in the scope for
64 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000065
66 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000067 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +000068
Daniel Dunbar451318c2008-08-26 06:07:48 +000069 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
70 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000071
Chris Lattner8123a952008-04-10 02:22:51 +000072 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000073 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
74 E = MDecl->param_end(); PI != E; ++PI)
75 if ((*PI)->getIdentifier())
76 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000077}
78
Chris Lattnerb28317a2009-03-28 19:18:32 +000079Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000080ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
81 IdentifierInfo *ClassName, SourceLocation ClassLoc,
82 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000083 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +000084 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000085 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000086 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner4d391482007-12-12 07:09:47 +000088 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +000089 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +000090 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000091 // Maybe we will complain about the shadowed template parameter.
92 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
93 // Just pretend that we didn't see the previous declaration.
94 PrevDecl = 0;
95 }
96
Ted Kremeneka526c5c2008-01-07 19:49:32 +000097 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000098 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000099 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000100 }
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000102 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000103 if (IDecl) {
104 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000105 if (!IDecl->isForwardDecl()) {
Chris Lattner1829a6d2009-02-23 22:00:08 +0000106 IDecl->setInvalidDecl();
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000107 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000108 Diag(IDecl->getLocation(), diag::note_previous_definition);
109
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000110 // Return the previous class interface.
111 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000112 return DeclPtrTy::make(IDecl);
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000113 } else {
Chris Lattner4d391482007-12-12 07:09:47 +0000114 IDecl->setLocation(AtInterfaceLoc);
115 IDecl->setForwardDecl(false);
Steve Naroff8b26cbd2009-09-11 00:12:01 +0000116 IDecl->setClassLoc(ClassLoc);
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000117
118 // Since this ObjCInterfaceDecl was created by a forward declaration,
119 // we now add it to the DeclContext since it wasn't added before
120 // (see ActOnForwardClassDeclaration).
121 CurContext->addDecl(IDecl);
122
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000123 if (AttrList)
124 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000125 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000126 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000127 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000128 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000129 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000130 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Steve Naroffa7503a72009-04-23 15:15:40 +0000132 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000133 }
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Chris Lattner4d391482007-12-12 07:09:47 +0000135 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000136 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000137 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000138
139 if (!PrevDecl) {
140 // Try to correct for a typo in the superclass name.
141 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
142 if (CorrectTypo(R, TUScope, 0) &&
143 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
144 Diag(SuperLoc, diag::err_undef_superclass_suggest)
145 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000146 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
147 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000148 }
149 }
150
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000151 if (PrevDecl == IDecl) {
152 Diag(SuperLoc, diag::err_recursive_superclass)
153 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
154 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000155 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000156 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000157 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000158
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000159 // Diagnose classes that inherit from deprecated classes.
160 if (SuperClassDecl)
161 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000163 if (PrevDecl && SuperClassDecl == 0) {
164 // The previous declaration was not a class decl. Check if we have a
165 // typedef. If we do, get the underlying class type.
166 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
167 QualType T = TDecl->getUnderlyingType();
168 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000169 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000170 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
171 }
172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000174 // This handles the following case:
175 //
176 // typedef int SuperClass;
177 // @interface MyClass : SuperClass {} @end
178 //
179 if (!SuperClassDecl) {
180 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
181 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000182 }
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000185 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
186 if (!SuperClassDecl)
187 Diag(SuperLoc, diag::err_undef_superclass)
188 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
189 else if (SuperClassDecl->isForwardDecl())
190 Diag(SuperLoc, diag::err_undef_superclass)
191 << SuperClassDecl->getDeclName() << ClassName
192 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000193 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000194 IDecl->setSuperClass(SuperClassDecl);
195 IDecl->setSuperClassLoc(SuperLoc);
196 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000197 }
Chris Lattner4d391482007-12-12 07:09:47 +0000198 } else { // we have a root class.
199 IDecl->setLocEnd(ClassLoc);
200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000202 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000203 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000204 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000205 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000206 IDecl->setLocEnd(EndProtoLoc);
207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Anders Carlsson15281452008-11-04 16:57:32 +0000209 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000210 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000211}
212
213/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000214/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000215Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000216 IdentifierInfo *AliasName,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000217 SourceLocation AliasLocation,
218 IdentifierInfo *ClassName,
219 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000220 // Look for previous declaration of alias name
John McCallf36e02d2009-10-09 21:13:30 +0000221 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000222 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000223 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000224 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000225 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000226 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000227 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000228 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000229 }
230 // Check for class declaration
John McCallf36e02d2009-10-09 21:13:30 +0000231 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000232 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
233 QualType T = TDecl->getUnderlyingType();
234 if (T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000235 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000236 ClassName = IDecl->getIdentifier();
John McCallf36e02d2009-10-09 21:13:30 +0000237 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000238 }
239 }
240 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000241 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
242 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000243 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000244 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000245 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000246 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000247 }
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000249 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000250 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000251 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Anders Carlsson15281452008-11-04 16:57:32 +0000253 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000254 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000255
Chris Lattnerb28317a2009-03-28 19:18:32 +0000256 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000257}
258
Steve Naroff61d68522009-03-05 15:22:01 +0000259void Sema::CheckForwardProtocolDeclarationForCircularDependency(
260 IdentifierInfo *PName,
261 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000262 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000263 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
264 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Douglas Gregor6e378de2009-04-23 23:18:26 +0000266 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff61d68522009-03-05 15:22:01 +0000267 if (PDecl->getIdentifier() == PName) {
268 Diag(Ploc, diag::err_protocol_has_circular_dependency);
269 Diag(PrevLoc, diag::note_previous_definition);
270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000272 PDecl->getLocation(), PDecl->getReferencedProtocols());
273 }
274 }
275}
276
Chris Lattnerb28317a2009-03-28 19:18:32 +0000277Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000278Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
279 IdentifierInfo *ProtocolName,
280 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000281 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000282 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000283 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000284 SourceLocation EndProtoLoc,
285 AttributeList *AttrList) {
286 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000287 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor6e378de2009-04-23 23:18:26 +0000288 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattner4d391482007-12-12 07:09:47 +0000289 if (PDecl) {
290 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000291 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000292 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000293 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000294 // Just return the protocol we already had.
295 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000296 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000297 }
Steve Naroff61d68522009-03-05 15:22:01 +0000298 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000299 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000300 CheckForwardProtocolDeclarationForCircularDependency(
301 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
302 PList.Destroy(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Steve Narofff11b5082008-08-13 16:39:22 +0000304 // Make sure the cached decl gets a valid start location.
305 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000306 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000307 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000308 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000309 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000310 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000311 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000312 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000313 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000314 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000315 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000316 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000317 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
318 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000319 PDecl->setLocEnd(EndProtoLoc);
320 }
Mike Stump1eb44332009-09-09 15:08:12 +0000321
322 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000323 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000324}
325
326/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000327/// issues an error if they are not declared. It returns list of
328/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000329void
Chris Lattnere13b9592008-07-26 04:03:38 +0000330Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000331 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000332 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000333 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000334 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000335 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattnereacc3922008-07-26 03:47:43 +0000336 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000337 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
338 LookupObjCProtocolName);
339 if (CorrectTypo(R, TUScope, 0) &&
340 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
341 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
342 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000343 Diag(PDecl->getLocation(), diag::note_previous_decl)
344 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000345 }
346 }
347
348 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000349 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000350 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000351 continue;
352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000354 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000355
356 // If this is a forward declaration and we are supposed to warn in this
357 // case, do it.
358 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000359 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000360 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000361 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000362 }
363}
364
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000365/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000366/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000367///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000368void
Mike Stump1eb44332009-09-09 15:08:12 +0000369Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000370 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000371 const IdentifierInfo *inheritedName) {
Mike Stump1eb44332009-09-09 15:08:12 +0000372 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000373 Property->getPropertyAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000374 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000375 SuperProperty->getPropertyAttributes();
376 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
377 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000378 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000379 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000380 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
381 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000382 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000383 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000384 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
385 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000386 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000387 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000389 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
390 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000391 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000392 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000393 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000394 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump1eb44332009-09-09 15:08:12 +0000395 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000396 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000397 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000398 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff15edf0d2009-03-03 15:43:24 +0000399
Mike Stump1eb44332009-09-09 15:08:12 +0000400 QualType LHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000401 Context.getCanonicalType(SuperProperty->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000402 QualType RHSType =
Steve Naroff15edf0d2009-03-03 15:43:24 +0000403 Context.getCanonicalType(Property->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Steve Naroff15edf0d2009-03-03 15:43:24 +0000405 if (!Context.typesAreCompatible(LHSType, RHSType)) {
406 // FIXME: Incorporate this test with typesAreCompatible.
407 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
Steve Naroff4084c302009-07-23 01:01:38 +0000408 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff15edf0d2009-03-03 15:43:24 +0000409 return;
410 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
411 << Property->getType() << SuperProperty->getType() << inheritedName;
412 }
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000413}
414
415/// ComparePropertiesInBaseAndSuper - This routine compares property
416/// declarations in base and its super class, if any, and issues
417/// diagnostics in a variety of inconsistant situations.
418///
Chris Lattner70f19542009-02-16 21:26:43 +0000419void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000420 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
421 if (!SDecl)
422 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000423 // FIXME: O(N^2)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000424 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
425 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000426 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000427 // Does property in super class has declaration in current class?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000428 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
429 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000430 ObjCPropertyDecl *PDecl = (*I);
431 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump1eb44332009-09-09 15:08:12 +0000432 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000433 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000434 }
435 }
436}
437
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000438/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
439/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000440/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000441void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000442Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000443 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000444 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
445 if (!IDecl) {
446 // Category
447 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
448 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000449 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
450 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000451 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000452 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000453 // Is this property already in category's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000454 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000455 if ((*CP)->getIdentifier() == Pr->getIdentifier())
456 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000457 if (CP != CE)
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000458 // Property protocol already exist in class. Diagnose any mismatch.
459 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
460 }
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000461 return;
462 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000463 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
464 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000465 ObjCPropertyDecl *Pr = (*P);
Steve Naroff09c47192009-01-09 15:36:25 +0000466 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000467 // Is this property already in class's list of properties?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000468 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000469 if ((*CP)->getIdentifier() == Pr->getIdentifier())
470 break;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000471 if (CP != CE)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000472 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000473 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000474 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000475}
476
477/// MergeProtocolPropertiesIntoClass - This routine merges properties
478/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000479/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000480///
Chris Lattner70f19542009-02-16 21:26:43 +0000481void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000482 DeclPtrTy MergeItsProtocols) {
483 Decl *ClassDecl = MergeItsProtocols.getAs<Decl>();
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000484 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
485
486 if (!IDecl) {
487 // Category
488 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
489 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
490 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
491 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
492 E = MDecl->protocol_end(); P != E; ++P)
493 // Merge properties of category (*P) into IDECL's
494 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000496 // Go thru the list of protocols for this category and recursively merge
497 // their properties into this class as well.
498 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
499 E = CatDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000500 MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000501 } else {
502 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
503 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
504 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000505 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000506 }
507 return;
508 }
509
Chris Lattnerb752f282008-07-21 07:06:49 +0000510 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000511 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
512 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000513 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000514 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000516 // Go thru the list of protocols for this class and recursively merge
517 // their properties into this class as well.
518 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
519 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000520 MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000521 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000522 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
523 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
524 E = MD->protocol_end(); P != E; ++P)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000525 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
Chris Lattnerb752f282008-07-21 07:06:49 +0000526 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000527}
528
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000529/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000530/// a class method in its extension.
531///
Mike Stump1eb44332009-09-09 15:08:12 +0000532void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000533 ObjCInterfaceDecl *ID) {
534 if (!ID)
535 return; // Possibly due to previous error
536
537 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000538 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
539 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000540 ObjCMethodDecl *MD = *i;
541 MethodMap[MD->getSelector()] = MD;
542 }
543
544 if (MethodMap.empty())
545 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000546 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
547 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000548 ObjCMethodDecl *Method = *i;
549 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
550 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
551 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
552 << Method->getDeclName();
553 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
554 }
555 }
556}
557
Chris Lattner58fe03b2009-04-12 08:43:13 +0000558/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000559Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000560Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000561 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000562 unsigned NumElts,
563 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000564 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000565 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Chris Lattner4d391482007-12-12 07:09:47 +0000567 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000568 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor6e378de2009-04-23 23:18:26 +0000569 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregord0434102009-01-09 00:49:46 +0000570 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000571 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000572 IdentList[i].second, Ident);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000573 PushOnScopeChains(PDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000574 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000575 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000576 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000577 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000578 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000579 }
Mike Stump1eb44332009-09-09 15:08:12 +0000580
581 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000582 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000583 Protocols.data(), Protocols.size(),
584 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000585 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000586 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000587 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000588}
589
Chris Lattnerb28317a2009-03-28 19:18:32 +0000590Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000591ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
592 IdentifierInfo *ClassName, SourceLocation ClassLoc,
593 IdentifierInfo *CategoryName,
594 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000595 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000596 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000597 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000598 SourceLocation EndProtoLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000599 ObjCCategoryDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000600 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
601 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000602 CurContext->addDecl(CDecl);
Chris Lattner70f19542009-02-16 21:26:43 +0000603
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000604 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000605 /// Check that class of this category is already completely declared.
Chris Lattner70f19542009-02-16 21:26:43 +0000606 if (!IDecl || IDecl->isForwardDecl()) {
607 CDecl->setInvalidDecl();
Chris Lattner3c73c412008-11-19 08:23:25 +0000608 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000609 return DeclPtrTy::make(CDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000610 }
Chris Lattner4d391482007-12-12 07:09:47 +0000611
Chris Lattner70f19542009-02-16 21:26:43 +0000612 CDecl->setClassInterface(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Chris Lattner16b34b42009-02-16 21:30:01 +0000614 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000615 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000616
617 /// Check for duplicate interface declaration for this category
618 ObjCCategoryDecl *CDeclChain;
619 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
620 CDeclChain = CDeclChain->getNextClassCategory()) {
621 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
622 Diag(CategoryLoc, diag::warn_dup_category_def)
623 << ClassName << CategoryName;
624 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
625 break;
626 }
627 }
628 if (!CDeclChain)
629 CDecl->insertNextClassCategory();
630
Chris Lattner4d391482007-12-12 07:09:47 +0000631 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000632 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000633 ProtoLocs, Context);
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000634 CDecl->setLocEnd(EndProtoLoc);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000635 // Protocols in the class extension belong to the class.
636 if (!CDecl->getIdentifier())
637 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000638 NumProtoRefs, ProtoLocs,
639 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000640 }
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Anders Carlsson15281452008-11-04 16:57:32 +0000642 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000643 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000644}
645
646/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000647/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000648/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000649Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000650 SourceLocation AtCatImplLoc,
651 IdentifierInfo *ClassName, SourceLocation ClassLoc,
652 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000653 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000654 ObjCCategoryDecl *CatIDecl = 0;
655 if (IDecl) {
656 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
657 if (!CatIDecl) {
658 // Category @implementation with no corresponding @interface.
659 // Create and install one.
660 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
661 CatName);
662 CatIDecl->setClassInterface(IDecl);
663 CatIDecl->insertNextClassCategory();
664 }
665 }
666
Mike Stump1eb44332009-09-09 15:08:12 +0000667 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000668 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
669 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000670 /// Check that class of this category is already completely declared.
671 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000672 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000673
Douglas Gregord0434102009-01-09 00:49:46 +0000674 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000675 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000676
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000677 /// Check that CatName, category name, is not used in another implementation.
678 if (CatIDecl) {
679 if (CatIDecl->getImplementation()) {
680 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
681 << CatName;
682 Diag(CatIDecl->getImplementation()->getLocation(),
683 diag::note_previous_definition);
684 } else
685 CatIDecl->setImplementation(CDecl);
686 }
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Anders Carlsson15281452008-11-04 16:57:32 +0000688 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000689 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000690}
691
Chris Lattnerb28317a2009-03-28 19:18:32 +0000692Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000693 SourceLocation AtClassImplLoc,
694 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000695 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000696 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000697 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000698 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000699 NamedDecl *PrevDecl
700 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000701 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000702 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000703 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000704 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
705 // If this is a forward declaration of an interface, warn.
706 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000707 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000708 IDecl = 0;
709 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000710 } else {
711 // We did not find anything with the name ClassName; try to correct for
712 // typos in the class name.
713 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
714 if (CorrectTypo(R, TUScope, 0) &&
715 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000716 // Suggest the (potentially) correct interface name. However, put the
717 // fix-it hint itself in a separate note, since changing the name in
718 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000719 // provide a code-modification hint or use the typo name for recovery,
720 // because this is just a warning. The program may actually be correct.
721 Diag(ClassLoc, diag::warn_undef_interface_suggest)
722 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000723 Diag(IDecl->getLocation(), diag::note_previous_decl)
724 << R.getLookupName()
725 << CodeModificationHint::CreateReplacement(ClassLoc,
726 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000727 IDecl = 0;
728 } else {
729 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
730 }
Chris Lattner4d391482007-12-12 07:09:47 +0000731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Chris Lattner4d391482007-12-12 07:09:47 +0000733 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000734 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000735 if (SuperClassname) {
736 // Check if a different kind of symbol declared in this scope.
John McCallf36e02d2009-10-09 21:13:30 +0000737 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000738 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000739 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
740 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000741 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000742 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000743 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000744 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000745 Diag(SuperClassLoc, diag::err_undef_superclass)
746 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000747 else if (IDecl && IDecl->getSuperClass() != SDecl) {
748 // This implementation and its interface do not have the same
749 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000750 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000751 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000752 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000753 }
754 }
755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Chris Lattner4d391482007-12-12 07:09:47 +0000757 if (!IDecl) {
758 // Legacy case of @implementation with no corresponding @interface.
759 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000760
Mike Stump390b4cc2009-05-16 07:39:55 +0000761 // FIXME: Do we support attributes on the @implementation? If so we should
762 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000763 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregord0434102009-01-09 00:49:46 +0000764 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000765 IDecl->setSuperClass(SDecl);
766 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000767
768 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbar24c89912009-04-21 21:41:56 +0000769 } else {
770 // Mark the interface as being completed, even if it was just as
771 // @class ....;
772 // declaration; the user cannot reopen it.
773 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
776 ObjCImplementationDecl* IMPDecl =
777 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000778 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Anders Carlsson15281452008-11-04 16:57:32 +0000780 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000781 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattner4d391482007-12-12 07:09:47 +0000783 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000784 if (IDecl->getImplementation()) {
Chris Lattner75c9cae2008-03-16 20:53:07 +0000785 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000786 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000787 Diag(IDecl->getImplementation()->getLocation(),
788 diag::note_previous_definition);
789 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000790 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000791 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000792 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000793 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000794}
795
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000796void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
797 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000798 SourceLocation RBrace) {
799 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000800 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000801 if (!IDecl)
802 return;
803 /// Check case of non-existing @interface decl.
804 /// (legacy objective-c @implementation decl without an @interface decl).
805 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000806 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000807 IDecl->setIVarList(ivars, numIvars, Context);
808 IDecl->setLocEnd(RBrace);
Chris Lattner4d391482007-12-12 07:09:47 +0000809 return;
810 }
811 // If implementation has empty ivar list, just return.
812 if (numIvars == 0)
813 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Chris Lattner4d391482007-12-12 07:09:47 +0000815 assert(ivars && "missing @implementation ivars");
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Chris Lattner4d391482007-12-12 07:09:47 +0000817 // Check interface's Ivar list against those in the implementation.
818 // names and types must match.
819 //
Chris Lattner4d391482007-12-12 07:09:47 +0000820 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000821 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000822 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
823 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000824 ObjCIvarDecl* ImplIvar = ivars[j++];
825 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000826 assert (ImplIvar && "missing implementation ivar");
827 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Steve Naroffca331292009-03-03 14:49:36 +0000829 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000830 if (Context.getCanonicalType(ImplIvar->getType()) !=
831 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000832 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000833 << ImplIvar->getIdentifier()
834 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000835 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000836 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
837 Expr *ImplBitWidth = ImplIvar->getBitWidth();
838 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000839 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
840 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000841 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
842 << ImplIvar->getIdentifier();
843 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845 }
Steve Naroffca331292009-03-03 14:49:36 +0000846 // Make sure the names are identical.
847 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000848 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000849 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000850 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000851 }
852 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000853 }
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Chris Lattner609e4c72007-12-12 18:11:49 +0000855 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000856 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000857 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000858 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000859}
860
Steve Naroff3c2eb662008-02-10 21:38:56 +0000861void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
862 bool &IncompleteImpl) {
863 if (!IncompleteImpl) {
864 Diag(ImpLoc, diag::warn_incomplete_impl);
865 IncompleteImpl = true;
866 }
Chris Lattner08631c52008-11-23 21:45:46 +0000867 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000868}
869
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000870void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
871 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000872 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000873 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000874 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
875 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000876 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000877 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
878 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000879 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattner3aff9192009-04-11 19:58:42 +0000882 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
883 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
884 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000885 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
886 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
887 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
888 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000889 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000890
891 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000892 << ImpMethodDecl->getDeclName() << (*IF)->getType()
893 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000894 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000895 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000896}
897
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000898/// isPropertyReadonly - Return true if property is readonly, by searching
899/// for the property in the class and in its categories and implementations
900///
901bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroff22dc0b02009-02-26 19:11:32 +0000902 ObjCInterfaceDecl *IDecl) {
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000903 // by far the most common case.
904 if (!PDecl->isReadOnly())
905 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000906 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000907 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000908 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000909 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000911 // Main class has the property as 'readonly'. Must search
Mike Stump1eb44332009-09-09 15:08:12 +0000912 // through the category list to see if the property's
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000913 // attribute has been over-ridden to 'readwrite'.
914 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
915 Category; Category = Category->getNextClassCategory()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000916 // Even if property is ready only, if a category has a user defined setter,
917 // it is not considered read only.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000918 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000919 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000920 ObjCPropertyDecl *P =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000921 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000922 if (P && !P->isReadOnly())
923 return false;
924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000926 // Also, check for definition of a setter method in the implementation if
927 // all else failed.
928 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000929 if (ObjCImplementationDecl *IMD =
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000930 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000931 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000932 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000933 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000934 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000935 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000936 return false;
937 }
938 }
Steve Naroff22dc0b02009-02-26 19:11:32 +0000939 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000940 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000941 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroff22dc0b02009-02-26 19:11:32 +0000942 return false;
Fariborz Jahanian50efe042009-04-06 16:59:10 +0000943 // If all fails, look at the super class.
944 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
945 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000946 return true;
947}
948
Mike Stump390b4cc2009-05-16 07:39:55 +0000949/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
950/// improve the efficiency of selector lookups and type checking by associating
951/// with each protocol / interface / category the flattened instance tables. If
952/// we used an immutable set to keep the table then it wouldn't add significant
953/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000954
Steve Naroffefe7f362008-02-08 22:06:17 +0000955/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000956/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000957void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
958 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000959 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000960 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000961 const llvm::DenseSet<Selector> &ClsMap,
962 ObjCInterfaceDecl *IDecl) {
963 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000964 ObjCInterfaceDecl *NSIDecl = 0;
965 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000966 // check to see if class implements forwardInvocation method and objects
967 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000968 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000969 // Under such conditions, which means that every method possible is
970 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000971 // found" warnings.
972 // FIXME: Use a general GetUnarySelector method for this.
973 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
974 Selector fISelector = Context.Selectors.getSelector(1, &II);
975 if (InsMap.count(fISelector))
976 // Is IDecl derived from 'NSProxy'? If so, no instance methods
977 // need be implemented in the implementation.
978 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
979 }
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000981 // If a method lookup fails locally we still need to look and see if
982 // the method was implemented by a base class or an inherited
983 // protocol. This lookup is slow, but occurs rarely in correct code
984 // and otherwise would terminate in a warning.
985
Chris Lattner4d391482007-12-12 07:09:47 +0000986 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000987 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000988 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000989 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000990 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000991 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000992 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000993 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000994 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000995 // Ugly, but necessary. Method declared in protcol might have
996 // have been synthesized due to a property declared in the class which
997 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000998 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000999 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001000 if (!MethodInClass || !MethodInClass->isSynthesized())
1001 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
1002 }
1003 }
Chris Lattner4d391482007-12-12 07:09:47 +00001004 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001005 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001006 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001007 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001008 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001009 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1010 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001011 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +00001012 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001013 }
Chris Lattner780f3292008-07-21 21:32:27 +00001014 // Check on this protocols's referenced protocols, recursively.
1015 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1016 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001017 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001018}
1019
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001020/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1021/// or protocol against those declared in their implementations.
1022///
1023void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1024 const llvm::DenseSet<Selector> &ClsMap,
1025 llvm::DenseSet<Selector> &InsMapSeen,
1026 llvm::DenseSet<Selector> &ClsMapSeen,
1027 ObjCImplDecl* IMPDecl,
1028 ObjCContainerDecl* CDecl,
1029 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +00001030 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001031 // Check and see if instance methods in class interface have been
1032 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001033 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1034 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001035 if (InsMapSeen.count((*I)->getSelector()))
1036 continue;
1037 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001038 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001039 !InsMap.count((*I)->getSelector())) {
1040 if (ImmediateClass)
1041 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1042 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001043 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001044 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001045 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001046 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001047 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001048 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001049 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1050 // ImpMethodDecl may be null as in a @dynamic property.
1051 if (ImpMethodDecl)
1052 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1053 }
1054 }
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001056 // Check and see if class methods in class interface have been
1057 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001058 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001059 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001060 if (ClsMapSeen.count((*I)->getSelector()))
1061 continue;
1062 ClsMapSeen.insert((*I)->getSelector());
1063 if (!ClsMap.count((*I)->getSelector())) {
1064 if (ImmediateClass)
1065 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001066 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001067 ObjCMethodDecl *ImpMethodDecl =
1068 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001069 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001070 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001071 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1072 }
1073 }
1074 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1075 // Check for any implementation of a methods declared in protocol.
1076 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1077 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001078 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1079 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001080 (*PI), IncompleteImpl, false);
1081 if (I->getSuperClass())
1082 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001083 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001084 I->getSuperClass(), IncompleteImpl, false);
1085 }
1086}
1087
Mike Stump1eb44332009-09-09 15:08:12 +00001088void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1089 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001090 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001091 llvm::DenseSet<Selector> InsMap;
1092 // Check and see if instance methods in class interface have been
1093 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001094 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001095 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001096 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001098 // Check and see if properties declared in the interface have either 1)
1099 // an implementation or 2) there is a @synthesize/@dynamic implementation
1100 // of the property in the @implementation.
1101 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001102 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1103 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001104 ObjCPropertyDecl *Prop = (*P);
1105 if (Prop->isInvalidDecl())
1106 continue;
1107 ObjCPropertyImplDecl *PI = 0;
1108 // Is there a matching propery synthesize/dynamic?
Mike Stump1eb44332009-09-09 15:08:12 +00001109 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001110 I = IMPDecl->propimpl_begin(),
1111 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001112 if ((*I)->getPropertyDecl() == Prop) {
1113 PI = (*I);
1114 break;
1115 }
1116 if (PI)
1117 continue;
1118 if (!InsMap.count(Prop->getGetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001119 Diag(Prop->getLocation(),
1120 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001121 << Prop->getDeclName() << Prop->getGetterName();
1122 Diag(IMPDecl->getLocation(),
1123 diag::note_property_impl_required);
1124 }
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001126 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001127 Diag(Prop->getLocation(),
1128 diag::warn_setter_getter_impl_required)
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001129 << Prop->getDeclName() << Prop->getSetterName();
1130 Diag(IMPDecl->getLocation(),
1131 diag::note_property_impl_required);
1132 }
1133 }
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Chris Lattner4d391482007-12-12 07:09:47 +00001135 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001136 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001137 I = IMPDecl->classmeth_begin(),
1138 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001139 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001141 // Check for type conflict of methods declared in a class/protocol and
1142 // its implementation; if any.
1143 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001144 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1145 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001146 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Chris Lattner4d391482007-12-12 07:09:47 +00001148 // Check the protocol list for unimplemented methods in the @implementation
1149 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001150 // Check and see if class methods in class interface have been
1151 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Chris Lattnercddc8882009-03-01 00:56:52 +00001153 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001154 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +00001155 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001156 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001157 InsMap, ClsMap, I);
1158 // Check class extensions (unnamed categories)
1159 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1160 Categories; Categories = Categories->getNextClassCategory()) {
1161 if (!Categories->getIdentifier()) {
1162 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1163 break;
1164 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001165 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001166 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001167 // For extended class, unimplemented methods in its protocols will
1168 // be reported in the primary class.
1169 if (C->getIdentifier()) {
1170 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1171 E = C->protocol_end(); PI != E; ++PI)
1172 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1173 InsMap, ClsMap, C->getClassInterface());
1174 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001175 } else
1176 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001177}
1178
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001179void
1180Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1181 ObjCContainerDecl* IDecl) {
1182 // Rules apply in non-GC mode only
1183 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1184 return;
1185 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1186 E = IDecl->prop_end();
1187 I != E; ++I) {
1188 ObjCPropertyDecl *Property = (*I);
1189 unsigned Attributes = Property->getPropertyAttributes();
1190 // We only care about readwrite atomic property.
1191 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1192 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1193 continue;
1194 if (const ObjCPropertyImplDecl *PIDecl
1195 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1196 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1197 continue;
1198 ObjCMethodDecl *GetterMethod =
1199 IMPDecl->getInstanceMethod(Property->getGetterName());
1200 ObjCMethodDecl *SetterMethod =
1201 IMPDecl->getInstanceMethod(Property->getSetterName());
1202 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1203 SourceLocation MethodLoc =
1204 (GetterMethod ? GetterMethod->getLocation()
1205 : SetterMethod->getLocation());
1206 Diag(MethodLoc, diag::warn_atomic_property_rule)
1207 << Property->getIdentifier();
1208 Diag(Property->getLocation(), diag::note_property_declare);
1209 }
1210 }
1211 }
1212}
1213
Mike Stump1eb44332009-09-09 15:08:12 +00001214/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001215Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001216Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001217 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001218 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001219 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001220 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Chris Lattner4d391482007-12-12 07:09:47 +00001222 for (unsigned i = 0; i != NumElts; ++i) {
1223 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001224 NamedDecl *PrevDecl
1225 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001226 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001227 // Maybe we will complain about the shadowed template parameter.
1228 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1229 // Just pretend that we didn't see the previous declaration.
1230 PrevDecl = 0;
1231 }
1232
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001233 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001234 // GCC apparently allows the following idiom:
1235 //
1236 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1237 // @class XCElementToggler;
1238 //
Mike Stump1eb44332009-09-09 15:08:12 +00001239 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001240 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1241 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001242 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001243 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001244 } else if (TDD) {
1245 // a forward class declaration matching a typedef name of a class refers
1246 // to the underlying class.
Mike Stump1eb44332009-09-09 15:08:12 +00001247 if (ObjCInterfaceType * OI =
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001248 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1249 PrevDecl = OI->getDecl();
1250 }
Chris Lattner4d391482007-12-12 07:09:47 +00001251 }
Mike Stump1eb44332009-09-09 15:08:12 +00001252 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001253 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001254 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001255 IdentList[i], IdentLocs[i], true);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001256
1257 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1258 // the current DeclContext. This prevents clients that walk DeclContext
1259 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1260 // declared later (if at all). We also take care to explicitly make
1261 // sure this declaration is visible for name lookup.
1262 PushOnScopeChains(IDecl, TUScope, false);
1263 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001264 }
1265
1266 Interfaces.push_back(IDecl);
1267 }
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Ted Kremenek321c22f2009-11-18 00:28:11 +00001269 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001270 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001271 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001272 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001273 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001274 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001275 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001276}
1277
1278
1279/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1280/// returns true, or false, accordingly.
1281/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001282bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001283 const ObjCMethodDecl *PrevMethod,
1284 bool matchBasedOnSizeAndAlignment) {
1285 QualType T1 = Context.getCanonicalType(Method->getResultType());
1286 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001288 if (T1 != T2) {
1289 // The result types are different.
1290 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +00001291 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001292 // Incomplete types don't have a size and alignment.
1293 if (T1->isIncompleteType() || T2->isIncompleteType())
1294 return false;
1295 // Check is based on size and alignment.
1296 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1297 return false;
1298 }
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Chris Lattner89951a82009-02-20 18:43:26 +00001300 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1301 E = Method->param_end();
1302 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Chris Lattner89951a82009-02-20 18:43:26 +00001304 for (; ParamI != E; ++ParamI, ++PrevI) {
1305 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1306 T1 = Context.getCanonicalType((*ParamI)->getType());
1307 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001308 if (T1 != T2) {
1309 // The result types are different.
1310 if (!matchBasedOnSizeAndAlignment)
1311 return false;
1312 // Incomplete types don't have a size and alignment.
1313 if (T1->isIncompleteType() || T2->isIncompleteType())
1314 return false;
1315 // Check is based on size and alignment.
1316 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1317 return false;
1318 }
Chris Lattner4d391482007-12-12 07:09:47 +00001319 }
1320 return true;
1321}
1322
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001323/// \brief Read the contents of the instance and factory method pools
1324/// for a given selector from external storage.
1325///
1326/// This routine should only be called once, when neither the instance
1327/// nor the factory method pool has an entry for this selector.
Mike Stump1eb44332009-09-09 15:08:12 +00001328Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001329 bool isInstance) {
1330 assert(ExternalSource && "We need an external AST source");
1331 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1332 "Selector data already loaded into the instance method pool");
1333 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1334 "Selector data already loaded into the factory method pool");
1335
1336 // Read the method list from the external source.
1337 std::pair<ObjCMethodList, ObjCMethodList> Methods
1338 = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001340 if (isInstance) {
1341 if (Methods.second.Method)
1342 FactoryMethodPool[Sel] = Methods.second;
1343 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump1eb44332009-09-09 15:08:12 +00001344 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001345
1346 if (Methods.first.Method)
1347 InstanceMethodPool[Sel] = Methods.first;
1348
1349 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1350}
1351
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001352void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001353 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1354 = InstanceMethodPool.find(Method->getSelector());
1355 if (Pos == InstanceMethodPool.end()) {
1356 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1357 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1358 else
1359 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1360 ObjCMethodList())).first;
1361 }
1362
1363 ObjCMethodList &Entry = Pos->second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001364 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001365 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001366 Entry.Method = Method;
1367 Entry.Next = 0;
1368 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001369 }
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Chris Lattnerb25df352009-03-04 05:16:45 +00001371 // We've seen a method with this name, see if we have already seen this type
1372 // signature.
1373 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1374 if (MatchTwoMethodDeclarations(Method, List->Method))
1375 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Chris Lattnerb25df352009-03-04 05:16:45 +00001377 // We have a new signature for an existing method - add it.
1378 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1379 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001380}
1381
Steve Naroff6f5f41c2008-10-21 10:50:19 +00001382// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump1eb44332009-09-09 15:08:12 +00001383ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001384 SourceRange R,
1385 bool warn) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001386 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1387 = InstanceMethodPool.find(Sel);
Douglas Gregor27a45662009-04-24 22:23:41 +00001388 if (Pos == InstanceMethodPool.end()) {
1389 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001390 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1391 else
1392 return 0;
1393 }
1394
1395 ObjCMethodList &MethList = Pos->second;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001396 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Steve Naroff037cda52008-09-30 14:38:43 +00001398 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001399 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1400 // This checks if the methods differ by size & alignment.
1401 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +00001402 issueWarning = warn;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001403 }
1404 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001405 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001406 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001407 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001408 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001409 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001410 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +00001411 }
1412 return MethList.Method;
1413}
1414
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001415void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001416 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1417 = FactoryMethodPool.find(Method->getSelector());
1418 if (Pos == FactoryMethodPool.end()) {
1419 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1420 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1421 else
1422 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1423 ObjCMethodList())).first;
1424 }
1425
1426 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattner4d391482007-12-12 07:09:47 +00001427 if (!FirstMethod.Method) {
1428 // Haven't seen a method with this selector name yet - add it.
1429 FirstMethod.Method = Method;
1430 FirstMethod.Next = 0;
1431 } else {
1432 // We've seen a method with this name, now check the type signature(s).
1433 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001434
1435 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +00001436 Next = Next->Next)
1437 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Chris Lattner4d391482007-12-12 07:09:47 +00001439 if (!match) {
1440 // We have a new signature for an existing method - add it.
1441 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001442 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001443 FirstMethod.Next = OMI;
1444 }
1445 }
1446}
1447
Mike Stump1eb44332009-09-09 15:08:12 +00001448ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001449 SourceRange R) {
1450 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1451 = FactoryMethodPool.find(Sel);
1452 if (Pos == FactoryMethodPool.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001453 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001454 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1455 else
1456 return 0;
1457 }
1458
1459 ObjCMethodList &MethList = Pos->second;
1460 bool issueWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001462 if (MethList.Method && MethList.Next) {
1463 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1464 // This checks if the methods differ by size & alignment.
1465 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1466 issueWarning = true;
1467 }
1468 if (issueWarning && (MethList.Method && MethList.Next)) {
1469 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCall41ce66f2009-12-10 19:51:03 +00001470 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001471 << MethList.Method->getSourceRange();
1472 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCall41ce66f2009-12-10 19:51:03 +00001473 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001474 << Next->Method->getSourceRange();
1475 }
1476 return MethList.Method;
1477}
1478
Mike Stump1eb44332009-09-09 15:08:12 +00001479/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff0701bbb2009-01-08 17:28:14 +00001480/// have the property type and issue diagnostics if they don't.
1481/// Also synthesize a getter/setter method if none exist (and update the
1482/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1483/// methods is the "right" thing to do.
Mike Stump1eb44332009-09-09 15:08:12 +00001484void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001485 ObjCContainerDecl *CD) {
1486 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00001487
1488 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001489 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump1eb44332009-09-09 15:08:12 +00001490 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianc001e892009-05-08 20:20:55 +00001491 property->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001493 if (SetterMethod) {
Fariborz Jahanian02deae82010-01-06 00:18:12 +00001494 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1495 property->getPropertyAttributes();
1496 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1497 Context.getCanonicalType(SetterMethod->getResultType()) !=
1498 Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001499 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattner89951a82009-02-20 18:43:26 +00001500 if (SetterMethod->param_size() != 1 ||
1501 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001502 Diag(property->getLocation(),
1503 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001504 << property->getDeclName()
Ted Kremenek8af2c162009-03-14 00:20:08 +00001505 << SetterMethod->getSelector();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001506 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1507 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001508 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001509
1510 // Synthesize getter/setter methods if none exist.
Steve Naroff92f863b2009-01-08 20:15:03 +00001511 // Find the default getter and if one not found, add one.
Mike Stump390b4cc2009-05-16 07:39:55 +00001512 // FIXME: The synthesized property we set here is misleading. We almost always
1513 // synthesize these methods unless the user explicitly provided prototypes
1514 // (which is odd, but allowed). Sema should be typechecking that the
1515 // declarations jive in that situation (which it is not currently).
Steve Naroff92f863b2009-01-08 20:15:03 +00001516 if (!GetterMethod) {
1517 // No instance method of same name as property getter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001518 // Declare a getter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001519 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001520 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1521 property->getLocation(), property->getGetterName(),
1522 property->getType(), CD, true, false, true,
1523 (property->getPropertyImplementation() ==
1524 ObjCPropertyDecl::Optional) ?
1525 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001526 ObjCMethodDecl::Required);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001527 CD->addDecl(GetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001528 } else
1529 // A user declared getter will be synthesize when @synthesize of
1530 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001531 GetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001532 property->setGetterMethodDecl(GetterMethod);
1533
1534 // Skip setter if property is read-only.
1535 if (!property->isReadOnly()) {
1536 // Find the default setter and if one not found, add one.
1537 if (!SetterMethod) {
1538 // No instance method of same name as property setter name was found.
Mike Stump1eb44332009-09-09 15:08:12 +00001539 // Declare a setter method and add it to the list of methods
Steve Naroff92f863b2009-01-08 20:15:03 +00001540 // for this class.
Mike Stump1eb44332009-09-09 15:08:12 +00001541 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1542 property->getLocation(),
1543 property->getSetterName(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001544 Context.VoidTy, CD, true, false, true,
Mike Stump1eb44332009-09-09 15:08:12 +00001545 (property->getPropertyImplementation() ==
1546 ObjCPropertyDecl::Optional) ?
1547 ObjCMethodDecl::Optional :
Steve Naroff92f863b2009-01-08 20:15:03 +00001548 ObjCMethodDecl::Required);
1549 // Invent the arguments for the setter. We don't bother making a
1550 // nice name for the argument.
1551 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump1eb44332009-09-09 15:08:12 +00001552 property->getLocation(),
Steve Naroff92f863b2009-01-08 20:15:03 +00001553 property->getIdentifier(),
1554 property->getType(),
John McCalla93c9342009-12-07 02:54:59 +00001555 /*TInfo=*/0,
Steve Naroff92f863b2009-01-08 20:15:03 +00001556 VarDecl::None,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001557 0);
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001558 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001559 CD->addDecl(SetterMethod);
Steve Naroff92f863b2009-01-08 20:15:03 +00001560 } else
1561 // A user declared setter will be synthesize when @synthesize of
1562 // the property with the same name is seen in the @implementation
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001563 SetterMethod->setSynthesized(true);
Steve Naroff92f863b2009-01-08 20:15:03 +00001564 property->setSetterMethodDecl(SetterMethod);
1565 }
Mike Stump1eb44332009-09-09 15:08:12 +00001566 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff0701bbb2009-01-08 17:28:14 +00001567 // handle the following, which is supported by GCC (and part of the design).
1568 //
1569 // @interface Foo
1570 // @property double bar;
1571 // @end
1572 //
1573 // void thisIsUnfortunate() {
1574 // id foo;
1575 // double bar = [foo bar];
1576 // }
1577 //
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001578 if (GetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001579 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001580 if (SetterMethod)
Mike Stump1eb44332009-09-09 15:08:12 +00001581 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001582}
1583
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001584/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1585/// identical selector names in current and its super classes and issues
1586/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001587void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1588 ObjCMethodDecl *Method,
1589 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001590 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1591 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001593 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001594 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001595 SD->lookupMethod(Method->getSelector(), IsInstance);
1596 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001597 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001598 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001599 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001600 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1601 E = Method->param_end();
1602 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1603 for (; ParamI != E; ++ParamI, ++PrevI) {
1604 // Number of parameters are the same and is guaranteed by selector match.
1605 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1606 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1607 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1608 // If type of arguement of method in this class does not match its
1609 // respective argument type in the super class method, issue warning;
1610 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001611 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001612 << T1 << T2;
1613 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1614 return;
1615 }
1616 }
1617 ID = SD;
1618 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001619}
1620
Steve Naroffa56f6162007-12-18 01:30:32 +00001621// Note: For class/category implemenations, allMethods/allProperties is
1622// always null.
Ted Kremenek782f2f52010-01-07 01:20:12 +00001623void Sema::ActOnAtEnd(SourceRange AtEnd,
1624 DeclPtrTy classDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001625 DeclPtrTy *allMethods, unsigned allNum,
1626 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001627 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001628 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001629
Steve Naroffa56f6162007-12-18 01:30:32 +00001630 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1631 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001632 // should be true.
1633 if (!ClassDecl)
1634 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001635
Mike Stump1eb44332009-09-09 15:08:12 +00001636 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001637 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1638 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001639 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001640
Ted Kremenek782f2f52010-01-07 01:20:12 +00001641 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1642 // FIXME: This is wrong. We shouldn't be pretending that there is
1643 // an '@end' in the declaration.
1644 SourceLocation L = ClassDecl->getLocation();
1645 AtEnd.setBegin(L);
1646 AtEnd.setEnd(L);
1647 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001648 }
1649
Steve Naroff0701bbb2009-01-08 17:28:14 +00001650 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001651
1652 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1653 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1654 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1655
Chris Lattner4d391482007-12-12 07:09:47 +00001656 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001657 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001658 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001659
1660 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001661 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001662 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001663 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001664 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001665 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001666 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001667 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001668 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001669 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001670 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001671 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001672 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001673 InsMap[Method->getSelector()] = Method;
1674 /// The following allows us to typecheck messages to "id".
1675 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001676 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001677 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001678 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001679 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001680 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001681 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001682 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001683 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001684 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001685 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001686 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001687 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001688 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001689 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001690 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001691 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001692 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001693 /// The following allows us to typecheck messages to "Class".
1694 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001695 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001696 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001697 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001698 }
1699 }
1700 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001701 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001702 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001703 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001704 ComparePropertiesInBaseAndSuper(I);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001705 MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001706 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001707 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001708 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001709 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001710
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001711 // Merge protocol properties into category
Chris Lattnerb28317a2009-03-28 19:18:32 +00001712 MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C));
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001713 if (C->getIdentifier() == 0)
1714 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001715 }
Steve Naroff09c47192009-01-09 15:36:25 +00001716 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1717 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1718 // user-defined setter/getter. It also synthesizes setter/getter methods
1719 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001720 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1721 E = CDecl->prop_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001722 I != E; ++I)
Chris Lattner97a58872009-02-16 18:32:47 +00001723 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001724 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001725 }
1726 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001727 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001728 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001729 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001730 AtomicPropertySetterGetterRules(IC, IDecl);
1731 }
Mike Stump1eb44332009-09-09 15:08:12 +00001732 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001733 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001734 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Chris Lattner4d391482007-12-12 07:09:47 +00001736 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001737 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001738 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001739 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001740 Categories; Categories = Categories->getNextClassCategory()) {
1741 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattnercddc8882009-03-01 00:56:52 +00001742 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001743 break;
1744 }
1745 }
1746 }
1747 }
Chris Lattner682bf922009-03-29 16:50:03 +00001748 if (isInterfaceDeclKind) {
1749 // Reject invalid vardecls.
1750 for (unsigned i = 0; i != tuvNum; i++) {
1751 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1752 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1753 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001754 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001755 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001756 }
Chris Lattner682bf922009-03-29 16:50:03 +00001757 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001758 }
Chris Lattner4d391482007-12-12 07:09:47 +00001759}
1760
1761
1762/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1763/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001764static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001765CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1766 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1767 if (PQTVal & ObjCDeclSpec::DQ_In)
1768 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1769 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1770 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1771 if (PQTVal & ObjCDeclSpec::DQ_Out)
1772 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1773 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1774 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1775 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1776 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1777 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1778 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001779
1780 return ret;
1781}
1782
Chris Lattnerb28317a2009-03-28 19:18:32 +00001783Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001784 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001785 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001786 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001787 Selector Sel,
1788 // optional arguments. The number of types/arguments is obtained
1789 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001790 ObjCArgInfo *ArgInfo,
Fariborz Jahanian439c6582009-01-09 00:38:19 +00001791 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattner4d391482007-12-12 07:09:47 +00001792 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1793 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001794 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001795
1796 // Make sure we can establish a context for the method.
1797 if (!ClassDecl) {
1798 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanian32844b32009-08-28 17:52:37 +00001799 FunctionLabelMap.clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001800 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001801 }
Chris Lattner4d391482007-12-12 07:09:47 +00001802 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Steve Naroffccef3712009-02-20 22:59:16 +00001804 if (ReturnType) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001805 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Steve Naroffccef3712009-02-20 22:59:16 +00001807 // Methods cannot return interface types. All ObjC objects are
1808 // passed by reference.
1809 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001810 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1811 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001812 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001813 }
1814 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001815 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001816
1817 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001818 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump1eb44332009-09-09 15:08:12 +00001819 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001820 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001821 false,
Mike Stump1eb44332009-09-09 15:08:12 +00001822 MethodDeclKind == tok::objc_optional ?
1823 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001824 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001825
Chris Lattner0ed844b2008-04-04 06:12:32 +00001826 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Chris Lattner7db638d2009-04-11 19:42:43 +00001828 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001829 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001830 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Chris Lattnere294d3f2009-04-11 18:57:04 +00001832 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001833 ArgType = Context.getObjCIdType();
1834 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001835 } else {
John McCall58e46772009-10-23 21:48:59 +00001836 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001837 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001838 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001839 }
Mike Stump1eb44332009-09-09 15:08:12 +00001840
John McCall58e46772009-10-23 21:48:59 +00001841 ParmVarDecl* Param
1842 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1843 ArgInfo[i].Name, ArgType, DI,
1844 VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001846 if (ArgType->isObjCInterfaceType()) {
1847 Diag(ArgInfo[i].NameLoc,
1848 diag::err_object_cannot_be_passed_returned_by_value)
1849 << 1 << ArgType;
1850 Param->setInvalidDecl();
1851 }
Mike Stump1eb44332009-09-09 15:08:12 +00001852
Chris Lattner0ed844b2008-04-04 06:12:32 +00001853 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001854 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001855
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001856 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001857 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Chris Lattner0ed844b2008-04-04 06:12:32 +00001859 Params.push_back(Param);
1860 }
1861
Jay Foadbeaaccd2009-05-21 09:52:38 +00001862 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001863 ObjCMethod->setObjCDeclQualifier(
1864 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1865 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001866
1867 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001868 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001869
John McCall54abf7d2009-11-04 02:18:39 +00001870 const ObjCMethodDecl *InterfaceMD = 0;
1871
Mike Stump1eb44332009-09-09 15:08:12 +00001872 // For implementations (which can be very "coarse grain"), we add the
1873 // method now. This allows the AST to implement lookup methods that work
1874 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001875 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001876 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001877 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001878 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001879 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1880 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001881 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001882 PrevMethod = ImpDecl->getClassMethod(Sel);
1883 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001884 }
John McCall54abf7d2009-11-04 02:18:39 +00001885 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1886 MethodType == tok::minus);
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001887 if (AttrList)
1888 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001889 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001890 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001891 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001892 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1893 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001894 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001895 PrevMethod = CatImpDecl->getClassMethod(Sel);
1896 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001897 }
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001898 if (AttrList)
1899 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001900 }
1901 if (PrevMethod) {
1902 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001903 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001904 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001905 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001906 }
John McCall54abf7d2009-11-04 02:18:39 +00001907
1908 // If the interface declared this method, and it was deprecated there,
1909 // mark it deprecated here.
1910 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1911 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1912
Chris Lattnerb28317a2009-03-28 19:18:32 +00001913 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001914}
1915
Mike Stump1eb44332009-09-09 15:08:12 +00001916void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001917 SourceLocation Loc,
1918 unsigned &Attributes) {
1919 // FIXME: Improve the reported location.
1920
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001921 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001922 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001923 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1924 ObjCDeclSpec::DQ_PR_assign |
1925 ObjCDeclSpec::DQ_PR_copy |
1926 ObjCDeclSpec::DQ_PR_retain))) {
1927 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1928 "readwrite" :
1929 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1930 "assign" :
1931 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1932 "copy" : "retain";
Mike Stump1eb44332009-09-09 15:08:12 +00001933
1934 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner28372fa2009-01-29 18:49:48 +00001935 diag::err_objc_property_attr_mutually_exclusive :
1936 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001937 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001938 }
1939
1940 // Check for copy or retain on non-object types.
1941 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001942 !PropertyTy->isObjCObjectPointerType() &&
1943 !PropertyTy->isBlockPointerType() &&
Steve Narofff4954562009-07-16 15:41:00 +00001944 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001945 Diag(Loc, diag::err_objc_property_requires_object)
1946 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001947 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1948 }
1949
1950 // Check for more than one of { assign, copy, retain }.
1951 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1952 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001953 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1954 << "assign" << "copy";
1955 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump1eb44332009-09-09 15:08:12 +00001956 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001957 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001958 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1959 << "assign" << "retain";
1960 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001961 }
1962 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1963 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001964 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1965 << "copy" << "retain";
1966 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001967 }
1968 }
1969
1970 // Warn if user supplied no assignment attribute, property is
1971 // readwrite, and this is an object type.
1972 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1973 ObjCDeclSpec::DQ_PR_retain)) &&
1974 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Narofff4954562009-07-16 15:41:00 +00001975 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001976 // Skip this warning in gc-only mode.
Mike Stump1eb44332009-09-09 15:08:12 +00001977 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001978 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1979
1980 // If non-gc code warn that this is likely inappropriate.
1981 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1982 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump1eb44332009-09-09 15:08:12 +00001983
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001984 // FIXME: Implement warning dependent on NSCopying being
1985 // implemented. See also:
1986 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1987 // (please trim this list while you are at it).
1988 }
Mike Stump046efd92009-05-07 23:06:50 +00001989
1990 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1991 && getLangOptions().getGCMode() == LangOptions::GCOnly
1992 && PropertyTy->isBlockPointerType())
1993 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001994}
1995
Mike Stump1eb44332009-09-09 15:08:12 +00001996Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001997 FieldDeclarator &FD,
1998 ObjCDeclSpec &ODS,
1999 Selector GetterSel,
2000 Selector SetterSel,
2001 DeclPtrTy ClassCategory,
2002 bool *isOverridingProperty,
2003 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002004 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002005 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
2006 // default is readwrite!
2007 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump1eb44332009-09-09 15:08:12 +00002008 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002009 // not retain or copy
2010 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump1eb44332009-09-09 15:08:12 +00002011 (isReadWrite &&
2012 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002013 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
2014 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahaniandd69aae2009-12-16 18:03:30 +00002015 if (T->isReferenceType()) {
2016 Diag(AtLoc, diag::error_reference_property);
2017 return DeclPtrTy();
2018 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002019 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002020 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002021 // May modify Attributes.
2022 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002023 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2024 if (!CDecl->getIdentifier()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002025 // This is a continuation class. property requires special
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002026 // handling.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002027 if ((CCPrimary = CDecl->getClassInterface())) {
2028 // Find the property in continuation class's primary class only.
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002029 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002030 if (ObjCPropertyDecl *PIDecl =
2031 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002032 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahanian22b6e062009-04-01 23:23:53 +00002033 // with continuation class's readwrite property attribute!
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002034 unsigned PIkind = PIDecl->getPropertyAttributes();
2035 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002036 unsigned retainCopyNonatomic =
Fariborz Jahaniand669be52009-11-06 22:59:12 +00002037 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002038 ObjCPropertyDecl::OBJC_PR_copy |
2039 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian4770a4a2009-11-10 19:31:09 +00002040 if ((Attributes & retainCopyNonatomic) !=
2041 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002042 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniancc667e22009-11-03 00:01:38 +00002043 Diag(PIDecl->getLocation(), diag::note_property_declare);
2044 }
Fariborz Jahanianb73e2812010-01-06 21:38:30 +00002045 DeclContext *DC = dyn_cast<DeclContext>(CCPrimary);
2046 assert(DC && "ClassDecl is not a DeclContext");
2047 DeclContext::lookup_result Found =
2048 DC->lookup(PIDecl->getDeclName());
2049 bool PropertyInPrimaryClass = false;
2050 for (; Found.first != Found.second; ++Found.first)
2051 if (isa<ObjCPropertyDecl>(*Found.first)) {
2052 PropertyInPrimaryClass = true;
2053 break;
2054 }
2055 if (!PropertyInPrimaryClass) {
2056 // Protocol is not in the primary class. Must build one for it.
2057 ObjCDeclSpec ProtocolPropertyODS;
2058 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind and
2059 // ObjCPropertyDecl::PropertyAttributeKind have identical values.
2060 // Should consolidate both into one enum type.
2061 ProtocolPropertyODS.setPropertyAttributes(
2062 (ObjCDeclSpec::ObjCPropertyAttributeKind)PIkind);
2063 DeclPtrTy ProtocolPtrTy =
2064 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
2065 PIDecl->getGetterName(),
2066 PIDecl->getSetterName(),
2067 DeclPtrTy::make(CCPrimary), isOverridingProperty,
2068 MethodImplKind);
2069 PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>();
2070 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002071 PIDecl->makeitReadWriteAttribute();
2072 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2073 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2074 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2075 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2076 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002077 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002078 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002079 << CCPrimary->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002080 Diag(PIDecl->getLocation(), diag::note_property_declare);
2081 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002082 *isOverridingProperty = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002083 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian50c314c2009-04-15 19:19:03 +00002084 // class's list.
2085 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002086 return DeclPtrTy();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002087 }
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002088 // No matching property found in the primary class. Just fall thru
2089 // and add property to continuation class's primary class.
2090 ClassDecl = CCPrimary;
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002091 } else {
Chris Lattnerf25df992009-02-20 21:38:52 +00002092 Diag(CDecl->getLocation(), diag::err_continuation_class);
2093 *isOverridingProperty = true;
Chris Lattnerb28317a2009-03-28 19:18:32 +00002094 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002095 }
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002096 }
Mike Stump1eb44332009-09-09 15:08:12 +00002097
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002098 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump1eb44332009-09-09 15:08:12 +00002099 // gc'able conforms to NSCopying protocol
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002100 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2101 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2102 if (T->isObjCObjectPointerType()) {
2103 QualType InterfaceTy = T->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00002104 if (const ObjCInterfaceType *OIT =
John McCall183700f2009-09-21 23:43:11 +00002105 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002106 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002107 if (IDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00002108 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002109 LookupProtocol(&Context.Idents.get("NSCopying")))
2110 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump1eb44332009-09-09 15:08:12 +00002111 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002112 << FD.D.getIdentifier();
Fariborz Jahanianb11d7982009-08-14 18:06:25 +00002113 }
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00002114 }
Fariborz Jahanianacf2d132009-08-12 18:17:53 +00002115 if (T->isObjCInterfaceType())
2116 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Steve Naroff93983f82009-01-11 12:47:58 +00002118 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2119 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002120 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump1eb44332009-09-09 15:08:12 +00002121 FD.D.getIdentifierLoc(),
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00002122 FD.D.getIdentifier(), T);
Fariborz Jahanian6dd30fc2009-12-17 00:49:09 +00002123 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
2124 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2125 Diag(PDecl->getLocation(), diag::err_duplicate_property);
2126 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2127 PDecl->setInvalidDecl();
2128 }
2129 else
2130 DC->addDecl(PDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002131
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +00002132 if (T->isArrayType() || T->isFunctionType()) {
2133 Diag(AtLoc, diag::err_property_type) << T;
2134 PDecl->setInvalidDecl();
2135 }
Mike Stump1eb44332009-09-09 15:08:12 +00002136
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002137 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregord0434102009-01-09 00:49:46 +00002138
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00002139 // Regardless of setter/getter attribute, we save the default getter/setter
2140 // selector names in anticipation of declaration of setter/getter methods.
2141 PDecl->setGetterName(GetterSel);
2142 PDecl->setSetterName(SetterSel);
Mike Stump1eb44332009-09-09 15:08:12 +00002143
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002144 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002145 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump1eb44332009-09-09 15:08:12 +00002146
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002147 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002148 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump1eb44332009-09-09 15:08:12 +00002149
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002150 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002151 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump1eb44332009-09-09 15:08:12 +00002152
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002153 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002154 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump1eb44332009-09-09 15:08:12 +00002155
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002156 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002157 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump1eb44332009-09-09 15:08:12 +00002158
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002159 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002160 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump1eb44332009-09-09 15:08:12 +00002161
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00002162 if (isAssign)
2163 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump1eb44332009-09-09 15:08:12 +00002164
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00002165 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002166 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump1eb44332009-09-09 15:08:12 +00002167
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00002168 if (MethodImplKind == tok::objc_required)
2169 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2170 else if (MethodImplKind == tok::objc_optional)
2171 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahaniand09a4562009-04-02 18:44:20 +00002172 // A case of continuation class adding a new property in the class. This
2173 // is not what it was meant for. However, gcc supports it and so should we.
2174 // Make sure setter/getters are declared here.
2175 if (CCPrimary)
2176 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump1eb44332009-09-09 15:08:12 +00002177
Chris Lattnerb28317a2009-03-28 19:18:32 +00002178 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002179}
2180
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002181/// ActOnPropertyImplDecl - This routine performs semantic checks and
2182/// builds the AST node for a property implementation declaration; declared
2183/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002184///
Mike Stump1eb44332009-09-09 15:08:12 +00002185Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002186 SourceLocation PropertyLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002187 bool Synthesize,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002188 DeclPtrTy ClassCatImpDecl,
2189 IdentifierInfo *PropertyId,
2190 IdentifierInfo *PropertyIvar) {
2191 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002192 // Make sure we have a context for the property implementation declaration.
2193 if (!ClassImpDecl) {
2194 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002195 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002196 }
2197 ObjCPropertyDecl *property = 0;
2198 ObjCInterfaceDecl* IDecl = 0;
2199 // Find the class or category class where this property must have
2200 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002201 ObjCImplementationDecl *IC = 0;
2202 ObjCCategoryImplDecl* CatImplClass = 0;
2203 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002204 IDecl = IC->getClassInterface();
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002205 // We always synthesize an interface for an implementation
2206 // without an interface decl. So, IDecl is always non-zero.
Mike Stump1eb44332009-09-09 15:08:12 +00002207 assert(IDecl &&
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002208 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump1eb44332009-09-09 15:08:12 +00002209
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002210 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002211 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002212 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002213 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002214 return DeclPtrTy();
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002215 }
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002216 if (const ObjCCategoryDecl *CD =
2217 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
2218 if (CD->getIdentifier()) {
2219 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +00002220 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanian3684bd42009-11-02 18:45:36 +00002221 return DeclPtrTy();
2222 }
2223 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002224 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002225 if (Synthesize) {
2226 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002227 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002228 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002229 IDecl = CatImplClass->getClassInterface();
2230 if (!IDecl) {
2231 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002232 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002233 }
Mike Stump1eb44332009-09-09 15:08:12 +00002234 ObjCCategoryDecl *Category =
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002235 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump1eb44332009-09-09 15:08:12 +00002236
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002237 // If category for this implementation not found, it is an error which
2238 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002239 if (!Category)
Chris Lattnerb28317a2009-03-28 19:18:32 +00002240 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002241 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002242 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00002243 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002244 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002245 << Category->getDeclName();
Chris Lattnerb28317a2009-03-28 19:18:32 +00002246 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002247 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00002248 } else {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002249 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002250 return DeclPtrTy();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002251 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00002252 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002253 // Check that we have a valid, previously declared ivar for @synthesize
2254 if (Synthesize) {
2255 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00002256 if (!PropertyIvar)
2257 PropertyIvar = PropertyId;
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +00002258 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002259 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002260 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002261 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002262 if (!Ivar) {
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002263 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002264 assert(EnclosingContext &&
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002265 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump1eb44332009-09-09 15:08:12 +00002266 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2267 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002268 ObjCIvarDecl::Public,
2269 (Expr *)0);
Fariborz Jahanian8019a7b2009-06-06 16:36:41 +00002270 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002271 IDecl->addDecl(Ivar);
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002272 property->setPropertyIvarDecl(Ivar);
2273 if (!getLangOptions().ObjCNonFragileABI)
Steve Narofff4c00ff2009-03-03 22:09:41 +00002274 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump1eb44332009-09-09 15:08:12 +00002275 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002276 // a property implementation and to avoid future warnings.
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002277 } else if (getLangOptions().ObjCNonFragileABI &&
2278 ClassDeclared != IDecl) {
Fariborz Jahaniane2f2c162009-04-30 21:39:24 +00002279 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump1eb44332009-09-09 15:08:12 +00002280 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanian29da66e2009-04-13 19:30:37 +00002281 << ClassDeclared->getDeclName();
2282 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2283 << Ivar << Ivar->getNameAsCString();
2284 // Note! I deliberately want it to fall thru so more errors are caught.
2285 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00002286 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002287
Steve Narofffbbe0ac2008-09-30 00:24:17 +00002288 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002289 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00002290 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00002291 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002292 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002293 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002294 // a property implementation and to avoid future warnings.
Steve Naroff3ce52d62008-09-30 10:07:56 +00002295 }
Mike Stump1eb44332009-09-09 15:08:12 +00002296
Chris Lattnerb28317a2009-03-28 19:18:32 +00002297 // FIXME! Rules for properties are somewhat different that those
2298 // for assignments. Use a new routine to consolidate all cases;
2299 // specifically for property redeclarations as well as for ivars.
2300 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2301 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump1eb44332009-09-09 15:08:12 +00002302 if (lhsType != rhsType &&
Chris Lattnerb28317a2009-03-28 19:18:32 +00002303 lhsType->isArithmeticType()) {
2304 Diag(PropertyLoc, diag::error_property_ivar_type)
2305 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002306 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002307 }
2308 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanianc8bafd72009-04-07 21:25:06 +00002309 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2310 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002311 Diag(PropertyLoc, diag::error_weak_property)
2312 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002313 // Fall thru - see previous comment
Chris Lattnerb28317a2009-03-28 19:18:32 +00002314 }
Mike Stump1eb44332009-09-09 15:08:12 +00002315 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian0a9217f2009-04-10 22:42:54 +00002316 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2317 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002318 Diag(PropertyLoc, diag::error_strong_property)
2319 << property->getDeclName() << Ivar->getDeclName();
Mike Stump1eb44332009-09-09 15:08:12 +00002320 // Fall thru - see previous comment
Fariborz Jahanianacdc33b2009-01-19 20:13:47 +00002321 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00002322 }
Fariborz Jahanian12bac252009-04-14 23:15:21 +00002323 } else if (PropertyIvar)
2324 // @dynamic
2325 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002326 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump1eb44332009-09-09 15:08:12 +00002327 ObjCPropertyImplDecl *PIDecl =
2328 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2329 property,
2330 (Synthesize ?
2331 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00002332 : ObjCPropertyImplDecl::Dynamic),
2333 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002334 if (IC) {
2335 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002336 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002337 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002338 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2339 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002340 << PropertyIvar;
2341 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2342 }
Mike Stump1eb44332009-09-09 15:08:12 +00002343
2344 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002345 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002346 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2347 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002348 return DeclPtrTy();
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002349 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002350 IC->addPropertyImplementation(PIDecl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002351 } else {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002352 if (Synthesize)
Mike Stump1eb44332009-09-09 15:08:12 +00002353 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002354 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002355 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2356 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002357 << PropertyIvar;
2358 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2359 }
Mike Stump1eb44332009-09-09 15:08:12 +00002360
2361 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002362 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002363 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2364 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002365 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00002366 }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002367 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00002368 }
Mike Stump1eb44332009-09-09 15:08:12 +00002369
Chris Lattnerb28317a2009-03-28 19:18:32 +00002370 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianf624f812008-04-18 00:19:30 +00002371}
Anders Carlsson15281452008-11-04 16:57:32 +00002372
Chris Lattnercc98eac2008-12-17 07:13:27 +00002373bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00002374 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002375 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002376
Anders Carlsson15281452008-11-04 16:57:32 +00002377 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2378 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002379
Anders Carlsson15281452008-11-04 16:57:32 +00002380 return true;
2381}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002382
Chris Lattnercc98eac2008-12-17 07:13:27 +00002383/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2384/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00002385void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002386 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002387 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002388 // Check that ClassName is a valid class
2389 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2390 if (!Class) {
2391 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2392 return;
2393 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002394 if (LangOpts.ObjCNonFragileABI) {
2395 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2396 return;
2397 }
Mike Stump1eb44332009-09-09 15:08:12 +00002398
Chris Lattnercc98eac2008-12-17 07:13:27 +00002399 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00002400 llvm::SmallVector<FieldDecl*, 32> RecFields;
2401 Context.CollectObjCIvars(Class, RecFields);
2402 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2403 for (unsigned i = 0; i < RecFields.size(); i++) {
2404 FieldDecl* ID = RecFields[i];
2405 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2406 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2407 ID->getIdentifier(), ID->getType(),
2408 ID->getBitWidth());
2409 Decls.push_back(Sema::DeclPtrTy::make(FD));
2410 }
Mike Stump1eb44332009-09-09 15:08:12 +00002411
Chris Lattnercc98eac2008-12-17 07:13:27 +00002412 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002413 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002414 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00002415 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00002416 if (getLangOptions().CPlusPlus)
2417 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00002418 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002419 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002420 }
2421}
2422