blob: 828e1fc8c612d113e7f94d3f815b4a2b572c8148 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000017#include "clang/Basic/Diagnostic.h"
18#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000019
20using namespace clang;
21
Ted Kremeneka526c5c2008-01-07 19:49:32 +000022/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000023/// and user declared, in the method definition's AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000024void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000025 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff394f3f42008-07-25 17:57:26 +000026 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
27
28 // If we don't have a valid method decl, simply return.
29 if (!MDecl)
30 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000031
32 // Allow the rest of sema to find private method decl implementations.
33 if (MDecl->isInstance())
34 AddInstanceMethodToGlobalPool(MDecl);
35 else
36 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000037
38 // Allow all of Sema to see that we are entering a method definition.
Chris Lattnerb048c982008-04-06 04:47:34 +000039 PushDeclContext(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000040
41 // Create Decl objects for each parameter, entrring them in the scope for
42 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000043
44 // Insert the invisible arguments, self and _cmd!
Daniel Dunbar451318c2008-08-26 06:07:48 +000045 MDecl->createImplicitParams(Context);
Chris Lattner4d391482007-12-12 07:09:47 +000046
Daniel Dunbar451318c2008-08-26 06:07:48 +000047 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
48 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000049
Chris Lattner8123a952008-04-10 02:22:51 +000050 // Introduce all of the other parameters into this scope.
Chris Lattner58cce3b2008-03-16 01:07:14 +000051 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +000052 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner04421082008-04-08 04:40:51 +000053 IdentifierInfo *II = PDecl->getIdentifier();
Argyrios Kyrtzidis642e38b2008-04-27 13:30:35 +000054 if (II)
55 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000056 }
57}
58
Chris Lattner7caeabd2008-07-21 22:17:28 +000059Sema::DeclTy *Sema::
60ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000063 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000064 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000065 assert(ClassName && "Missing class identifier");
66
67 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +000068 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Douglas Gregorf57172b2008-12-08 18:40:42 +000069 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000070 // Maybe we will complain about the shadowed template parameter.
71 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
72 // Just pretend that we didn't see the previous declaration.
73 PrevDecl = 0;
74 }
75
Ted Kremeneka526c5c2008-01-07 19:49:32 +000076 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000077 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000078 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000079 }
80
Ted Kremeneka526c5c2008-01-07 19:49:32 +000081 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000082 if (IDecl) {
83 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000084 if (!IDecl->isForwardDecl()) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000085 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000086 Diag(IDecl->getLocation(), diag::note_previous_definition);
87
Steve Naroffcfe8bf32008-11-18 19:15:30 +000088 // Return the previous class interface.
89 // FIXME: don't leak the objects passed in!
90 return IDecl;
91 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000092 IDecl->setLocation(AtInterfaceLoc);
93 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000094 }
Chris Lattnerb752f282008-07-21 07:06:49 +000095 } else {
Daniel Dunbarf6414922008-08-20 18:02:42 +000096 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000097 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000098 if (AttrList)
99 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000100
Steve Naroff31102512008-04-02 18:30:49 +0000101 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000102 // Remember that this needs to be removed when the scope is popped.
103 TUScope->AddDecl(IDecl);
104 }
105
106 if (SuperName) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000107 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000108 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000109 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000110 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000111 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000112 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000113 }
114 else {
115 // Check that super class is previously defined
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000116 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000117
118 if (!SuperClassEntry)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000119 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000120 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
121 else if (SuperClassEntry->isForwardDecl())
122 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner08631c52008-11-23 21:45:46 +0000123 << SuperClassEntry->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000124 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000125 }
126 IDecl->setSuperClass(SuperClassEntry);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000127 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000128 IDecl->setLocEnd(SuperLoc);
129 } else { // we have a root class.
130 IDecl->setLocEnd(ClassLoc);
131 }
132
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000133 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000134 if (NumProtoRefs) {
135 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000136 IDecl->setLocEnd(EndProtoLoc);
137 }
Anders Carlsson15281452008-11-04 16:57:32 +0000138
139 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000140 return IDecl;
141}
142
143/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000144/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000145Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
146 IdentifierInfo *AliasName,
147 SourceLocation AliasLocation,
148 IdentifierInfo *ClassName,
149 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000150 // Look for previous declaration of alias name
Steve Naroffb327ce02008-04-02 14:35:35 +0000151 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000152 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000153 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000154 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000155 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000156 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000157 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000158 return 0;
159 }
160 // Check for class declaration
Steve Naroffb327ce02008-04-02 14:35:35 +0000161 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000162 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
163 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000164 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000165 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000166 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000167 return 0;
168 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000169
170 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000171 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe8043c32008-04-01 23:04:06 +0000172 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
173
174 ObjCAliasDecls[AliasName] = AliasDecl;
Anders Carlsson15281452008-11-04 16:57:32 +0000175
176 if (!CheckObjCDeclScope(AliasDecl))
177 TUScope->AddDecl(AliasDecl);
178
Chris Lattner4d391482007-12-12 07:09:47 +0000179 return AliasDecl;
180}
181
Chris Lattnere13b9592008-07-26 04:03:38 +0000182Sema::DeclTy *
183Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
184 IdentifierInfo *ProtocolName,
185 SourceLocation ProtocolLoc,
186 DeclTy * const *ProtoRefs,
187 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000188 SourceLocation EndProtoLoc,
189 AttributeList *AttrList) {
190 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000191 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000192 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000193 if (PDecl) {
194 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000195 if (!PDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000196 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000197 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000198 // Just return the protocol we already had.
199 // FIXME: don't leak the objects passed in!
200 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000201 }
Steve Narofff11b5082008-08-13 16:39:22 +0000202 // Make sure the cached decl gets a valid start location.
203 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000204 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000205 } else {
Chris Lattner780f3292008-07-21 21:32:27 +0000206 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattnerc8581052008-03-16 20:19:15 +0000207 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000208 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000209 }
Chris Lattner4d391482007-12-12 07:09:47 +0000210
211 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000212 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000213 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000214 PDecl->setLocEnd(EndProtoLoc);
215 }
Anders Carlsson15281452008-11-04 16:57:32 +0000216
217 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000218 return PDecl;
219}
220
221/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000222/// issues an error if they are not declared. It returns list of
223/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000224void
Chris Lattnere13b9592008-07-26 04:03:38 +0000225Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000226 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000227 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000228 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000229 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000230 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
231 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000232 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000233 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000234 continue;
235 }
236
237 // If this is a forward declaration and we are supposed to warn in this
238 // case, do it.
239 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000240 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000241 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000242 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000243 }
244}
245
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000246/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000247/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000248///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000249void
250Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
251 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000252 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000253 ObjCPropertyDecl::PropertyAttributeKind CAttr =
254 Property->getPropertyAttributes();
255 ObjCPropertyDecl::PropertyAttributeKind SAttr =
256 SuperProperty->getPropertyAttributes();
257 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
258 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000259 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000260 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000261 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
262 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000263 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000264 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000265 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
266 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000267 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000268 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000269
270 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
271 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000272 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000273 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000274 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000275 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000276 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000277 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000278 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000279 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000280
Chris Lattner717250a2008-07-26 20:50:02 +0000281 if (Context.getCanonicalType(Property->getType()) !=
282 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000283 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000284 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000285
286}
287
288/// ComparePropertiesInBaseAndSuper - This routine compares property
289/// declarations in base and its super class, if any, and issues
290/// diagnostics in a variety of inconsistant situations.
291///
292void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000293Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000294 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
295 if (!SDecl)
296 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000297 // FIXME: O(N^2)
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000298 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
299 E = SDecl->classprop_end(); S != E; ++S) {
300 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000301 // Does property in super class has declaration in current class?
302 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
303 E = IDecl->classprop_end(); I != E; ++I) {
304 ObjCPropertyDecl *PDecl = (*I);
305 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000306 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000307 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000308 }
309 }
310}
311
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000312/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
313/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000314/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000315void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000316Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000317 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000318 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000319 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
320 if (!IDecl) {
321 // Category
322 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
323 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
324 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
325 E = PDecl->classprop_end(); P != E; ++P) {
326 ObjCPropertyDecl *Pr = (*P);
327 ObjCCategoryDecl::classprop_iterator CP, CE;
328 // Is this property already in category's list of properties?
329 for (CP = CatDecl->classprop_begin(), CE = CatDecl->classprop_end();
330 CP != CE; ++CP)
331 if ((*CP)->getIdentifier() == Pr->getIdentifier())
332 break;
333 if (CP == CE)
334 // Add this property to list of properties for thie class.
335 mergeProperties.push_back(Pr);
336 else
337 // Property protocol already exist in class. Diagnose any mismatch.
338 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
339 }
340 CatDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
341 return;
342 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000343 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
344 E = PDecl->classprop_end(); P != E; ++P) {
345 ObjCPropertyDecl *Pr = (*P);
346 ObjCInterfaceDecl::classprop_iterator CP, CE;
347 // Is this property already in class's list of properties?
348 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
349 CP != CE; ++CP)
350 if ((*CP)->getIdentifier() == Pr->getIdentifier())
351 break;
352 if (CP == CE)
353 // Add this property to list of properties for thie class.
354 mergeProperties.push_back(Pr);
355 else
356 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000357 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000358 }
359 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
360}
361
362/// MergeProtocolPropertiesIntoClass - This routine merges properties
363/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000364/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000365///
366
367void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000368Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000369 DeclTy *MergeItsProtocols) {
370 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000371 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
372
373 if (!IDecl) {
374 // Category
375 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
376 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
377 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
378 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
379 E = MDecl->protocol_end(); P != E; ++P)
380 // Merge properties of category (*P) into IDECL's
381 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
382
383 // Go thru the list of protocols for this category and recursively merge
384 // their properties into this class as well.
385 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
386 E = CatDecl->protocol_end(); P != E; ++P)
387 MergeProtocolPropertiesIntoClass(CatDecl, *P);
388 } else {
389 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
390 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
391 E = MD->protocol_end(); P != E; ++P)
392 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
393 }
394 return;
395 }
396
Chris Lattnerb752f282008-07-21 07:06:49 +0000397 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000398 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
399 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000400 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000401 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
402
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000403 // Go thru the list of protocols for this class and recursively merge
404 // their properties into this class as well.
405 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
406 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000407 MergeProtocolPropertiesIntoClass(IDecl, *P);
408 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000409 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
410 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
411 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000412 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000413 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000414}
415
Chris Lattner4d391482007-12-12 07:09:47 +0000416/// ActOnForwardProtocolDeclaration -
417Action::DeclTy *
418Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000419 const IdentifierLocPair *IdentList,
420 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000421 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000422
423 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000424 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000425 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattner7caeabd2008-07-21 22:17:28 +0000426 if (PDecl == 0) // Not already seen?
427 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner4d391482007-12-12 07:09:47 +0000428
429 Protocols.push_back(PDecl);
430 }
Anders Carlsson15281452008-11-04 16:57:32 +0000431
432 ObjCForwardProtocolDecl *PDecl =
433 ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
434 &Protocols[0], Protocols.size());
435
436 CheckObjCDeclScope(PDecl);
437 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000438}
439
Chris Lattner7caeabd2008-07-21 22:17:28 +0000440Sema::DeclTy *Sema::
441ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
442 IdentifierInfo *ClassName, SourceLocation ClassLoc,
443 IdentifierInfo *CategoryName,
444 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000445 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000446 unsigned NumProtoRefs,
447 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000448 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000449
Chris Lattner61f9d412008-03-16 20:34:23 +0000450 ObjCCategoryDecl *CDecl =
Chris Lattner68c82cf2008-03-16 20:47:45 +0000451 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000452 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000453
454 /// Check that class of this category is already completely declared.
455 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000456 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffd100c802008-06-05 15:03:27 +0000457 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000458 /// Check for duplicate interface declaration for this category
459 ObjCCategoryDecl *CDeclChain;
460 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
461 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000462 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000463 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner3c73c412008-11-19 08:23:25 +0000464 << ClassName << CategoryName;
Chris Lattner6ff0fc32008-11-23 22:38:38 +0000465 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000466 break;
467 }
Chris Lattner4d391482007-12-12 07:09:47 +0000468 }
Steve Naroffd100c802008-06-05 15:03:27 +0000469 if (!CDeclChain)
470 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000471 }
Chris Lattner4d391482007-12-12 07:09:47 +0000472
473 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000474 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
475 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000476 }
Anders Carlsson15281452008-11-04 16:57:32 +0000477
478 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000479 return CDecl;
480}
481
482/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000483/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000484/// object.
485Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
486 SourceLocation AtCatImplLoc,
487 IdentifierInfo *ClassName, SourceLocation ClassLoc,
488 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000489 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000490 ObjCCategoryImplDecl *CDecl =
491 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000492 /// Check that class of this category is already completely declared.
493 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000494 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000495
496 /// TODO: Check that CatName, category name, is not used in another
497 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000498 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000499
500 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000501 return CDecl;
502}
503
504Sema::DeclTy *Sema::ActOnStartClassImplementation(
505 SourceLocation AtClassImplLoc,
506 IdentifierInfo *ClassName, SourceLocation ClassLoc,
507 IdentifierInfo *SuperClassname,
508 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000509 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000510 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000511 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000512 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000513 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000514 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000515 }
516 else {
517 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000518 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000519 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000520 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000521 }
522
523 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000524 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000525 if (SuperClassname) {
526 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000527 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000528 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000529 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
530 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000531 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000532 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000533 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000534 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000535 Diag(SuperClassLoc, diag::err_undef_superclass)
536 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000537 else if (IDecl && IDecl->getSuperClass() != SDecl) {
538 // This implementation and its interface do not have the same
539 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000540 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000541 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000542 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000543 }
544 }
545 }
546
547 if (!IDecl) {
548 // Legacy case of @implementation with no corresponding @interface.
549 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000550
551 // FIXME: Do we support attributes on the @implementation? If so
552 // we should copy them over.
Chris Lattnerb752f282008-07-21 07:06:49 +0000553 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000554 ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000555 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000556 IDecl->setSuperClass(SDecl);
557 IDecl->setLocEnd(ClassLoc);
558
559 // Remember that this needs to be removed when the scope is popped.
560 TUScope->AddDecl(IDecl);
561 }
562
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000563 ObjCImplementationDecl* IMPDecl =
Chris Lattner75c9cae2008-03-16 20:53:07 +0000564 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
565 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000566
Anders Carlsson15281452008-11-04 16:57:32 +0000567 if (CheckObjCDeclScope(IMPDecl))
568 return IMPDecl;
569
Chris Lattner4d391482007-12-12 07:09:47 +0000570 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000571 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000572 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000573 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000574 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000575 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000576 return IMPDecl;
577}
578
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000579void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
580 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000581 SourceLocation RBrace) {
582 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000583 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000584 if (!IDecl)
585 return;
586 /// Check case of non-existing @interface decl.
587 /// (legacy objective-c @implementation decl without an @interface decl).
588 /// Add implementations's ivar to the synthesize class's ivar list.
589 if (IDecl->ImplicitInterfaceDecl()) {
590 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
591 return;
592 }
593 // If implementation has empty ivar list, just return.
594 if (numIvars == 0)
595 return;
596
597 assert(ivars && "missing @implementation ivars");
598
599 // Check interface's Ivar list against those in the implementation.
600 // names and types must match.
601 //
Chris Lattner4d391482007-12-12 07:09:47 +0000602 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000603 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000604 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
605 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000606 ObjCIvarDecl* ImplIvar = ivars[j++];
607 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000608 assert (ImplIvar && "missing implementation ivar");
609 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000610 if (Context.getCanonicalType(ImplIvar->getType()) !=
611 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000612 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000613 << ImplIvar->getIdentifier()
614 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000615 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000616 }
617 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
618 // as error.
619 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000620 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000621 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000622 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000623 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000624 }
625 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000626 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000627
628 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000629 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000630 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000631 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000632}
633
Steve Naroff3c2eb662008-02-10 21:38:56 +0000634void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
635 bool &IncompleteImpl) {
636 if (!IncompleteImpl) {
637 Diag(ImpLoc, diag::warn_incomplete_impl);
638 IncompleteImpl = true;
639 }
Chris Lattner08631c52008-11-23 21:45:46 +0000640 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000641}
642
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000643void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
644 ObjCMethodDecl *IntfMethodDecl) {
645 bool err = false;
646 QualType ImpMethodQType =
647 Context.getCanonicalType(ImpMethodDecl->getResultType());
648 QualType IntfMethodQType =
649 Context.getCanonicalType(IntfMethodDecl->getResultType());
650 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
651 err = true;
652 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
653 IF=IntfMethodDecl->param_begin(),
654 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
655 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
656 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
657 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
658 err = true;
659 break;
660 }
661 }
662 if (err) {
663 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
664 << ImpMethodDecl->getDeclName();
665 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
666 }
667}
668
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000669/// FIXME: Type hierarchies in Objective-C can be deep. We could most
670/// likely improve the efficiency of selector lookups and type
671/// checking by associating with each protocol / interface / category
672/// the flattened instance tables. If we used an immutable set to keep
673/// the table then it wouldn't add significant memory cost and it
674/// would be handy for lookups.
675
Steve Naroffefe7f362008-02-08 22:06:17 +0000676/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000677/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000678void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
679 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000680 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000681 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000682 const llvm::DenseSet<Selector> &ClsMap,
683 ObjCInterfaceDecl *IDecl) {
684 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
685
686 // If a method lookup fails locally we still need to look and see if
687 // the method was implemented by a base class or an inherited
688 // protocol. This lookup is slow, but occurs rarely in correct code
689 // and otherwise would terminate in a warning.
690
Chris Lattner4d391482007-12-12 07:09:47 +0000691 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000692 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000693 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000694 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000695 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000696 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000697 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000698 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000699 }
700 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000701 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000702 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000703 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000704 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
705 !ClsMap.count(method->getSelector()) &&
706 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000707 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000708 }
Chris Lattner780f3292008-07-21 21:32:27 +0000709 // Check on this protocols's referenced protocols, recursively.
710 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
711 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000712 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000713}
714
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000715void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
716 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000717 llvm::DenseSet<Selector> InsMap;
718 // Check and see if instance methods in class interface have been
719 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000720 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000721 E = IMPDecl->instmeth_end(); I != E; ++I)
722 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000723
724 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000725 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000726 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000727 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000728 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahaniande739412008-12-05 01:35:25 +0000729 else if (!(*I)->isSynthesized()){
Fariborz Jahaniande739412008-12-05 01:35:25 +0000730 ObjCMethodDecl *ImpMethodDecl =
731 IMPDecl->getInstanceMethod((*I)->getSelector());
732 ObjCMethodDecl *IntfMethodDecl =
733 IDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000734 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
735
Fariborz Jahaniande739412008-12-05 01:35:25 +0000736 }
Chris Lattner4c525092007-12-12 17:58:05 +0000737
Chris Lattner4d391482007-12-12 07:09:47 +0000738 llvm::DenseSet<Selector> ClsMap;
739 // Check and see if class methods in class interface have been
740 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000741 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000742 E = IMPDecl->classmeth_end(); I != E; ++I)
743 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000744
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000745 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000746 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000747 if (!ClsMap.count((*I)->getSelector()))
748 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000749 else {
750 ObjCMethodDecl *ImpMethodDecl =
751 IMPDecl->getClassMethod((*I)->getSelector());
752 ObjCMethodDecl *IntfMethodDecl =
753 IDecl->getClassMethod((*I)->getSelector());
754 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
755 }
756
Chris Lattner4d391482007-12-12 07:09:47 +0000757
758 // Check the protocol list for unimplemented methods in the @implementation
759 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000760 const ObjCList<ObjCProtocolDecl> &Protocols =
761 IDecl->getReferencedProtocols();
762 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
763 E = Protocols.end(); I != E; ++I)
764 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000765 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000766}
767
768/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000769/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000770void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
771 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000772 llvm::DenseSet<Selector> InsMap;
773 // Check and see if instance methods in category interface have been
774 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000775 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000776 E = CatImplDecl->instmeth_end(); I != E; ++I)
777 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000778
779 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000780 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000781 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000782 if (!InsMap.count((*I)->getSelector()))
783 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000784 else {
785 ObjCMethodDecl *ImpMethodDecl =
786 CatImplDecl->getInstanceMethod((*I)->getSelector());
787 ObjCMethodDecl *IntfMethodDecl =
788 CatClassDecl->getInstanceMethod((*I)->getSelector());
789 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
790 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000791
Chris Lattner4d391482007-12-12 07:09:47 +0000792 llvm::DenseSet<Selector> ClsMap;
793 // Check and see if class methods in category interface have been
794 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000795 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000796 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
797 I != E; ++I)
798 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000799
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000800 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000801 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000802 if (!ClsMap.count((*I)->getSelector()))
803 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000804 else {
805 ObjCMethodDecl *ImpMethodDecl =
806 CatImplDecl->getClassMethod((*I)->getSelector());
807 ObjCMethodDecl *IntfMethodDecl =
808 CatClassDecl->getClassMethod((*I)->getSelector());
809 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
810 }
Chris Lattner4d391482007-12-12 07:09:47 +0000811 // Check the protocol list for unimplemented methods in the @implementation
812 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000813 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
814 E = CatClassDecl->protocol_end(); PI != E; ++PI)
815 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000816 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000817}
818
819/// ActOnForwardClassDeclaration -
820Action::DeclTy *
821Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
822 IdentifierInfo **IdentList, unsigned NumElts)
823{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000824 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000825
826 for (unsigned i = 0; i != NumElts; ++i) {
827 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000828 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000829 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000830 // Maybe we will complain about the shadowed template parameter.
831 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
832 // Just pretend that we didn't see the previous declaration.
833 PrevDecl = 0;
834 }
835
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000836 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000837 // GCC apparently allows the following idiom:
838 //
839 // typedef NSObject < XCElementTogglerP > XCElementToggler;
840 // @class XCElementToggler;
841 //
842 // FIXME: Make an extension?
843 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
844 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000845 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000846 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000847 }
Chris Lattner4d391482007-12-12 07:09:47 +0000848 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000849 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000850 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattnerb752f282008-07-21 07:06:49 +0000851 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000852 SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000853 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000854
855 // Remember that this needs to be removed when the scope is popped.
856 TUScope->AddDecl(IDecl);
857 }
858
859 Interfaces.push_back(IDecl);
860 }
861
Anders Carlsson15281452008-11-04 16:57:32 +0000862 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
863 &Interfaces[0],
864 Interfaces.size());
865
866 CheckObjCDeclScope(CDecl);
867 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000868}
869
870
871/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
872/// returns true, or false, accordingly.
873/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000874bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000875 const ObjCMethodDecl *PrevMethod,
876 bool matchBasedOnSizeAndAlignment) {
877 QualType T1 = Context.getCanonicalType(Method->getResultType());
878 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
879
880 if (T1 != T2) {
881 // The result types are different.
882 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000883 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000884 // Incomplete types don't have a size and alignment.
885 if (T1->isIncompleteType() || T2->isIncompleteType())
886 return false;
887 // Check is based on size and alignment.
888 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
889 return false;
890 }
891 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
892 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
893 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
894 if (T1 != T2) {
895 // The result types are different.
896 if (!matchBasedOnSizeAndAlignment)
897 return false;
898 // Incomplete types don't have a size and alignment.
899 if (T1->isIncompleteType() || T2->isIncompleteType())
900 return false;
901 // Check is based on size and alignment.
902 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
903 return false;
904 }
Chris Lattner4d391482007-12-12 07:09:47 +0000905 }
906 return true;
907}
908
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000909void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
910 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000911 if (!FirstMethod.Method) {
912 // Haven't seen a method with this selector name yet - add it.
913 FirstMethod.Method = Method;
914 FirstMethod.Next = 0;
915 } else {
916 // We've seen a method with this name, now check the type signature(s).
917 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
918
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000919 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000920 Next = Next->Next)
921 match = MatchTwoMethodDeclarations(Method, Next->Method);
922
923 if (!match) {
924 // We have a new signature for an existing method - add it.
925 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +0000926 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +0000927 }
928 }
929}
930
Steve Naroff6f5f41c2008-10-21 10:50:19 +0000931// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +0000932ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
933 SourceRange R) {
934 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000935 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +0000936
937 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000938 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
939 // This checks if the methods differ by size & alignment.
940 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
941 issueWarning = true;
942 }
943 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000944 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +0000945 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000946 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +0000947 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +0000948 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000949 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +0000950 }
951 return MethList.Method;
952}
953
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000954void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
955 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000956 if (!FirstMethod.Method) {
957 // Haven't seen a method with this selector name yet - add it.
958 FirstMethod.Method = Method;
959 FirstMethod.Next = 0;
960 } else {
961 // We've seen a method with this name, now check the type signature(s).
962 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
963
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000964 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000965 Next = Next->Next)
966 match = MatchTwoMethodDeclarations(Method, Next->Method);
967
968 if (!match) {
969 // We have a new signature for an existing method - add it.
970 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000971 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000972 FirstMethod.Next = OMI;
973 }
974 }
975}
976
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +0000977/// diagnosePropertySetterGetterMismatch - Make sure that use-defined
978/// setter/getter methods have the property type and issue diagnostics
979/// if they don't.
980///
981void
982Sema::diagnosePropertySetterGetterMismatch(ObjCPropertyDecl *property,
983 const ObjCMethodDecl *GetterMethod,
984 const ObjCMethodDecl *SetterMethod) {
985 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +0000986 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +0000987 Diag(property->getLocation(),
988 diag::err_accessor_property_type_mismatch)
989 << property->getDeclName()
990 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +0000991 Diag(GetterMethod->getLocation(), diag::note_declared_at);
992 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +0000993
994 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +0000995 if (Context.getCanonicalType(SetterMethod->getResultType())
996 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +0000997 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
998 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +0000999 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001000 Diag(property->getLocation(),
1001 diag::err_accessor_property_type_mismatch)
1002 << property->getDeclName()
1003 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001004 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1005 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001006 }
1007}
1008
Steve Naroffa56f6162007-12-18 01:30:32 +00001009// Note: For class/category implemenations, allMethods/allProperties is
1010// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001011void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1012 DeclTy **allMethods, unsigned allNum,
1013 DeclTy **allProperties, unsigned pNum) {
1014 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1015
Steve Naroffa56f6162007-12-18 01:30:32 +00001016 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1017 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001018 // should be true.
1019 if (!ClassDecl)
1020 return;
1021
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001022 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
1023 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +00001024
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001025 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1026 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001027
1028 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001029 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1030 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001031 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001032
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +00001033 if (pNum != 0) {
Chris Lattner55d13b42008-03-16 21:23:50 +00001034 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
1035 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +00001036 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1037 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
1038 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001039 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +00001040 else
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +00001041 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +00001042 }
Chris Lattner4d391482007-12-12 07:09:47 +00001043
1044 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001045 ObjCMethodDecl *Method =
1046 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001047
1048 if (!Method) continue; // Already issued a diagnostic.
1049 if (Method->isInstance()) {
1050 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001051 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001052 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1053 : false;
1054 if (isInterfaceDeclKind && PrevMethod && !match
1055 || checkIdenticalMethods && match) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001056 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001057 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001058 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001059 } else {
1060 insMethods.push_back(Method);
1061 InsMap[Method->getSelector()] = Method;
1062 /// The following allows us to typecheck messages to "id".
1063 AddInstanceMethodToGlobalPool(Method);
1064 }
1065 }
1066 else {
1067 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001068 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001069 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1070 : false;
1071 if (isInterfaceDeclKind && PrevMethod && !match
1072 || checkIdenticalMethods && match) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001073 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001074 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001075 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001076 } else {
1077 clsMethods.push_back(Method);
1078 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001079 /// The following allows us to typecheck messages to "Class".
1080 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001081 }
1082 }
1083 }
Steve Naroff5e0a74f2008-09-29 14:20:56 +00001084 // Save the size so we can detect if we've added any property methods.
1085 unsigned int insMethodsSizePriorToPropAdds = insMethods.size();
1086 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size();
Chris Lattner4d391482007-12-12 07:09:47 +00001087
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001088 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001089 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001090 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001091 ComparePropertiesInBaseAndSuper(I);
1092 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001093 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001094 e = I->classprop_end(); i != e; ++i) {
1095 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1096 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianb85cce62008-12-02 00:19:12 +00001097 I->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001098 }
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001099 I->addMethods(&insMethods[0], insMethods.size(),
1100 &clsMethods[0], clsMethods.size(), AtEndLoc);
1101
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001102 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001103 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001104 e = P->classprop_end(); i != e; ++i) {
1105 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1106 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianb85cce62008-12-02 00:19:12 +00001107 P->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001108 }
Chris Lattner4d391482007-12-12 07:09:47 +00001109 P->addMethods(&insMethods[0], insMethods.size(),
1110 &clsMethods[0], clsMethods.size(), AtEndLoc);
1111 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001112 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001113 // Categories are used to extend the class by declaring new methods.
1114 // By the same token, they are also used to add new properties. No
1115 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001116
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001117 // Merge protocol properties into category
1118 MergeProtocolPropertiesIntoClass(C, C);
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001119 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001120 e = C->classprop_end(); i != e; ++i) {
1121 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1122 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianb85cce62008-12-02 00:19:12 +00001123 C->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001124 }
Chris Lattner4d391482007-12-12 07:09:47 +00001125 C->addMethods(&insMethods[0], insMethods.size(),
1126 &clsMethods[0], clsMethods.size(), AtEndLoc);
1127 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001128 else if (ObjCImplementationDecl *IC =
1129 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001130 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001131 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +00001132 ImplMethodsVsClassMethods(IC, IDecl);
1133 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001134 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001135 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001136 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001137 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001138 // in this interface are implemented in the category @implementation.
Chris Lattner4d391482007-12-12 07:09:47 +00001139 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001140 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001141 Categories; Categories = Categories->getNextClassCategory()) {
1142 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1143 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1144 break;
1145 }
1146 }
1147 }
1148 }
Steve Naroff5e0a74f2008-09-29 14:20:56 +00001149 // Add any synthesized methods to the global pool. This allows us to
1150 // handle the following, which is supported by GCC (and part of the design).
1151 //
1152 // @interface Foo
1153 // @property double bar;
1154 // @end
1155 //
1156 // void thisIsUnfortunate() {
1157 // id foo;
1158 // double bar = [foo bar];
1159 // }
1160 //
1161 if (insMethodsSizePriorToPropAdds < insMethods.size())
1162 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++)
1163 AddInstanceMethodToGlobalPool(insMethods[i]);
1164 if (clsMethodsSizePriorToPropAdds < clsMethods.size())
1165 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++)
1166 AddFactoryMethodToGlobalPool(clsMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001167}
1168
1169
1170/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1171/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001172static Decl::ObjCDeclQualifier
1173CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1174 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1175 if (PQTVal & ObjCDeclSpec::DQ_In)
1176 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1177 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1178 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1179 if (PQTVal & ObjCDeclSpec::DQ_Out)
1180 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1181 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1182 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1183 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1184 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1185 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1186 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001187
1188 return ret;
1189}
1190
1191Sema::DeclTy *Sema::ActOnMethodDeclaration(
1192 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001193 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001194 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001195 Selector Sel,
1196 // optional arguments. The number of types/arguments is obtained
1197 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001198 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +00001199 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1200 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001201 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001202
1203 // Make sure we can establish a context for the method.
1204 if (!ClassDecl) {
1205 Diag(MethodLoc, diag::error_missing_method_context);
1206 return 0;
1207 }
Chris Lattner4d391482007-12-12 07:09:47 +00001208 QualType resultDeclType;
1209
1210 if (ReturnType)
1211 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1212 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001213 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001214
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001215 ObjCMethodDecl* ObjCMethod =
1216 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbarf6414922008-08-20 18:02:42 +00001217 ClassDecl,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001218 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001219 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001220 MethodDeclKind == tok::objc_optional ?
1221 ObjCMethodDecl::Optional :
1222 ObjCMethodDecl::Required);
1223
Chris Lattner0ed844b2008-04-04 06:12:32 +00001224 llvm::SmallVector<ParmVarDecl*, 16> Params;
1225
1226 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1227 // FIXME: arg->AttrList must be stored too!
1228 QualType argType;
1229
1230 if (ArgTypes[i])
1231 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1232 else
1233 argType = Context.getObjCIdType();
1234 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1235 SourceLocation(/*FIXME*/),
1236 ArgNames[i], argType,
Chris Lattner04421082008-04-08 04:40:51 +00001237 VarDecl::None, 0, 0);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001238 Param->setObjCDeclQualifier(
1239 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1240 Params.push_back(Param);
1241 }
1242
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001243 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1244 ObjCMethod->setObjCDeclQualifier(
1245 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1246 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001247
1248 if (AttrList)
1249 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001250
1251 // For implementations (which can be very "coarse grain"), we add the
1252 // method now. This allows the AST to implement lookup methods that work
1253 // incrementally (without waiting until we parse the @end). It also allows
1254 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001255 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001256 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001257 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001258 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001259 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001260 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001261 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001262 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001263 }
1264 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001265 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001266 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001267 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001268 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001269 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001270 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001271 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001272 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001273 }
1274 }
1275 if (PrevMethod) {
1276 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001277 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001278 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001279 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001280 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001281 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001282}
1283
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001284void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1285 SourceLocation Loc,
1286 unsigned &Attributes) {
1287 // FIXME: Improve the reported location.
1288
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001289 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001290 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001291 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1292 ObjCDeclSpec::DQ_PR_assign |
1293 ObjCDeclSpec::DQ_PR_copy |
1294 ObjCDeclSpec::DQ_PR_retain))) {
1295 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1296 "readwrite" :
1297 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1298 "assign" :
1299 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1300 "copy" : "retain";
1301
Chris Lattner5dc266a2008-11-20 06:13:02 +00001302 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001303 << "readonly" << which;
1304 return;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001305 }
1306
1307 // Check for copy or retain on non-object types.
1308 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1309 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001310 Diag(Loc, diag::err_objc_property_requires_object)
1311 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001312 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1313 }
1314
1315 // Check for more than one of { assign, copy, retain }.
1316 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1317 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001318 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1319 << "assign" << "copy";
1320 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001321 }
1322 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001323 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1324 << "assign" << "retain";
1325 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001326 }
1327 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1328 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001329 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1330 << "copy" << "retain";
1331 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001332 }
1333 }
1334
1335 // Warn if user supplied no assignment attribute, property is
1336 // readwrite, and this is an object type.
1337 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1338 ObjCDeclSpec::DQ_PR_retain)) &&
1339 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1340 Context.isObjCObjectPointerType(PropertyTy)) {
1341 // Skip this warning in gc-only mode.
1342 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1343 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1344
1345 // If non-gc code warn that this is likely inappropriate.
1346 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1347 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1348
1349 // FIXME: Implement warning dependent on NSCopying being
1350 // implemented. See also:
1351 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1352 // (please trim this list while you are at it).
1353 }
1354}
1355
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001356Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1357 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001358 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001359 Selector GetterSel,
1360 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001361 DeclTy *ClassCategory,
1362 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001363 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001364 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001365 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1366 // default is readwrite!
1367 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1368 // property is defaulted to 'assign' if it is readwrite and is
1369 // not retain or copy
1370 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1371 (isReadWrite &&
1372 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1373 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1374 QualType T = GetTypeForDeclarator(FD.D, S);
1375 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001376
1377 // May modify Attributes.
1378 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001379
1380 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1381 if (!CDecl->getIdentifier()) {
1382 // This is an anonymous category. property requires special
1383 // handling.
1384 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1385 if (ObjCPropertyDecl *PIDecl =
1386 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1387 // property 'PIDecl's readonly attribute will be over-ridden
1388 // with anonymous category's readwrite property attribute!
1389 unsigned PIkind = PIDecl->getPropertyAttributes();
1390 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001391 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001392 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1393 Diag(AtLoc, diag::warn_property_attr_mismatch);
1394 PIDecl->makeitReadWriteAttribute();
1395 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1396 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1397 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1398 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1399 PIDecl->setSetterName(SetterSel);
1400 // FIXME: use a common routine with addPropertyMethods.
1401 ObjCMethodDecl *SetterDecl =
1402 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1403 Context.VoidTy,
1404 ICDecl,
1405 true, false, true,
1406 ObjCMethodDecl::Required);
1407 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1408 SetterDecl,
1409 SourceLocation(),
1410 FD.D.getIdentifier(),
1411 T,
1412 VarDecl::None,
1413 0, 0);
1414 SetterDecl->setMethodParams(&Argument, 1);
1415 PIDecl->setSetterMethodDecl(SetterDecl);
1416 }
1417 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001418 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001419 *isOverridingProperty = true;
1420 return 0;
1421 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001422 // No matching property found in the main class. Just fall thru
1423 // and add property to the anonymous category. It looks like
1424 // it works as is. This category becomes just like a category
1425 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001426 } else {
1427 Diag(CDecl->getLocation(), diag::err_continuation_class);
1428 *isOverridingProperty = true;
1429 return 0;
1430 }
1431 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001432
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001433 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1434 FD.D.getIdentifier(), T);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001435 // Regardless of setter/getter attribute, we save the default getter/setter
1436 // selector names in anticipation of declaration of setter/getter methods.
1437 PDecl->setGetterName(GetterSel);
1438 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001439
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001440 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001441 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001442
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001443 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001444 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001445
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001446 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001447 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001448
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001449 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001450 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001451
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001452 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001453 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001454
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001455 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001456 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001457
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001458 if (isAssign)
1459 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1460
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001461 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001462 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001463
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001464 if (MethodImplKind == tok::objc_required)
1465 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1466 else if (MethodImplKind == tok::objc_optional)
1467 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1468
Chris Lattner4d391482007-12-12 07:09:47 +00001469 return PDecl;
1470}
1471
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001472/// ActOnPropertyImplDecl - This routine performs semantic checks and
1473/// builds the AST node for a property implementation declaration; declared
1474/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001475///
1476Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1477 SourceLocation PropertyLoc,
1478 bool Synthesize,
1479 DeclTy *ClassCatImpDecl,
1480 IdentifierInfo *PropertyId,
1481 IdentifierInfo *PropertyIvar) {
1482 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1483 // Make sure we have a context for the property implementation declaration.
1484 if (!ClassImpDecl) {
1485 Diag(AtLoc, diag::error_missing_property_context);
1486 return 0;
1487 }
1488 ObjCPropertyDecl *property = 0;
1489 ObjCInterfaceDecl* IDecl = 0;
1490 // Find the class or category class where this property must have
1491 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001492 ObjCImplementationDecl *IC = 0;
1493 ObjCCategoryImplDecl* CatImplClass = 0;
1494 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001495 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001496 // We always synthesize an interface for an implementation
1497 // without an interface decl. So, IDecl is always non-zero.
1498 assert(IDecl &&
1499 "ActOnPropertyImplDecl - @implementation without @interface");
1500
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001501 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001502 property = IDecl->FindPropertyDeclaration(PropertyId);
1503 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001504 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001505 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001506 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001507 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001508 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001509 if (Synthesize) {
1510 Diag(AtLoc, diag::error_synthesize_category_decl);
1511 return 0;
1512 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001513 IDecl = CatImplClass->getClassInterface();
1514 if (!IDecl) {
1515 Diag(AtLoc, diag::error_missing_property_interface);
1516 return 0;
1517 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001518 ObjCCategoryDecl *Category =
1519 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1520
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001521 // If category for this implementation not found, it is an error which
1522 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001523 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001524 return 0;
1525 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001526 property = Category->FindPropertyDeclaration(PropertyId);
1527 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001528 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001529 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001530 return 0;
1531 }
1532 }
1533 else {
1534 Diag(AtLoc, diag::error_bad_property_context);
1535 return 0;
1536 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001537 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001538 // Check that we have a valid, previously declared ivar for @synthesize
1539 if (Synthesize) {
1540 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001541 if (!PropertyIvar)
1542 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001543 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001544 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001545 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001546 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001547 return 0;
1548 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001549 QualType PropType = Context.getCanonicalType(property->getType());
1550 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1551
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001552 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001553 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001554 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001555 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001556 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001557 return 0;
1558 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001559 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001560 } else if (PropertyIvar) {
1561 // @dynamic
1562 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1563 return 0;
1564 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001565 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001566 ObjCPropertyImplDecl *PIDecl =
1567 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1568 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001569 ObjCPropertyImplDecl::Synthesize
1570 : ObjCPropertyImplDecl::Dynamic),
1571 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001572 if (IC) {
1573 if (Synthesize)
1574 if (ObjCPropertyImplDecl *PPIDecl =
1575 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1576 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1577 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1578 << PropertyIvar;
1579 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1580 }
1581
1582 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1583 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1584 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1585 return 0;
1586 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001587 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001588 }
1589 else {
1590 if (Synthesize)
1591 if (ObjCPropertyImplDecl *PPIDecl =
1592 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1593 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1594 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1595 << PropertyIvar;
1596 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1597 }
1598
1599 if (ObjCPropertyImplDecl *PPIDecl =
1600 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1601 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1602 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1603 return 0;
1604 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001605 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001606 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001607
1608 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001609}
Anders Carlsson15281452008-11-04 16:57:32 +00001610
1611bool Sema::CheckObjCDeclScope(Decl *D)
1612{
1613 if (isa<TranslationUnitDecl>(CurContext))
1614 return false;
1615
1616 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1617 D->setInvalidDecl();
1618
1619 return true;
1620}