blob: 8bcb616285fc3929064601de06f9d2652b7798e9 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregore737f502010-08-12 20:07:10 +000014#include "clang/Sema/Sema.h"
15#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
Steve Naroffca331292009-03-03 14:49:36 +000017#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000021using namespace clang;
22
Steve Naroffebf64432009-02-28 16:59:13 +000023/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000024/// and user declared, in the method definition's AST.
Chris Lattnerb28317a2009-03-28 19:18:32 +000025void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000026 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Chris Lattnerb28317a2009-03-28 19:18:32 +000027 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
Mike Stump1eb44332009-09-09 15:08:12 +000028
Steve Naroff394f3f42008-07-25 17:57:26 +000029 // If we don't have a valid method decl, simply return.
30 if (!MDecl)
31 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000032
33 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000034 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000035 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +000036 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000037 AddFactoryMethodToGlobalPool(MDecl, true);
38
Chris Lattner4d391482007-12-12 07:09:47 +000039 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000040 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +000041 PushFunctionScope();
42
Chris Lattner4d391482007-12-12 07:09:47 +000043 // Create Decl objects for each parameter, entrring them in the scope for
44 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000045
46 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000047 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +000048
Daniel Dunbar451318c2008-08-26 06:07:48 +000049 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
50 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000051
Chris Lattner8123a952008-04-10 02:22:51 +000052 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000053 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
54 E = MDecl->param_end(); PI != E; ++PI)
55 if ((*PI)->getIdentifier())
56 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000057}
58
Chris Lattnerb28317a2009-03-28 19:18:32 +000059Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000060ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +000063 const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +000064 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000065 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000066 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000067
Chris Lattner4d391482007-12-12 07:09:47 +000068 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +000069 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +000070 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +000071
Ted Kremeneka526c5c2008-01-07 19:49:32 +000072 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000073 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000074 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000075 }
Mike Stump1eb44332009-09-09 15:08:12 +000076
Douglas Gregordeacbdc2010-08-11 12:19:30 +000077 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
78 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +000079 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +000080 if (!IDecl->isForwardDecl()) {
81 IDecl->setInvalidDecl();
82 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
83 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +000084
Douglas Gregordeacbdc2010-08-11 12:19:30 +000085 // Return the previous class interface.
86 // FIXME: don't leak the objects passed in!
87 return DeclPtrTy::make(IDecl);
88 } else {
89 IDecl->setLocation(AtInterfaceLoc);
90 IDecl->setForwardDecl(false);
91 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +000092 // If the forward decl was in a PCH, we need to write it again in a
93 // chained PCH.
94 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +000095
96 // Since this ObjCInterfaceDecl was created by a forward declaration,
97 // we now add it to the DeclContext since it wasn't added before
98 // (see ActOnForwardClassDeclaration).
99 IDecl->setLexicalDeclContext(CurContext);
100 CurContext->addDecl(IDecl);
101
102 if (AttrList)
103 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000104 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000105 } else {
106 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
107 ClassName, ClassLoc);
108 if (AttrList)
109 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
110
111 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000112 }
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Chris Lattner4d391482007-12-12 07:09:47 +0000114 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000115 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000116 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
117 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000118
119 if (!PrevDecl) {
120 // Try to correct for a typo in the superclass name.
121 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000122 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000123 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
124 Diag(SuperLoc, diag::err_undef_superclass_suggest)
125 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000126 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
127 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000128 }
129 }
130
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000131 if (PrevDecl == IDecl) {
132 Diag(SuperLoc, diag::err_recursive_superclass)
133 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
134 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000135 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000136 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000137 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000138
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000139 // Diagnose classes that inherit from deprecated classes.
140 if (SuperClassDecl)
141 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000143 if (PrevDecl && SuperClassDecl == 0) {
144 // The previous declaration was not a class decl. Check if we have a
145 // typedef. If we do, get the underlying class type.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000146 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000147 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000148 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000149 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
150 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000151 }
152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000154 // This handles the following case:
155 //
156 // typedef int SuperClass;
157 // @interface MyClass : SuperClass {} @end
158 //
159 if (!SuperClassDecl) {
160 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
161 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000162 }
163 }
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000165 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
166 if (!SuperClassDecl)
167 Diag(SuperLoc, diag::err_undef_superclass)
168 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
169 else if (SuperClassDecl->isForwardDecl())
170 Diag(SuperLoc, diag::err_undef_superclass)
171 << SuperClassDecl->getDeclName() << ClassName
172 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000173 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000174 IDecl->setSuperClass(SuperClassDecl);
175 IDecl->setSuperClassLoc(SuperLoc);
176 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000177 }
Chris Lattner4d391482007-12-12 07:09:47 +0000178 } else { // we have a root class.
179 IDecl->setLocEnd(ClassLoc);
180 }
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Sebastian Redl0b17c612010-08-13 00:28:03 +0000182 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000183 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000184 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000185 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000186 IDecl->setLocEnd(EndProtoLoc);
187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Anders Carlsson15281452008-11-04 16:57:32 +0000189 CheckObjCDeclScope(IDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000190 return DeclPtrTy::make(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000191}
192
193/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000194/// @compatibility_alias declaration. It sets up the alias relationships.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000195Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000196 IdentifierInfo *AliasName,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000197 SourceLocation AliasLocation,
198 IdentifierInfo *ClassName,
199 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000200 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000201 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000202 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000203 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000204 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000205 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000206 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000207 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000208 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000209 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000210 }
211 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000212 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000213 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000214 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
215 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000216 if (T->isObjCObjectType()) {
217 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000218 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000219 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000220 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000221 }
222 }
223 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000224 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
225 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000226 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000227 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000228 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000229 return DeclPtrTy();
Chris Lattner4d391482007-12-12 07:09:47 +0000230 }
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000232 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000233 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000234 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Anders Carlsson15281452008-11-04 16:57:32 +0000236 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000237 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000238
Chris Lattnerb28317a2009-03-28 19:18:32 +0000239 return DeclPtrTy::make(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000240}
241
Steve Naroff61d68522009-03-05 15:22:01 +0000242void Sema::CheckForwardProtocolDeclarationForCircularDependency(
243 IdentifierInfo *PName,
244 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000245 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000246 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
247 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Douglas Gregorc83c6872010-04-15 22:33:43 +0000249 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
250 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000251 if (PDecl->getIdentifier() == PName) {
252 Diag(Ploc, diag::err_protocol_has_circular_dependency);
253 Diag(PrevLoc, diag::note_previous_definition);
254 }
Mike Stump1eb44332009-09-09 15:08:12 +0000255 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000256 PDecl->getLocation(), PDecl->getReferencedProtocols());
257 }
258 }
259}
260
Chris Lattnerb28317a2009-03-28 19:18:32 +0000261Sema::DeclPtrTy
Chris Lattnere13b9592008-07-26 04:03:38 +0000262Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
263 IdentifierInfo *ProtocolName,
264 SourceLocation ProtocolLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000265 const DeclPtrTy *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000266 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000267 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000268 SourceLocation EndProtoLoc,
269 AttributeList *AttrList) {
270 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000271 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000272 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000273 if (PDecl) {
274 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000275 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000276 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000277 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000278 // Just return the protocol we already had.
279 // FIXME: don't leak the objects passed in!
Chris Lattnerb28317a2009-03-28 19:18:32 +0000280 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000281 }
Steve Naroff61d68522009-03-05 15:22:01 +0000282 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000283 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000284 CheckForwardProtocolDeclarationForCircularDependency(
285 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Steve Narofff11b5082008-08-13 16:39:22 +0000287 // Make sure the cached decl gets a valid start location.
288 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000289 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000290 CurContext->addDecl(PDecl);
291 // Repeat in dependent PCHs.
292 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000293 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000294 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000295 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000296 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000297 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000298 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000299 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000300 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000301 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000302 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000303 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
304 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000305 PDecl->setLocEnd(EndProtoLoc);
306 }
Mike Stump1eb44332009-09-09 15:08:12 +0000307
308 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000309 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000310}
311
312/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000313/// issues an error if they are not declared. It returns list of
314/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000315void
Chris Lattnere13b9592008-07-26 04:03:38 +0000316Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000317 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000318 unsigned NumProtocols,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000319 llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000320 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000321 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
322 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000323 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000324 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
325 LookupObjCProtocolName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000326 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000327 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
328 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
329 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000330 Diag(PDecl->getLocation(), diag::note_previous_decl)
331 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000332 }
333 }
334
335 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000336 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000337 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000338 continue;
339 }
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000341 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000342
343 // If this is a forward declaration and we are supposed to warn in this
344 // case, do it.
345 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000346 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000347 << ProtocolId[i].first;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000348 Protocols.push_back(DeclPtrTy::make(PDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000349 }
350}
351
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000352/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000353/// a class method in its extension.
354///
Mike Stump1eb44332009-09-09 15:08:12 +0000355void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000356 ObjCInterfaceDecl *ID) {
357 if (!ID)
358 return; // Possibly due to previous error
359
360 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000361 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
362 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000363 ObjCMethodDecl *MD = *i;
364 MethodMap[MD->getSelector()] = MD;
365 }
366
367 if (MethodMap.empty())
368 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000369 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
370 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000371 ObjCMethodDecl *Method = *i;
372 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
373 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
374 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
375 << Method->getDeclName();
376 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
377 }
378 }
379}
380
Chris Lattner58fe03b2009-04-12 08:43:13 +0000381/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000382Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000383Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000384 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000385 unsigned NumElts,
386 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000387 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000388 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattner4d391482007-12-12 07:09:47 +0000390 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000391 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000392 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000393 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000394 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000395 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000396 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000397 PushOnScopeChains(PDecl, TUScope, false);
398 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000399 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000400 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000401 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000402 if (!isNew)
403 PDecl->setChangedSinceDeserialization(true);
404 }
Chris Lattner4d391482007-12-12 07:09:47 +0000405 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000406 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000407 }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
409 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000410 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000411 Protocols.data(), Protocols.size(),
412 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000413 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000414 CheckObjCDeclScope(PDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000415 return DeclPtrTy::make(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000416}
417
Chris Lattnerb28317a2009-03-28 19:18:32 +0000418Sema::DeclPtrTy Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000419ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
420 IdentifierInfo *ClassName, SourceLocation ClassLoc,
421 IdentifierInfo *CategoryName,
422 SourceLocation CategoryLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000423 const DeclPtrTy *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000424 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000425 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000426 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000427 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000428 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000429
430 /// Check that class of this category is already completely declared.
431 if (!IDecl || IDecl->isForwardDecl()) {
432 // Create an invalid ObjCCategoryDecl to serve as context for
433 // the enclosing method declarations. We mark the decl invalid
434 // to make it clear that this isn't a valid AST.
435 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
436 ClassLoc, CategoryLoc, CategoryName);
437 CDecl->setInvalidDecl();
438 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
439 return DeclPtrTy::make(CDecl);
440 }
441
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000442 if (!CategoryName && IDecl->getImplementation()) {
443 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
444 Diag(IDecl->getImplementation()->getLocation(),
445 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000446 }
447
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000448 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
449 ClassLoc, CategoryLoc, CategoryName);
450 // FIXME: PushOnScopeChains?
451 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000452
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000453 CDecl->setClassInterface(IDecl);
454 // Insert class extension to the list of class's categories.
455 if (!CategoryName)
456 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Chris Lattner16b34b42009-02-16 21:30:01 +0000458 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000459 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000460
Fariborz Jahanian25760612010-02-15 21:55:26 +0000461 if (CategoryName) {
462 /// Check for duplicate interface declaration for this category
463 ObjCCategoryDecl *CDeclChain;
464 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
465 CDeclChain = CDeclChain->getNextClassCategory()) {
466 if (CDeclChain->getIdentifier() == CategoryName) {
467 // Class extensions can be declared multiple times.
468 Diag(CategoryLoc, diag::warn_dup_category_def)
469 << ClassName << CategoryName;
470 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
471 break;
472 }
Chris Lattner70f19542009-02-16 21:26:43 +0000473 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000474 if (!CDeclChain)
475 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000476 }
Chris Lattner70f19542009-02-16 21:26:43 +0000477
Chris Lattner4d391482007-12-12 07:09:47 +0000478 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000479 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000480 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000481 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000482 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000483 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000484 NumProtoRefs, ProtoLocs,
485 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Anders Carlsson15281452008-11-04 16:57:32 +0000488 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000489 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000490}
491
492/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000493/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000494/// object.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000495Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000496 SourceLocation AtCatImplLoc,
497 IdentifierInfo *ClassName, SourceLocation ClassLoc,
498 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000499 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000500 ObjCCategoryDecl *CatIDecl = 0;
501 if (IDecl) {
502 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
503 if (!CatIDecl) {
504 // Category @implementation with no corresponding @interface.
505 // Create and install one.
506 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000507 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000508 CatName);
509 CatIDecl->setClassInterface(IDecl);
510 CatIDecl->insertNextClassCategory();
511 }
512 }
513
Mike Stump1eb44332009-09-09 15:08:12 +0000514 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000515 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
516 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000517 /// Check that class of this category is already completely declared.
518 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000519 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000520
Douglas Gregord0434102009-01-09 00:49:46 +0000521 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000522 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000523
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000524 /// Check that CatName, category name, is not used in another implementation.
525 if (CatIDecl) {
526 if (CatIDecl->getImplementation()) {
527 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
528 << CatName;
529 Diag(CatIDecl->getImplementation()->getLocation(),
530 diag::note_previous_definition);
531 } else
532 CatIDecl->setImplementation(CDecl);
533 }
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Anders Carlsson15281452008-11-04 16:57:32 +0000535 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000536 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000537}
538
Chris Lattnerb28317a2009-03-28 19:18:32 +0000539Sema::DeclPtrTy Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000540 SourceLocation AtClassImplLoc,
541 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000542 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000543 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000544 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000545 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000546 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000547 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
548 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000549 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000550 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000551 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000552 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
553 // If this is a forward declaration of an interface, warn.
554 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000555 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000556 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000557 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000558 } else {
559 // We did not find anything with the name ClassName; try to correct for
560 // typos in the class name.
561 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000562 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor95ff7422010-01-04 17:27:12 +0000563 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000564 // Suggest the (potentially) correct interface name. However, put the
565 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000566 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000567 // provide a code-modification hint or use the typo name for recovery,
568 // because this is just a warning. The program may actually be correct.
569 Diag(ClassLoc, diag::warn_undef_interface_suggest)
570 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000571 Diag(IDecl->getLocation(), diag::note_previous_decl)
572 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000573 << FixItHint::CreateReplacement(ClassLoc,
574 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000575 IDecl = 0;
576 } else {
577 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
578 }
Chris Lattner4d391482007-12-12 07:09:47 +0000579 }
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Chris Lattner4d391482007-12-12 07:09:47 +0000581 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000582 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000583 if (SuperClassname) {
584 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000585 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
586 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000587 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000588 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
589 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000590 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000591 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000592 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000593 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000594 Diag(SuperClassLoc, diag::err_undef_superclass)
595 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000596 else if (IDecl && IDecl->getSuperClass() != SDecl) {
597 // This implementation and its interface do not have the same
598 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000599 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000600 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000601 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000602 }
603 }
604 }
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Chris Lattner4d391482007-12-12 07:09:47 +0000606 if (!IDecl) {
607 // Legacy case of @implementation with no corresponding @interface.
608 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000609
Mike Stump390b4cc2009-05-16 07:39:55 +0000610 // FIXME: Do we support attributes on the @implementation? If so we should
611 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000612 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000613 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000614 IDecl->setSuperClass(SDecl);
615 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000616
617 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000618 } else {
619 // Mark the interface as being completed, even if it was just as
620 // @class ....;
621 // declaration; the user cannot reopen it.
622 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000623 }
Mike Stump1eb44332009-09-09 15:08:12 +0000624
625 ObjCImplementationDecl* IMPDecl =
626 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000627 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Anders Carlsson15281452008-11-04 16:57:32 +0000629 if (CheckObjCDeclScope(IMPDecl))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000630 return DeclPtrTy::make(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Chris Lattner4d391482007-12-12 07:09:47 +0000632 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000633 if (IDecl->getImplementation()) {
634 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000635 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000636 Diag(IDecl->getImplementation()->getLocation(),
637 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000638 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000639 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000640 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000641 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000642 return DeclPtrTy::make(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000643}
644
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000645void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
646 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000647 SourceLocation RBrace) {
648 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000649 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000650 if (!IDecl)
651 return;
652 /// Check case of non-existing @interface decl.
653 /// (legacy objective-c @implementation decl without an @interface decl).
654 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000655 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000656 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000657 // Add ivar's to class's DeclContext.
658 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000659 ivars[i]->setLexicalDeclContext(ImpDecl);
660 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000661 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000662 }
663
Chris Lattner4d391482007-12-12 07:09:47 +0000664 return;
665 }
666 // If implementation has empty ivar list, just return.
667 if (numIvars == 0)
668 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Chris Lattner4d391482007-12-12 07:09:47 +0000670 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000671 if (LangOpts.ObjCNonFragileABI2) {
672 if (ImpDecl->getSuperClass())
673 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
674 for (unsigned i = 0; i < numIvars; i++) {
675 ObjCIvarDecl* ImplIvar = ivars[i];
676 if (const ObjCIvarDecl *ClsIvar =
677 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
678 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
679 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
680 continue;
681 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000682 // Instance ivar to Implementation's DeclContext.
683 ImplIvar->setLexicalDeclContext(ImpDecl);
684 IDecl->makeDeclVisibleInContext(ImplIvar, false);
685 ImpDecl->addDecl(ImplIvar);
686 }
687 return;
688 }
Chris Lattner4d391482007-12-12 07:09:47 +0000689 // Check interface's Ivar list against those in the implementation.
690 // names and types must match.
691 //
Chris Lattner4d391482007-12-12 07:09:47 +0000692 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000693 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000694 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
695 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000696 ObjCIvarDecl* ImplIvar = ivars[j++];
697 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000698 assert (ImplIvar && "missing implementation ivar");
699 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Steve Naroffca331292009-03-03 14:49:36 +0000701 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000702 if (Context.getCanonicalType(ImplIvar->getType()) !=
703 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000704 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000705 << ImplIvar->getIdentifier()
706 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000707 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000708 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
709 Expr *ImplBitWidth = ImplIvar->getBitWidth();
710 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000711 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
712 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000713 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
714 << ImplIvar->getIdentifier();
715 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
716 }
Mike Stump1eb44332009-09-09 15:08:12 +0000717 }
Steve Naroffca331292009-03-03 14:49:36 +0000718 // Make sure the names are identical.
719 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000720 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000721 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000722 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000723 }
724 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000725 }
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Chris Lattner609e4c72007-12-12 18:11:49 +0000727 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000728 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000729 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000730 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000731}
732
Steve Naroff3c2eb662008-02-10 21:38:56 +0000733void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000734 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000735 if (!IncompleteImpl) {
736 Diag(ImpLoc, diag::warn_incomplete_impl);
737 IncompleteImpl = true;
738 }
Fariborz Jahanian52146832010-03-31 18:23:33 +0000739 Diag(method->getLocation(), DiagID)
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000740 << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000741}
742
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000743void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
744 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000745 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000746 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000747 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
748 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000749 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000750 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
751 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000752 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
753 }
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Chris Lattner3aff9192009-04-11 19:58:42 +0000755 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
756 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
757 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000758 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
759 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
760 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
761 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000762 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000763
764 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000765 << ImpMethodDecl->getDeclName() << (*IF)->getType()
766 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000767 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000768 }
Fariborz Jahanian561da7e2010-05-21 23:28:58 +0000769 if (ImpMethodDecl->isVariadic() != IntfMethodDecl->isVariadic()) {
770 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
771 Diag(IntfMethodDecl->getLocation(), diag::note_previous_declaration);
772 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000773}
774
Mike Stump390b4cc2009-05-16 07:39:55 +0000775/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
776/// improve the efficiency of selector lookups and type checking by associating
777/// with each protocol / interface / category the flattened instance tables. If
778/// we used an immutable set to keep the table then it wouldn't add significant
779/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000780
Steve Naroffefe7f362008-02-08 22:06:17 +0000781/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000782/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000783void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
784 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000785 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000786 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000787 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000788 ObjCContainerDecl *CDecl) {
789 ObjCInterfaceDecl *IDecl;
790 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
791 IDecl = C->getClassInterface();
792 else
793 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
794 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
795
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000796 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000797 ObjCInterfaceDecl *NSIDecl = 0;
798 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000799 // check to see if class implements forwardInvocation method and objects
800 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000801 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000802 // Under such conditions, which means that every method possible is
803 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000804 // found" warnings.
805 // FIXME: Use a general GetUnarySelector method for this.
806 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
807 Selector fISelector = Context.Selectors.getSelector(1, &II);
808 if (InsMap.count(fISelector))
809 // Is IDecl derived from 'NSProxy'? If so, no instance methods
810 // need be implemented in the implementation.
811 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
812 }
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000814 // If a method lookup fails locally we still need to look and see if
815 // the method was implemented by a base class or an inherited
816 // protocol. This lookup is slow, but occurs rarely in correct code
817 // and otherwise would terminate in a warning.
818
Chris Lattner4d391482007-12-12 07:09:47 +0000819 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000820 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000821 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000822 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000823 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000824 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000825 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000826 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000827 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000828 // Ugly, but necessary. Method declared in protcol might have
829 // have been synthesized due to a property declared in the class which
830 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000831 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000832 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000833 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000834 unsigned DIAG = diag::warn_unimplemented_protocol_method;
835 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
836 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
837 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
838 << PDecl->getDeclName();
839 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000840 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000841 }
842 }
Chris Lattner4d391482007-12-12 07:09:47 +0000843 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000844 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000845 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000846 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000847 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000848 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
849 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000850 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000851 unsigned DIAG = diag::warn_unimplemented_protocol_method;
852 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
853 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
854 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
855 PDecl->getDeclName();
856 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000857 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000858 }
Chris Lattner780f3292008-07-21 21:32:27 +0000859 // Check on this protocols's referenced protocols, recursively.
860 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
861 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000862 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000863}
864
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000865/// MatchAllMethodDeclarations - Check methods declaraed in interface or
866/// or protocol against those declared in their implementations.
867///
868void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
869 const llvm::DenseSet<Selector> &ClsMap,
870 llvm::DenseSet<Selector> &InsMapSeen,
871 llvm::DenseSet<Selector> &ClsMapSeen,
872 ObjCImplDecl* IMPDecl,
873 ObjCContainerDecl* CDecl,
874 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +0000875 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000876 // Check and see if instance methods in class interface have been
877 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000878 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
879 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000880 if (InsMapSeen.count((*I)->getSelector()))
881 continue;
882 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000883 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000884 !InsMap.count((*I)->getSelector())) {
885 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +0000886 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
887 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000888 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000889 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000890 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000891 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000892 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000893 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000894 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000895 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
896 // ImpMethodDecl may be null as in a @dynamic property.
897 if (ImpMethodDecl)
898 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
899 }
900 }
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000902 // Check and see if class methods in class interface have been
903 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +0000904 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000905 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000906 if (ClsMapSeen.count((*I)->getSelector()))
907 continue;
908 ClsMapSeen.insert((*I)->getSelector());
909 if (!ClsMap.count((*I)->getSelector())) {
910 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +0000911 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
912 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000913 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000914 ObjCMethodDecl *ImpMethodDecl =
915 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000916 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000917 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000918 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
919 }
920 }
921 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
922 // Check for any implementation of a methods declared in protocol.
923 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
924 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +0000925 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
926 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000927 (*PI), IncompleteImpl, false);
928 if (I->getSuperClass())
929 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +0000930 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000931 I->getSuperClass(), IncompleteImpl, false);
932 }
933}
934
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000935void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000936 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +0000937 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000938 llvm::DenseSet<Selector> InsMap;
939 // Check and see if instance methods in class interface have been
940 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +0000941 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000942 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000943 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000945 // Check and see if properties declared in the interface have either 1)
946 // an implementation or 2) there is a @synthesize/@dynamic implementation
947 // of the property in the @implementation.
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000948 if (isa<ObjCInterfaceDecl>(CDecl) && !LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000949 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +0000950
Chris Lattner4d391482007-12-12 07:09:47 +0000951 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +0000952 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000953 I = IMPDecl->classmeth_begin(),
954 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000955 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000957 // Check for type conflict of methods declared in a class/protocol and
958 // its implementation; if any.
959 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +0000960 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
961 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000962 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Chris Lattner4d391482007-12-12 07:09:47 +0000964 // Check the protocol list for unimplemented methods in the @implementation
965 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000966 // Check and see if class methods in class interface have been
967 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Chris Lattnercddc8882009-03-01 00:56:52 +0000969 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000970 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +0000971 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +0000972 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +0000973 InsMap, ClsMap, I);
974 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000975 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
976 Categories; Categories = Categories->getNextClassExtension())
977 ImplMethodsVsClassMethods(S, IMPDecl,
978 const_cast<ObjCCategoryDecl*>(Categories),
979 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +0000980 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000981 // For extended class, unimplemented methods in its protocols will
982 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000983 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000984 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
985 E = C->protocol_end(); PI != E; ++PI)
986 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000987 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +0000988 // Report unimplemented properties in the category as well.
989 // When reporting on missing setter/getters, do not report when
990 // setter/getter is implemented in category's primary class
991 // implementation.
992 if (ObjCInterfaceDecl *ID = C->getClassInterface())
993 if (ObjCImplDecl *IMP = ID->getImplementation()) {
994 for (ObjCImplementationDecl::instmeth_iterator
995 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
996 InsMap.insert((*I)->getSelector());
997 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000998 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +0000999 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001000 } else
1001 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001002}
1003
Mike Stump1eb44332009-09-09 15:08:12 +00001004/// ActOnForwardClassDeclaration -
Chris Lattnerb28317a2009-03-28 19:18:32 +00001005Action::DeclPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001006Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001007 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001008 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001009 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001010 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Chris Lattner4d391482007-12-12 07:09:47 +00001012 for (unsigned i = 0; i != NumElts; ++i) {
1013 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001014 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001015 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001016 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001017 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001018 // Maybe we will complain about the shadowed template parameter.
1019 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1020 // Just pretend that we didn't see the previous declaration.
1021 PrevDecl = 0;
1022 }
1023
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001024 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001025 // GCC apparently allows the following idiom:
1026 //
1027 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1028 // @class XCElementToggler;
1029 //
Mike Stump1eb44332009-09-09 15:08:12 +00001030 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001031 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001032 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001033 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001034 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001035 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001036 // a forward class declaration matching a typedef name of a class refers
1037 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001038 if (const ObjCObjectType *OI =
1039 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1040 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001041 }
Chris Lattner4d391482007-12-12 07:09:47 +00001042 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001043 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1044 if (!IDecl) { // Not already seen? Make a forward decl.
1045 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1046 IdentList[i], IdentLocs[i], true);
1047
1048 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1049 // the current DeclContext. This prevents clients that walk DeclContext
1050 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1051 // declared later (if at all). We also take care to explicitly make
1052 // sure this declaration is visible for name lookup.
1053 PushOnScopeChains(IDecl, TUScope, false);
1054 CurContext->makeDeclVisibleInContext(IDecl, true);
1055 }
Chris Lattner4d391482007-12-12 07:09:47 +00001056
1057 Interfaces.push_back(IDecl);
1058 }
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Ted Kremenek321c22f2009-11-18 00:28:11 +00001060 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001061 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001062 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001063 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001064 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001065 CheckObjCDeclScope(CDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001066 return DeclPtrTy::make(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001067}
1068
1069
1070/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1071/// returns true, or false, accordingly.
1072/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001073bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001074 const ObjCMethodDecl *PrevMethod,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001075 bool matchBasedOnSizeAndAlignment,
1076 bool matchBasedOnStrictEqulity) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001077 QualType T1 = Context.getCanonicalType(Method->getResultType());
1078 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001080 if (T1 != T2) {
1081 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001082 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Chris Lattner4d391482007-12-12 07:09:47 +00001083 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001084 // Incomplete types don't have a size and alignment.
1085 if (T1->isIncompleteType() || T2->isIncompleteType())
1086 return false;
1087 // Check is based on size and alignment.
1088 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1089 return false;
1090 }
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Chris Lattner89951a82009-02-20 18:43:26 +00001092 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1093 E = Method->param_end();
1094 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Chris Lattner89951a82009-02-20 18:43:26 +00001096 for (; ParamI != E; ++ParamI, ++PrevI) {
1097 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1098 T1 = Context.getCanonicalType((*ParamI)->getType());
1099 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001100 if (T1 != T2) {
1101 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001102 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001103 return false;
1104 // Incomplete types don't have a size and alignment.
1105 if (T1->isIncompleteType() || T2->isIncompleteType())
1106 return false;
1107 // Check is based on size and alignment.
1108 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1109 return false;
1110 }
Chris Lattner4d391482007-12-12 07:09:47 +00001111 }
1112 return true;
1113}
1114
Sebastian Redldb9d2142010-08-02 23:18:59 +00001115/// \brief Read the contents of the method pool for a given selector from
1116/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001117///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001118/// This routine should only be called once, when the method pool has no entry
1119/// for this selector.
1120Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001121 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001122 assert(MethodPool.find(Sel) == MethodPool.end() &&
1123 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001124
1125 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001126 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Sebastian Redldb9d2142010-08-02 23:18:59 +00001128 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001129}
1130
Sebastian Redldb9d2142010-08-02 23:18:59 +00001131void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1132 bool instance) {
1133 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1134 if (Pos == MethodPool.end()) {
1135 if (ExternalSource)
1136 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001137 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001138 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1139 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001140 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001141 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001142 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001143 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001144 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001145 Entry.Method = Method;
1146 Entry.Next = 0;
1147 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001148 }
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Chris Lattnerb25df352009-03-04 05:16:45 +00001150 // We've seen a method with this name, see if we have already seen this type
1151 // signature.
1152 for (ObjCMethodList *List = &Entry; List; List = List->Next)
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001153 if (MatchTwoMethodDeclarations(Method, List->Method)) {
1154 List->Method->setDefined(impl);
Chris Lattnerb25df352009-03-04 05:16:45 +00001155 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001156 }
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Chris Lattnerb25df352009-03-04 05:16:45 +00001158 // We have a new signature for an existing method - add it.
1159 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001160 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1161 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001162}
1163
Sebastian Redldb9d2142010-08-02 23:18:59 +00001164ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001165 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001166 bool warn, bool instance) {
1167 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1168 if (Pos == MethodPool.end()) {
1169 if (ExternalSource)
1170 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001171 else
1172 return 0;
1173 }
1174
Sebastian Redldb9d2142010-08-02 23:18:59 +00001175 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001177 bool strictSelectorMatch = receiverIdOrClass && warn &&
1178 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl) !=
1179 Diagnostic::Ignored);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001180 if (warn && MethList.Method && MethList.Next) {
1181 bool issueWarning = false;
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001182 if (strictSelectorMatch)
1183 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1184 // This checks if the methods differ in type mismatch.
1185 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true))
1186 issueWarning = true;
1187 }
1188
1189 if (!issueWarning)
1190 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1191 // This checks if the methods differ by size & alignment.
1192 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1193 issueWarning = true;
1194 }
1195
Sebastian Redldb9d2142010-08-02 23:18:59 +00001196 if (issueWarning) {
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001197 if (strictSelectorMatch)
1198 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1199 else
1200 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Sebastian Redldb9d2142010-08-02 23:18:59 +00001201 Diag(MethList.Method->getLocStart(), diag::note_using)
1202 << MethList.Method->getSourceRange();
1203 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1204 Diag(Next->Method->getLocStart(), diag::note_also_found)
1205 << Next->Method->getSourceRange();
1206 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001207 }
1208 return MethList.Method;
1209}
1210
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001211ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001212 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1213 if (Pos == MethodPool.end())
1214 return 0;
1215
1216 GlobalMethods &Methods = Pos->second;
1217
1218 if (Methods.first.Method && Methods.first.Method->isDefined())
1219 return Methods.first.Method;
1220 if (Methods.second.Method && Methods.second.Method->isDefined())
1221 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001222 return 0;
1223}
1224
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001225/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1226/// identical selector names in current and its super classes and issues
1227/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001228void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1229 ObjCMethodDecl *Method,
1230 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001231 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1232 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001234 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001235 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001236 SD->lookupMethod(Method->getSelector(), IsInstance);
1237 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001238 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001239 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001240 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001241 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1242 E = Method->param_end();
1243 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1244 for (; ParamI != E; ++ParamI, ++PrevI) {
1245 // Number of parameters are the same and is guaranteed by selector match.
1246 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1247 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1248 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1249 // If type of arguement of method in this class does not match its
1250 // respective argument type in the super class method, issue warning;
1251 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001252 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001253 << T1 << T2;
1254 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1255 return;
1256 }
1257 }
1258 ID = SD;
1259 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001260}
1261
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001262/// DiagnoseDuplicateIvars -
1263/// Check for duplicate ivars in the entire class at the start of
1264/// @implementation. This becomes necesssary because class extension can
1265/// add ivars to a class in random order which will not be known until
1266/// class's @implementation is seen.
1267void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1268 ObjCInterfaceDecl *SID) {
1269 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1270 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1271 ObjCIvarDecl* Ivar = (*IVI);
1272 if (Ivar->isInvalidDecl())
1273 continue;
1274 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1275 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1276 if (prevIvar) {
1277 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1278 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1279 Ivar->setInvalidDecl();
1280 }
1281 }
1282 }
1283}
1284
Steve Naroffa56f6162007-12-18 01:30:32 +00001285// Note: For class/category implemenations, allMethods/allProperties is
1286// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001287void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
Ted Kremenek782f2f52010-01-07 01:20:12 +00001288 DeclPtrTy classDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001289 DeclPtrTy *allMethods, unsigned allNum,
1290 DeclPtrTy *allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001291 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001292 Decl *ClassDecl = classDecl.getAs<Decl>();
Chris Lattner4d391482007-12-12 07:09:47 +00001293
Steve Naroffa56f6162007-12-18 01:30:32 +00001294 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1295 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001296 // should be true.
1297 if (!ClassDecl)
1298 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001299
Mike Stump1eb44332009-09-09 15:08:12 +00001300 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001301 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1302 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001303 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001304
Ted Kremenek782f2f52010-01-07 01:20:12 +00001305 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1306 // FIXME: This is wrong. We shouldn't be pretending that there is
1307 // an '@end' in the declaration.
1308 SourceLocation L = ClassDecl->getLocation();
1309 AtEnd.setBegin(L);
1310 AtEnd.setEnd(L);
1311 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001312 }
1313
Steve Naroff0701bbb2009-01-08 17:28:14 +00001314 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001315
1316 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1317 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1318 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1319
Chris Lattner4d391482007-12-12 07:09:47 +00001320 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001321 ObjCMethodDecl *Method =
Chris Lattnerb28317a2009-03-28 19:18:32 +00001322 cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
Chris Lattner4d391482007-12-12 07:09:47 +00001323
1324 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001325 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001326 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001327 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001328 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001329 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001330 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001331 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001332 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001333 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001334 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001335 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001336 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001337 InsMap[Method->getSelector()] = Method;
1338 /// The following allows us to typecheck messages to "id".
1339 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001340 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001341 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001342 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001343 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001344 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001345 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001346 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001347 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001348 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001349 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001350 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001351 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001352 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001353 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001354 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001355 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001356 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001357 /// The following allows us to typecheck messages to "Class".
1358 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001359 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001360 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001361 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001362 }
1363 }
1364 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001365 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001366 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001367 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001368 ComparePropertiesInBaseAndSuper(I);
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001369 CompareProperties(I, DeclPtrTy::make(I));
Steve Naroff09c47192009-01-09 15:36:25 +00001370 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001371 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001372 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001373 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001374
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001375 // Compare protocol properties with those in category
1376 CompareProperties(C, DeclPtrTy::make(C));
Fariborz Jahanian25760612010-02-15 21:55:26 +00001377 if (C->IsClassExtension())
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001378 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001379 }
Steve Naroff09c47192009-01-09 15:36:25 +00001380 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001381 if (CDecl->getIdentifier())
1382 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1383 // user-defined setter/getter. It also synthesizes setter/getter methods
1384 // and adds them to the DeclContext and global method pools.
1385 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1386 E = CDecl->prop_end();
1387 I != E; ++I)
1388 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001389 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001390 }
1391 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001392 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001393 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001394 if (LangOpts.ObjCNonFragileABI2)
1395 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001396 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001397 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001398 if (LangOpts.ObjCNonFragileABI2)
1399 while (IDecl->getSuperClass()) {
1400 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1401 IDecl = IDecl->getSuperClass();
1402 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001403 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001404 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00001405 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001406 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001407 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Chris Lattner4d391482007-12-12 07:09:47 +00001409 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001410 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001411 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001412 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001413 Categories; Categories = Categories->getNextClassCategory()) {
1414 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001415 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001416 break;
1417 }
1418 }
1419 }
1420 }
Chris Lattner682bf922009-03-29 16:50:03 +00001421 if (isInterfaceDeclKind) {
1422 // Reject invalid vardecls.
1423 for (unsigned i = 0; i != tuvNum; i++) {
1424 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1425 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1426 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001427 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001428 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001429 }
Chris Lattner682bf922009-03-29 16:50:03 +00001430 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001431 }
Chris Lattner4d391482007-12-12 07:09:47 +00001432}
1433
1434
1435/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1436/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001437static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001438CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1439 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1440 if (PQTVal & ObjCDeclSpec::DQ_In)
1441 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1442 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1443 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1444 if (PQTVal & ObjCDeclSpec::DQ_Out)
1445 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1446 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1447 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1448 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1449 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1450 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1451 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001452
1453 return ret;
1454}
1455
Ted Kremenek422bae72010-04-18 04:59:38 +00001456static inline
1457bool containsInvalidMethodImplAttribute(const AttributeList *A) {
1458 // The 'ibaction' attribute is allowed on method definitions because of
1459 // how the IBAction macro is used on both method declarations and definitions.
1460 // If the method definitions contains any other attributes, return true.
1461 while (A && A->getKind() == AttributeList::AT_IBAction)
1462 A = A->getNext();
1463 return A != NULL;
1464}
1465
Chris Lattnerb28317a2009-03-28 19:18:32 +00001466Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001467 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001468 tok::TokenKind MethodType, DeclPtrTy classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001469 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001470 Selector Sel,
1471 // optional arguments. The number of types/arguments is obtained
1472 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001473 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001474 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00001475 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1476 bool isVariadic) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001477 Decl *ClassDecl = classDecl.getAs<Decl>();
Steve Naroffda323ad2008-02-29 21:48:07 +00001478
1479 // Make sure we can establish a context for the method.
1480 if (!ClassDecl) {
1481 Diag(MethodLoc, diag::error_missing_method_context);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +00001482 getLabelMap().clear();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001483 return DeclPtrTy();
Steve Naroffda323ad2008-02-29 21:48:07 +00001484 }
Chris Lattner4d391482007-12-12 07:09:47 +00001485 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001487 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001488 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001489 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Steve Naroffccef3712009-02-20 22:59:16 +00001491 // Methods cannot return interface types. All ObjC objects are
1492 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00001493 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001494 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1495 << 0 << resultDeclType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001496 return DeclPtrTy();
Steve Naroffccef3712009-02-20 22:59:16 +00001497 }
1498 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001499 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001500
1501 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001502 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001503 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001504 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001505 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001506 false, false,
Mike Stump1eb44332009-09-09 15:08:12 +00001507 MethodDeclKind == tok::objc_optional ?
1508 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001509 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Chris Lattner0ed844b2008-04-04 06:12:32 +00001511 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Chris Lattner7db638d2009-04-11 19:42:43 +00001513 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001514 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001515 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Chris Lattnere294d3f2009-04-11 18:57:04 +00001517 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001518 ArgType = Context.getObjCIdType();
1519 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001520 } else {
John McCall58e46772009-10-23 21:48:59 +00001521 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001522 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001523 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001524 }
Mike Stump1eb44332009-09-09 15:08:12 +00001525
John McCall58e46772009-10-23 21:48:59 +00001526 ParmVarDecl* Param
1527 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1528 ArgInfo[i].Name, ArgType, DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001529 VarDecl::None, VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001530
John McCallc12c5bb2010-05-15 11:32:37 +00001531 if (ArgType->isObjCObjectType()) {
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001532 Diag(ArgInfo[i].NameLoc,
1533 diag::err_object_cannot_be_passed_returned_by_value)
1534 << 1 << ArgType;
1535 Param->setInvalidDecl();
1536 }
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Chris Lattner0ed844b2008-04-04 06:12:32 +00001538 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001539 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001541 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001542 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Chris Lattner0ed844b2008-04-04 06:12:32 +00001544 Params.push_back(Param);
1545 }
1546
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001547 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
1548 ParmVarDecl *Param = CParamInfo[i].Param.getAs<ParmVarDecl>();
1549 QualType ArgType = Param->getType();
1550 if (ArgType.isNull())
1551 ArgType = Context.getObjCIdType();
1552 else
1553 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1554 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00001555 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001556 Diag(Param->getLocation(),
1557 diag::err_object_cannot_be_passed_returned_by_value)
1558 << 1 << ArgType;
1559 Param->setInvalidDecl();
1560 }
1561 Param->setDeclContext(ObjCMethod);
Douglas Gregor59c451e2010-08-07 12:29:18 +00001562 if (Param->getDeclName())
1563 IdResolver.RemoveDecl(Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001564 Params.push_back(Param);
1565 }
1566
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001567 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
1568 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001569 ObjCMethod->setObjCDeclQualifier(
1570 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1571 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001572
1573 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001574 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001575
John McCall54abf7d2009-11-04 02:18:39 +00001576 const ObjCMethodDecl *InterfaceMD = 0;
1577
Mike Stump1eb44332009-09-09 15:08:12 +00001578 // For implementations (which can be very "coarse grain"), we add the
1579 // method now. This allows the AST to implement lookup methods that work
1580 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001581 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001582 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001583 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001584 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001585 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1586 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001587 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001588 PrevMethod = ImpDecl->getClassMethod(Sel);
1589 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001590 }
John McCall54abf7d2009-11-04 02:18:39 +00001591 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1592 MethodType == tok::minus);
Ted Kremenek422bae72010-04-18 04:59:38 +00001593 if (containsInvalidMethodImplAttribute(AttrList))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001594 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001595 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001596 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001597 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001598 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1599 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001600 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001601 PrevMethod = CatImpDecl->getClassMethod(Sel);
1602 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001603 }
Ted Kremenek422bae72010-04-18 04:59:38 +00001604 if (containsInvalidMethodImplAttribute(AttrList))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001605 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001606 }
1607 if (PrevMethod) {
1608 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001609 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001610 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001611 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001612 }
John McCall54abf7d2009-11-04 02:18:39 +00001613
1614 // If the interface declared this method, and it was deprecated there,
1615 // mark it deprecated here.
1616 if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
1617 ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
1618
Chris Lattnerb28317a2009-03-28 19:18:32 +00001619 return DeclPtrTy::make(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001620}
1621
Chris Lattnercc98eac2008-12-17 07:13:27 +00001622bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001623 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001624 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Anders Carlsson15281452008-11-04 16:57:32 +00001626 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1627 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Anders Carlsson15281452008-11-04 16:57:32 +00001629 return true;
1630}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001631
Chris Lattnercc98eac2008-12-17 07:13:27 +00001632/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1633/// instance variables of ClassName into Decls.
Mike Stump1eb44332009-09-09 15:08:12 +00001634void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001635 IdentifierInfo *ClassName,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001636 llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001637 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00001638 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001639 if (!Class) {
1640 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1641 return;
1642 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001643 if (LangOpts.ObjCNonFragileABI) {
1644 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1645 return;
1646 }
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Chris Lattnercc98eac2008-12-17 07:13:27 +00001648 // Collect the instance variables
Fariborz Jahanian41833352009-06-04 17:08:55 +00001649 llvm::SmallVector<FieldDecl*, 32> RecFields;
1650 Context.CollectObjCIvars(Class, RecFields);
1651 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1652 for (unsigned i = 0; i < RecFields.size(); i++) {
1653 FieldDecl* ID = RecFields[i];
1654 RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
1655 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1656 ID->getIdentifier(), ID->getType(),
1657 ID->getBitWidth());
1658 Decls.push_back(Sema::DeclPtrTy::make(FD));
1659 }
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Chris Lattnercc98eac2008-12-17 07:13:27 +00001661 // Introduce all of these fields into the appropriate scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001662 for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001663 D != Decls.end(); ++D) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001664 FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
Chris Lattnercc98eac2008-12-17 07:13:27 +00001665 if (getLangOptions().CPlusPlus)
1666 PushOnScopeChains(cast<FieldDecl>(FD), S);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001667 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001668 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001669 }
1670}
1671
Douglas Gregor160b5632010-04-26 17:32:49 +00001672/// \brief Build a type-check a new Objective-C exception variable declaration.
1673VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo,
1674 QualType T,
1675 IdentifierInfo *Name,
1676 SourceLocation NameLoc,
1677 bool Invalid) {
1678 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
1679 // duration shall not be qualified by an address-space qualifier."
1680 // Since all parameters have automatic store duration, they can not have
1681 // an address space.
1682 if (T.getAddressSpace() != 0) {
1683 Diag(NameLoc, diag::err_arg_with_address_space);
1684 Invalid = true;
1685 }
1686
1687 // An @catch parameter must be an unqualified object pointer type;
1688 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
1689 if (Invalid) {
1690 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00001691 } else if (T->isDependentType()) {
1692 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00001693 } else if (!T->isObjCObjectPointerType()) {
1694 Invalid = true;
1695 Diag(NameLoc ,diag::err_catch_param_not_objc_type);
1696 } else if (T->isObjCQualifiedIdType()) {
1697 Invalid = true;
1698 Diag(NameLoc, diag::err_illegal_qualifiers_on_catch_parm);
1699 }
1700
1701 VarDecl *New = VarDecl::Create(Context, CurContext, NameLoc, Name, T, TInfo,
Douglas Gregor324b54d2010-05-03 18:51:14 +00001702 VarDecl::None, VarDecl::None);
1703 New->setExceptionVariable(true);
1704
Douglas Gregor160b5632010-04-26 17:32:49 +00001705 if (Invalid)
1706 New->setInvalidDecl();
1707 return New;
1708}
1709
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001710Sema::DeclPtrTy Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00001711 const DeclSpec &DS = D.getDeclSpec();
1712
1713 // We allow the "register" storage class on exception variables because
1714 // GCC did, but we drop it completely. Any other storage class is an error.
1715 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
1716 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
1717 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
1718 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
1719 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
1720 << DS.getStorageClassSpec();
1721 }
1722 if (D.getDeclSpec().isThreadSpecified())
1723 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
1724 D.getMutableDeclSpec().ClearStorageClassSpecs();
1725
1726 DiagnoseFunctionSpecifiers(D);
1727
1728 // Check that there are no default arguments inside the type of this
1729 // exception object (C++ only).
1730 if (getLangOptions().CPlusPlus)
1731 CheckExtraCXXDefaultArguments(D);
1732
Douglas Gregor160b5632010-04-26 17:32:49 +00001733 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00001734 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
1735 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00001736
1737 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
1738 // Objective-C++: Types shall not be defined in exception types.
1739 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
1740 << Context.getTypeDeclType(OwnedDecl);
1741 }
1742
1743 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, D.getIdentifier(),
1744 D.getIdentifierLoc(),
1745 D.isInvalidType());
1746
1747 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
1748 if (D.getCXXScopeSpec().isSet()) {
1749 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
1750 << D.getCXXScopeSpec().getRange();
1751 New->setInvalidDecl();
1752 }
1753
1754 // Add the parameter declaration into this scope.
1755 S->AddDecl(DeclPtrTy::make(New));
1756 if (D.getIdentifier())
1757 IdResolver.AddDecl(New);
1758
1759 ProcessDeclAttributes(S, New, D);
1760
1761 if (New->hasAttr<BlocksAttr>())
1762 Diag(New->getLocation(), diag::err_block_on_nonlocal);
1763 return DeclPtrTy::make(New);
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001764}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001765
1766/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001767/// initialization.
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001768void Sema::CollectIvarsToConstructOrDestruct(const ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001769 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001770 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1771 E = OI->ivar_end(); I != E; ++I) {
1772 ObjCIvarDecl *Iv = (*I);
1773 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00001774 if (QT->isRecordType())
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001775 Ivars.push_back(*I);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001776 }
1777
1778 // Find ivars to construct/destruct in class extension.
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001779 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
1780 CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001781 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
1782 E = CDecl->ivar_end(); I != E; ++I) {
1783 ObjCIvarDecl *Iv = (*I);
1784 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00001785 if (QT->isRecordType())
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001786 Ivars.push_back(*I);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001787 }
1788 }
1789
1790 // Also add any ivar defined in this class's implementation. This
1791 // includes synthesized ivars.
1792 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
1793 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1794 E = ImplDecl->ivar_end(); I != E; ++I) {
1795 ObjCIvarDecl *Iv = (*I);
1796 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00001797 if (QT->isRecordType())
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001798 Ivars.push_back(*I);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001799 }
1800 }
1801}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001802
1803void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1804 CXXBaseOrMemberInitializer ** initializers,
1805 unsigned numInitializers) {
1806 if (numInitializers > 0) {
1807 NumIvarInitializers = numInitializers;
1808 CXXBaseOrMemberInitializer **ivarInitializers =
1809 new (C) CXXBaseOrMemberInitializer*[NumIvarInitializers];
1810 memcpy(ivarInitializers, initializers,
1811 numInitializers * sizeof(CXXBaseOrMemberInitializer*));
1812 IvarInitializers = ivarInitializers;
1813 }
1814}
1815
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001816void Sema::DiagnoseUseOfUnimplementedSelectors() {
1817 if (ReferencedSelectors.empty())
1818 return;
1819 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
1820 ReferencedSelectors.begin(),
1821 E = ReferencedSelectors.end(); S != E; ++S) {
1822 Selector Sel = (*S).first;
1823 if (!LookupImplementedMethodInGlobalPool(Sel))
1824 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
1825 }
1826 return;
1827}