blob: 465c4a9ee187ccd5d964133a58aa92d0b335656b [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");
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000449 if (!CatDecl->IsClassExtension())
450 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
451 E = PDecl->prop_end(); P != E; ++P) {
452 ObjCPropertyDecl *Pr = (*P);
453 ObjCCategoryDecl::prop_iterator CP, CE;
454 // Is this property already in category's list of properties?
455 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP)
456 if ((*CP)->getIdentifier() == Pr->getIdentifier())
457 break;
458 if (CP != CE)
459 // Property protocol already exist in class. Diagnose any mismatch.
460 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
461 }
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000462 return;
463 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000464 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
465 E = PDecl->prop_end(); P != E; ++P) {
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000466 ObjCPropertyDecl *Pr = (*P);
Steve Naroffb3a87982009-01-09 15:36:25 +0000467 ObjCInterfaceDecl::prop_iterator CP, CE;
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000468 // Is this property already in class's list of properties?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000469 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000470 if ((*CP)->getIdentifier() == Pr->getIdentifier())
471 break;
Fariborz Jahanian519976c2009-01-09 21:04:52 +0000472 if (CP != CE)
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000473 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner86d7d912008-11-24 03:54:41 +0000474 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000475 }
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000476}
477
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000478/// CompareProperties - This routine compares properties
479/// declared in 'ClassOrProtocol' objects (which can be a class or an
480/// inherited protocol with the list of properties for class/category 'CDecl'
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000481///
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000482void Sema::CompareProperties(Decl *CDecl,
483 DeclPtrTy ClassOrProtocol) {
484 Decl *ClassDecl = ClassOrProtocol.getAs<Decl>();
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000485 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
486
487 if (!IDecl) {
488 // Category
489 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000490 assert (CatDecl && "CompareProperties");
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000491 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
492 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
493 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000494 // Match properties of category with those of protocol (*P)
495 MatchOneProtocolPropertiesInClass(CatDecl, *P);
Mike Stump11289f42009-09-09 15:08:12 +0000496
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000497 // Go thru the list of protocols for this category and recursively match
498 // their properties with those in the category.
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000499 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
500 E = CatDecl->protocol_end(); P != E; ++P)
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000501 CompareProperties(CatDecl, DeclPtrTy::make(*P));
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000502 } else {
503 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
504 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
505 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000506 MatchOneProtocolPropertiesInClass(CatDecl, *P);
Fariborz Jahaniand2c2ad52008-12-06 23:03:39 +0000507 }
508 return;
509 }
510
Chris Lattnerca1e8482008-07-21 07:06:49 +0000511 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000512 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
513 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000514 // Match properties of class IDecl with those of protocol (*P).
515 MatchOneProtocolPropertiesInClass(IDecl, *P);
Mike Stump11289f42009-09-09 15:08:12 +0000516
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000517 // Go thru the list of protocols for this class and recursively match
518 // their properties with those declared in the class.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000519 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
520 E = IDecl->protocol_end(); P != E; ++P)
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000521 CompareProperties(IDecl, DeclPtrTy::make(*P));
Chris Lattnerca1e8482008-07-21 07:06:49 +0000522 } else {
Argyrios Kyrtzidisb3fa8632008-07-21 09:18:38 +0000523 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
524 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
525 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahaniancdb85752010-01-18 18:41:16 +0000526 MatchOneProtocolPropertiesInClass(IDecl, *P);
Chris Lattnerca1e8482008-07-21 07:06:49 +0000527 }
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +0000528}
529
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000530/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000531/// a class method in its extension.
532///
Mike Stump11289f42009-09-09 15:08:12 +0000533void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000534 ObjCInterfaceDecl *ID) {
535 if (!ID)
536 return; // Possibly due to previous error
537
538 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000539 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
540 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000541 ObjCMethodDecl *MD = *i;
542 MethodMap[MD->getSelector()] = MD;
543 }
544
545 if (MethodMap.empty())
546 return;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000547 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
548 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000549 ObjCMethodDecl *Method = *i;
550 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
551 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
552 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
553 << Method->getDeclName();
554 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
555 }
556 }
557}
558
Chris Lattnerdac168d2009-04-12 08:43:13 +0000559/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattner83f095c2009-03-28 19:18:32 +0000560Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000561Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000562 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000563 unsigned NumElts,
564 AttributeList *attrList) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000565 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor002b6712010-01-16 15:02:53 +0000566 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump11289f42009-09-09 15:08:12 +0000567
Chris Lattnerda463fe2007-12-12 07:09:47 +0000568 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000569 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000570 ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000571 if (PDecl == 0) { // Not already seen?
Mike Stump11289f42009-09-09 15:08:12 +0000572 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000573 IdentList[i].second, Ident);
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000574 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000575 }
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000576 if (attrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000577 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000578 Protocols.push_back(PDecl);
Douglas Gregor002b6712010-01-16 15:02:53 +0000579 ProtoLocs.push_back(IdentList[i].second);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000580 }
Mike Stump11289f42009-09-09 15:08:12 +0000581
582 ObjCForwardProtocolDecl *PDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000583 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor002b6712010-01-16 15:02:53 +0000584 Protocols.data(), Protocols.size(),
585 ProtoLocs.data());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000586 CurContext->addDecl(PDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +0000587 CheckObjCDeclScope(PDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000588 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000589}
590
Chris Lattner83f095c2009-03-28 19:18:32 +0000591Sema::DeclPtrTy Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000592ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
593 IdentifierInfo *ClassName, SourceLocation ClassLoc,
594 IdentifierInfo *CategoryName,
595 SourceLocation CategoryLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000596 const DeclPtrTy *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000597 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000598 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000599 SourceLocation EndProtoLoc) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000600 ObjCCategoryDecl *CDecl = 0;
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000601 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000602 if (!CategoryName) {
603 // Class extensions require a special treatment. Use an existing one.
604 for (CDecl = IDecl->getCategoryList(); CDecl;
605 CDecl = CDecl->getNextClassCategory())
606 if (CDecl->IsClassExtension())
607 break;
Fariborz Jahanian9a5124a2008-01-17 20:33:24 +0000608 }
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000609 if (!CDecl) {
610 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, ClassLoc,
611 CategoryLoc, CategoryName);
612 // FIXME: PushOnScopeChains?
613 CurContext->addDecl(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000614
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000615 /// Check that class of this category is already completely declared.
616 if (!IDecl || IDecl->isForwardDecl()) {
617 CDecl->setInvalidDecl();
618 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
619 return DeclPtrTy::make(CDecl);
620 }
621
622 CDecl->setClassInterface(IDecl);
623 // Insert first use of class extension to the list of class's categories.
624 if (!CategoryName)
625 CDecl->insertNextClassCategory();
626 }
Mike Stump11289f42009-09-09 15:08:12 +0000627
Chris Lattnerced903b2009-02-16 21:30:01 +0000628 // If the interface is deprecated, warn about it.
Douglas Gregor171c45a2009-02-18 21:56:37 +0000629 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner9018ca82009-02-16 21:26:43 +0000630
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000631 if (CategoryName) {
632 /// Check for duplicate interface declaration for this category
633 ObjCCategoryDecl *CDeclChain;
634 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
635 CDeclChain = CDeclChain->getNextClassCategory()) {
636 if (CDeclChain->getIdentifier() == CategoryName) {
637 // Class extensions can be declared multiple times.
638 Diag(CategoryLoc, diag::warn_dup_category_def)
639 << ClassName << CategoryName;
640 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
641 break;
642 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000643 }
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000644 if (!CDeclChain)
645 CDecl->insertNextClassCategory();
Chris Lattner9018ca82009-02-16 21:26:43 +0000646 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000647
Chris Lattnerda463fe2007-12-12 07:09:47 +0000648 if (NumProtoRefs) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000649 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000650 ProtoLocs, Context);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000651 // Protocols in the class extension belong to the class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000652 if (CDecl->IsClassExtension())
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000653 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000654 NumProtoRefs, ProtoLocs,
655 Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000656 }
Mike Stump11289f42009-09-09 15:08:12 +0000657
Anders Carlssona6b508a2008-11-04 16:57:32 +0000658 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000659 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000660}
661
662/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000663/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000664/// object.
Chris Lattner83f095c2009-03-28 19:18:32 +0000665Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000666 SourceLocation AtCatImplLoc,
667 IdentifierInfo *ClassName, SourceLocation ClassLoc,
668 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000669 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000670 ObjCCategoryDecl *CatIDecl = 0;
671 if (IDecl) {
672 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
673 if (!CatIDecl) {
674 // Category @implementation with no corresponding @interface.
675 // Create and install one.
676 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor071676f2010-01-16 16:38:58 +0000677 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000678 CatName);
679 CatIDecl->setClassInterface(IDecl);
680 CatIDecl->insertNextClassCategory();
681 }
682 }
683
Mike Stump11289f42009-09-09 15:08:12 +0000684 ObjCCategoryImplDecl *CDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000685 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
686 IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000687 /// Check that class of this category is already completely declared.
688 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000689 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000690
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000691 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000692 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000693
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000694 /// Check that CatName, category name, is not used in another implementation.
695 if (CatIDecl) {
696 if (CatIDecl->getImplementation()) {
697 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
698 << CatName;
699 Diag(CatIDecl->getImplementation()->getLocation(),
700 diag::note_previous_definition);
701 } else
702 CatIDecl->setImplementation(CDecl);
703 }
Mike Stump11289f42009-09-09 15:08:12 +0000704
Anders Carlssona6b508a2008-11-04 16:57:32 +0000705 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +0000706 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000707}
708
Chris Lattner83f095c2009-03-28 19:18:32 +0000709Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000710 SourceLocation AtClassImplLoc,
711 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000712 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000713 SourceLocation SuperClassLoc) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000714 ObjCInterfaceDecl* IDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000715 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +0000716 NamedDecl *PrevDecl
717 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000718 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000719 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000720 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor40f7a002010-01-04 17:27:12 +0000721 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
722 // If this is a forward declaration of an interface, warn.
723 if (IDecl->isForwardDecl()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000724 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Fariborz Jahanian6f0f25b2009-04-23 21:49:04 +0000725 IDecl = 0;
726 }
Douglas Gregor40f7a002010-01-04 17:27:12 +0000727 } else {
728 // We did not find anything with the name ClassName; try to correct for
729 // typos in the class name.
730 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
731 if (CorrectTypo(R, TUScope, 0) &&
732 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregor10f1e4d2010-01-06 23:44:25 +0000733 // Suggest the (potentially) correct interface name. However, put the
734 // fix-it hint itself in a separate note, since changing the name in
735 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor40f7a002010-01-04 17:27:12 +0000736 // provide a code-modification hint or use the typo name for recovery,
737 // because this is just a warning. The program may actually be correct.
738 Diag(ClassLoc, diag::warn_undef_interface_suggest)
739 << ClassName << R.getLookupName();
Douglas Gregor10f1e4d2010-01-06 23:44:25 +0000740 Diag(IDecl->getLocation(), diag::note_previous_decl)
741 << R.getLookupName()
742 << CodeModificationHint::CreateReplacement(ClassLoc,
743 R.getLookupName().getAsString());
Douglas Gregor40f7a002010-01-04 17:27:12 +0000744 IDecl = 0;
745 } else {
746 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
747 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000748 }
Mike Stump11289f42009-09-09 15:08:12 +0000749
Chris Lattnerda463fe2007-12-12 07:09:47 +0000750 // Check that super class name is valid class name
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000751 ObjCInterfaceDecl* SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000752 if (SuperClassname) {
753 // Check if a different kind of symbol declared in this scope.
John McCall9f3059a2009-10-09 21:13:30 +0000754 PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000755 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000756 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
757 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +0000758 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000759 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000760 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000761 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000762 Diag(SuperClassLoc, diag::err_undef_superclass)
763 << SuperClassname << ClassName;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000764 else if (IDecl && IDecl->getSuperClass() != SDecl) {
765 // This implementation and its interface do not have the same
766 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000767 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000768 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +0000769 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000770 }
771 }
772 }
Mike Stump11289f42009-09-09 15:08:12 +0000773
Chris Lattnerda463fe2007-12-12 07:09:47 +0000774 if (!IDecl) {
775 // Legacy case of @implementation with no corresponding @interface.
776 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +0000777
Mike Stump87c57ac2009-05-16 07:39:55 +0000778 // FIXME: Do we support attributes on the @implementation? If so we should
779 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +0000780 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000781 ClassName, ClassLoc, false, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000782 IDecl->setSuperClass(SDecl);
783 IDecl->setLocEnd(ClassLoc);
Douglas Gregorac345a32009-04-24 00:16:12 +0000784
785 PushOnScopeChains(IDecl, TUScope);
Daniel Dunbard1148a72009-04-21 21:41:56 +0000786 } else {
787 // Mark the interface as being completed, even if it was just as
788 // @class ....;
789 // declaration; the user cannot reopen it.
790 IDecl->setForwardDecl(false);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000791 }
Mike Stump11289f42009-09-09 15:08:12 +0000792
793 ObjCImplementationDecl* IMPDecl =
794 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000795 IDecl, SDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000796
Anders Carlssona6b508a2008-11-04 16:57:32 +0000797 if (CheckObjCDeclScope(IMPDecl))
Chris Lattner83f095c2009-03-28 19:18:32 +0000798 return DeclPtrTy::make(IMPDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000799
Chris Lattnerda463fe2007-12-12 07:09:47 +0000800 // Check that there is no duplicate implementation of this class.
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000801 if (IDecl->getImplementation()) {
Chris Lattner36ac1ca2008-03-16 20:53:07 +0000802 // FIXME: Don't leak everything!
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000803 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000804 Diag(IDecl->getImplementation()->getLocation(),
805 diag::note_previous_definition);
806 } else { // add it to the list.
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000807 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +0000808 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000809 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000810 return DeclPtrTy::make(IMPDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000811}
812
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000813void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
814 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000815 SourceLocation RBrace) {
816 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000817 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000818 if (!IDecl)
819 return;
820 /// Check case of non-existing @interface decl.
821 /// (legacy objective-c @implementation decl without an @interface decl).
822 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +0000823 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner22298722009-02-20 21:35:13 +0000824 IDecl->setIVarList(ivars, numIvars, Context);
825 IDecl->setLocEnd(RBrace);
Fariborz Jahanian20912d62010-02-17 17:00:07 +0000826 // Add ivar's to class's DeclContext.
827 for (unsigned i = 0, e = numIvars; i != e; ++i) {
828 ivars[i]->setLexicalDeclContext(IDecl);
829 IDecl->addDecl(ivars[i]);
830 }
831
Chris Lattnerda463fe2007-12-12 07:09:47 +0000832 return;
833 }
834 // If implementation has empty ivar list, just return.
835 if (numIvars == 0)
836 return;
Mike Stump11289f42009-09-09 15:08:12 +0000837
Chris Lattnerda463fe2007-12-12 07:09:47 +0000838 assert(ivars && "missing @implementation ivars");
Mike Stump11289f42009-09-09 15:08:12 +0000839
Chris Lattnerda463fe2007-12-12 07:09:47 +0000840 // Check interface's Ivar list against those in the implementation.
841 // names and types must match.
842 //
Chris Lattnerda463fe2007-12-12 07:09:47 +0000843 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000844 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +0000845 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
846 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000847 ObjCIvarDecl* ImplIvar = ivars[j++];
848 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000849 assert (ImplIvar && "missing implementation ivar");
850 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +0000851
Steve Naroff157599f2009-03-03 14:49:36 +0000852 // First, make sure the types match.
Chris Lattner35ffe332008-07-27 00:05:05 +0000853 if (Context.getCanonicalType(ImplIvar->getType()) !=
854 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +0000855 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000856 << ImplIvar->getIdentifier()
857 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +0000858 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroff157599f2009-03-03 14:49:36 +0000859 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
860 Expr *ImplBitWidth = ImplIvar->getBitWidth();
861 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000862 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
863 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroff157599f2009-03-03 14:49:36 +0000864 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
865 << ImplIvar->getIdentifier();
866 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
867 }
Mike Stump11289f42009-09-09 15:08:12 +0000868 }
Steve Naroff157599f2009-03-03 14:49:36 +0000869 // Make sure the names are identical.
870 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000871 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000872 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +0000873 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000874 }
875 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000876 }
Mike Stump11289f42009-09-09 15:08:12 +0000877
Chris Lattner0f29d982007-12-12 18:11:49 +0000878 if (numIvars > 0)
Chris Lattner83021e92007-12-12 18:19:52 +0000879 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +0000880 else if (IVI != IVE)
Chris Lattner83021e92007-12-12 18:19:52 +0000881 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000882}
883
Steve Naroff15833ed2008-02-10 21:38:56 +0000884void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
885 bool &IncompleteImpl) {
886 if (!IncompleteImpl) {
887 Diag(ImpLoc, diag::warn_incomplete_impl);
888 IncompleteImpl = true;
889 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000890 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff15833ed2008-02-10 21:38:56 +0000891}
892
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000893void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
894 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattnera9d0ffe2009-04-11 18:01:59 +0000895 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian0c20bdd2009-05-14 23:52:54 +0000896 ImpMethodDecl->getResultType()) &&
Steve Naroff8e6aee52009-07-23 01:01:38 +0000897 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
898 ImpMethodDecl->getResultType())) {
Mike Stump11289f42009-09-09 15:08:12 +0000899 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000900 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
901 << ImpMethodDecl->getResultType();
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000902 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
903 }
Mike Stump11289f42009-09-09 15:08:12 +0000904
Chris Lattner67f35b02009-04-11 19:58:42 +0000905 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
906 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
907 IM != EM; ++IM, ++IF) {
Fariborz Jahanian41e803d2009-11-18 18:56:09 +0000908 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
909 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
910 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
911 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner67f35b02009-04-11 19:58:42 +0000912 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000913
914 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner67f35b02009-04-11 19:58:42 +0000915 << ImpMethodDecl->getDeclName() << (*IF)->getType()
916 << (*IM)->getType();
Chris Lattner5300acc2009-04-11 20:14:49 +0000917 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner67f35b02009-04-11 19:58:42 +0000918 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +0000919}
920
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000921/// isPropertyReadonly - Return true if property is readonly, by searching
922/// for the property in the class and in its categories and implementations
923///
924bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
Steve Naroffde680012009-02-26 19:11:32 +0000925 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000926 // by far the most common case.
927 if (!PDecl->isReadOnly())
928 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000929 // Even if property is ready only, if interface has a user defined setter,
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000930 // it is not considered read only.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000931 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000932 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000933
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000934 // Main class has the property as 'readonly'. Must search
Mike Stump11289f42009-09-09 15:08:12 +0000935 // through the category list to see if the property's
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000936 // attribute has been over-ridden to 'readwrite'.
937 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
938 Category; Category = Category->getNextClassCategory()) {
Mike Stump11289f42009-09-09 15:08:12 +0000939 // Even if property is ready only, if a category has a user defined setter,
940 // it is not considered read only.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000941 if (Category->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000942 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000943 ObjCPropertyDecl *P =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000944 Category->FindPropertyDeclaration(PDecl->getIdentifier());
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000945 if (P && !P->isReadOnly())
946 return false;
947 }
Mike Stump11289f42009-09-09 15:08:12 +0000948
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000949 // Also, check for definition of a setter method in the implementation if
950 // all else failed.
951 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
Mike Stump11289f42009-09-09 15:08:12 +0000952 if (ObjCImplementationDecl *IMD =
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000953 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000954 if (IMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000955 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000956 } else if (ObjCCategoryImplDecl *CIMD =
Mike Stump12b8ce12009-08-04 21:02:39 +0000957 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000958 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000959 return false;
960 }
961 }
Steve Naroffde680012009-02-26 19:11:32 +0000962 // Lastly, look through the implementation (if one is in scope).
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000963 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000964 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
Steve Naroffde680012009-02-26 19:11:32 +0000965 return false;
Fariborz Jahanian15e3a5c2009-04-06 16:59:10 +0000966 // If all fails, look at the super class.
967 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
968 return isPropertyReadonly(PDecl, SIDecl);
Fariborz Jahanian8e1555c2009-01-12 19:55:42 +0000969 return true;
970}
971
Mike Stump87c57ac2009-05-16 07:39:55 +0000972/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
973/// improve the efficiency of selector lookups and type checking by associating
974/// with each protocol / interface / category the flattened instance tables. If
975/// we used an immutable set to keep the table then it wouldn't add significant
976/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +0000977
Steve Naroffa36992242008-02-08 22:06:17 +0000978/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +0000979/// Declared in protocol, and those referenced by it.
Steve Naroffa36992242008-02-08 22:06:17 +0000980void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
981 ObjCProtocolDecl *PDecl,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000982 bool& IncompleteImpl,
Steve Naroffa36992242008-02-08 22:06:17 +0000983 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000984 const llvm::DenseSet<Selector> &ClsMap,
985 ObjCInterfaceDecl *IDecl) {
986 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000987 ObjCInterfaceDecl *NSIDecl = 0;
988 if (getLangOptions().NeXTRuntime) {
Mike Stump11289f42009-09-09 15:08:12 +0000989 // check to see if class implements forwardInvocation method and objects
990 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000991 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +0000992 // Under such conditions, which means that every method possible is
993 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000994 // found" warnings.
995 // FIXME: Use a general GetUnarySelector method for this.
996 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
997 Selector fISelector = Context.Selectors.getSelector(1, &II);
998 if (InsMap.count(fISelector))
999 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1000 // need be implemented in the implementation.
1001 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1002 }
Mike Stump11289f42009-09-09 15:08:12 +00001003
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001004 // If a method lookup fails locally we still need to look and see if
1005 // the method was implemented by a base class or an inherited
1006 // protocol. This lookup is slow, but occurs rarely in correct code
1007 // and otherwise would terminate in a warning.
1008
Chris Lattnerda463fe2007-12-12 07:09:47 +00001009 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001010 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +00001011 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001012 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001013 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001014 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001015 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001016 (!Super ||
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001017 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001018 // Ugly, but necessary. Method declared in protcol might have
1019 // have been synthesized due to a property declared in the class which
1020 // uses the protocol.
Mike Stump11289f42009-09-09 15:08:12 +00001021 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001022 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001023 if (!MethodInClass || !MethodInClass->isSynthesized())
1024 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
1025 }
1026 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001027 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +00001028 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001029 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001030 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001031 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001032 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1033 !ClsMap.count(method->getSelector()) &&
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001034 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff15833ed2008-02-10 21:38:56 +00001035 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff3ce37a62007-12-14 23:37:57 +00001036 }
Chris Lattner390d39a2008-07-21 21:32:27 +00001037 // Check on this protocols's referenced protocols, recursively.
1038 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1039 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001040 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001041}
1042
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001043/// MatchAllMethodDeclarations - Check methods declaraed in interface or
1044/// or protocol against those declared in their implementations.
1045///
1046void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1047 const llvm::DenseSet<Selector> &ClsMap,
1048 llvm::DenseSet<Selector> &InsMapSeen,
1049 llvm::DenseSet<Selector> &ClsMapSeen,
1050 ObjCImplDecl* IMPDecl,
1051 ObjCContainerDecl* CDecl,
1052 bool &IncompleteImpl,
Mike Stump11289f42009-09-09 15:08:12 +00001053 bool ImmediateClass) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001054 // Check and see if instance methods in class interface have been
1055 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001056 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1057 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001058 if (InsMapSeen.count((*I)->getSelector()))
1059 continue;
1060 InsMapSeen.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001061 if (!(*I)->isSynthesized() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001062 !InsMap.count((*I)->getSelector())) {
1063 if (ImmediateClass)
1064 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
1065 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +00001066 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001067 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001068 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001069 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001070 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001071 assert(IntfMethodDecl &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001072 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
1073 // ImpMethodDecl may be null as in a @dynamic property.
1074 if (ImpMethodDecl)
1075 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1076 }
1077 }
Mike Stump11289f42009-09-09 15:08:12 +00001078
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001079 // Check and see if class methods in class interface have been
1080 // implemented in the implementation class. If so, their types match.
Mike Stump11289f42009-09-09 15:08:12 +00001081 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001082 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001083 if (ClsMapSeen.count((*I)->getSelector()))
1084 continue;
1085 ClsMapSeen.insert((*I)->getSelector());
1086 if (!ClsMap.count((*I)->getSelector())) {
1087 if (ImmediateClass)
1088 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Mike Stump12b8ce12009-08-04 21:02:39 +00001089 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001090 ObjCMethodDecl *ImpMethodDecl =
1091 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001092 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001093 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001094 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
1095 }
1096 }
1097 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1098 // Check for any implementation of a methods declared in protocol.
1099 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
1100 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001101 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1102 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001103 (*PI), IncompleteImpl, false);
1104 if (I->getSuperClass())
1105 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +00001106 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001107 I->getSuperClass(), IncompleteImpl, false);
1108 }
1109}
1110
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001111/// CollectImmediateProperties - This routine collects all properties in
1112/// the class and its conforming protocols; but not those it its super class.
1113void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
1114 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1115 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1116 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1117 E = IDecl->prop_end(); P != E; ++P) {
1118 ObjCPropertyDecl *Prop = (*P);
1119 PropMap[Prop->getIdentifier()] = Prop;
1120 }
1121 // scan through class's protocols.
1122 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
1123 E = IDecl->protocol_end(); PI != E; ++PI)
1124 CollectImmediateProperties((*PI), PropMap);
1125 }
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00001126 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001127 if (!CATDecl->IsClassExtension())
1128 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1129 E = CATDecl->prop_end(); P != E; ++P) {
1130 ObjCPropertyDecl *Prop = (*P);
1131 PropMap[Prop->getIdentifier()] = Prop;
1132 }
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00001133 // scan through class's protocols.
1134 for (ObjCInterfaceDecl::protocol_iterator PI = CATDecl->protocol_begin(),
1135 E = CATDecl->protocol_end(); PI != E; ++PI)
1136 CollectImmediateProperties((*PI), PropMap);
1137 }
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001138 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1139 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1140 E = PDecl->prop_end(); P != E; ++P) {
1141 ObjCPropertyDecl *Prop = (*P);
1142 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1143 if (!PropEntry)
1144 PropEntry = Prop;
1145 }
1146 // scan through protocol's protocols.
1147 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1148 E = PDecl->protocol_end(); PI != E; ++PI)
1149 CollectImmediateProperties((*PI), PropMap);
1150 }
1151}
1152
Fariborz Jahanian45878032010-02-09 19:31:38 +00001153/// LookupPropertyDecl - Looks up a property in the current class and all
1154/// its protocols.
1155ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1156 IdentifierInfo *II) {
1157 if (const ObjCInterfaceDecl *IDecl =
1158 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1159 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1160 E = IDecl->prop_end(); P != E; ++P) {
1161 ObjCPropertyDecl *Prop = (*P);
1162 if (Prop->getIdentifier() == II)
1163 return Prop;
1164 }
1165 // scan through class's protocols.
1166 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
1167 E = IDecl->protocol_end(); PI != E; ++PI) {
1168 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1169 if (Prop)
1170 return Prop;
1171 }
1172 }
1173 else if (const ObjCProtocolDecl *PDecl =
1174 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1175 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1176 E = PDecl->prop_end(); P != E; ++P) {
1177 ObjCPropertyDecl *Prop = (*P);
1178 if (Prop->getIdentifier() == II)
1179 return Prop;
1180 }
1181 // scan through protocol's protocols.
1182 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1183 E = PDecl->protocol_end(); PI != E; ++PI) {
1184 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1185 if (Prop)
1186 return Prop;
1187 }
1188 }
1189 return 0;
1190}
1191
1192
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001193void Sema::DiagnoseUnimplementedProperties(ObjCImplDecl* IMPDecl,
1194 ObjCContainerDecl *CDecl,
1195 const llvm::DenseSet<Selector>& InsMap) {
1196 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1197 CollectImmediateProperties(CDecl, PropMap);
Fariborz Jahanianb36606a2010-01-20 17:27:59 +00001198 if (PropMap.empty())
1199 return;
1200
1201 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1202 for (ObjCImplDecl::propimpl_iterator
1203 I = IMPDecl->propimpl_begin(),
1204 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1205 PropImplMap.insert((*I)->getPropertyDecl());
1206
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001207 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1208 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1209 ObjCPropertyDecl *Prop = P->second;
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001210 // Is there a matching propery synthesize/dynamic?
Fariborz Jahanianb36606a2010-01-20 17:27:59 +00001211 if (Prop->isInvalidDecl() ||
1212 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1213 PropImplMap.count(Prop))
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001214 continue;
Fariborz Jahanian45878032010-02-09 19:31:38 +00001215 if (LangOpts.ObjCNonFragileABI2) {
1216 ActOnPropertyImplDecl(IMPDecl->getLocation(),
1217 SourceLocation(),
1218 true, DeclPtrTy::make(IMPDecl),
1219 Prop->getIdentifier(),
1220 Prop->getIdentifier());
1221 continue;
1222 }
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001223 if (!InsMap.count(Prop->getGetterName())) {
1224 Diag(Prop->getLocation(),
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00001225 isa<ObjCCategoryDecl>(CDecl) ?
1226 diag::warn_setter_getter_impl_required_in_category :
1227 diag::warn_setter_getter_impl_required)
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001228 << Prop->getDeclName() << Prop->getGetterName();
1229 Diag(IMPDecl->getLocation(),
1230 diag::note_property_impl_required);
1231 }
1232
1233 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1234 Diag(Prop->getLocation(),
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00001235 isa<ObjCCategoryDecl>(CDecl) ?
1236 diag::warn_setter_getter_impl_required_in_category :
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001237 diag::warn_setter_getter_impl_required)
1238 << Prop->getDeclName() << Prop->getSetterName();
1239 Diag(IMPDecl->getLocation(),
1240 diag::note_property_impl_required);
1241 }
1242 }
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001243}
1244
Mike Stump11289f42009-09-09 15:08:12 +00001245void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
1246 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001247 bool IncompleteImpl) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001248 llvm::DenseSet<Selector> InsMap;
1249 // Check and see if instance methods in class interface have been
1250 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001251 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001252 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001253 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001254
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001255 // Check and see if properties declared in the interface have either 1)
1256 // an implementation or 2) there is a @synthesize/@dynamic implementation
1257 // of the property in the @implementation.
1258 if (isa<ObjCInterfaceDecl>(CDecl))
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001259 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
1260
Chris Lattnerda463fe2007-12-12 07:09:47 +00001261 llvm::DenseSet<Selector> ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +00001262 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001263 I = IMPDecl->classmeth_begin(),
1264 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001265 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001266
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001267 // Check for type conflict of methods declared in a class/protocol and
1268 // its implementation; if any.
1269 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +00001270 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1271 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001272 IncompleteImpl, true);
Mike Stump11289f42009-09-09 15:08:12 +00001273
Chris Lattnerda463fe2007-12-12 07:09:47 +00001274 // Check the protocol list for unimplemented methods in the @implementation
1275 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001276 // Check and see if class methods in class interface have been
1277 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001278
Chris Lattner9ef10f42009-03-01 00:56:52 +00001279 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001280 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattner9ef10f42009-03-01 00:56:52 +00001281 E = I->protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001282 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001283 InsMap, ClsMap, I);
1284 // Check class extensions (unnamed categories)
1285 for (ObjCCategoryDecl *Categories = I->getCategoryList();
1286 Categories; Categories = Categories->getNextClassCategory()) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001287 if (Categories->IsClassExtension()) {
Chris Lattner9ef10f42009-03-01 00:56:52 +00001288 ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
1289 break;
1290 }
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +00001291 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001292 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001293 // For extended class, unimplemented methods in its protocols will
1294 // be reported in the primary class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001295 if (!C->IsClassExtension()) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001296 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1297 E = C->protocol_end(); PI != E; ++PI)
1298 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1299 InsMap, ClsMap, C->getClassInterface());
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00001300 // Report unimplemented properties in the category as well.
1301 // When reporting on missing setter/getters, do not report when
1302 // setter/getter is implemented in category's primary class
1303 // implementation.
1304 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1305 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1306 for (ObjCImplementationDecl::instmeth_iterator
1307 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1308 InsMap.insert((*I)->getSelector());
1309 }
1310 DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
1311 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001312 } else
1313 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00001314}
1315
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001316void
1317Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1318 ObjCContainerDecl* IDecl) {
1319 // Rules apply in non-GC mode only
1320 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1321 return;
1322 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1323 E = IDecl->prop_end();
1324 I != E; ++I) {
1325 ObjCPropertyDecl *Property = (*I);
1326 unsigned Attributes = Property->getPropertyAttributes();
1327 // We only care about readwrite atomic property.
1328 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1329 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1330 continue;
1331 if (const ObjCPropertyImplDecl *PIDecl
1332 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1333 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1334 continue;
1335 ObjCMethodDecl *GetterMethod =
1336 IMPDecl->getInstanceMethod(Property->getGetterName());
1337 ObjCMethodDecl *SetterMethod =
1338 IMPDecl->getInstanceMethod(Property->getSetterName());
1339 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1340 SourceLocation MethodLoc =
1341 (GetterMethod ? GetterMethod->getLocation()
1342 : SetterMethod->getLocation());
1343 Diag(MethodLoc, diag::warn_atomic_property_rule)
1344 << Property->getIdentifier();
1345 Diag(Property->getLocation(), diag::note_property_declare);
1346 }
1347 }
1348 }
1349}
1350
Mike Stump11289f42009-09-09 15:08:12 +00001351/// ActOnForwardClassDeclaration -
Chris Lattner83f095c2009-03-28 19:18:32 +00001352Action::DeclPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00001353Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00001354 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00001355 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00001356 unsigned NumElts) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001357 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump11289f42009-09-09 15:08:12 +00001358
Chris Lattnerda463fe2007-12-12 07:09:47 +00001359 for (unsigned i = 0; i != NumElts; ++i) {
1360 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001361 NamedDecl *PrevDecl
1362 = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
Douglas Gregor5daeee22008-12-08 18:40:42 +00001363 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001364 // Maybe we will complain about the shadowed template parameter.
1365 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1366 // Just pretend that we didn't see the previous declaration.
1367 PrevDecl = 0;
1368 }
1369
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001370 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00001371 // GCC apparently allows the following idiom:
1372 //
1373 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1374 // @class XCElementToggler;
1375 //
Mike Stump11289f42009-09-09 15:08:12 +00001376 // FIXME: Make an extension?
Steve Naroff946166f2008-06-05 22:57:10 +00001377 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
1378 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001379 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00001380 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Mike Stump12b8ce12009-08-04 21:02:39 +00001381 } else if (TDD) {
1382 // a forward class declaration matching a typedef name of a class refers
1383 // to the underlying class.
Mike Stump11289f42009-09-09 15:08:12 +00001384 if (ObjCInterfaceType * OI =
Fariborz Jahanian0d451812009-05-07 21:49:26 +00001385 dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
1386 PrevDecl = OI->getDecl();
1387 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001388 }
Mike Stump11289f42009-09-09 15:08:12 +00001389 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001390 if (!IDecl) { // Not already seen? Make a forward decl.
Mike Stump11289f42009-09-09 15:08:12 +00001391 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001392 IdentList[i], IdentLocs[i], true);
Ted Kremenek707ece62009-11-17 22:58:30 +00001393
1394 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1395 // the current DeclContext. This prevents clients that walk DeclContext
1396 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1397 // declared later (if at all). We also take care to explicitly make
1398 // sure this declaration is visible for name lookup.
1399 PushOnScopeChains(IDecl, TUScope, false);
1400 CurContext->makeDeclVisibleInContext(IDecl, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001401 }
1402
1403 Interfaces.push_back(IDecl);
1404 }
Mike Stump11289f42009-09-09 15:08:12 +00001405
Ted Kremenek9b124e12009-11-18 00:28:11 +00001406 assert(Interfaces.size() == NumElts);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001407 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek9b124e12009-11-18 00:28:11 +00001408 Interfaces.data(), IdentLocs,
Anders Carlssona6b508a2008-11-04 16:57:32 +00001409 Interfaces.size());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001410 CurContext->addDecl(CDecl);
Anders Carlssona6b508a2008-11-04 16:57:32 +00001411 CheckObjCDeclScope(CDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001412 return DeclPtrTy::make(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001413}
1414
1415
1416/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1417/// returns true, or false, accordingly.
1418/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump11289f42009-09-09 15:08:12 +00001419bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffa0ed1652008-10-21 10:37:50 +00001420 const ObjCMethodDecl *PrevMethod,
1421 bool matchBasedOnSizeAndAlignment) {
1422 QualType T1 = Context.getCanonicalType(Method->getResultType());
1423 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump11289f42009-09-09 15:08:12 +00001424
Steve Naroffa0ed1652008-10-21 10:37:50 +00001425 if (T1 != T2) {
1426 // The result types are different.
1427 if (!matchBasedOnSizeAndAlignment)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001428 return false;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001429 // Incomplete types don't have a size and alignment.
1430 if (T1->isIncompleteType() || T2->isIncompleteType())
1431 return false;
1432 // Check is based on size and alignment.
1433 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1434 return false;
1435 }
Mike Stump11289f42009-09-09 15:08:12 +00001436
Chris Lattnera4997152009-02-20 18:43:26 +00001437 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1438 E = Method->param_end();
1439 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001440
Chris Lattnera4997152009-02-20 18:43:26 +00001441 for (; ParamI != E; ++ParamI, ++PrevI) {
1442 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1443 T1 = Context.getCanonicalType((*ParamI)->getType());
1444 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Naroffa0ed1652008-10-21 10:37:50 +00001445 if (T1 != T2) {
1446 // The result types are different.
1447 if (!matchBasedOnSizeAndAlignment)
1448 return false;
1449 // Incomplete types don't have a size and alignment.
1450 if (T1->isIncompleteType() || T2->isIncompleteType())
1451 return false;
1452 // Check is based on size and alignment.
1453 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1454 return false;
1455 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001456 }
1457 return true;
1458}
1459
Douglas Gregorc78d3462009-04-24 21:10:55 +00001460/// \brief Read the contents of the instance and factory method pools
1461/// for a given selector from external storage.
1462///
1463/// This routine should only be called once, when neither the instance
1464/// nor the factory method pool has an entry for this selector.
Mike Stump11289f42009-09-09 15:08:12 +00001465Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001466 bool isInstance) {
1467 assert(ExternalSource && "We need an external AST source");
1468 assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
1469 "Selector data already loaded into the instance method pool");
1470 assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
1471 "Selector data already loaded into the factory method pool");
1472
1473 // Read the method list from the external source.
1474 std::pair<ObjCMethodList, ObjCMethodList> Methods
1475 = ExternalSource->ReadMethodPool(Sel);
Mike Stump11289f42009-09-09 15:08:12 +00001476
Douglas Gregorc78d3462009-04-24 21:10:55 +00001477 if (isInstance) {
1478 if (Methods.second.Method)
1479 FactoryMethodPool[Sel] = Methods.second;
1480 return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
Mike Stump11289f42009-09-09 15:08:12 +00001481 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001482
1483 if (Methods.first.Method)
1484 InstanceMethodPool[Sel] = Methods.first;
1485
1486 return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
1487}
1488
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001489void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001490 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1491 = InstanceMethodPool.find(Method->getSelector());
1492 if (Pos == InstanceMethodPool.end()) {
1493 if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
1494 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
1495 else
1496 Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
1497 ObjCMethodList())).first;
1498 }
1499
1500 ObjCMethodList &Entry = Pos->second;
Chris Lattner7b26b292009-03-04 05:16:45 +00001501 if (Entry.Method == 0) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001502 // Haven't seen a method with this selector name yet - add it.
Chris Lattner7b26b292009-03-04 05:16:45 +00001503 Entry.Method = Method;
1504 Entry.Next = 0;
1505 return;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001506 }
Mike Stump11289f42009-09-09 15:08:12 +00001507
Chris Lattner7b26b292009-03-04 05:16:45 +00001508 // We've seen a method with this name, see if we have already seen this type
1509 // signature.
1510 for (ObjCMethodList *List = &Entry; List; List = List->Next)
1511 if (MatchTwoMethodDeclarations(Method, List->Method))
1512 return;
Mike Stump11289f42009-09-09 15:08:12 +00001513
Chris Lattner7b26b292009-03-04 05:16:45 +00001514 // We have a new signature for an existing method - add it.
1515 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenekda4abf12010-02-11 00:53:01 +00001516 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1517 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001518}
1519
Steve Naroff9ebc0502008-10-21 10:50:19 +00001520// FIXME: Finish implementing -Wno-strict-selector-match.
Mike Stump11289f42009-09-09 15:08:12 +00001521ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001522 SourceRange R,
1523 bool warn) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001524 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1525 = InstanceMethodPool.find(Sel);
Douglas Gregor9a1899b2009-04-24 22:23:41 +00001526 if (Pos == InstanceMethodPool.end()) {
1527 if (ExternalSource && !FactoryMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001528 Pos = ReadMethodPool(Sel, /*isInstance=*/true);
1529 else
1530 return 0;
1531 }
1532
1533 ObjCMethodList &MethList = Pos->second;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001534 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001535
Steve Naroff4a82d812008-09-30 14:38:43 +00001536 if (MethList.Method && MethList.Next) {
Steve Naroffa0ed1652008-10-21 10:37:50 +00001537 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1538 // This checks if the methods differ by size & alignment.
1539 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +00001540 issueWarning = warn;
Steve Naroffa0ed1652008-10-21 10:37:50 +00001541 }
1542 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattnere4b95692008-11-24 03:33:13 +00001543 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCalle29c5cd2009-12-10 19:51:03 +00001544 Diag(MethList.Method->getLocStart(), diag::note_using)
Chris Lattnerf490e152008-11-19 05:27:50 +00001545 << MethList.Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001546 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCalle29c5cd2009-12-10 19:51:03 +00001547 Diag(Next->Method->getLocStart(), diag::note_also_found)
Chris Lattnerf490e152008-11-19 05:27:50 +00001548 << Next->Method->getSourceRange();
Steve Naroff4a82d812008-09-30 14:38:43 +00001549 }
1550 return MethList.Method;
1551}
1552
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001553void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001554 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1555 = FactoryMethodPool.find(Method->getSelector());
1556 if (Pos == FactoryMethodPool.end()) {
1557 if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
1558 Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
1559 else
1560 Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
1561 ObjCMethodList())).first;
1562 }
1563
1564 ObjCMethodList &FirstMethod = Pos->second;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001565 if (!FirstMethod.Method) {
1566 // Haven't seen a method with this selector name yet - add it.
1567 FirstMethod.Method = Method;
1568 FirstMethod.Next = 0;
1569 } else {
1570 // We've seen a method with this name, now check the type signature(s).
1571 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
Mike Stump11289f42009-09-09 15:08:12 +00001572
1573 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001574 Next = Next->Next)
1575 match = MatchTwoMethodDeclarations(Method, Next->Method);
Mike Stump11289f42009-09-09 15:08:12 +00001576
Chris Lattnerda463fe2007-12-12 07:09:47 +00001577 if (!match) {
1578 // We have a new signature for an existing method - add it.
1579 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenekda4abf12010-02-11 00:53:01 +00001580 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1581 ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001582 FirstMethod.Next = OMI;
1583 }
1584 }
1585}
1586
Mike Stump11289f42009-09-09 15:08:12 +00001587ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001588 SourceRange R) {
1589 llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
1590 = FactoryMethodPool.find(Sel);
1591 if (Pos == FactoryMethodPool.end()) {
Mike Stump11289f42009-09-09 15:08:12 +00001592 if (ExternalSource && !InstanceMethodPool.count(Sel))
Douglas Gregorc78d3462009-04-24 21:10:55 +00001593 Pos = ReadMethodPool(Sel, /*isInstance=*/false);
1594 else
1595 return 0;
1596 }
1597
1598 ObjCMethodList &MethList = Pos->second;
1599 bool issueWarning = false;
Mike Stump11289f42009-09-09 15:08:12 +00001600
Douglas Gregorc78d3462009-04-24 21:10:55 +00001601 if (MethList.Method && MethList.Next) {
1602 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1603 // This checks if the methods differ by size & alignment.
1604 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1605 issueWarning = true;
1606 }
1607 if (issueWarning && (MethList.Method && MethList.Next)) {
1608 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCalle29c5cd2009-12-10 19:51:03 +00001609 Diag(MethList.Method->getLocStart(), diag::note_using)
Douglas Gregorc78d3462009-04-24 21:10:55 +00001610 << MethList.Method->getSourceRange();
1611 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
John McCalle29c5cd2009-12-10 19:51:03 +00001612 Diag(Next->Method->getLocStart(), diag::note_also_found)
Douglas Gregorc78d3462009-04-24 21:10:55 +00001613 << Next->Method->getSourceRange();
1614 }
1615 return MethList.Method;
1616}
1617
Mike Stump11289f42009-09-09 15:08:12 +00001618/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
Steve Naroff35c62ae2009-01-08 17:28:14 +00001619/// have the property type and issue diagnostics if they don't.
1620/// Also synthesize a getter/setter method if none exist (and update the
1621/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1622/// methods is the "right" thing to do.
Mike Stump11289f42009-09-09 15:08:12 +00001623void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Steve Naroff35c62ae2009-01-08 17:28:14 +00001624 ObjCContainerDecl *CD) {
1625 ObjCMethodDecl *GetterMethod, *SetterMethod;
Mike Stump11289f42009-09-09 15:08:12 +00001626
1627 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001628 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Mike Stump11289f42009-09-09 15:08:12 +00001629 DiagnosePropertyAccessorMismatch(property, GetterMethod,
Fariborz Jahanianfe9e3942009-05-08 20:20:55 +00001630 property->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001631
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001632 if (SetterMethod) {
Fariborz Jahanian1a5f2922010-01-06 00:18:12 +00001633 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1634 property->getPropertyAttributes();
1635 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1636 Context.getCanonicalType(SetterMethod->getResultType()) !=
1637 Context.VoidTy)
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001638 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
Chris Lattnera4997152009-02-20 18:43:26 +00001639 if (SetterMethod->param_size() != 1 ||
1640 ((*SetterMethod->param_begin())->getType() != property->getType())) {
Mike Stump11289f42009-09-09 15:08:12 +00001641 Diag(property->getLocation(),
1642 diag::warn_accessor_property_type_mismatch)
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001643 << property->getDeclName()
Ted Kremenek9a468042009-03-14 00:20:08 +00001644 << SetterMethod->getSelector();
Fariborz Jahanianff839982008-12-06 21:48:16 +00001645 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1646 }
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001647 }
Steve Naroff35c62ae2009-01-08 17:28:14 +00001648
1649 // Synthesize getter/setter methods if none exist.
Steve Naroffe538c5f2009-01-08 20:15:03 +00001650 // Find the default getter and if one not found, add one.
Mike Stump87c57ac2009-05-16 07:39:55 +00001651 // FIXME: The synthesized property we set here is misleading. We almost always
1652 // synthesize these methods unless the user explicitly provided prototypes
1653 // (which is odd, but allowed). Sema should be typechecking that the
1654 // declarations jive in that situation (which it is not currently).
Steve Naroffe538c5f2009-01-08 20:15:03 +00001655 if (!GetterMethod) {
1656 // No instance method of same name as property getter name was found.
Mike Stump11289f42009-09-09 15:08:12 +00001657 // Declare a getter method and add it to the list of methods
Steve Naroffe538c5f2009-01-08 20:15:03 +00001658 // for this class.
Mike Stump11289f42009-09-09 15:08:12 +00001659 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1660 property->getLocation(), property->getGetterName(),
1661 property->getType(), CD, true, false, true,
1662 (property->getPropertyImplementation() ==
1663 ObjCPropertyDecl::Optional) ?
1664 ObjCMethodDecl::Optional :
Steve Naroffe538c5f2009-01-08 20:15:03 +00001665 ObjCMethodDecl::Required);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001666 CD->addDecl(GetterMethod);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001667 } else
1668 // A user declared getter will be synthesize when @synthesize of
1669 // the property with the same name is seen in the @implementation
Steve Naroff04f2d142009-04-20 15:06:07 +00001670 GetterMethod->setSynthesized(true);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001671 property->setGetterMethodDecl(GetterMethod);
1672
1673 // Skip setter if property is read-only.
1674 if (!property->isReadOnly()) {
1675 // Find the default setter and if one not found, add one.
1676 if (!SetterMethod) {
1677 // No instance method of same name as property setter name was found.
Mike Stump11289f42009-09-09 15:08:12 +00001678 // Declare a setter method and add it to the list of methods
Steve Naroffe538c5f2009-01-08 20:15:03 +00001679 // for this class.
Mike Stump11289f42009-09-09 15:08:12 +00001680 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1681 property->getLocation(),
1682 property->getSetterName(),
Steve Naroffe538c5f2009-01-08 20:15:03 +00001683 Context.VoidTy, CD, true, false, true,
Mike Stump11289f42009-09-09 15:08:12 +00001684 (property->getPropertyImplementation() ==
1685 ObjCPropertyDecl::Optional) ?
1686 ObjCMethodDecl::Optional :
Steve Naroffe538c5f2009-01-08 20:15:03 +00001687 ObjCMethodDecl::Required);
1688 // Invent the arguments for the setter. We don't bother making a
1689 // nice name for the argument.
1690 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
Mike Stump11289f42009-09-09 15:08:12 +00001691 property->getLocation(),
Steve Naroffe538c5f2009-01-08 20:15:03 +00001692 property->getIdentifier(),
1693 property->getType(),
John McCallbcd03502009-12-07 02:54:59 +00001694 /*TInfo=*/0,
Steve Naroffe538c5f2009-01-08 20:15:03 +00001695 VarDecl::None,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001696 0);
Steve Naroff04f2d142009-04-20 15:06:07 +00001697 SetterMethod->setMethodParams(Context, &Argument, 1);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001698 CD->addDecl(SetterMethod);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001699 } else
1700 // A user declared setter will be synthesize when @synthesize of
1701 // the property with the same name is seen in the @implementation
Steve Naroff04f2d142009-04-20 15:06:07 +00001702 SetterMethod->setSynthesized(true);
Steve Naroffe538c5f2009-01-08 20:15:03 +00001703 property->setSetterMethodDecl(SetterMethod);
1704 }
Mike Stump11289f42009-09-09 15:08:12 +00001705 // Add any synthesized methods to the global pool. This allows us to
Steve Naroff35c62ae2009-01-08 17:28:14 +00001706 // handle the following, which is supported by GCC (and part of the design).
1707 //
1708 // @interface Foo
1709 // @property double bar;
1710 // @end
1711 //
1712 // void thisIsUnfortunate() {
1713 // id foo;
1714 // double bar = [foo bar];
1715 // }
1716 //
Douglas Gregor020713e2009-01-09 19:42:16 +00001717 if (GetterMethod)
Mike Stump11289f42009-09-09 15:08:12 +00001718 AddInstanceMethodToGlobalPool(GetterMethod);
Douglas Gregor020713e2009-01-09 19:42:16 +00001719 if (SetterMethod)
Mike Stump11289f42009-09-09 15:08:12 +00001720 AddInstanceMethodToGlobalPool(SetterMethod);
Fariborz Jahanianeae373e2008-12-02 18:39:49 +00001721}
1722
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001723/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1724/// identical selector names in current and its super classes and issues
1725/// a warning if any of their argument types are incompatible.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001726void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1727 ObjCMethodDecl *Method,
1728 bool IsInstance) {
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001729 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1730 if (ID == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +00001731
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001732 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump11289f42009-09-09 15:08:12 +00001733 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001734 SD->lookupMethod(Method->getSelector(), IsInstance);
1735 if (SuperMethodDecl == 0) {
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001736 ID = SD;
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001737 continue;
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001738 }
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001739 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1740 E = Method->param_end();
1741 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1742 for (; ParamI != E; ++ParamI, ++PrevI) {
1743 // Number of parameters are the same and is guaranteed by selector match.
1744 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1745 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1746 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1747 // If type of arguement of method in this class does not match its
1748 // respective argument type in the super class method, issue warning;
1749 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump11289f42009-09-09 15:08:12 +00001750 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001751 << T1 << T2;
1752 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1753 return;
1754 }
1755 }
1756 ID = SD;
1757 }
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001758}
1759
Steve Naroff1d2538c2007-12-18 01:30:32 +00001760// Note: For class/category implemenations, allMethods/allProperties is
1761// always null.
Ted Kremenekc7c64312010-01-07 01:20:12 +00001762void Sema::ActOnAtEnd(SourceRange AtEnd,
1763 DeclPtrTy classDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +00001764 DeclPtrTy *allMethods, unsigned allNum,
1765 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001766 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001767 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001768
Steve Naroff1d2538c2007-12-18 01:30:32 +00001769 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1770 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattnerda463fe2007-12-12 07:09:47 +00001771 // should be true.
1772 if (!ClassDecl)
1773 return;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001774
Mike Stump11289f42009-09-09 15:08:12 +00001775 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00001776 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1777 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001778 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00001779
Ted Kremenekc7c64312010-01-07 01:20:12 +00001780 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1781 // FIXME: This is wrong. We shouldn't be pretending that there is
1782 // an '@end' in the declaration.
1783 SourceLocation L = ClassDecl->getLocation();
1784 AtEnd.setBegin(L);
1785 AtEnd.setEnd(L);
1786 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001787 }
1788
Steve Naroff35c62ae2009-01-08 17:28:14 +00001789 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff35c62ae2009-01-08 17:28:14 +00001790
1791 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1792 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1793 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1794
Chris Lattnerda463fe2007-12-12 07:09:47 +00001795 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001796 ObjCMethodDecl *Method =
Chris Lattner83f095c2009-03-28 19:18:32 +00001797 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001798
1799 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00001800 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001801 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001802 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001803 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001804 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001805 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001806 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001807 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001808 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001809 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001810 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001811 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001812 InsMap[Method->getSelector()] = Method;
1813 /// The following allows us to typecheck messages to "id".
1814 AddInstanceMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001815 // verify that the instance method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001816 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001817 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001818 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001819 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001820 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001821 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00001822 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00001823 : false;
Mike Stump11289f42009-09-09 15:08:12 +00001824 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00001825 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00001826 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00001827 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001828 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001829 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001830 DC->addDecl(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001831 ClsMap[Method->getSelector()] = Method;
Steve Naroff1d2538c2007-12-18 01:30:32 +00001832 /// The following allows us to typecheck messages to "Class".
1833 AddFactoryMethodToGlobalPool(Method);
Mike Stump11289f42009-09-09 15:08:12 +00001834 // verify that the class method conforms to the same definition of
Fariborz Jahanianb61af4c2009-08-04 17:01:09 +00001835 // parent methods if it shadows one.
Fariborz Jahanian10ff7862009-08-04 01:07:16 +00001836 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001837 }
1838 }
1839 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001840 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump11289f42009-09-09 15:08:12 +00001841 // Compares properties declared in this class to those of its
Fariborz Jahanian7cf18862008-05-01 00:03:38 +00001842 // super class.
Fariborz Jahanian98a6c4f2008-05-02 19:17:30 +00001843 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001844 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroffb3a87982009-01-09 15:36:25 +00001845 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001846 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00001847 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00001848 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001849
Fariborz Jahaniancdb85752010-01-18 18:41:16 +00001850 // Compare protocol properties with those in category
1851 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001852 if (C->IsClassExtension())
Fariborz Jahanian33afd772009-03-02 19:05:07 +00001853 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattnerda463fe2007-12-12 07:09:47 +00001854 }
Steve Naroffb3a87982009-01-09 15:36:25 +00001855 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001856 if (CDecl->getIdentifier())
1857 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1858 // user-defined setter/getter. It also synthesizes setter/getter methods
1859 // and adds them to the DeclContext and global method pools.
1860 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1861 E = CDecl->prop_end();
1862 I != E; ++I)
1863 ProcessPropertyDecl(*I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00001864 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00001865 }
1866 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001867 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001868 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001869 ImplMethodsVsClassMethods(IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00001870 AtomicPropertySetterGetterRules(IC, IDecl);
1871 }
Mike Stump11289f42009-09-09 15:08:12 +00001872 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00001873 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001874 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00001875
Chris Lattnerda463fe2007-12-12 07:09:47 +00001876 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00001877 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00001878 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001879 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001880 Categories; Categories = Categories->getNextClassCategory()) {
1881 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Chris Lattner9ef10f42009-03-01 00:56:52 +00001882 ImplMethodsVsClassMethods(CatImplClass, Categories);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001883 break;
1884 }
1885 }
1886 }
1887 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001888 if (isInterfaceDeclKind) {
1889 // Reject invalid vardecls.
1890 for (unsigned i = 0; i != tuvNum; i++) {
1891 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1892 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1893 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00001894 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00001895 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00001896 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001897 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00001898 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001899}
1900
1901
1902/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1903/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00001904static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001905CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1906 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1907 if (PQTVal & ObjCDeclSpec::DQ_In)
1908 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1909 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1910 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1911 if (PQTVal & ObjCDeclSpec::DQ_Out)
1912 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1913 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1914 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1915 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1916 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1917 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1918 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001919
1920 return ret;
1921}
1922
Chris Lattner83f095c2009-03-28 19:18:32 +00001923Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattnerda463fe2007-12-12 07:09:47 +00001924 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001925 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001926 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001927 Selector Sel,
1928 // optional arguments. The number of types/arguments is obtained
1929 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001930 ObjCArgInfo *ArgInfo,
Fariborz Jahaniane84858c2009-01-09 00:38:19 +00001931 llvm::SmallVectorImpl<Declarator> &Cdecls,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001932 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1933 bool isVariadic) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001934 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroff83777fe2008-02-29 21:48:07 +00001935
1936 // Make sure we can establish a context for the method.
1937 if (!ClassDecl) {
1938 Diag(MethodLoc, diag::error_missing_method_context);
Fariborz Jahanianb1771e42009-08-28 17:52:37 +00001939 FunctionLabelMap.clear();
Chris Lattner83f095c2009-03-28 19:18:32 +00001940 return DeclPtrTy();
Steve Naroff83777fe2008-02-29 21:48:07 +00001941 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001942 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00001943
Steve Naroff32606412009-02-20 22:59:16 +00001944 if (ReturnType) {
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +00001945 resultDeclType = GetTypeFromParser(ReturnType);
Mike Stump11289f42009-09-09 15:08:12 +00001946
Steve Naroff32606412009-02-20 22:59:16 +00001947 // Methods cannot return interface types. All ObjC objects are
1948 // passed by reference.
1949 if (resultDeclType->isObjCInterfaceType()) {
Chris Lattnerde5a5312009-04-11 19:08:56 +00001950 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1951 << 0 << resultDeclType;
Chris Lattner83f095c2009-03-28 19:18:32 +00001952 return DeclPtrTy();
Steve Naroff32606412009-02-20 22:59:16 +00001953 }
1954 } else // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001955 resultDeclType = Context.getObjCIdType();
Mike Stump11289f42009-09-09 15:08:12 +00001956
1957 ObjCMethodDecl* ObjCMethod =
Chris Lattner8d8829e2008-03-16 00:49:28 +00001958 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Mike Stump11289f42009-09-09 15:08:12 +00001959 cast<DeclContext>(ClassDecl),
Chris Lattner8d8829e2008-03-16 00:49:28 +00001960 MethodType == tok::minus, isVariadic,
Fariborz Jahanian8983f172008-05-07 20:53:44 +00001961 false,
Mike Stump11289f42009-09-09 15:08:12 +00001962 MethodDeclKind == tok::objc_optional ?
1963 ObjCMethodDecl::Optional :
Chris Lattner8d8829e2008-03-16 00:49:28 +00001964 ObjCMethodDecl::Required);
Mike Stump11289f42009-09-09 15:08:12 +00001965
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001966 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00001967
Chris Lattner23b0faf2009-04-11 19:42:43 +00001968 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00001969 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00001970 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00001971
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001972 if (ArgInfo[i].Type == 0) {
John McCall856bbea2009-10-23 21:48:59 +00001973 ArgType = Context.getObjCIdType();
1974 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001975 } else {
John McCall856bbea2009-10-23 21:48:59 +00001976 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroffb0498ee2008-12-09 19:36:17 +00001977 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattner9713a1c2009-04-11 19:34:56 +00001978 ArgType = adjustParameterType(ArgType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001979 }
Mike Stump11289f42009-09-09 15:08:12 +00001980
John McCall856bbea2009-10-23 21:48:59 +00001981 ParmVarDecl* Param
1982 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1983 ArgInfo[i].Name, ArgType, DI,
1984 VarDecl::None, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001985
Chris Lattner9713a1c2009-04-11 19:34:56 +00001986 if (ArgType->isObjCInterfaceType()) {
1987 Diag(ArgInfo[i].NameLoc,
1988 diag::err_object_cannot_be_passed_returned_by_value)
1989 << 1 << ArgType;
1990 Param->setInvalidDecl();
1991 }
Mike Stump11289f42009-09-09 15:08:12 +00001992
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001993 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001994 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00001995
Chris Lattner9713a1c2009-04-11 19:34:56 +00001996 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00001997 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001998
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001999 Params.push_back(Param);
2000 }
2001
Jay Foad7d0479f2009-05-21 09:52:38 +00002002 ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002003 ObjCMethod->setObjCDeclQualifier(
2004 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
2005 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00002006
2007 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00002008 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00002009
John McCall28a6aea2009-11-04 02:18:39 +00002010 const ObjCMethodDecl *InterfaceMD = 0;
2011
Mike Stump11289f42009-09-09 15:08:12 +00002012 // For implementations (which can be very "coarse grain"), we add the
2013 // method now. This allows the AST to implement lookup methods that work
2014 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattnerda463fe2007-12-12 07:09:47 +00002015 // us to flag multiple declaration errors as they occur.
Mike Stump11289f42009-09-09 15:08:12 +00002016 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner8d8829e2008-03-16 00:49:28 +00002017 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002018 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002019 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2020 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002021 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002022 PrevMethod = ImpDecl->getClassMethod(Sel);
2023 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002024 }
John McCall28a6aea2009-11-04 02:18:39 +00002025 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
2026 MethodType == tok::minus);
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00002027 if (AttrList)
2028 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump11289f42009-09-09 15:08:12 +00002029 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stump12b8ce12009-08-04 21:02:39 +00002030 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002031 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002032 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
2033 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002034 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002035 PrevMethod = CatImpDecl->getClassMethod(Sel);
2036 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002037 }
Fariborz Jahanian2bd617c2009-05-12 21:36:23 +00002038 if (AttrList)
2039 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002040 }
2041 if (PrevMethod) {
2042 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00002043 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002044 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002045 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump11289f42009-09-09 15:08:12 +00002046 }
John McCall28a6aea2009-11-04 02:18:39 +00002047
2048 // If the interface declared this method, and it was deprecated there,
2049 // mark it deprecated here.
2050 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
2051 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
2052
Chris Lattner83f095c2009-03-28 19:18:32 +00002053 return DeclPtrTy::make(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002054}
2055
Mike Stump11289f42009-09-09 15:08:12 +00002056void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002057 SourceLocation Loc,
2058 unsigned &Attributes) {
2059 // FIXME: Improve the reported location.
2060
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00002061 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002062 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00002063 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
2064 ObjCDeclSpec::DQ_PR_assign |
2065 ObjCDeclSpec::DQ_PR_copy |
2066 ObjCDeclSpec::DQ_PR_retain))) {
2067 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
2068 "readwrite" :
2069 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
2070 "assign" :
2071 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
2072 "copy" : "retain";
Mike Stump11289f42009-09-09 15:08:12 +00002073
2074 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Chris Lattner409f5552009-01-29 18:49:48 +00002075 diag::err_objc_property_attr_mutually_exclusive :
2076 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian5a3422f2008-12-06 01:12:43 +00002077 << "readonly" << which;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002078 }
2079
2080 // Check for copy or retain on non-object types.
2081 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
Mike Stump11289f42009-09-09 15:08:12 +00002082 !PropertyTy->isObjCObjectPointerType() &&
2083 !PropertyTy->isBlockPointerType() &&
Steve Naroff79d12152009-07-16 15:41:00 +00002084 !Context.isObjCNSObjectType(PropertyTy)) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002085 Diag(Loc, diag::err_objc_property_requires_object)
2086 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002087 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
2088 }
2089
2090 // Check for more than one of { assign, copy, retain }.
2091 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2092 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002093 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2094 << "assign" << "copy";
2095 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Mike Stump11289f42009-09-09 15:08:12 +00002096 }
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002097 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002098 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2099 << "assign" << "retain";
2100 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002101 }
2102 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2103 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002104 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2105 << "copy" << "retain";
2106 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002107 }
2108 }
2109
2110 // Warn if user supplied no assignment attribute, property is
2111 // readwrite, and this is an object type.
2112 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
2113 ObjCDeclSpec::DQ_PR_retain)) &&
2114 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Steve Naroff79d12152009-07-16 15:41:00 +00002115 PropertyTy->isObjCObjectPointerType()) {
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002116 // Skip this warning in gc-only mode.
Mike Stump11289f42009-09-09 15:08:12 +00002117 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002118 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
2119
2120 // If non-gc code warn that this is likely inappropriate.
2121 if (getLangOptions().getGCMode() == LangOptions::NonGC)
2122 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Mike Stump11289f42009-09-09 15:08:12 +00002123
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002124 // FIXME: Implement warning dependent on NSCopying being
2125 // implemented. See also:
2126 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2127 // (please trim this list while you are at it).
2128 }
Mike Stump5580bdc2009-05-07 23:06:50 +00002129
2130 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2131 && getLangOptions().getGCMode() == LangOptions::GCOnly
2132 && PropertyTy->isBlockPointerType())
2133 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002134}
2135
Mike Stump11289f42009-09-09 15:08:12 +00002136Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00002137 FieldDeclarator &FD,
2138 ObjCDeclSpec &ODS,
2139 Selector GetterSel,
2140 Selector SetterSel,
2141 DeclPtrTy ClassCategory,
2142 bool *isOverridingProperty,
2143 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002144 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002145 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
2146 // default is readwrite!
2147 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Mike Stump11289f42009-09-09 15:08:12 +00002148 // property is defaulted to 'assign' if it is readwrite and is
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002149 // not retain or copy
2150 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Mike Stump11289f42009-09-09 15:08:12 +00002151 (isReadWrite &&
2152 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002153 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
2154 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian00857fc2009-12-16 18:03:30 +00002155 if (T->isReferenceType()) {
2156 Diag(AtLoc, diag::error_reference_property);
2157 return DeclPtrTy();
2158 }
Chris Lattner83f095c2009-03-28 19:18:32 +00002159 Decl *ClassDecl = ClassCategory.getAs<Decl>();
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002160 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002161 // May modify Attributes.
2162 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002163 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanian30a42922010-02-15 21:55:26 +00002164 if (CDecl->IsClassExtension()) {
2165 // Diagnose if this property is already in continuation class.
2166 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2167 assert(DC && "ClassDecl is not a DeclContext");
2168 DeclContext::lookup_result Found = DC->lookup(FD.D.getIdentifier());
2169 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2170 Diag(AtLoc, diag::err_duplicate_property);
2171 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2172 return DeclPtrTy();
2173 }
2174 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
2175 FD.D.getIdentifierLoc(),
2176 FD.D.getIdentifier(),
2177 AtLoc, T);
2178 DC->addDecl(PDecl);
2179
Mike Stump11289f42009-09-09 15:08:12 +00002180 // This is a continuation class. property requires special
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002181 // handling.
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002182 if ((CCPrimary = CDecl->getClassInterface())) {
2183 // Find the property in continuation class's primary class only.
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002184 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniande8db162009-11-02 22:45:15 +00002185 if (ObjCPropertyDecl *PIDecl =
2186 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002187 // property 'PIDecl's readonly attribute will be over-ridden
Fariborz Jahaniane4fd6402009-04-01 23:23:53 +00002188 // with continuation class's readwrite property attribute!
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002189 unsigned PIkind = PIDecl->getPropertyAttributes();
2190 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandc216102009-11-10 19:31:09 +00002191 unsigned retainCopyNonatomic =
Fariborz Jahanian3600f412009-11-06 22:59:12 +00002192 (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahaniana386d952009-11-03 00:01:38 +00002193 ObjCPropertyDecl::OBJC_PR_copy |
2194 ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahaniandc216102009-11-10 19:31:09 +00002195 if ((Attributes & retainCopyNonatomic) !=
2196 (PIkind & retainCopyNonatomic)) {
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002197 Diag(AtLoc, diag::warn_property_attr_mismatch);
Fariborz Jahaniana386d952009-11-03 00:01:38 +00002198 Diag(PIDecl->getLocation(), diag::note_property_declare);
2199 }
Fariborz Jahanian8e356bf2010-01-06 21:38:30 +00002200 DeclContext *DC = dyn_cast<DeclContext>(CCPrimary);
2201 assert(DC && "ClassDecl is not a DeclContext");
2202 DeclContext::lookup_result Found =
2203 DC->lookup(PIDecl->getDeclName());
2204 bool PropertyInPrimaryClass = false;
2205 for (; Found.first != Found.second; ++Found.first)
2206 if (isa<ObjCPropertyDecl>(*Found.first)) {
2207 PropertyInPrimaryClass = true;
2208 break;
2209 }
2210 if (!PropertyInPrimaryClass) {
2211 // Protocol is not in the primary class. Must build one for it.
2212 ObjCDeclSpec ProtocolPropertyODS;
2213 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind and
2214 // ObjCPropertyDecl::PropertyAttributeKind have identical values.
2215 // Should consolidate both into one enum type.
2216 ProtocolPropertyODS.setPropertyAttributes(
2217 (ObjCDeclSpec::ObjCPropertyAttributeKind)PIkind);
2218 DeclPtrTy ProtocolPtrTy =
2219 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
2220 PIDecl->getGetterName(),
2221 PIDecl->getSetterName(),
2222 DeclPtrTy::make(CCPrimary), isOverridingProperty,
2223 MethodImplKind);
2224 PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>();
2225 }
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002226 PIDecl->makeitReadWriteAttribute();
2227 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
2228 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
2229 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
2230 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
2231 PIDecl->setSetterName(SetterSel);
Fariborz Jahaniande8db162009-11-02 22:45:15 +00002232 } else {
Mike Stump11289f42009-09-09 15:08:12 +00002233 Diag(AtLoc, diag::err_use_continuation_class)
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002234 << CCPrimary->getDeclName();
Fariborz Jahaniande8db162009-11-02 22:45:15 +00002235 Diag(PIDecl->getLocation(), diag::note_property_declare);
2236 }
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002237 *isOverridingProperty = true;
Mike Stump11289f42009-09-09 15:08:12 +00002238 // Make sure setter decl is synthesized, and added to primary
Fariborz Jahanian47b21082009-04-15 19:19:03 +00002239 // class's list.
2240 ProcessPropertyDecl(PIDecl, CCPrimary);
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002241 return DeclPtrTy();
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002242 }
Fariborz Jahanian30a42922010-02-15 21:55:26 +00002243
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002244 // No matching property found in the primary class. Just fall thru
2245 // and add property to continuation class's primary class.
2246 ClassDecl = CCPrimary;
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002247 } else {
Chris Lattner68d42c72009-02-20 21:38:52 +00002248 Diag(CDecl->getLocation(), diag::err_continuation_class);
2249 *isOverridingProperty = true;
Chris Lattner83f095c2009-03-28 19:18:32 +00002250 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002251 }
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002252 }
Mike Stump11289f42009-09-09 15:08:12 +00002253
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002254 // Issue a warning if property is 'assign' as default and its object, which is
Mike Stump11289f42009-09-09 15:08:12 +00002255 // gc'able conforms to NSCopying protocol
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002256 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
2257 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
2258 if (T->isObjCObjectPointerType()) {
2259 QualType InterfaceTy = T->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +00002260 if (const ObjCInterfaceType *OIT =
John McCall9dd450b2009-09-21 23:43:11 +00002261 InterfaceTy->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian83b000c2009-08-14 18:06:25 +00002262 ObjCInterfaceDecl *IDecl = OIT->getDecl();
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002263 if (IDecl)
Mike Stump11289f42009-09-09 15:08:12 +00002264 if (ObjCProtocolDecl* PNSCopying =
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002265 LookupProtocol(&Context.Idents.get("NSCopying")))
2266 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Mike Stump11289f42009-09-09 15:08:12 +00002267 Diag(AtLoc, diag::warn_implements_nscopying)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002268 << FD.D.getIdentifier();
Fariborz Jahanian83b000c2009-08-14 18:06:25 +00002269 }
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00002270 }
Fariborz Jahanian887cd6a2009-08-12 18:17:53 +00002271 if (T->isObjCInterfaceType())
2272 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
Mike Stump11289f42009-09-09 15:08:12 +00002273
Steve Naroffba3dc382009-01-11 12:47:58 +00002274 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
2275 assert(DC && "ClassDecl is not a DeclContext");
Chris Lattner5300acc2009-04-11 20:14:49 +00002276 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
Mike Stump11289f42009-09-09 15:08:12 +00002277 FD.D.getIdentifierLoc(),
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00002278 FD.D.getIdentifier(),
2279 AtLoc, T);
Fariborz Jahanian057a17e2009-12-17 00:49:09 +00002280 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
2281 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
2282 Diag(PDecl->getLocation(), diag::err_duplicate_property);
2283 Diag((*Found.first)->getLocation(), diag::note_property_declare);
2284 PDecl->setInvalidDecl();
2285 }
2286 else
2287 DC->addDecl(PDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002288
Chris Lattner5300acc2009-04-11 20:14:49 +00002289 if (T->isArrayType() || T->isFunctionType()) {
2290 Diag(AtLoc, diag::err_property_type) << T;
2291 PDecl->setInvalidDecl();
2292 }
Mike Stump11289f42009-09-09 15:08:12 +00002293
Douglas Gregor758a8692009-06-17 21:51:59 +00002294 ProcessDeclAttributes(S, PDecl, FD.D);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002295
Fariborz Jahanianec6e4c82008-05-07 17:43:59 +00002296 // Regardless of setter/getter attribute, we save the default getter/setter
2297 // selector names in anticipation of declaration of setter/getter methods.
2298 PDecl->setGetterName(GetterSel);
2299 PDecl->setSetterName(SetterSel);
Mike Stump11289f42009-09-09 15:08:12 +00002300
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002301 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002302 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Mike Stump11289f42009-09-09 15:08:12 +00002303
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002304 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002305 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Mike Stump11289f42009-09-09 15:08:12 +00002306
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002307 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002308 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Mike Stump11289f42009-09-09 15:08:12 +00002309
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002310 if (isReadWrite)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002311 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Mike Stump11289f42009-09-09 15:08:12 +00002312
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002313 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002314 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Mike Stump11289f42009-09-09 15:08:12 +00002315
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002316 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002317 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Mike Stump11289f42009-09-09 15:08:12 +00002318
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +00002319 if (isAssign)
2320 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Mike Stump11289f42009-09-09 15:08:12 +00002321
Daniel Dunbare8a06e62008-09-23 21:53:23 +00002322 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002323 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Mike Stump11289f42009-09-09 15:08:12 +00002324
Fariborz Jahanian8d916862008-05-05 18:51:55 +00002325 if (MethodImplKind == tok::objc_required)
2326 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
2327 else if (MethodImplKind == tok::objc_optional)
2328 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Fariborz Jahanian38a5c962009-04-02 18:44:20 +00002329 // A case of continuation class adding a new property in the class. This
2330 // is not what it was meant for. However, gcc supports it and so should we.
2331 // Make sure setter/getters are declared here.
2332 if (CCPrimary)
2333 ProcessPropertyDecl(PDecl, CCPrimary);
Mike Stump11289f42009-09-09 15:08:12 +00002334
Chris Lattner83f095c2009-03-28 19:18:32 +00002335 return DeclPtrTy::make(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002336}
2337
Fariborz Jahanian5db52812010-02-09 21:49:50 +00002338ObjCIvarDecl*
2339Sema::SynthesizeNewPropertyIvar(ObjCInterfaceDecl *IDecl,
2340 IdentifierInfo *NameII) {
2341 ObjCIvarDecl *Ivar = 0;
2342 ObjCPropertyDecl *Prop = LookupPropertyDecl(IDecl, NameII);
2343 if (Prop && !Prop->isInvalidDecl()) {
2344 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
2345 QualType PropType = Context.getCanonicalType(Prop->getType());
2346 assert(EnclosingContext &&
2347 "null DeclContext for synthesized ivar - SynthesizeNewPropertyIvar");
2348 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext,
2349 Prop->getLocation(),
2350 NameII, PropType, /*Dinfo=*/0,
2351 ObjCIvarDecl::Public,
2352 (Expr *)0);
2353 Ivar->setLexicalDeclContext(IDecl);
2354 IDecl->addDecl(Ivar);
2355 Prop->setPropertyIvarDecl(Ivar);
2356 }
2357 return Ivar;
2358}
2359
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002360/// ActOnPropertyImplDecl - This routine performs semantic checks and
2361/// builds the AST node for a property implementation declaration; declared
2362/// as @synthesize or @dynamic.
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002363///
Mike Stump11289f42009-09-09 15:08:12 +00002364Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00002365 SourceLocation PropertyLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002366 bool Synthesize,
Chris Lattner83f095c2009-03-28 19:18:32 +00002367 DeclPtrTy ClassCatImpDecl,
2368 IdentifierInfo *PropertyId,
2369 IdentifierInfo *PropertyIvar) {
2370 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002371 // Make sure we have a context for the property implementation declaration.
2372 if (!ClassImpDecl) {
2373 Diag(AtLoc, diag::error_missing_property_context);
Chris Lattner83f095c2009-03-28 19:18:32 +00002374 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002375 }
2376 ObjCPropertyDecl *property = 0;
2377 ObjCInterfaceDecl* IDecl = 0;
2378 // Find the class or category class where this property must have
2379 // a declaration.
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002380 ObjCImplementationDecl *IC = 0;
2381 ObjCCategoryImplDecl* CatImplClass = 0;
2382 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002383 IDecl = IC->getClassInterface();
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002384 // We always synthesize an interface for an implementation
2385 // without an interface decl. So, IDecl is always non-zero.
Mike Stump11289f42009-09-09 15:08:12 +00002386 assert(IDecl &&
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002387 "ActOnPropertyImplDecl - @implementation without @interface");
Mike Stump11289f42009-09-09 15:08:12 +00002388
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002389 // Look for this property declaration in the @implementation's @interface
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002390 property = IDecl->FindPropertyDeclaration(PropertyId);
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002391 if (!property) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002392 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Chris Lattner83f095c2009-03-28 19:18:32 +00002393 return DeclPtrTy();
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002394 }
Fariborz Jahanianec344ed2009-11-02 18:45:36 +00002395 if (const ObjCCategoryDecl *CD =
2396 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00002397 if (!CD->IsClassExtension()) {
Fariborz Jahanianec344ed2009-11-02 18:45:36 +00002398 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
Fariborz Jahaniande8db162009-11-02 22:45:15 +00002399 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianec344ed2009-11-02 18:45:36 +00002400 return DeclPtrTy();
2401 }
2402 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002403 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002404 if (Synthesize) {
2405 Diag(AtLoc, diag::error_synthesize_category_decl);
Chris Lattner83f095c2009-03-28 19:18:32 +00002406 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002407 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002408 IDecl = CatImplClass->getClassInterface();
2409 if (!IDecl) {
2410 Diag(AtLoc, diag::error_missing_property_interface);
Chris Lattner83f095c2009-03-28 19:18:32 +00002411 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002412 }
Mike Stump11289f42009-09-09 15:08:12 +00002413 ObjCCategoryDecl *Category =
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002414 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
Mike Stump11289f42009-09-09 15:08:12 +00002415
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002416 // If category for this implementation not found, it is an error which
2417 // has already been reported eralier.
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002418 if (!Category)
Chris Lattner83f095c2009-03-28 19:18:32 +00002419 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002420 // Look for this property declaration in @implementation's category
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002421 property = Category->FindPropertyDeclaration(PropertyId);
Fariborz Jahaniana054e992008-04-21 19:04:53 +00002422 if (!property) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002423 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002424 << Category->getDeclName();
Chris Lattner83f095c2009-03-28 19:18:32 +00002425 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002426 }
Chris Lattner83f095c2009-03-28 19:18:32 +00002427 } else {
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002428 Diag(AtLoc, diag::error_bad_property_context);
Chris Lattner83f095c2009-03-28 19:18:32 +00002429 return DeclPtrTy();
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002430 }
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002431 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002432 // Check that we have a valid, previously declared ivar for @synthesize
2433 if (Synthesize) {
2434 // @synthesize
Fariborz Jahanianc6bec7b2008-04-21 21:57:36 +00002435 if (!PropertyIvar)
2436 PropertyIvar = PropertyId;
Fariborz Jahanianb35b4a92009-03-31 00:06:29 +00002437 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002438 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanianb6d5b542009-04-13 19:30:37 +00002439 ObjCInterfaceDecl *ClassDeclared;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002440 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002441 if (!Ivar) {
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002442 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002443 assert(EnclosingContext &&
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002444 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
Mike Stump11289f42009-09-09 15:08:12 +00002445 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
2446 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002447 ObjCIvarDecl::Public,
2448 (Expr *)0);
Fariborz Jahanian28c5a8b2009-06-06 16:36:41 +00002449 Ivar->setLexicalDeclContext(IDecl);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002450 IDecl->addDecl(Ivar);
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002451 property->setPropertyIvarDecl(Ivar);
2452 if (!getLangOptions().ObjCNonFragileABI)
Steve Naroffc03f6b92009-03-03 22:09:41 +00002453 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Mike Stump11289f42009-09-09 15:08:12 +00002454 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002455 // a property implementation and to avoid future warnings.
Mike Stump12b8ce12009-08-04 21:02:39 +00002456 } else if (getLangOptions().ObjCNonFragileABI &&
2457 ClassDeclared != IDecl) {
Fariborz Jahanian68592fc2009-04-30 21:39:24 +00002458 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
Mike Stump11289f42009-09-09 15:08:12 +00002459 << property->getDeclName() << Ivar->getDeclName()
Fariborz Jahanianb6d5b542009-04-13 19:30:37 +00002460 << ClassDeclared->getDeclName();
2461 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
2462 << Ivar << Ivar->getNameAsCString();
2463 // Note! I deliberately want it to fall thru so more errors are caught.
2464 }
Steve Naroff9f9774c2008-09-30 10:07:56 +00002465 QualType IvarType = Context.getCanonicalType(Ivar->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002466
Steve Naroff506e7172008-09-30 00:24:17 +00002467 // Check that type of property and its ivar are type compatible.
Steve Naroff9f9774c2008-09-30 10:07:56 +00002468 if (PropType != IvarType) {
Steve Naroff9546eee2008-10-16 14:59:30 +00002469 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattnerf7e3f6d2008-11-20 06:13:02 +00002470 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002471 << property->getDeclName() << Ivar->getDeclName();
Mike Stump11289f42009-09-09 15:08:12 +00002472 // Note! I deliberately want it to fall thru so, we have a
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002473 // a property implementation and to avoid future warnings.
Steve Naroff9f9774c2008-09-30 10:07:56 +00002474 }
Mike Stump11289f42009-09-09 15:08:12 +00002475
Chris Lattner83f095c2009-03-28 19:18:32 +00002476 // FIXME! Rules for properties are somewhat different that those
2477 // for assignments. Use a new routine to consolidate all cases;
2478 // specifically for property redeclarations as well as for ivars.
2479 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
2480 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
Mike Stump11289f42009-09-09 15:08:12 +00002481 if (lhsType != rhsType &&
Chris Lattner83f095c2009-03-28 19:18:32 +00002482 lhsType->isArithmeticType()) {
2483 Diag(PropertyLoc, diag::error_property_ivar_type)
2484 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002485 // Fall thru - see previous comment
Chris Lattner83f095c2009-03-28 19:18:32 +00002486 }
2487 // __weak is explicit. So it works on Canonical type.
Fariborz Jahanian9ecb84b2009-04-07 21:25:06 +00002488 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
2489 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002490 Diag(PropertyLoc, diag::error_weak_property)
2491 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002492 // Fall thru - see previous comment
Chris Lattner83f095c2009-03-28 19:18:32 +00002493 }
Mike Stump11289f42009-09-09 15:08:12 +00002494 if ((property->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian60331be2009-04-10 22:42:54 +00002495 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
2496 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002497 Diag(PropertyLoc, diag::error_strong_property)
2498 << property->getDeclName() << Ivar->getDeclName();
Mike Stump11289f42009-09-09 15:08:12 +00002499 // Fall thru - see previous comment
Fariborz Jahanian54fa4182009-01-19 20:13:47 +00002500 }
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002501 }
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00002502 } else if (PropertyIvar)
2503 // @dynamic
2504 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002505 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Mike Stump11289f42009-09-09 15:08:12 +00002506 ObjCPropertyImplDecl *PIDecl =
2507 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
2508 property,
2509 (Synthesize ?
2510 ObjCPropertyImplDecl::Synthesize
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002511 : ObjCPropertyImplDecl::Dynamic),
2512 Ivar);
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002513 if (IC) {
2514 if (Synthesize)
Mike Stump11289f42009-09-09 15:08:12 +00002515 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002516 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump11289f42009-09-09 15:08:12 +00002517 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2518 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002519 << PropertyIvar;
2520 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2521 }
Mike Stump11289f42009-09-09 15:08:12 +00002522
2523 if (ObjCPropertyImplDecl *PPIDecl
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002524 = IC->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002525 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2526 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +00002527 return DeclPtrTy();
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002528 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002529 IC->addPropertyImplementation(PIDecl);
Mike Stump12b8ce12009-08-04 21:02:39 +00002530 } else {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002531 if (Synthesize)
Mike Stump11289f42009-09-09 15:08:12 +00002532 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002533 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Mike Stump11289f42009-09-09 15:08:12 +00002534 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
2535 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002536 << PropertyIvar;
2537 Diag(PPIDecl->getLocation(), diag::note_previous_use);
2538 }
Mike Stump11289f42009-09-09 15:08:12 +00002539
2540 if (ObjCPropertyImplDecl *PPIDecl =
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002541 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002542 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
2543 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Chris Lattner83f095c2009-03-28 19:18:32 +00002544 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002545 }
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002546 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002547 }
Mike Stump11289f42009-09-09 15:08:12 +00002548
Chris Lattner83f095c2009-03-28 19:18:32 +00002549 return DeclPtrTy::make(PIDecl);
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002550}
Anders Carlssona6b508a2008-11-04 16:57:32 +00002551
Chris Lattner438e5012008-12-17 07:13:27 +00002552bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00002553 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlssona6b508a2008-11-04 16:57:32 +00002554 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002555
Anders Carlssona6b508a2008-11-04 16:57:32 +00002556 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2557 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00002558
Anders Carlssona6b508a2008-11-04 16:57:32 +00002559 return true;
2560}
Chris Lattner438e5012008-12-17 07:13:27 +00002561
Chris Lattner438e5012008-12-17 07:13:27 +00002562/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2563/// instance variables of ClassName into Decls.
Mike Stump11289f42009-09-09 15:08:12 +00002564void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00002565 IdentifierInfo *ClassName,
Chris Lattner83f095c2009-03-28 19:18:32 +00002566 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00002567 // Check that ClassName is a valid class
2568 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
2569 if (!Class) {
2570 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2571 return;
2572 }
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00002573 if (LangOpts.ObjCNonFragileABI) {
2574 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2575 return;
2576 }
Mike Stump11289f42009-09-09 15:08:12 +00002577
Chris Lattner438e5012008-12-17 07:13:27 +00002578 // Collect the instance variables
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00002579 llvm::SmallVector<FieldDecl*, 32> RecFields;
2580 Context.CollectObjCIvars(Class, RecFields);
2581 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2582 for (unsigned i = 0; i < RecFields.size(); i++) {
2583 FieldDecl* ID = RecFields[i];
2584 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
2585 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
2586 ID->getIdentifier(), ID->getType(),
2587 ID->getBitWidth());
2588 Decls.push_back(Sema::DeclPtrTy::make(FD));
2589 }
Mike Stump11289f42009-09-09 15:08:12 +00002590
Chris Lattner438e5012008-12-17 07:13:27 +00002591 // Introduce all of these fields into the appropriate scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002592 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00002593 D != Decls.end(); ++D) {
Chris Lattner83f095c2009-03-28 19:18:32 +00002594 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattner438e5012008-12-17 07:13:27 +00002595 if (getLangOptions().CPlusPlus)
2596 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattner83f095c2009-03-28 19:18:32 +00002597 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002598 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00002599 }
2600}
2601