blob: c0cbc416d94d13f45350c4a18a984a0cb0fb5bce [file] [log] [blame]
Chris Lattnerda463fe2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda463fe2007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Douglas Gregor35b0bac2010-01-03 18:01:57 +000015#include "Lookup.h"
Douglas Gregorc78d3462009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
Steve Naroff157599f2009-03-03 14:49:36 +000017#include "clang/AST/Expr.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattnerda463fe2007-12-12 07:09:47 +000021using namespace clang;
22
Fariborz Jahanianfe9e3942009-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 Naroff79d12152009-07-16 15:41:00 +000029 if (property->getType()->isObjCObjectPointerType())
Fariborz Jahanian3e68a1f2009-05-08 21:10:00 +000030 result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType());
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +000031 if (result != Compatible) {
Mike Stump11289f42009-09-09 15:08:12 +000032 Diag(Loc, diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianfe9e3942009-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 Naroff70f41d62009-02-28 16:59:13 +000042/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattnerda463fe2007-12-12 07:09:47 +000043/// and user declared, in the method definition's AST.
Chris Lattner83f095c2009-03-28 19:18:32 +000044void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis853fbea2008-06-28 06:07:14 +000045 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattner83f095c2009-03-28 19:18:32 +000046 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump11289f42009-09-09 15:08:12 +000047
Steve Naroff542cd5d2008-07-25 17:57:26 +000048 // If we don't have a valid method decl, simply return.
49 if (!MDecl)
50 return;
Steve Naroff1d2538c2007-12-18 01:30:32 +000051
Chris Lattner9fecd742009-04-19 05:21:20 +000052 CurFunctionNeedsScopeChecking = false;
53
Steve Naroff1d2538c2007-12-18 01:30:32 +000054 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorffca3a22009-01-09 17:18:27 +000055 if (MDecl->isInstanceMethod())
Steve Naroff1d2538c2007-12-18 01:30:32 +000056 AddInstanceMethodToGlobalPool(MDecl);
57 else
58 AddFactoryMethodToGlobalPool(MDecl);
Mike Stump11289f42009-09-09 15:08:12 +000059
Chris Lattnerda463fe2007-12-12 07:09:47 +000060 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor91f84212008-12-11 16:49:14 +000061 PushDeclContext(FnBodyScope, MDecl);
Chris Lattnerda463fe2007-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 Lattnerda463fe2007-12-12 07:09:47 +000065
66 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +000067 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump11289f42009-09-09 15:08:12 +000068
Daniel Dunbar279d1cc2008-08-26 06:07:48 +000069 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
70 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +000071
Chris Lattner58258242008-04-10 02:22:51 +000072 // Introduce all of the other parameters into this scope.
Chris Lattnera4997152009-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 Lattnerda463fe2007-12-12 07:09:47 +000077}
78
Chris Lattner83f095c2009-03-28 19:18:32 +000079Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +000080ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
81 IdentifierInfo *ClassName, SourceLocation ClassLoc,
82 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +000083 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +000084 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +000085 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattnerda463fe2007-12-12 07:09:47 +000086 assert(ClassName && "Missing class identifier");
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattnerda463fe2007-12-12 07:09:47 +000088 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +000089 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor5daeee22008-12-08 18:40:42 +000090 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-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 Kremenek1b0ea822008-01-07 19:49:32 +000097 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +000098 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +000099 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000100 }
Mike Stump11289f42009-09-09 15:08:12 +0000101
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000102 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000103 if (IDecl) {
104 // Class already seen. Is it a forward declaration?
Steve Naroff119f60e2008-11-18 19:15:30 +0000105 if (!IDecl->isForwardDecl()) {
Chris Lattnerd13b8b52009-02-23 22:00:08 +0000106 IDecl->setInvalidDecl();
Chris Lattnerf3d3fae2008-11-24 05:29:24 +0000107 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnere6447ef2008-11-23 22:46:27 +0000108 Diag(IDecl->getLocation(), diag::note_previous_definition);
109
Steve Naroff119f60e2008-11-18 19:15:30 +0000110 // Return the previous class interface.
111 // FIXME: don't leak the objects passed in!
Chris Lattner83f095c2009-03-28 19:18:32 +0000112 return DeclPtrTy::make(IDecl);
Steve Naroff119f60e2008-11-18 19:15:30 +0000113 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000114 IDecl->setLocation(AtInterfaceLoc);
115 IDecl->setForwardDecl(false);
Steve Naroffe0064d22009-09-11 00:12:01 +0000116 IDecl->setClassLoc(ClassLoc);
Ted Kremenek707ece62009-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 Jahaniand3612392009-11-17 19:08:08 +0000123 if (AttrList)
124 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000125 }
Chris Lattnerca1e8482008-07-21 07:06:49 +0000126 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000127 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
Steve Naroff5a4611c2008-04-11 19:35:35 +0000128 ClassName, ClassLoc);
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000129 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000130 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +0000131
Steve Naroff3c301dc2009-04-23 15:15:40 +0000132 PushOnScopeChains(IDecl, TUScope);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000133 }
Mike Stump11289f42009-09-09 15:08:12 +0000134
Chris Lattnerda463fe2007-12-12 07:09:47 +0000135 if (SuperName) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000136 // Check if a different kind of symbol declared in this scope.
John McCall9f3059a2009-10-09 21:13:30 +0000137 PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
Douglas Gregor35b0bac2010-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 Gregor6da83622010-01-07 00:17:44 +0000146 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
147 << PrevDecl->getDeclName();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000148 }
149 }
150
Fariborz Jahanian5582f232009-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 Stump12b8ce12009-08-04 21:02:39 +0000155 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000156 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000157 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000158
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000159 // Diagnose classes that inherit from deprecated classes.
160 if (SuperClassDecl)
161 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000162
Fariborz Jahanian5582f232009-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 McCall9dd450b2009-09-21 23:43:11 +0000169 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000170 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
171 }
172 }
Mike Stump11289f42009-09-09 15:08:12 +0000173
Fariborz Jahanian5582f232009-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 Naroff189d41f2009-02-04 17:14:05 +0000182 }
183 }
Mike Stump11289f42009-09-09 15:08:12 +0000184
Fariborz Jahanian5582f232009-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 Naroff189d41f2009-02-04 17:14:05 +0000193 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000194 IDecl->setSuperClass(SuperClassDecl);
195 IDecl->setSuperClassLoc(SuperLoc);
196 IDecl->setLocEnd(SuperLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000197 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000198 } else { // we have a root class.
199 IDecl->setLocEnd(ClassLoc);
200 }
Mike Stump11289f42009-09-09 15:08:12 +0000201
Steve Naroff119f60e2008-11-18 19:15:30 +0000202 /// Check then save referenced protocols.
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000203 if (NumProtoRefs) {
Chris Lattner22298722009-02-20 21:35:13 +0000204 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000205 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000206 IDecl->setLocEnd(EndProtoLoc);
207 }
Mike Stump11289f42009-09-09 15:08:12 +0000208
Anders Carlssona6b508a2008-11-04 16:57:32 +0000209 CheckObjCDeclScope(IDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000210 return DeclPtrTy::make(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000211}
212
213/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000214/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattner83f095c2009-03-28 19:18:32 +0000215Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000216 IdentifierInfo *AliasName,
Chris Lattner83f095c2009-03-28 19:18:32 +0000217 SourceLocation AliasLocation,
218 IdentifierInfo *ClassName,
219 SourceLocation ClassLocation) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000220 // Look for previous declaration of alias name
John McCall9f3059a2009-10-09 21:13:30 +0000221 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000222 if (ADecl) {
Chris Lattnerd0685032008-11-23 23:20:13 +0000223 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattnerda463fe2007-12-12 07:09:47 +0000224 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerd0685032008-11-23 23:20:13 +0000225 else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000226 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerd0685032008-11-23 23:20:13 +0000227 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000228 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000229 }
230 // Check for class declaration
John McCall9f3059a2009-10-09 21:13:30 +0000231 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian17290c32009-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 McCall9dd450b2009-09-21 23:43:11 +0000235 if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000236 ClassName = IDecl->getIdentifier();
John McCall9f3059a2009-10-09 21:13:30 +0000237 CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000238 }
239 }
240 }
Chris Lattner219b3e92008-03-16 21:17:37 +0000241 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
242 if (CDecl == 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000243 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner219b3e92008-03-16 21:17:37 +0000244 if (CDeclU)
Chris Lattnerd0685032008-11-23 23:20:13 +0000245 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +0000246 return DeclPtrTy();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000247 }
Mike Stump11289f42009-09-09 15:08:12 +0000248
Chris Lattner219b3e92008-03-16 21:17:37 +0000249 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump11289f42009-09-09 15:08:12 +0000250 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000251 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000252
Anders Carlssona6b508a2008-11-04 16:57:32 +0000253 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor38feed82009-04-24 02:57:34 +0000254 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000255
Chris Lattner83f095c2009-03-28 19:18:32 +0000256 return DeclPtrTy::make(AliasDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000257}
258
Steve Naroff41d09ad2009-03-05 15:22:01 +0000259void Sema::CheckForwardProtocolDeclarationForCircularDependency(
260 IdentifierInfo *PName,
261 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000262 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000263 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
264 E = PList.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000265
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000266 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
Steve Naroff41d09ad2009-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 Stump11289f42009-09-09 15:08:12 +0000271 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff41d09ad2009-03-05 15:22:01 +0000272 PDecl->getLocation(), PDecl->getReferencedProtocols());
273 }
274 }
275}
276
Chris Lattner83f095c2009-03-28 19:18:32 +0000277Sema::DeclPtrTy
Chris Lattner3bbae002008-07-26 04:03:38 +0000278Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
279 IdentifierInfo *ProtocolName,
280 SourceLocation ProtocolLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000281 const DeclPtrTy *ProtoRefs,
Chris Lattner3bbae002008-07-26 04:03:38 +0000282 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000283 const SourceLocation *ProtoLocs,
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000284 SourceLocation EndProtoLoc,
285 AttributeList *AttrList) {
286 // FIXME: Deal with AttrList.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000287 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000288 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000289 if (PDecl) {
290 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner5074f8f2008-03-16 01:25:17 +0000291 if (!PDecl->isForwardDecl()) {
Fariborz Jahanian54d569c2009-04-06 23:43:32 +0000292 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnere6447ef2008-11-23 22:46:27 +0000293 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000294 // Just return the protocol we already had.
295 // FIXME: don't leak the objects passed in!
Chris Lattner83f095c2009-03-28 19:18:32 +0000296 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000297 }
Steve Naroff41d09ad2009-03-05 15:22:01 +0000298 ObjCList<ObjCProtocolDecl> PList;
Mike Stump11289f42009-09-09 15:08:12 +0000299 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff41d09ad2009-03-05 15:22:01 +0000300 CheckForwardProtocolDeclarationForCircularDependency(
301 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
302 PList.Destroy(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000303
Steve Naroffeb03dac2008-08-13 16:39:22 +0000304 // Make sure the cached decl gets a valid start location.
305 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000306 PDecl->setForwardDecl(false);
Chris Lattner5074f8f2008-03-16 01:25:17 +0000307 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000308 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000309 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000310 PushOnScopeChains(PDecl, TUScope);
Chris Lattneracc04a92008-03-16 20:19:15 +0000311 PDecl->setForwardDecl(false);
Chris Lattnerf87ca0a2008-03-16 01:23:04 +0000312 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000313 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000314 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000315 if (NumProtoRefs) {
Chris Lattneracc04a92008-03-16 20:19:15 +0000316 /// Check then save referenced protocols.
Douglas Gregor002b6712010-01-16 15:02:53 +0000317 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
318 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000319 PDecl->setLocEnd(EndProtoLoc);
320 }
Mike Stump11289f42009-09-09 15:08:12 +0000321
322 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000323 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000324}
325
326/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbarc7dfbfd2008-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 Lattnerda463fe2007-12-12 07:09:47 +0000329void
Chris Lattner3bbae002008-07-26 04:03:38 +0000330Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000331 const IdentifierLocPair *ProtocolId,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000332 unsigned NumProtocols,
Chris Lattner83f095c2009-03-28 19:18:32 +0000333 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000334 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000335 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000336 if (!PDecl) {
Douglas Gregor35b0bac2010-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 Gregor6da83622010-01-07 00:17:44 +0000343 Diag(PDecl->getLocation(), diag::note_previous_decl)
344 << PDecl->getDeclName();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000345 }
346 }
347
348 if (!PDecl) {
Chris Lattner3b054132008-11-19 05:08:23 +0000349 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000350 << ProtocolId[i].first;
Chris Lattner9c1842b2008-07-26 03:47:43 +0000351 continue;
352 }
Mike Stump11289f42009-09-09 15:08:12 +0000353
Douglas Gregor171c45a2009-02-18 21:56:37 +0000354 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner9c1842b2008-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 Lattner3b054132008-11-19 05:08:23 +0000359 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000360 << ProtocolId[i].first;
Chris Lattner83f095c2009-03-28 19:18:32 +0000361 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattnerda463fe2007-12-12 07:09:47 +0000362 }
363}
364
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000365/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar4684f372008-08-27 05:40:03 +0000366/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000367///
Fariborz Jahanian7cf18862008-05-01 00:03:38 +0000368void
Mike Stump11289f42009-09-09 15:08:12 +0000369Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
Fariborz Jahanian7cf18862008-05-01 00:03:38 +0000370 ObjCPropertyDecl *SuperProperty,
Chris Lattner86d7d912008-11-24 03:54:41 +0000371 const IdentifierInfo *inheritedName) {
Mike Stump11289f42009-09-09 15:08:12 +0000372 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000373 Property->getPropertyAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000374 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000375 SuperProperty->getPropertyAttributes();
376 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
377 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner3b054132008-11-19 05:08:23 +0000378 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner86d7d912008-11-24 03:54:41 +0000379 << Property->getDeclName() << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000380 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
381 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner377d1f82008-11-18 22:52:51 +0000382 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000383 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000384 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
385 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner377d1f82008-11-18 22:52:51 +0000386 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000387 << Property->getDeclName() << "retain" << inheritedName;
Mike Stump11289f42009-09-09 15:08:12 +0000388
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000389 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
390 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner377d1f82008-11-18 22:52:51 +0000391 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000392 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000393 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner377d1f82008-11-18 22:52:51 +0000394 Diag(Property->getLocation(), diag::warn_property_attribute)
Mike Stump11289f42009-09-09 15:08:12 +0000395 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000396 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner377d1f82008-11-18 22:52:51 +0000397 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner86d7d912008-11-24 03:54:41 +0000398 << Property->getDeclName() << "getter" << inheritedName;
Steve Naroff3e1181e2009-03-03 15:43:24 +0000399
Mike Stump11289f42009-09-09 15:08:12 +0000400 QualType LHSType =
Steve Naroff3e1181e2009-03-03 15:43:24 +0000401 Context.getCanonicalType(SuperProperty->getType());
Mike Stump11289f42009-09-09 15:08:12 +0000402 QualType RHSType =
Steve Naroff3e1181e2009-03-03 15:43:24 +0000403 Context.getCanonicalType(Property->getType());
Mike Stump11289f42009-09-09 15:08:12 +0000404
Steve Naroff3e1181e2009-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 Naroff8e6aee52009-07-23 01:01:38 +0000408 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
Steve Naroff3e1181e2009-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 Jahanian0a070ff2008-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 Lattner9018ca82009-02-16 21:26:43 +0000419void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000420 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
421 if (!SDecl)
422 return;
Daniel Dunbar4684f372008-08-27 05:40:03 +0000423 // FIXME: O(N^2)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000424 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
425 E = SDecl->prop_end(); S != E; ++S) {
Fariborz Jahanian7cf18862008-05-01 00:03:38 +0000426 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000427 // Does property in super class has declaration in current class?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000428 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
429 E = IDecl->prop_end(); I != E; ++I) {
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000430 ObjCPropertyDecl *PDecl = (*I);
431 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Mike Stump11289f42009-09-09 15:08:12 +0000432 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner86d7d912008-11-24 03:54:41 +0000433 SDecl->getIdentifier());
Fariborz Jahanian0a070ff2008-04-24 19:58:34 +0000434 }
435 }
436}
437
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000438/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
439/// of properties declared in a protocol and compares their attribute against
440/// the same property declared in the class or category.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000441void
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000442Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
Chris Lattner86d7d912008-11-24 03:54:41 +0000443 ObjCProtocolDecl *PDecl) {
Fariborz Jahaniand2c2ad52008-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);
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000448 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000449 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
450 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000451 ObjCPropertyDecl *Pr = (*P);
Steve Naroffb3a87982009-01-09 15:36:25 +0000452 ObjCCategoryDecl::prop_iterator CP, CE;
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000453 // Is this property already in category's list of properties?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000454 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000455 if ((*CP)->getIdentifier() == Pr->getIdentifier())
456 break;
Fariborz Jahanian519976c2009-01-09 21:04:52 +0000457 if (CP != CE)
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000458 // Property protocol already exist in class. Diagnose any mismatch.
459 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
460 }
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000461 return;
462 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000463 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
464 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000465 ObjCPropertyDecl *Pr = (*P);
Steve Naroffb3a87982009-01-09 15:36:25 +0000466 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000467 // Is this property already in class's list of properties?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000468 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000469 if ((*CP)->getIdentifier() == Pr->getIdentifier())
470 break;
Fariborz Jahanian519976c2009-01-09 21:04:52 +0000471 if (CP != CE)
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000472 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner86d7d912008-11-24 03:54:41 +0000473 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000474 }
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000475}
476
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000477/// CompareProperties - This routine compares properties
478/// declared in 'ClassOrProtocol' objects (which can be a class or an
479/// inherited protocol with the list of properties for class/category 'CDecl'
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000480///
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000481void Sema::CompareProperties(Decl *CDecl,
482 DeclPtrTy ClassOrProtocol) {
483 Decl *ClassDecl = ClassOrProtocol.getAs<Decl>();
Fariborz Jahaniand2c2ad52008-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);
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000489 assert (CatDecl && "CompareProperties");
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000490 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
491 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
492 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000493 // Match properties of category with those of protocol (*P)
494 MatchOneProtocolPropertiesInClass(CatDecl, *P);
Mike Stump11289f42009-09-09 15:08:12 +0000495
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000496 // Go thru the list of protocols for this category and recursively match
497 // their properties with those in the category.
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000498 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
499 E = CatDecl->protocol_end(); P != E; ++P)
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000500 CompareProperties(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahaniand2c2ad52008-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)
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000505 MatchOneProtocolPropertiesInClass(CatDecl, *P);
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000506 }
507 return;
508 }
509
Chris Lattnerca1e8482008-07-21 07:06:49 +0000510 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000511 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
512 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000513 // Match properties of class IDecl with those of protocol (*P).
514 MatchOneProtocolPropertiesInClass(IDecl, *P);
Mike Stump11289f42009-09-09 15:08:12 +0000515
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000516 // Go thru the list of protocols for this class and recursively match
517 // their properties with those declared in the class.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000518 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
519 E = IDecl->protocol_end(); P != E; ++P)
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000520 CompareProperties(IDecl, DeclPtrTy::make(*P));
Chris Lattnerca1e8482008-07-21 07:06:49 +0000521 } else {
Argyrios Kyrtzidisb3fa8632008-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)
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000525 MatchOneProtocolPropertiesInClass(IDecl, *P);
Chris Lattnerca1e8482008-07-21 07:06:49 +0000526 }
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000527}
528
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000529/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000530/// a class method in its extension.
531///
Mike Stump11289f42009-09-09 15:08:12 +0000532void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000538 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
539 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000540 ObjCMethodDecl *MD = *i;
541 MethodMap[MD->getSelector()] = MD;
542 }
543
544 if (MethodMap.empty())
545 return;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000546 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
547 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-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 Lattnerdac168d2009-04-12 08:43:13 +0000558/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner83f095c2009-03-28 19:18:32 +0000559Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000560Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000561 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000562 unsigned NumElts,
563 AttributeList *attrList) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000564 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor002b6712010-01-16 15:02:53 +0000565 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump11289f42009-09-09 15:08:12 +0000566
Chris Lattnerda463fe2007-12-12 07:09:47 +0000567 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000568 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000569 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000570 if (PDecl == 0) { // Not already seen?
Mike Stump11289f42009-09-09 15:08:12 +0000571 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000572 IdentList[i].second, Ident);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000573 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000574 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000575 if (attrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000576 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000577 Protocols.push_back(PDecl);
Douglas Gregor002b6712010-01-16 15:02:53 +0000578 ProtoLocs.push_back(IdentList[i].second);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000579 }
Mike Stump11289f42009-09-09 15:08:12 +0000580
581 ObjCForwardProtocolDecl *PDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000582 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor002b6712010-01-16 15:02:53 +0000583 Protocols.data(), Protocols.size(),
584 ProtoLocs.data());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000585 CurContext->addDecl(PDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +0000586 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000587 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000588}
589
Chris Lattner83f095c2009-03-28 19:18:32 +0000590Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000591ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
592 IdentifierInfo *ClassName, SourceLocation ClassLoc,
593 IdentifierInfo *CategoryName,
594 SourceLocation CategoryLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000595 const DeclPtrTy *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000596 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000597 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000598 SourceLocation EndProtoLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000599 ObjCCategoryDecl *CDecl =
Douglas Gregor071676f2010-01-16 16:38:58 +0000600 ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, ClassLoc,
601 CategoryLoc, CategoryName);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000602 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000603 CurContext->addDecl(CDecl);
Chris Lattner9018ca82009-02-16 21:26:43 +0000604
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000605 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Fariborz Jahanian9a5124a2008-01-17 20:33:24 +0000606 /// Check that class of this category is already completely declared.
Chris Lattner9018ca82009-02-16 21:26:43 +0000607 if (!IDecl || IDecl->isForwardDecl()) {
608 CDecl->setInvalidDecl();
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000609 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner83f095c2009-03-28 19:18:32 +0000610 return DeclPtrTy::make(CDecl);
Fariborz Jahanian9a5124a2008-01-17 20:33:24 +0000611 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000612
Chris Lattner9018ca82009-02-16 21:26:43 +0000613 CDecl->setClassInterface(IDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000614
Chris Lattnerced903b2009-02-16 21:30:01 +0000615 // If the interface is deprecated, warn about it.
Douglas Gregor171c45a2009-02-18 21:56:37 +0000616 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner9018ca82009-02-16 21:26:43 +0000617
618 /// Check for duplicate interface declaration for this category
619 ObjCCategoryDecl *CDeclChain;
620 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
621 CDeclChain = CDeclChain->getNextClassCategory()) {
622 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
623 Diag(CategoryLoc, diag::warn_dup_category_def)
624 << ClassName << CategoryName;
625 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
626 break;
627 }
628 }
629 if (!CDeclChain)
630 CDecl->insertNextClassCategory();
631
Chris Lattnerda463fe2007-12-12 07:09:47 +0000632 if (NumProtoRefs) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000633 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000634 ProtoLocs, Context);
Fariborz Jahanian092cd6e2009-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 Gregor002b6712010-01-16 15:02:53 +0000638 NumProtoRefs, ProtoLocs,
639 Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000640 }
Mike Stump11289f42009-09-09 15:08:12 +0000641
Anders Carlssona6b508a2008-11-04 16:57:32 +0000642 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000643 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000644}
645
646/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000647/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000648/// object.
Chris Lattner83f095c2009-03-28 19:18:32 +0000649Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000650 SourceLocation AtCatImplLoc,
651 IdentifierInfo *ClassName, SourceLocation ClassLoc,
652 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000653 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis6d9fab72009-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(),
Douglas Gregor071676f2010-01-16 16:38:58 +0000661 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000662 CatName);
663 CatIDecl->setClassInterface(IDecl);
664 CatIDecl->insertNextClassCategory();
665 }
666 }
667
Mike Stump11289f42009-09-09 15:08:12 +0000668 ObjCCategoryImplDecl *CDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000669 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
670 IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000671 /// Check that class of this category is already completely declared.
672 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000673 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000674
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000675 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000676 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000677
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000678 /// Check that CatName, category name, is not used in another implementation.
679 if (CatIDecl) {
680 if (CatIDecl->getImplementation()) {
681 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
682 << CatName;
683 Diag(CatIDecl->getImplementation()->getLocation(),
684 diag::note_previous_definition);
685 } else
686 CatIDecl->setImplementation(CDecl);
687 }
Mike Stump11289f42009-09-09 15:08:12 +0000688
Anders Carlssona6b508a2008-11-04 16:57:32 +0000689 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000690 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000691}
692
Chris Lattner83f095c2009-03-28 19:18:32 +0000693Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000694 SourceLocation AtClassImplLoc,
695 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000696 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000697 SourceLocation SuperClassLoc) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000698 ObjCInterfaceDecl* IDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000699 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +0000700 NamedDecl *PrevDecl
701 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000702 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000703 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000704 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor40f7a002010-01-04 17:27:12 +0000705 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
706 // If this is a forward declaration of an interface, warn.
707 if (IDecl->isForwardDecl()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000708 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian6f0f25b2009-04-23 21:49:04 +0000709 IDecl = 0;
710 }
Douglas Gregor40f7a002010-01-04 17:27:12 +0000711 } else {
712 // We did not find anything with the name ClassName; try to correct for
713 // typos in the class name.
714 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
715 if (CorrectTypo(R, TUScope, 0) &&
716 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregor10f1e4d2010-01-06 23:44:25 +0000717 // Suggest the (potentially) correct interface name. However, put the
718 // fix-it hint itself in a separate note, since changing the name in
719 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor40f7a002010-01-04 17:27:12 +0000720 // provide a code-modification hint or use the typo name for recovery,
721 // because this is just a warning. The program may actually be correct.
722 Diag(ClassLoc, diag::warn_undef_interface_suggest)
723 << ClassName << R.getLookupName();
Douglas Gregor10f1e4d2010-01-06 23:44:25 +0000724 Diag(IDecl->getLocation(), diag::note_previous_decl)
725 << R.getLookupName()
726 << CodeModificationHint::CreateReplacement(ClassLoc,
727 R.getLookupName().getAsString());
Douglas Gregor40f7a002010-01-04 17:27:12 +0000728 IDecl = 0;
729 } else {
730 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
731 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000732 }
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattnerda463fe2007-12-12 07:09:47 +0000734 // Check that super class name is valid class name
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000735 ObjCInterfaceDecl* SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000736 if (SuperClassname) {
737 // Check if a different kind of symbol declared in this scope.
John McCall9f3059a2009-10-09 21:13:30 +0000738 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000739 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000740 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
741 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +0000742 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000743 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000744 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000745 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000746 Diag(SuperClassLoc, diag::err_undef_superclass)
747 << SuperClassname << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000748 else if (IDecl && IDecl->getSuperClass() != SDecl) {
749 // This implementation and its interface do not have the same
750 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000751 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000752 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +0000753 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000754 }
755 }
756 }
Mike Stump11289f42009-09-09 15:08:12 +0000757
Chris Lattnerda463fe2007-12-12 07:09:47 +0000758 if (!IDecl) {
759 // Legacy case of @implementation with no corresponding @interface.
760 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000761
Mike Stump87c57ac2009-05-16 07:39:55 +0000762 // FIXME: Do we support attributes on the @implementation? If so we should
763 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +0000764 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000765 ClassName, ClassLoc, false, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000766 IDecl->setSuperClass(SDecl);
767 IDecl->setLocEnd(ClassLoc);
Douglas Gregorac345a32009-04-24 00:16:12 +0000768
769 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbard1148a72009-04-21 21:41:56 +0000770 } else {
771 // Mark the interface as being completed, even if it was just as
772 // @class ....;
773 // declaration; the user cannot reopen it.
774 IDecl->setForwardDecl(false);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
777 ObjCImplementationDecl* IMPDecl =
778 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000779 IDecl, SDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000780
Anders Carlssona6b508a2008-11-04 16:57:32 +0000781 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner83f095c2009-03-28 19:18:32 +0000782 return DeclPtrTy::make(IMPDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000783
Chris Lattnerda463fe2007-12-12 07:09:47 +0000784 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000785 if (IDecl->getImplementation()) {
Chris Lattner36ac1ca2008-03-16 20:53:07 +0000786 // FIXME: Don't leak everything!
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000787 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000788 Diag(IDecl->getImplementation()->getLocation(),
789 diag::note_previous_definition);
790 } else { // add it to the list.
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000791 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +0000792 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000793 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000794 return DeclPtrTy::make(IMPDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000795}
796
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000797void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
798 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000799 SourceLocation RBrace) {
800 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000801 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000802 if (!IDecl)
803 return;
804 /// Check case of non-existing @interface decl.
805 /// (legacy objective-c @implementation decl without an @interface decl).
806 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +0000807 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner22298722009-02-20 21:35:13 +0000808 IDecl->setIVarList(ivars, numIvars, Context);
809 IDecl->setLocEnd(RBrace);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000810 return;
811 }
812 // If implementation has empty ivar list, just return.
813 if (numIvars == 0)
814 return;
Mike Stump11289f42009-09-09 15:08:12 +0000815
Chris Lattnerda463fe2007-12-12 07:09:47 +0000816 assert(ivars && "missing @implementation ivars");
Mike Stump11289f42009-09-09 15:08:12 +0000817
Chris Lattnerda463fe2007-12-12 07:09:47 +0000818 // Check interface's Ivar list against those in the implementation.
819 // names and types must match.
820 //
Chris Lattnerda463fe2007-12-12 07:09:47 +0000821 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000822 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +0000823 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
824 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000825 ObjCIvarDecl* ImplIvar = ivars[j++];
826 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000827 assert (ImplIvar && "missing implementation ivar");
828 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +0000829
Steve Naroff157599f2009-03-03 14:49:36 +0000830 // First, make sure the types match.
Chris Lattner35ffe332008-07-27 00:05:05 +0000831 if (Context.getCanonicalType(ImplIvar->getType()) !=
832 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +0000833 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000834 << ImplIvar->getIdentifier()
835 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +0000836 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff157599f2009-03-03 14:49:36 +0000837 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
838 Expr *ImplBitWidth = ImplIvar->getBitWidth();
839 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000840 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
841 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff157599f2009-03-03 14:49:36 +0000842 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
843 << ImplIvar->getIdentifier();
844 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
845 }
Mike Stump11289f42009-09-09 15:08:12 +0000846 }
Steve Naroff157599f2009-03-03 14:49:36 +0000847 // Make sure the names are identical.
848 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000849 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000850 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +0000851 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000852 }
853 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000854 }
Mike Stump11289f42009-09-09 15:08:12 +0000855
Chris Lattner0f29d982007-12-12 18:11:49 +0000856 if (numIvars > 0)
Chris Lattner83021e92007-12-12 18:19:52 +0000857 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +0000858 else if (IVI != IVE)
Chris Lattner83021e92007-12-12 18:19:52 +0000859 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000860}
861
Steve Naroff15833ed2008-02-10 21:38:56 +0000862void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
863 bool &IncompleteImpl) {
864 if (!IncompleteImpl) {
865 Diag(ImpLoc, diag::warn_incomplete_impl);
866 IncompleteImpl = true;
867 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000868 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff15833ed2008-02-10 21:38:56 +0000869}
870
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000871void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
872 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnera9d0ffe2009-04-11 18:01:59 +0000873 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian0c20bdd2009-05-14 23:52:54 +0000874 ImpMethodDecl->getResultType()) &&
Steve Naroff8e6aee52009-07-23 01:01:38 +0000875 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
876 ImpMethodDecl->getResultType())) {
Mike Stump11289f42009-09-09 15:08:12 +0000877 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000878 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
879 << ImpMethodDecl->getResultType();
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000880 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
881 }
Mike Stump11289f42009-09-09 15:08:12 +0000882
Chris Lattner67f35b02009-04-11 19:58:42 +0000883 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
884 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
885 IM != EM; ++IM, ++IF) {
Fariborz Jahanian41e803d2009-11-18 18:56:09 +0000886 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
887 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
888 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
889 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner67f35b02009-04-11 19:58:42 +0000890 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000891
892 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000893 << ImpMethodDecl->getDeclName() << (*IF)->getType()
894 << (*IM)->getType();
Chris Lattner5300acc2009-04-11 20:14:49 +0000895 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner67f35b02009-04-11 19:58:42 +0000896 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000897}
898
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000899/// isPropertyReadonly - Return true if property is readonly, by searching
900/// for the property in the class and in its categories and implementations
901///
902bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffde680012009-02-26 19:11:32 +0000903 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000904 // by far the most common case.
905 if (!PDecl->isReadOnly())
906 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000907 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000908 // it is not considered read only.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000909 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000910 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000911
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000912 // Main class has the property as 'readonly'. Must search
Mike Stump11289f42009-09-09 15:08:12 +0000913 // through the category list to see if the property's
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000914 // attribute has been over-ridden to 'readwrite'.
915 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
916 Category; Category = Category->getNextClassCategory()) {
Mike Stump11289f42009-09-09 15:08:12 +0000917 // Even if property is ready only, if a category has a user defined setter,
918 // it is not considered read only.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000919 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000920 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000921 ObjCPropertyDecl *P =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000922 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000923 if (P && !P->isReadOnly())
924 return false;
925 }
Mike Stump11289f42009-09-09 15:08:12 +0000926
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000927 // Also, check for definition of a setter method in the implementation if
928 // all else failed.
929 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump11289f42009-09-09 15:08:12 +0000930 if (ObjCImplementationDecl *IMD =
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000931 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000932 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000933 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000934 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stump12b8ce12009-08-04 21:02:39 +0000935 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000936 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000937 return false;
938 }
939 }
Steve Naroffde680012009-02-26 19:11:32 +0000940 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000941 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000942 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroffde680012009-02-26 19:11:32 +0000943 return false;
Fariborz Jahanian15e3a5c2009-04-06 16:59:10 +0000944 // If all fails, look at the super class.
945 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
946 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000947 return true;
948}
949
Mike Stump87c57ac2009-05-16 07:39:55 +0000950/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
951/// improve the efficiency of selector lookups and type checking by associating
952/// with each protocol / interface / category the flattened instance tables. If
953/// we used an immutable set to keep the table then it wouldn't add significant
954/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +0000955
Steve Naroffa36992242008-02-08 22:06:17 +0000956/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +0000957/// Declared in protocol, and those referenced by it.
Steve Naroffa36992242008-02-08 22:06:17 +0000958void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
959 ObjCProtocolDecl *PDecl,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000960 bool& IncompleteImpl,
Steve Naroffa36992242008-02-08 22:06:17 +0000961 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000962 const llvm::DenseSet<Selector> &ClsMap,
963 ObjCInterfaceDecl *IDecl) {
964 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000965 ObjCInterfaceDecl *NSIDecl = 0;
966 if (getLangOptions().NeXTRuntime) {
Mike Stump11289f42009-09-09 15:08:12 +0000967 // check to see if class implements forwardInvocation method and objects
968 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000969 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +0000970 // Under such conditions, which means that every method possible is
971 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000972 // found" warnings.
973 // FIXME: Use a general GetUnarySelector method for this.
974 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
975 Selector fISelector = Context.Selectors.getSelector(1, &II);
976 if (InsMap.count(fISelector))
977 // Is IDecl derived from 'NSProxy'? If so, no instance methods
978 // need be implemented in the implementation.
979 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
980 }
Mike Stump11289f42009-09-09 15:08:12 +0000981
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000982 // If a method lookup fails locally we still need to look and see if
983 // the method was implemented by a base class or an inherited
984 // protocol. This lookup is slow, but occurs rarely in correct code
985 // and otherwise would terminate in a warning.
986
Chris Lattnerda463fe2007-12-12 07:09:47 +0000987 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000988 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +0000989 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000990 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000991 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000992 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000993 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000994 (!Super ||
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000995 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000996 // Ugly, but necessary. Method declared in protcol might have
997 // have been synthesized due to a property declared in the class which
998 // uses the protocol.
Mike Stump11289f42009-09-09 15:08:12 +0000999 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001000 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001001 if (!MethodInClass || !MethodInClass->isSynthesized())
1002 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
1003 }
1004 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001005 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +00001006 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001007 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001008 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001009 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001010 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1011 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001012 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff15833ed2008-02-10 21:38:56 +00001013 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff3ce37a62007-12-14 23:37:57 +00001014 }
Chris Lattner390d39a2008-07-21 21:32:27 +00001015 // Check on this protocols's referenced protocols, recursively.
1016 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1017 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001018 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001019}
1020
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001021/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1022/// or protocol against those declared in their implementations.
1023///
1024void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1025 const llvm::DenseSet<Selector> &ClsMap,
1026 llvm::DenseSet<Selector> &InsMapSeen,
1027 llvm::DenseSet<Selector> &ClsMapSeen,
1028 ObjCImplDecl* IMPDecl,
1029 ObjCContainerDecl* CDecl,
1030 bool &IncompleteImpl,
Mike Stump11289f42009-09-09 15:08:12 +00001031 bool ImmediateClass) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001032 // Check and see if instance methods in class interface have been
1033 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001034 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1035 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001036 if (InsMapSeen.count((*I)->getSelector()))
1037 continue;
1038 InsMapSeen.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001039 if (!(*I)->isSynthesized() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001040 !InsMap.count((*I)->getSelector())) {
1041 if (ImmediateClass)
1042 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1043 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +00001044 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001045 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001046 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001047 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001048 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001049 assert(IntfMethodDecl &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001050 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1051 // ImpMethodDecl may be null as in a @dynamic property.
1052 if (ImpMethodDecl)
1053 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1054 }
1055 }
Mike Stump11289f42009-09-09 15:08:12 +00001056
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001057 // Check and see if class methods in class interface have been
1058 // implemented in the implementation class. If so, their types match.
Mike Stump11289f42009-09-09 15:08:12 +00001059 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001060 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001061 if (ClsMapSeen.count((*I)->getSelector()))
1062 continue;
1063 ClsMapSeen.insert((*I)->getSelector());
1064 if (!ClsMap.count((*I)->getSelector())) {
1065 if (ImmediateClass)
1066 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stump12b8ce12009-08-04 21:02:39 +00001067 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001068 ObjCMethodDecl *ImpMethodDecl =
1069 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001070 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001071 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001072 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1073 }
1074 }
1075 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1076 // Check for any implementation of a methods declared in protocol.
1077 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1078 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001079 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1080 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001081 (*PI), IncompleteImpl, false);
1082 if (I->getSuperClass())
1083 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +00001084 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001085 I->getSuperClass(), IncompleteImpl, false);
1086 }
1087}
1088
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001089/// CheckPropertyImplementation - Check that all required properties are
1090/// synthesized in class's implementation. This includes properties
1091/// declared in current class and in class's protocols (direct or indirect).
1092void Sema::CheckPropertyImplementation(ObjCImplDecl* IMPDecl,
1093 ObjCInterfaceDecl *CDecl) {
1094}
1095
Mike Stump11289f42009-09-09 15:08:12 +00001096void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1097 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001098 bool IncompleteImpl) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001099 llvm::DenseSet<Selector> InsMap;
1100 // Check and see if instance methods in class interface have been
1101 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001102 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001103 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001104 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001105
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001106 // Check and see if properties declared in the interface have either 1)
1107 // an implementation or 2) there is a @synthesize/@dynamic implementation
1108 // of the property in the @implementation.
1109 if (isa<ObjCInterfaceDecl>(CDecl))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001110 for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
1111 E = CDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001112 ObjCPropertyDecl *Prop = (*P);
1113 if (Prop->isInvalidDecl())
1114 continue;
1115 ObjCPropertyImplDecl *PI = 0;
1116 // Is there a matching propery synthesize/dynamic?
Mike Stump11289f42009-09-09 15:08:12 +00001117 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001118 I = IMPDecl->propimpl_begin(),
1119 EI = IMPDecl->propimpl_end(); I != EI; ++I)
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001120 if ((*I)->getPropertyDecl() == Prop) {
1121 PI = (*I);
1122 break;
1123 }
1124 if (PI)
1125 continue;
1126 if (!InsMap.count(Prop->getGetterName())) {
Mike Stump11289f42009-09-09 15:08:12 +00001127 Diag(Prop->getLocation(),
1128 diag::warn_setter_getter_impl_required)
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001129 << Prop->getDeclName() << Prop->getGetterName();
1130 Diag(IMPDecl->getLocation(),
1131 diag::note_property_impl_required);
1132 }
Mike Stump11289f42009-09-09 15:08:12 +00001133
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001134 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Mike Stump11289f42009-09-09 15:08:12 +00001135 Diag(Prop->getLocation(),
1136 diag::warn_setter_getter_impl_required)
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001137 << Prop->getDeclName() << Prop->getSetterName();
1138 Diag(IMPDecl->getLocation(),
1139 diag::note_property_impl_required);
1140 }
1141 }
Mike Stump11289f42009-09-09 15:08:12 +00001142
Chris Lattnerda463fe2007-12-12 07:09:47 +00001143 llvm::DenseSet<Selector> ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +00001144 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001145 I = IMPDecl->classmeth_begin(),
1146 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001147 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001148
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001149 // Check for type conflict of methods declared in a class/protocol and
1150 // its implementation; if any.
1151 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +00001152 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1153 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001154 IncompleteImpl, true);
Mike Stump11289f42009-09-09 15:08:12 +00001155
Chris Lattnerda463fe2007-12-12 07:09:47 +00001156 // Check the protocol list for unimplemented methods in the @implementation
1157 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001158 // Check and see if class methods in class interface have been
1159 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001160
Chris Lattner9ef10f42009-03-01 00:56:52 +00001161 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001162 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner9ef10f42009-03-01 00:56:52 +00001163 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001164 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001165 InsMap, ClsMap, I);
1166 // Check class extensions (unnamed categories)
1167 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1168 Categories; Categories = Categories->getNextClassCategory()) {
1169 if (!Categories->getIdentifier()) {
1170 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1171 break;
1172 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +00001173 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001174 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001175 // For extended class, unimplemented methods in its protocols will
1176 // be reported in the primary class.
1177 if (C->getIdentifier()) {
1178 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1179 E = C->protocol_end(); PI != E; ++PI)
1180 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1181 InsMap, ClsMap, C->getClassInterface());
1182 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001183 } else
1184 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00001185}
1186
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001187void
1188Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1189 ObjCContainerDecl* IDecl) {
1190 // Rules apply in non-GC mode only
1191 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1192 return;
1193 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1194 E = IDecl->prop_end();
1195 I != E; ++I) {
1196 ObjCPropertyDecl *Property = (*I);
1197 unsigned Attributes = Property->getPropertyAttributes();
1198 // We only care about readwrite atomic property.
1199 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1200 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1201 continue;
1202 if (const ObjCPropertyImplDecl *PIDecl
1203 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1204 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1205 continue;
1206 ObjCMethodDecl *GetterMethod =
1207 IMPDecl->getInstanceMethod(Property->getGetterName());
1208 ObjCMethodDecl *SetterMethod =
1209 IMPDecl->getInstanceMethod(Property->getSetterName());
1210 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1211 SourceLocation MethodLoc =
1212 (GetterMethod ? GetterMethod->getLocation()
1213 : SetterMethod->getLocation());
1214 Diag(MethodLoc, diag::warn_atomic_property_rule)
1215 << Property->getIdentifier();
1216 Diag(Property->getLocation(), diag::note_property_declare);
1217 }
1218 }
1219 }
1220}
1221
Mike Stump11289f42009-09-09 15:08:12 +00001222/// ActOnForwardClassDeclaration -
Chris Lattner83f095c2009-03-28 19:18:32 +00001223Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00001224Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00001225 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00001226 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00001227 unsigned NumElts) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001228 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump11289f42009-09-09 15:08:12 +00001229
Chris Lattnerda463fe2007-12-12 07:09:47 +00001230 for (unsigned i = 0; i != NumElts; ++i) {
1231 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001232 NamedDecl *PrevDecl
1233 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor5daeee22008-12-08 18:40:42 +00001234 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001235 // Maybe we will complain about the shadowed template parameter.
1236 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1237 // Just pretend that we didn't see the previous declaration.
1238 PrevDecl = 0;
1239 }
1240
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001241 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00001242 // GCC apparently allows the following idiom:
1243 //
1244 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1245 // @class XCElementToggler;
1246 //
Mike Stump11289f42009-09-09 15:08:12 +00001247 // FIXME: Make an extension?
Steve Naroff946166f2008-06-05 22:57:10 +00001248 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1249 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001250 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00001251 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stump12b8ce12009-08-04 21:02:39 +00001252 } else if (TDD) {
1253 // a forward class declaration matching a typedef name of a class refers
1254 // to the underlying class.
Mike Stump11289f42009-09-09 15:08:12 +00001255 if (ObjCInterfaceType * OI =
Fariborz Jahanian0d451812009-05-07 21:49:26 +00001256 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1257 PrevDecl = OI->getDecl();
1258 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001259 }
Mike Stump11289f42009-09-09 15:08:12 +00001260 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001261 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump11289f42009-09-09 15:08:12 +00001262 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001263 IdentList[i], IdentLocs[i], true);
Ted Kremenek707ece62009-11-17 22:58:30 +00001264
1265 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1266 // the current DeclContext. This prevents clients that walk DeclContext
1267 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1268 // declared later (if at all). We also take care to explicitly make
1269 // sure this declaration is visible for name lookup.
1270 PushOnScopeChains(IDecl, TUScope, false);
1271 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001272 }
1273
1274 Interfaces.push_back(IDecl);
1275 }
Mike Stump11289f42009-09-09 15:08:12 +00001276
Ted Kremenek9b124e12009-11-18 00:28:11 +00001277 assert(Interfaces.size() == NumElts);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001278 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001279 Interfaces.data(), IdentLocs,
Anders Carlssona6b508a2008-11-04 16:57:32 +00001280 Interfaces.size());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001281 CurContext->addDecl(CDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +00001282 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001283 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001284}
1285
1286
1287/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1288/// returns true, or false, accordingly.
1289/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump11289f42009-09-09 15:08:12 +00001290bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffa0ed1652008-10-21 10:37:50 +00001291 const ObjCMethodDecl *PrevMethod,
1292 bool matchBasedOnSizeAndAlignment) {
1293 QualType T1 = Context.getCanonicalType(Method->getResultType());
1294 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump11289f42009-09-09 15:08:12 +00001295
Steve Naroffa0ed1652008-10-21 10:37:50 +00001296 if (T1 != T2) {
1297 // The result types are different.
1298 if (!matchBasedOnSizeAndAlignment)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001299 return false;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001300 // Incomplete types don't have a size and alignment.
1301 if (T1->isIncompleteType() || T2->isIncompleteType())
1302 return false;
1303 // Check is based on size and alignment.
1304 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1305 return false;
1306 }
Mike Stump11289f42009-09-09 15:08:12 +00001307
Chris Lattnera4997152009-02-20 18:43:26 +00001308 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1309 E = Method->param_end();
1310 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001311
Chris Lattnera4997152009-02-20 18:43:26 +00001312 for (; ParamI != E; ++ParamI, ++PrevI) {
1313 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1314 T1 = Context.getCanonicalType((*ParamI)->getType());
1315 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffa0ed1652008-10-21 10:37:50 +00001316 if (T1 != T2) {
1317 // The result types are different.
1318 if (!matchBasedOnSizeAndAlignment)
1319 return false;
1320 // Incomplete types don't have a size and alignment.
1321 if (T1->isIncompleteType() || T2->isIncompleteType())
1322 return false;
1323 // Check is based on size and alignment.
1324 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1325 return false;
1326 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001327 }
1328 return true;
1329}
1330
Douglas Gregorc78d3462009-04-24 21:10:55 +00001331/// \brief Read the contents of the instance and factory method pools
1332/// for a given selector from external storage.
1333///
1334/// This routine should only be called once, when neither the instance
1335/// nor the factory method pool has an entry for this selector.
Mike Stump11289f42009-09-09 15:08:12 +00001336Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001337 bool isInstance) {
1338 assert(ExternalSource && "We need an external AST source");
1339 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1340 "Selector data already loaded into the instance method pool");
1341 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1342 "Selector data already loaded into the factory method pool");
1343
1344 // Read the method list from the external source.
1345 std::pair<ObjCMethodList, ObjCMethodList> Methods
1346 = ExternalSource->ReadMethodPool(Sel);
Mike Stump11289f42009-09-09 15:08:12 +00001347
Douglas Gregorc78d3462009-04-24 21:10:55 +00001348 if (isInstance) {
1349 if (Methods.second.Method)
1350 FactoryMethodPool[Sel] = Methods.second;
1351 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump11289f42009-09-09 15:08:12 +00001352 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001353
1354 if (Methods.first.Method)
1355 InstanceMethodPool[Sel] = Methods.first;
1356
1357 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1358}
1359
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001360void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001361 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1362 = InstanceMethodPool.find(Method->getSelector());
1363 if (Pos == InstanceMethodPool.end()) {
1364 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1365 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1366 else
1367 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1368 ObjCMethodList())).first;
1369 }
1370
1371 ObjCMethodList &Entry = Pos->second;
Chris Lattner7b26b292009-03-04 05:16:45 +00001372 if (Entry.Method == 0) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001373 // Haven't seen a method with this selector name yet - add it.
Chris Lattner7b26b292009-03-04 05:16:45 +00001374 Entry.Method = Method;
1375 Entry.Next = 0;
1376 return;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001377 }
Mike Stump11289f42009-09-09 15:08:12 +00001378
Chris Lattner7b26b292009-03-04 05:16:45 +00001379 // We've seen a method with this name, see if we have already seen this type
1380 // signature.
1381 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1382 if (MatchTwoMethodDeclarations(Method, List->Method))
1383 return;
Mike Stump11289f42009-09-09 15:08:12 +00001384
Chris Lattner7b26b292009-03-04 05:16:45 +00001385 // We have a new signature for an existing method - add it.
1386 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1387 Entry.Next = new ObjCMethodList(Method, Entry.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001388}
1389
Steve Naroff9ebc0502008-10-21 10:50:19 +00001390// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump11289f42009-09-09 15:08:12 +00001391ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001392 SourceRange R,
1393 bool warn) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001394 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1395 = InstanceMethodPool.find(Sel);
Douglas Gregor9a1899b2009-04-24 22:23:41 +00001396 if (Pos == InstanceMethodPool.end()) {
1397 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001398 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1399 else
1400 return 0;
1401 }
1402
1403 ObjCMethodList &MethList = Pos->second;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001404 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001405
Steve Naroff4a82d812008-09-30 14:38:43 +00001406 if (MethList.Method && MethList.Next) {
Steve Naroffa0ed1652008-10-21 10:37:50 +00001407 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1408 // This checks if the methods differ by size & alignment.
1409 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001410 issueWarning = warn;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001411 }
1412 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattnere4b95692008-11-24 03:33:13 +00001413 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCalle29c5cd2009-12-10 19:51:03 +00001414 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerf490e152008-11-19 05:27:50 +00001415 << MethList.Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001416 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCalle29c5cd2009-12-10 19:51:03 +00001417 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerf490e152008-11-19 05:27:50 +00001418 << Next->Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001419 }
1420 return MethList.Method;
1421}
1422
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001423void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001424 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1425 = FactoryMethodPool.find(Method->getSelector());
1426 if (Pos == FactoryMethodPool.end()) {
1427 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1428 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1429 else
1430 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1431 ObjCMethodList())).first;
1432 }
1433
1434 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001435 if (!FirstMethod.Method) {
1436 // Haven't seen a method with this selector name yet - add it.
1437 FirstMethod.Method = Method;
1438 FirstMethod.Next = 0;
1439 } else {
1440 // We've seen a method with this name, now check the type signature(s).
1441 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump11289f42009-09-09 15:08:12 +00001442
1443 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001444 Next = Next->Next)
1445 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump11289f42009-09-09 15:08:12 +00001446
Chris Lattnerda463fe2007-12-12 07:09:47 +00001447 if (!match) {
1448 // We have a new signature for an existing method - add it.
1449 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001450 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001451 FirstMethod.Next = OMI;
1452 }
1453 }
1454}
1455
Mike Stump11289f42009-09-09 15:08:12 +00001456ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001457 SourceRange R) {
1458 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1459 = FactoryMethodPool.find(Sel);
1460 if (Pos == FactoryMethodPool.end()) {
Mike Stump11289f42009-09-09 15:08:12 +00001461 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001462 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1463 else
1464 return 0;
1465 }
1466
1467 ObjCMethodList &MethList = Pos->second;
1468 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001469
Douglas Gregorc78d3462009-04-24 21:10:55 +00001470 if (MethList.Method && MethList.Next) {
1471 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1472 // This checks if the methods differ by size & alignment.
1473 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1474 issueWarning = true;
1475 }
1476 if (issueWarning && (MethList.Method && MethList.Next)) {
1477 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCalle29c5cd2009-12-10 19:51:03 +00001478 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorc78d3462009-04-24 21:10:55 +00001479 << MethList.Method->getSourceRange();
1480 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCalle29c5cd2009-12-10 19:51:03 +00001481 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorc78d3462009-04-24 21:10:55 +00001482 << Next->Method->getSourceRange();
1483 }
1484 return MethList.Method;
1485}
1486
Mike Stump11289f42009-09-09 15:08:12 +00001487/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff35c62ae2009-01-08 17:28:14 +00001488/// have the property type and issue diagnostics if they don't.
1489/// Also synthesize a getter/setter method if none exist (and update the
1490/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1491/// methods is the "right" thing to do.
Mike Stump11289f42009-09-09 15:08:12 +00001492void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff35c62ae2009-01-08 17:28:14 +00001493 ObjCContainerDecl *CD) {
1494 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump11289f42009-09-09 15:08:12 +00001495
1496 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001497 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump11289f42009-09-09 15:08:12 +00001498 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +00001499 property->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001500
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001501 if (SetterMethod) {
Fariborz Jahanian1a5f2922010-01-06 00:18:12 +00001502 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1503 property->getPropertyAttributes();
1504 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1505 Context.getCanonicalType(SetterMethod->getResultType()) !=
1506 Context.VoidTy)
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001507 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattnera4997152009-02-20 18:43:26 +00001508 if (SetterMethod->param_size() != 1 ||
1509 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump11289f42009-09-09 15:08:12 +00001510 Diag(property->getLocation(),
1511 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001512 << property->getDeclName()
Ted Kremenek9a468042009-03-14 00:20:08 +00001513 << SetterMethod->getSelector();
Fariborz Jahanianff839982008-12-06 21:48:16 +00001514 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1515 }
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001516 }
Steve Naroff35c62ae2009-01-08 17:28:14 +00001517
1518 // Synthesize getter/setter methods if none exist.
Steve Naroffe538c5f2009-01-08 20:15:03 +00001519 // Find the default getter and if one not found, add one.
Mike Stump87c57ac2009-05-16 07:39:55 +00001520 // FIXME: The synthesized property we set here is misleading. We almost always
1521 // synthesize these methods unless the user explicitly provided prototypes
1522 // (which is odd, but allowed). Sema should be typechecking that the
1523 // declarations jive in that situation (which it is not currently).
Steve Naroffe538c5f2009-01-08 20:15:03 +00001524 if (!GetterMethod) {
1525 // No instance method of same name as property getter name was found.
Mike Stump11289f42009-09-09 15:08:12 +00001526 // Declare a getter method and add it to the list of methods
Steve Naroffe538c5f2009-01-08 20:15:03 +00001527 // for this class.
Mike Stump11289f42009-09-09 15:08:12 +00001528 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1529 property->getLocation(), property->getGetterName(),
1530 property->getType(), CD, true, false, true,
1531 (property->getPropertyImplementation() ==
1532 ObjCPropertyDecl::Optional) ?
1533 ObjCMethodDecl::Optional :
Steve Naroffe538c5f2009-01-08 20:15:03 +00001534 ObjCMethodDecl::Required);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001535 CD->addDecl(GetterMethod);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001536 } else
1537 // A user declared getter will be synthesize when @synthesize of
1538 // the property with the same name is seen in the @implementation
Steve Naroff04f2d142009-04-20 15:06:07 +00001539 GetterMethod->setSynthesized(true);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001540 property->setGetterMethodDecl(GetterMethod);
1541
1542 // Skip setter if property is read-only.
1543 if (!property->isReadOnly()) {
1544 // Find the default setter and if one not found, add one.
1545 if (!SetterMethod) {
1546 // No instance method of same name as property setter name was found.
Mike Stump11289f42009-09-09 15:08:12 +00001547 // Declare a setter method and add it to the list of methods
Steve Naroffe538c5f2009-01-08 20:15:03 +00001548 // for this class.
Mike Stump11289f42009-09-09 15:08:12 +00001549 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1550 property->getLocation(),
1551 property->getSetterName(),
Steve Naroffe538c5f2009-01-08 20:15:03 +00001552 Context.VoidTy, CD, true, false, true,
Mike Stump11289f42009-09-09 15:08:12 +00001553 (property->getPropertyImplementation() ==
1554 ObjCPropertyDecl::Optional) ?
1555 ObjCMethodDecl::Optional :
Steve Naroffe538c5f2009-01-08 20:15:03 +00001556 ObjCMethodDecl::Required);
1557 // Invent the arguments for the setter. We don't bother making a
1558 // nice name for the argument.
1559 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump11289f42009-09-09 15:08:12 +00001560 property->getLocation(),
Steve Naroffe538c5f2009-01-08 20:15:03 +00001561 property->getIdentifier(),
1562 property->getType(),
John McCallbcd03502009-12-07 02:54:59 +00001563 /*TInfo=*/0,
Steve Naroffe538c5f2009-01-08 20:15:03 +00001564 VarDecl::None,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001565 0);
Steve Naroff04f2d142009-04-20 15:06:07 +00001566 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001567 CD->addDecl(SetterMethod);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001568 } else
1569 // A user declared setter will be synthesize when @synthesize of
1570 // the property with the same name is seen in the @implementation
Steve Naroff04f2d142009-04-20 15:06:07 +00001571 SetterMethod->setSynthesized(true);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001572 property->setSetterMethodDecl(SetterMethod);
1573 }
Mike Stump11289f42009-09-09 15:08:12 +00001574 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff35c62ae2009-01-08 17:28:14 +00001575 // handle the following, which is supported by GCC (and part of the design).
1576 //
1577 // @interface Foo
1578 // @property double bar;
1579 // @end
1580 //
1581 // void thisIsUnfortunate() {
1582 // id foo;
1583 // double bar = [foo bar];
1584 // }
1585 //
Douglas Gregor020713e2009-01-09 19:42:16 +00001586 if (GetterMethod)
Mike Stump11289f42009-09-09 15:08:12 +00001587 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor020713e2009-01-09 19:42:16 +00001588 if (SetterMethod)
Mike Stump11289f42009-09-09 15:08:12 +00001589 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001590}
1591
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001592/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1593/// identical selector names in current and its super classes and issues
1594/// a warning if any of their argument types are incompatible.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001595void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1596 ObjCMethodDecl *Method,
1597 bool IsInstance) {
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001598 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1599 if (ID == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +00001600
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001601 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump11289f42009-09-09 15:08:12 +00001602 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001603 SD->lookupMethod(Method->getSelector(), IsInstance);
1604 if (SuperMethodDecl == 0) {
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001605 ID = SD;
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001606 continue;
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001607 }
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001608 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1609 E = Method->param_end();
1610 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1611 for (; ParamI != E; ++ParamI, ++PrevI) {
1612 // Number of parameters are the same and is guaranteed by selector match.
1613 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1614 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1615 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1616 // If type of arguement of method in this class does not match its
1617 // respective argument type in the super class method, issue warning;
1618 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump11289f42009-09-09 15:08:12 +00001619 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001620 << T1 << T2;
1621 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1622 return;
1623 }
1624 }
1625 ID = SD;
1626 }
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001627}
1628
Steve Naroff1d2538c2007-12-18 01:30:32 +00001629// Note: For class/category implemenations, allMethods/allProperties is
1630// always null.
Ted Kremenekc7c64312010-01-07 01:20:12 +00001631void Sema::ActOnAtEnd(SourceRange AtEnd,
1632 DeclPtrTy classDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +00001633 DeclPtrTy *allMethods, unsigned allNum,
1634 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001635 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001636 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001637
Steve Naroff1d2538c2007-12-18 01:30:32 +00001638 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1639 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattnerda463fe2007-12-12 07:09:47 +00001640 // should be true.
1641 if (!ClassDecl)
1642 return;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001643
Mike Stump11289f42009-09-09 15:08:12 +00001644 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00001645 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1646 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001647 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00001648
Ted Kremenekc7c64312010-01-07 01:20:12 +00001649 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1650 // FIXME: This is wrong. We shouldn't be pretending that there is
1651 // an '@end' in the declaration.
1652 SourceLocation L = ClassDecl->getLocation();
1653 AtEnd.setBegin(L);
1654 AtEnd.setEnd(L);
1655 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001656 }
1657
Steve Naroff35c62ae2009-01-08 17:28:14 +00001658 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff35c62ae2009-01-08 17:28:14 +00001659
1660 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1661 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1662 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1663
Chris Lattnerda463fe2007-12-12 07:09:47 +00001664 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001665 ObjCMethodDecl *Method =
Chris Lattner83f095c2009-03-28 19:18:32 +00001666 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001667
1668 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00001669 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001670 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001671 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001672 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001673 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001674 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001675 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001676 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001677 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001678 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001679 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001680 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001681 InsMap[Method->getSelector()] = Method;
1682 /// The following allows us to typecheck messages to "id".
1683 AddInstanceMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001684 // verify that the instance method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001685 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001686 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001687 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001688 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001689 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001690 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001691 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001692 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001693 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001694 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001695 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001696 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001697 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001698 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001699 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001700 ClsMap[Method->getSelector()] = Method;
Steve Naroff1d2538c2007-12-18 01:30:32 +00001701 /// The following allows us to typecheck messages to "Class".
1702 AddFactoryMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001703 // verify that the class method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001704 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001705 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001706 }
1707 }
1708 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001709 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump11289f42009-09-09 15:08:12 +00001710 // Compares properties declared in this class to those of its
Fariborz Jahanian7cf18862008-05-01 00:03:38 +00001711 // super class.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +00001712 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001713 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroffb3a87982009-01-09 15:36:25 +00001714 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001715 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00001716 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001717 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001718
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001719 // Compare protocol properties with those in category
1720 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian33afd772009-03-02 19:05:07 +00001721 if (C->getIdentifier() == 0)
1722 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001723 }
Steve Naroffb3a87982009-01-09 15:36:25 +00001724 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
1725 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1726 // user-defined setter/getter. It also synthesizes setter/getter methods
1727 // and adds them to the DeclContext and global method pools.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001728 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1729 E = CDecl->prop_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001730 I != E; ++I)
Chris Lattner41fd42e2009-02-16 18:32:47 +00001731 ProcessPropertyDecl(*I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00001732 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00001733 }
1734 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001735 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001736 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001737 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001738 AtomicPropertySetterGetterRules(IC, IDecl);
1739 }
Mike Stump11289f42009-09-09 15:08:12 +00001740 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00001741 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001742 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00001743
Chris Lattnerda463fe2007-12-12 07:09:47 +00001744 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00001745 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00001746 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001747 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001748 Categories; Categories = Categories->getNextClassCategory()) {
1749 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner9ef10f42009-03-01 00:56:52 +00001750 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001751 break;
1752 }
1753 }
1754 }
1755 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001756 if (isInterfaceDeclKind) {
1757 // Reject invalid vardecls.
1758 for (unsigned i = 0; i != tuvNum; i++) {
1759 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1760 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1761 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00001762 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00001763 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00001764 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001765 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00001766 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001767}
1768
1769
1770/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1771/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00001772static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001773CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1774 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1775 if (PQTVal & ObjCDeclSpec::DQ_In)
1776 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1777 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1778 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1779 if (PQTVal & ObjCDeclSpec::DQ_Out)
1780 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1781 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1782 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1783 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1784 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1785 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1786 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001787
1788 return ret;
1789}
1790
Chris Lattner83f095c2009-03-28 19:18:32 +00001791Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattnerda463fe2007-12-12 07:09:47 +00001792 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001793 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001794 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001795 Selector Sel,
1796 // optional arguments. The number of types/arguments is obtained
1797 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001798 ObjCArgInfo *ArgInfo,
Fariborz Jahaniane84858c2009-01-09 00:38:19 +00001799 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001800 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1801 bool isVariadic) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001802 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff83777fe2008-02-29 21:48:07 +00001803
1804 // Make sure we can establish a context for the method.
1805 if (!ClassDecl) {
1806 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanianb1771e42009-08-28 17:52:37 +00001807 FunctionLabelMap.clear();
Chris Lattner83f095c2009-03-28 19:18:32 +00001808 return DeclPtrTy();
Steve Naroff83777fe2008-02-29 21:48:07 +00001809 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001810 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00001811
Steve Naroff32606412009-02-20 22:59:16 +00001812 if (ReturnType) {
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +00001813 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump11289f42009-09-09 15:08:12 +00001814
Steve Naroff32606412009-02-20 22:59:16 +00001815 // Methods cannot return interface types. All ObjC objects are
1816 // passed by reference.
1817 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattnerde5a5312009-04-11 19:08:56 +00001818 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1819 << 0 << resultDeclType;
Chris Lattner83f095c2009-03-28 19:18:32 +00001820 return DeclPtrTy();
Steve Naroff32606412009-02-20 22:59:16 +00001821 }
1822 } else // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001823 resultDeclType = Context.getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +00001824
1825 ObjCMethodDecl* ObjCMethod =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001826 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump11289f42009-09-09 15:08:12 +00001827 cast<DeclContext>(ClassDecl),
Chris Lattner8d8829e2008-03-16 00:49:28 +00001828 MethodType == tok::minus, isVariadic,
Fariborz Jahanian8983f172008-05-07 20:53:44 +00001829 false,
Mike Stump11289f42009-09-09 15:08:12 +00001830 MethodDeclKind == tok::objc_optional ?
1831 ObjCMethodDecl::Optional :
Chris Lattner8d8829e2008-03-16 00:49:28 +00001832 ObjCMethodDecl::Required);
Mike Stump11289f42009-09-09 15:08:12 +00001833
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001834 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00001835
Chris Lattner23b0faf2009-04-11 19:42:43 +00001836 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00001837 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00001838 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00001839
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001840 if (ArgInfo[i].Type == 0) {
John McCall856bbea2009-10-23 21:48:59 +00001841 ArgType = Context.getObjCIdType();
1842 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001843 } else {
John McCall856bbea2009-10-23 21:48:59 +00001844 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroffb0498ee2008-12-09 19:36:17 +00001845 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner9713a1c2009-04-11 19:34:56 +00001846 ArgType = adjustParameterType(ArgType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001847 }
Mike Stump11289f42009-09-09 15:08:12 +00001848
John McCall856bbea2009-10-23 21:48:59 +00001849 ParmVarDecl* Param
1850 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1851 ArgInfo[i].Name, ArgType, DI,
1852 VarDecl::None, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001853
Chris Lattner9713a1c2009-04-11 19:34:56 +00001854 if (ArgType->isObjCInterfaceType()) {
1855 Diag(ArgInfo[i].NameLoc,
1856 diag::err_object_cannot_be_passed_returned_by_value)
1857 << 1 << ArgType;
1858 Param->setInvalidDecl();
1859 }
Mike Stump11289f42009-09-09 15:08:12 +00001860
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001861 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001862 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00001863
Chris Lattner9713a1c2009-04-11 19:34:56 +00001864 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00001865 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001866
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001867 Params.push_back(Param);
1868 }
1869
Jay Foad7d0479f2009-05-21 09:52:38 +00001870 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001871 ObjCMethod->setObjCDeclQualifier(
1872 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1873 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001874
1875 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00001876 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00001877
John McCall28a6aea2009-11-04 02:18:39 +00001878 const ObjCMethodDecl *InterfaceMD = 0;
1879
Mike Stump11289f42009-09-09 15:08:12 +00001880 // For implementations (which can be very "coarse grain"), we add the
1881 // method now. This allows the AST to implement lookup methods that work
1882 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattnerda463fe2007-12-12 07:09:47 +00001883 // us to flag multiple declaration errors as they occur.
Mike Stump11289f42009-09-09 15:08:12 +00001884 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001885 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001886 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001887 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1888 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001889 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001890 PrevMethod = ImpDecl->getClassMethod(Sel);
1891 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001892 }
John McCall28a6aea2009-11-04 02:18:39 +00001893 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1894 MethodType == tok::minus);
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001895 if (AttrList)
1896 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump11289f42009-09-09 15:08:12 +00001897 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stump12b8ce12009-08-04 21:02:39 +00001898 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001899 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001900 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1901 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001902 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001903 PrevMethod = CatImpDecl->getClassMethod(Sel);
1904 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001905 }
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00001906 if (AttrList)
1907 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001908 }
1909 if (PrevMethod) {
1910 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00001911 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001912 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001913 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump11289f42009-09-09 15:08:12 +00001914 }
John McCall28a6aea2009-11-04 02:18:39 +00001915
1916 // If the interface declared this method, and it was deprecated there,
1917 // mark it deprecated here.
1918 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1919 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1920
Chris Lattner83f095c2009-03-28 19:18:32 +00001921 return DeclPtrTy::make(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001922}
1923
Mike Stump11289f42009-09-09 15:08:12 +00001924void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001925 SourceLocation Loc,
1926 unsigned &Attributes) {
1927 // FIXME: Improve the reported location.
1928
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00001929 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001930 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00001931 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1932 ObjCDeclSpec::DQ_PR_assign |
1933 ObjCDeclSpec::DQ_PR_copy |
1934 ObjCDeclSpec::DQ_PR_retain))) {
1935 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1936 "readwrite" :
1937 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1938 "assign" :
1939 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1940 "copy" : "retain";
Mike Stump11289f42009-09-09 15:08:12 +00001941
1942 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner409f5552009-01-29 18:49:48 +00001943 diag::err_objc_property_attr_mutually_exclusive :
1944 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00001945 << "readonly" << which;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001946 }
1947
1948 // Check for copy or retain on non-object types.
1949 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump11289f42009-09-09 15:08:12 +00001950 !PropertyTy->isObjCObjectPointerType() &&
1951 !PropertyTy->isBlockPointerType() &&
Steve Naroff79d12152009-07-16 15:41:00 +00001952 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001953 Diag(Loc, diag::err_objc_property_requires_object)
1954 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001955 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1956 }
1957
1958 // Check for more than one of { assign, copy, retain }.
1959 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1960 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001961 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1962 << "assign" << "copy";
1963 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump11289f42009-09-09 15:08:12 +00001964 }
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001965 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001966 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1967 << "assign" << "retain";
1968 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001969 }
1970 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1971 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00001972 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1973 << "copy" << "retain";
1974 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001975 }
1976 }
1977
1978 // Warn if user supplied no assignment attribute, property is
1979 // readwrite, and this is an object type.
1980 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1981 ObjCDeclSpec::DQ_PR_retain)) &&
1982 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Naroff79d12152009-07-16 15:41:00 +00001983 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001984 // Skip this warning in gc-only mode.
Mike Stump11289f42009-09-09 15:08:12 +00001985 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001986 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1987
1988 // If non-gc code warn that this is likely inappropriate.
1989 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1990 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump11289f42009-09-09 15:08:12 +00001991
Daniel Dunbare8a06e62008-09-23 21:53:23 +00001992 // FIXME: Implement warning dependent on NSCopying being
1993 // implemented. See also:
1994 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1995 // (please trim this list while you are at it).
1996 }
Mike Stump5580bdc2009-05-07 23:06:50 +00001997
1998 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1999 && getLangOptions().getGCMode() == LangOptions::GCOnly
2000 && PropertyTy->isBlockPointerType())
2001 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002002}
2003
Mike Stump11289f42009-09-09 15:08:12 +00002004Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00002005 FieldDeclarator &FD,
2006 ObjCDeclSpec &ODS,
2007 Selector GetterSel,
2008 Selector SetterSel,
2009 DeclPtrTy ClassCategory,
2010 bool *isOverridingProperty,
2011 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002012 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002013 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
2014 // default is readwrite!
2015 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump11289f42009-09-09 15:08:12 +00002016 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002017 // not retain or copy
2018 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump11289f42009-09-09 15:08:12 +00002019 (isReadWrite &&
2020 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002021 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
2022 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian00857fc2009-12-16 18:03:30 +00002023 if (T->isReferenceType()) {
2024 Diag(AtLoc, diag::error_reference_property);
2025 return DeclPtrTy();
2026 }
Chris Lattner83f095c2009-03-28 19:18:32 +00002027 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002028 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002029 // May modify Attributes.
2030 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002031 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2032 if (!CDecl->getIdentifier()) {
Mike Stump11289f42009-09-09 15:08:12 +00002033 // This is a continuation class. property requires special
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002034 // handling.
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002035 if ((CCPrimary = CDecl->getClassInterface())) {
2036 // Find the property in continuation class's primary class only.
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002037 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniande8db162009-11-02 22:45:15 +00002038 if (ObjCPropertyDecl *PIDecl =
2039 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002040 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahaniane4fd6402009-04-01 23:23:53 +00002041 // with continuation class's readwrite property attribute!
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002042 unsigned PIkind = PIDecl->getPropertyAttributes();
2043 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandc216102009-11-10 19:31:09 +00002044 unsigned retainCopyNonatomic =
Fariborz Jahanian3600f412009-11-06 22:59:12 +00002045 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniana386d952009-11-03 00:01:38 +00002046 ObjCPropertyDecl::OBJC_PR_copy |
2047 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahaniandc216102009-11-10 19:31:09 +00002048 if ((Attributes & retainCopyNonatomic) !=
2049 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002050 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniana386d952009-11-03 00:01:38 +00002051 Diag(PIDecl->getLocation(), diag::note_property_declare);
2052 }
Fariborz Jahanian8e356bf2010-01-06 21:38:30 +00002053 DeclContext *DC = dyn_cast<DeclContext>(CCPrimary);
2054 assert(DC && "ClassDecl is not a DeclContext");
2055 DeclContext::lookup_result Found =
2056 DC->lookup(PIDecl->getDeclName());
2057 bool PropertyInPrimaryClass = false;
2058 for (; Found.first != Found.second; ++Found.first)
2059 if (isa<ObjCPropertyDecl>(*Found.first)) {
2060 PropertyInPrimaryClass = true;
2061 break;
2062 }
2063 if (!PropertyInPrimaryClass) {
2064 // Protocol is not in the primary class. Must build one for it.
2065 ObjCDeclSpec ProtocolPropertyODS;
2066 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind and
2067 // ObjCPropertyDecl::PropertyAttributeKind have identical values.
2068 // Should consolidate both into one enum type.
2069 ProtocolPropertyODS.setPropertyAttributes(
2070 (ObjCDeclSpec::ObjCPropertyAttributeKind)PIkind);
2071 DeclPtrTy ProtocolPtrTy =
2072 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
2073 PIDecl->getGetterName(),
2074 PIDecl->getSetterName(),
2075 DeclPtrTy::make(CCPrimary), isOverridingProperty,
2076 MethodImplKind);
2077 PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>();
2078 }
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002079 PIDecl->makeitReadWriteAttribute();
2080 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2081 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2082 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2083 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2084 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniande8db162009-11-02 22:45:15 +00002085 } else {
Mike Stump11289f42009-09-09 15:08:12 +00002086 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002087 << CCPrimary->getDeclName();
Fariborz Jahaniande8db162009-11-02 22:45:15 +00002088 Diag(PIDecl->getLocation(), diag::note_property_declare);
2089 }
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002090 *isOverridingProperty = true;
Mike Stump11289f42009-09-09 15:08:12 +00002091 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian47b21082009-04-15 19:19:03 +00002092 // class's list.
2093 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002094 return DeclPtrTy();
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002095 }
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002096 // No matching property found in the primary class. Just fall thru
2097 // and add property to continuation class's primary class.
2098 ClassDecl = CCPrimary;
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002099 } else {
Chris Lattner68d42c72009-02-20 21:38:52 +00002100 Diag(CDecl->getLocation(), diag::err_continuation_class);
2101 *isOverridingProperty = true;
Chris Lattner83f095c2009-03-28 19:18:32 +00002102 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002103 }
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002104 }
Mike Stump11289f42009-09-09 15:08:12 +00002105
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002106 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump11289f42009-09-09 15:08:12 +00002107 // gc'able conforms to NSCopying protocol
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002108 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2109 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2110 if (T->isObjCObjectPointerType()) {
2111 QualType InterfaceTy = T->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +00002112 if (const ObjCInterfaceType *OIT =
John McCall9dd450b2009-09-21 23:43:11 +00002113 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian83b000c2009-08-14 18:06:25 +00002114 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002115 if (IDecl)
Mike Stump11289f42009-09-09 15:08:12 +00002116 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002117 LookupProtocol(&Context.Idents.get("NSCopying")))
2118 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump11289f42009-09-09 15:08:12 +00002119 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002120 << FD.D.getIdentifier();
Fariborz Jahanian83b000c2009-08-14 18:06:25 +00002121 }
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002122 }
Fariborz Jahanian887cd6a2009-08-12 18:17:53 +00002123 if (T->isObjCInterfaceType())
2124 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump11289f42009-09-09 15:08:12 +00002125
Steve Naroffba3dc382009-01-11 12:47:58 +00002126 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2127 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattner5300acc2009-04-11 20:14:49 +00002128 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump11289f42009-09-09 15:08:12 +00002129 FD.D.getIdentifierLoc(),
Fariborz Jahanian0152a1a2008-04-14 23:36:35 +00002130 FD.D.getIdentifier(), T);
Fariborz Jahanian057a17e2009-12-17 00:49:09 +00002131 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
2132 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2133 Diag(PDecl->getLocation(), diag::err_duplicate_property);
2134 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2135 PDecl->setInvalidDecl();
2136 }
2137 else
2138 DC->addDecl(PDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002139
Chris Lattner5300acc2009-04-11 20:14:49 +00002140 if (T->isArrayType() || T->isFunctionType()) {
2141 Diag(AtLoc, diag::err_property_type) << T;
2142 PDecl->setInvalidDecl();
2143 }
Mike Stump11289f42009-09-09 15:08:12 +00002144
Douglas Gregor758a8692009-06-17 21:51:59 +00002145 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002146
Fariborz Jahanianec6e4c82008-05-07 17:43:59 +00002147 // Regardless of setter/getter attribute, we save the default getter/setter
2148 // selector names in anticipation of declaration of setter/getter methods.
2149 PDecl->setGetterName(GetterSel);
2150 PDecl->setSetterName(SetterSel);
Mike Stump11289f42009-09-09 15:08:12 +00002151
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002152 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002153 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump11289f42009-09-09 15:08:12 +00002154
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002155 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002156 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump11289f42009-09-09 15:08:12 +00002157
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002158 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002159 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump11289f42009-09-09 15:08:12 +00002160
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002161 if (isReadWrite)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002162 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump11289f42009-09-09 15:08:12 +00002163
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002164 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002165 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump11289f42009-09-09 15:08:12 +00002166
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002167 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002168 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump11289f42009-09-09 15:08:12 +00002169
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002170 if (isAssign)
2171 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump11289f42009-09-09 15:08:12 +00002172
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002173 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002174 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump11289f42009-09-09 15:08:12 +00002175
Fariborz Jahanian8d916862008-05-05 18:51:55 +00002176 if (MethodImplKind == tok::objc_required)
2177 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2178 else if (MethodImplKind == tok::objc_optional)
2179 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002180 // A case of continuation class adding a new property in the class. This
2181 // is not what it was meant for. However, gcc supports it and so should we.
2182 // Make sure setter/getters are declared here.
2183 if (CCPrimary)
2184 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump11289f42009-09-09 15:08:12 +00002185
Chris Lattner83f095c2009-03-28 19:18:32 +00002186 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002187}
2188
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002189/// ActOnPropertyImplDecl - This routine performs semantic checks and
2190/// builds the AST node for a property implementation declaration; declared
2191/// as @synthesize or @dynamic.
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002192///
Mike Stump11289f42009-09-09 15:08:12 +00002193Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00002194 SourceLocation PropertyLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002195 bool Synthesize,
Chris Lattner83f095c2009-03-28 19:18:32 +00002196 DeclPtrTy ClassCatImpDecl,
2197 IdentifierInfo *PropertyId,
2198 IdentifierInfo *PropertyIvar) {
2199 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002200 // Make sure we have a context for the property implementation declaration.
2201 if (!ClassImpDecl) {
2202 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner83f095c2009-03-28 19:18:32 +00002203 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002204 }
2205 ObjCPropertyDecl *property = 0;
2206 ObjCInterfaceDecl* IDecl = 0;
2207 // Find the class or category class where this property must have
2208 // a declaration.
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002209 ObjCImplementationDecl *IC = 0;
2210 ObjCCategoryImplDecl* CatImplClass = 0;
2211 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002212 IDecl = IC->getClassInterface();
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002213 // We always synthesize an interface for an implementation
2214 // without an interface decl. So, IDecl is always non-zero.
Mike Stump11289f42009-09-09 15:08:12 +00002215 assert(IDecl &&
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002216 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump11289f42009-09-09 15:08:12 +00002217
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002218 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002219 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002220 if (!property) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002221 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner83f095c2009-03-28 19:18:32 +00002222 return DeclPtrTy();
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002223 }
Fariborz Jahanianec344ed2009-11-02 18:45:36 +00002224 if (const ObjCCategoryDecl *CD =
2225 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
2226 if (CD->getIdentifier()) {
2227 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniande8db162009-11-02 22:45:15 +00002228 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianec344ed2009-11-02 18:45:36 +00002229 return DeclPtrTy();
2230 }
2231 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002232 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002233 if (Synthesize) {
2234 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner83f095c2009-03-28 19:18:32 +00002235 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002236 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002237 IDecl = CatImplClass->getClassInterface();
2238 if (!IDecl) {
2239 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner83f095c2009-03-28 19:18:32 +00002240 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002241 }
Mike Stump11289f42009-09-09 15:08:12 +00002242 ObjCCategoryDecl *Category =
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002243 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump11289f42009-09-09 15:08:12 +00002244
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002245 // If category for this implementation not found, it is an error which
2246 // has already been reported eralier.
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002247 if (!Category)
Chris Lattner83f095c2009-03-28 19:18:32 +00002248 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002249 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002250 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002251 if (!property) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002252 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002253 << Category->getDeclName();
Chris Lattner83f095c2009-03-28 19:18:32 +00002254 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002255 }
Chris Lattner83f095c2009-03-28 19:18:32 +00002256 } else {
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002257 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner83f095c2009-03-28 19:18:32 +00002258 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002259 }
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002260 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002261 // Check that we have a valid, previously declared ivar for @synthesize
2262 if (Synthesize) {
2263 // @synthesize
Fariborz Jahanianc6bec7b2008-04-21 21:57:36 +00002264 if (!PropertyIvar)
2265 PropertyIvar = PropertyId;
Fariborz Jahanianb35b4a92009-03-31 00:06:29 +00002266 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002267 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanianb6d5b542009-04-13 19:30:37 +00002268 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002269 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002270 if (!Ivar) {
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002271 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002272 assert(EnclosingContext &&
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002273 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump11289f42009-09-09 15:08:12 +00002274 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2275 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002276 ObjCIvarDecl::Public,
2277 (Expr *)0);
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002278 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002279 IDecl->addDecl(Ivar);
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002280 property->setPropertyIvarDecl(Ivar);
2281 if (!getLangOptions().ObjCNonFragileABI)
Steve Naroffc03f6b92009-03-03 22:09:41 +00002282 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump11289f42009-09-09 15:08:12 +00002283 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002284 // a property implementation and to avoid future warnings.
Mike Stump12b8ce12009-08-04 21:02:39 +00002285 } else if (getLangOptions().ObjCNonFragileABI &&
2286 ClassDeclared != IDecl) {
Fariborz Jahanian68592fc2009-04-30 21:39:24 +00002287 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump11289f42009-09-09 15:08:12 +00002288 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanianb6d5b542009-04-13 19:30:37 +00002289 << ClassDeclared->getDeclName();
2290 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2291 << Ivar << Ivar->getNameAsCString();
2292 // Note! I deliberately want it to fall thru so more errors are caught.
2293 }
Steve Naroff9f9774c2008-09-30 10:07:56 +00002294 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002295
Steve Naroff506e7172008-09-30 00:24:17 +00002296 // Check that type of property and its ivar are type compatible.
Steve Naroff9f9774c2008-09-30 10:07:56 +00002297 if (PropType != IvarType) {
Steve Naroff9546eee2008-10-16 14:59:30 +00002298 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002299 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002300 << property->getDeclName() << Ivar->getDeclName();
Mike Stump11289f42009-09-09 15:08:12 +00002301 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002302 // a property implementation and to avoid future warnings.
Steve Naroff9f9774c2008-09-30 10:07:56 +00002303 }
Mike Stump11289f42009-09-09 15:08:12 +00002304
Chris Lattner83f095c2009-03-28 19:18:32 +00002305 // FIXME! Rules for properties are somewhat different that those
2306 // for assignments. Use a new routine to consolidate all cases;
2307 // specifically for property redeclarations as well as for ivars.
2308 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2309 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump11289f42009-09-09 15:08:12 +00002310 if (lhsType != rhsType &&
Chris Lattner83f095c2009-03-28 19:18:32 +00002311 lhsType->isArithmeticType()) {
2312 Diag(PropertyLoc, diag::error_property_ivar_type)
2313 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002314 // Fall thru - see previous comment
Chris Lattner83f095c2009-03-28 19:18:32 +00002315 }
2316 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanian9ecb84b2009-04-07 21:25:06 +00002317 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2318 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002319 Diag(PropertyLoc, diag::error_weak_property)
2320 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002321 // Fall thru - see previous comment
Chris Lattner83f095c2009-03-28 19:18:32 +00002322 }
Mike Stump11289f42009-09-09 15:08:12 +00002323 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian60331be2009-04-10 22:42:54 +00002324 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2325 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002326 Diag(PropertyLoc, diag::error_strong_property)
2327 << property->getDeclName() << Ivar->getDeclName();
Mike Stump11289f42009-09-09 15:08:12 +00002328 // Fall thru - see previous comment
Fariborz Jahanian54fa4182009-01-19 20:13:47 +00002329 }
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002330 }
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002331 } else if (PropertyIvar)
2332 // @dynamic
2333 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002334 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump11289f42009-09-09 15:08:12 +00002335 ObjCPropertyImplDecl *PIDecl =
2336 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2337 property,
2338 (Synthesize ?
2339 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002340 : ObjCPropertyImplDecl::Dynamic),
2341 Ivar);
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002342 if (IC) {
2343 if (Synthesize)
Mike Stump11289f42009-09-09 15:08:12 +00002344 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002345 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump11289f42009-09-09 15:08:12 +00002346 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2347 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002348 << PropertyIvar;
2349 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2350 }
Mike Stump11289f42009-09-09 15:08:12 +00002351
2352 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002353 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002354 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2355 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +00002356 return DeclPtrTy();
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002357 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002358 IC->addPropertyImplementation(PIDecl);
Mike Stump12b8ce12009-08-04 21:02:39 +00002359 } else {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002360 if (Synthesize)
Mike Stump11289f42009-09-09 15:08:12 +00002361 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002362 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump11289f42009-09-09 15:08:12 +00002363 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2364 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002365 << PropertyIvar;
2366 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2367 }
Mike Stump11289f42009-09-09 15:08:12 +00002368
2369 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002370 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002371 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2372 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +00002373 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002374 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002375 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002376 }
Mike Stump11289f42009-09-09 15:08:12 +00002377
Chris Lattner83f095c2009-03-28 19:18:32 +00002378 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002379}
Anders Carlssona6b508a2008-11-04 16:57:32 +00002380
Chris Lattner438e5012008-12-17 07:13:27 +00002381bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00002382 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlssona6b508a2008-11-04 16:57:32 +00002383 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002384
Anders Carlssona6b508a2008-11-04 16:57:32 +00002385 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2386 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00002387
Anders Carlssona6b508a2008-11-04 16:57:32 +00002388 return true;
2389}
Chris Lattner438e5012008-12-17 07:13:27 +00002390
Chris Lattner438e5012008-12-17 07:13:27 +00002391/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2392/// instance variables of ClassName into Decls.
Mike Stump11289f42009-09-09 15:08:12 +00002393void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00002394 IdentifierInfo *ClassName,
Chris Lattner83f095c2009-03-28 19:18:32 +00002395 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00002396 // Check that ClassName is a valid class
2397 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2398 if (!Class) {
2399 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2400 return;
2401 }
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00002402 if (LangOpts.ObjCNonFragileABI) {
2403 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2404 return;
2405 }
Mike Stump11289f42009-09-09 15:08:12 +00002406
Chris Lattner438e5012008-12-17 07:13:27 +00002407 // Collect the instance variables
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00002408 llvm::SmallVector<FieldDecl*, 32> RecFields;
2409 Context.CollectObjCIvars(Class, RecFields);
2410 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2411 for (unsigned i = 0; i < RecFields.size(); i++) {
2412 FieldDecl* ID = RecFields[i];
2413 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2414 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2415 ID->getIdentifier(), ID->getType(),
2416 ID->getBitWidth());
2417 Decls.push_back(Sema::DeclPtrTy::make(FD));
2418 }
Mike Stump11289f42009-09-09 15:08:12 +00002419
Chris Lattner438e5012008-12-17 07:13:27 +00002420 // Introduce all of these fields into the appropriate scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002421 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00002422 D != Decls.end(); ++D) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002423 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner438e5012008-12-17 07:13:27 +00002424 if (getLangOptions().CPlusPlus)
2425 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner83f095c2009-03-28 19:18:32 +00002426 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002427 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00002428 }
2429}
2430