blob: b9a63fda260f294b1c6955b8e1715442d1d72a41 [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.
Douglas Gregor44b43212008-12-11 16:49:14 +000039 PushDeclContext(FnBodyScope, 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!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000045 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
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 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000210 if (AttrList)
211 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000212 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000213 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000214 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000215 PDecl->setLocEnd(EndProtoLoc);
216 }
Anders Carlsson15281452008-11-04 16:57:32 +0000217
218 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000219 return PDecl;
220}
221
222/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000223/// issues an error if they are not declared. It returns list of
224/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000225void
Chris Lattnere13b9592008-07-26 04:03:38 +0000226Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000227 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000228 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000229 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000230 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000231 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
232 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000233 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000234 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000235 continue;
236 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000237 for (const Attr *attr = PDecl->getAttrs(); attr; attr = attr->getNext()) {
238 if (attr->hasKind(Attr::Unavailable))
239 Diag(ProtocolId[i].second, diag::warn_unavailable) <<
240 PDecl->getDeclName();
241 if (attr->hasKind(Attr::Deprecated))
242 Diag(ProtocolId[i].second, diag::warn_deprecated) <<
243 PDecl->getDeclName();
244 }
Chris Lattnereacc3922008-07-26 03:47:43 +0000245
246 // If this is a forward declaration and we are supposed to warn in this
247 // case, do it.
248 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000249 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000250 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000251 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000252 }
253}
254
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000255/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000256/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000257///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000258void
259Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
260 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000261 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000262 ObjCPropertyDecl::PropertyAttributeKind CAttr =
263 Property->getPropertyAttributes();
264 ObjCPropertyDecl::PropertyAttributeKind SAttr =
265 SuperProperty->getPropertyAttributes();
266 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
267 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000268 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000269 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000270 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
271 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
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() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000274 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
275 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000276 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000277 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000278
279 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
280 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000281 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000282 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000283 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000284 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000285 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000286 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000287 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000288 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000289
Chris Lattner717250a2008-07-26 20:50:02 +0000290 if (Context.getCanonicalType(Property->getType()) !=
291 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000292 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000293 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000294
295}
296
297/// ComparePropertiesInBaseAndSuper - This routine compares property
298/// declarations in base and its super class, if any, and issues
299/// diagnostics in a variety of inconsistant situations.
300///
301void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000302Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000303 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
304 if (!SDecl)
305 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000306 // FIXME: O(N^2)
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000307 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
308 E = SDecl->classprop_end(); S != E; ++S) {
309 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000310 // Does property in super class has declaration in current class?
311 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
312 E = IDecl->classprop_end(); I != E; ++I) {
313 ObjCPropertyDecl *PDecl = (*I);
314 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000315 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000316 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000317 }
318 }
319}
320
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000321/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
322/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000323/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000324void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000325Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000326 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000327 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000328 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
329 if (!IDecl) {
330 // Category
331 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
332 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
333 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
334 E = PDecl->classprop_end(); P != E; ++P) {
335 ObjCPropertyDecl *Pr = (*P);
336 ObjCCategoryDecl::classprop_iterator CP, CE;
337 // Is this property already in category's list of properties?
338 for (CP = CatDecl->classprop_begin(), CE = CatDecl->classprop_end();
339 CP != CE; ++CP)
340 if ((*CP)->getIdentifier() == Pr->getIdentifier())
341 break;
342 if (CP == CE)
343 // Add this property to list of properties for thie class.
344 mergeProperties.push_back(Pr);
345 else
346 // Property protocol already exist in class. Diagnose any mismatch.
347 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
348 }
349 CatDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
350 return;
351 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000352 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
353 E = PDecl->classprop_end(); P != E; ++P) {
354 ObjCPropertyDecl *Pr = (*P);
355 ObjCInterfaceDecl::classprop_iterator CP, CE;
356 // Is this property already in class's list of properties?
357 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
358 CP != CE; ++CP)
359 if ((*CP)->getIdentifier() == Pr->getIdentifier())
360 break;
361 if (CP == CE)
362 // Add this property to list of properties for thie class.
363 mergeProperties.push_back(Pr);
364 else
365 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000366 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000367 }
368 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
369}
370
371/// MergeProtocolPropertiesIntoClass - This routine merges properties
372/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000373/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000374///
375
376void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000377Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000378 DeclTy *MergeItsProtocols) {
379 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000380 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
381
382 if (!IDecl) {
383 // Category
384 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
385 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
386 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
387 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
388 E = MDecl->protocol_end(); P != E; ++P)
389 // Merge properties of category (*P) into IDECL's
390 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
391
392 // Go thru the list of protocols for this category and recursively merge
393 // their properties into this class as well.
394 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
395 E = CatDecl->protocol_end(); P != E; ++P)
396 MergeProtocolPropertiesIntoClass(CatDecl, *P);
397 } else {
398 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
399 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
400 E = MD->protocol_end(); P != E; ++P)
401 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
402 }
403 return;
404 }
405
Chris Lattnerb752f282008-07-21 07:06:49 +0000406 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000407 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
408 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000409 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000410 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
411
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000412 // Go thru the list of protocols for this class and recursively merge
413 // their properties into this class as well.
414 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
415 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000416 MergeProtocolPropertiesIntoClass(IDecl, *P);
417 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000418 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
419 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
420 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000421 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000422 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000423}
424
Chris Lattner4d391482007-12-12 07:09:47 +0000425/// ActOnForwardProtocolDeclaration -
426Action::DeclTy *
427Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000428 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000429 unsigned NumElts,
430 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000431 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000432
433 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000434 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000435 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattner7caeabd2008-07-21 22:17:28 +0000436 if (PDecl == 0) // Not already seen?
437 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000438 if (attrList)
439 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000440 Protocols.push_back(PDecl);
441 }
Anders Carlsson15281452008-11-04 16:57:32 +0000442
443 ObjCForwardProtocolDecl *PDecl =
444 ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
445 &Protocols[0], Protocols.size());
446
447 CheckObjCDeclScope(PDecl);
448 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000449}
450
Chris Lattner7caeabd2008-07-21 22:17:28 +0000451Sema::DeclTy *Sema::
452ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
453 IdentifierInfo *ClassName, SourceLocation ClassLoc,
454 IdentifierInfo *CategoryName,
455 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000456 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000457 unsigned NumProtoRefs,
458 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000459 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000460
Chris Lattner61f9d412008-03-16 20:34:23 +0000461 ObjCCategoryDecl *CDecl =
Chris Lattner68c82cf2008-03-16 20:47:45 +0000462 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000463 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000464
465 /// Check that class of this category is already completely declared.
466 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000467 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffd100c802008-06-05 15:03:27 +0000468 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000469 /// Check for duplicate interface declaration for this category
470 ObjCCategoryDecl *CDeclChain;
471 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
472 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000473 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000474 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner3c73c412008-11-19 08:23:25 +0000475 << ClassName << CategoryName;
Chris Lattner6ff0fc32008-11-23 22:38:38 +0000476 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000477 break;
478 }
Chris Lattner4d391482007-12-12 07:09:47 +0000479 }
Steve Naroffd100c802008-06-05 15:03:27 +0000480 if (!CDeclChain)
481 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000482 }
Chris Lattner4d391482007-12-12 07:09:47 +0000483
484 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000485 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
486 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000487 }
Anders Carlsson15281452008-11-04 16:57:32 +0000488
489 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000490 return CDecl;
491}
492
493/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000494/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000495/// object.
496Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
497 SourceLocation AtCatImplLoc,
498 IdentifierInfo *ClassName, SourceLocation ClassLoc,
499 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000500 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000501 ObjCCategoryImplDecl *CDecl =
502 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000503 /// Check that class of this category is already completely declared.
504 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000505 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000506
507 /// TODO: Check that CatName, category name, is not used in another
508 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000509 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000510
511 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000512 return CDecl;
513}
514
515Sema::DeclTy *Sema::ActOnStartClassImplementation(
516 SourceLocation AtClassImplLoc,
517 IdentifierInfo *ClassName, SourceLocation ClassLoc,
518 IdentifierInfo *SuperClassname,
519 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000520 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000521 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000522 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000523 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000524 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000525 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000526 }
527 else {
528 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000529 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000530 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000531 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000532 }
533
534 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000535 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000536 if (SuperClassname) {
537 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000538 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000539 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000540 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
541 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000542 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000543 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000544 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000545 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000546 Diag(SuperClassLoc, diag::err_undef_superclass)
547 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000548 else if (IDecl && IDecl->getSuperClass() != SDecl) {
549 // This implementation and its interface do not have the same
550 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000551 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000552 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000553 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000554 }
555 }
556 }
557
558 if (!IDecl) {
559 // Legacy case of @implementation with no corresponding @interface.
560 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000561
562 // FIXME: Do we support attributes on the @implementation? If so
563 // we should copy them over.
Chris Lattnerb752f282008-07-21 07:06:49 +0000564 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000565 ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000566 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000567 IDecl->setSuperClass(SDecl);
568 IDecl->setLocEnd(ClassLoc);
569
570 // Remember that this needs to be removed when the scope is popped.
571 TUScope->AddDecl(IDecl);
572 }
573
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000574 ObjCImplementationDecl* IMPDecl =
Chris Lattner75c9cae2008-03-16 20:53:07 +0000575 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
576 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000577
Anders Carlsson15281452008-11-04 16:57:32 +0000578 if (CheckObjCDeclScope(IMPDecl))
579 return IMPDecl;
580
Chris Lattner4d391482007-12-12 07:09:47 +0000581 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000582 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000583 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000584 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000585 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000586 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000587 return IMPDecl;
588}
589
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000590void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
591 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000592 SourceLocation RBrace) {
593 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000594 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000595 if (!IDecl)
596 return;
597 /// Check case of non-existing @interface decl.
598 /// (legacy objective-c @implementation decl without an @interface decl).
599 /// Add implementations's ivar to the synthesize class's ivar list.
600 if (IDecl->ImplicitInterfaceDecl()) {
601 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
Fariborz Jahanian9ee92e82008-12-15 21:58:08 +0000602 IDecl->addRecordToClass(Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000603 return;
604 }
605 // If implementation has empty ivar list, just return.
606 if (numIvars == 0)
607 return;
608
609 assert(ivars && "missing @implementation ivars");
610
611 // Check interface's Ivar list against those in the implementation.
612 // names and types must match.
613 //
Chris Lattner4d391482007-12-12 07:09:47 +0000614 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000615 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000616 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
617 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000618 ObjCIvarDecl* ImplIvar = ivars[j++];
619 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000620 assert (ImplIvar && "missing implementation ivar");
621 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000622 if (Context.getCanonicalType(ImplIvar->getType()) !=
623 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000624 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000625 << ImplIvar->getIdentifier()
626 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000627 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000628 }
629 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
630 // as error.
631 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000632 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000633 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000634 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000635 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000636 }
637 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000638 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000639
640 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000641 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000642 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000643 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000644}
645
Steve Naroff3c2eb662008-02-10 21:38:56 +0000646void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
647 bool &IncompleteImpl) {
648 if (!IncompleteImpl) {
649 Diag(ImpLoc, diag::warn_incomplete_impl);
650 IncompleteImpl = true;
651 }
Chris Lattner08631c52008-11-23 21:45:46 +0000652 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000653}
654
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000655void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
656 ObjCMethodDecl *IntfMethodDecl) {
657 bool err = false;
658 QualType ImpMethodQType =
659 Context.getCanonicalType(ImpMethodDecl->getResultType());
660 QualType IntfMethodQType =
661 Context.getCanonicalType(IntfMethodDecl->getResultType());
662 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
663 err = true;
664 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
665 IF=IntfMethodDecl->param_begin(),
666 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
667 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
668 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
669 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
670 err = true;
671 break;
672 }
673 }
674 if (err) {
675 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
676 << ImpMethodDecl->getDeclName();
677 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
678 }
679}
680
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000681/// FIXME: Type hierarchies in Objective-C can be deep. We could most
682/// likely improve the efficiency of selector lookups and type
683/// checking by associating with each protocol / interface / category
684/// the flattened instance tables. If we used an immutable set to keep
685/// the table then it wouldn't add significant memory cost and it
686/// would be handy for lookups.
687
Steve Naroffefe7f362008-02-08 22:06:17 +0000688/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000689/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000690void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
691 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000692 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000693 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000694 const llvm::DenseSet<Selector> &ClsMap,
695 ObjCInterfaceDecl *IDecl) {
696 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
697
698 // If a method lookup fails locally we still need to look and see if
699 // the method was implemented by a base class or an inherited
700 // protocol. This lookup is slow, but occurs rarely in correct code
701 // and otherwise would terminate in a warning.
702
Chris Lattner4d391482007-12-12 07:09:47 +0000703 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000704 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000705 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000706 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000707 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000708 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000709 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000710 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000711 }
712 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000713 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000714 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000715 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000716 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
717 !ClsMap.count(method->getSelector()) &&
718 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000719 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000720 }
Chris Lattner780f3292008-07-21 21:32:27 +0000721 // Check on this protocols's referenced protocols, recursively.
722 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
723 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000724 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000725}
726
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000727void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
728 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000729 llvm::DenseSet<Selector> InsMap;
730 // Check and see if instance methods in class interface have been
731 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000732 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000733 E = IMPDecl->instmeth_end(); I != E; ++I)
734 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000735
736 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000737 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000738 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000739 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000740 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahaniande739412008-12-05 01:35:25 +0000741 else if (!(*I)->isSynthesized()){
Fariborz Jahaniande739412008-12-05 01:35:25 +0000742 ObjCMethodDecl *ImpMethodDecl =
743 IMPDecl->getInstanceMethod((*I)->getSelector());
744 ObjCMethodDecl *IntfMethodDecl =
745 IDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000746 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
747
Fariborz Jahaniande739412008-12-05 01:35:25 +0000748 }
Chris Lattner4c525092007-12-12 17:58:05 +0000749
Chris Lattner4d391482007-12-12 07:09:47 +0000750 llvm::DenseSet<Selector> ClsMap;
751 // Check and see if class methods in class interface have been
752 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000753 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000754 E = IMPDecl->classmeth_end(); I != E; ++I)
755 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000756
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000757 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000758 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000759 if (!ClsMap.count((*I)->getSelector()))
760 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000761 else {
762 ObjCMethodDecl *ImpMethodDecl =
763 IMPDecl->getClassMethod((*I)->getSelector());
764 ObjCMethodDecl *IntfMethodDecl =
765 IDecl->getClassMethod((*I)->getSelector());
766 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
767 }
768
Chris Lattner4d391482007-12-12 07:09:47 +0000769
770 // Check the protocol list for unimplemented methods in the @implementation
771 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000772 const ObjCList<ObjCProtocolDecl> &Protocols =
773 IDecl->getReferencedProtocols();
774 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
775 E = Protocols.end(); I != E; ++I)
776 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000777 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000778}
779
780/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000781/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000782void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
783 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000784 llvm::DenseSet<Selector> InsMap;
785 // Check and see if instance methods in category interface have been
786 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000787 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000788 E = CatImplDecl->instmeth_end(); I != E; ++I)
789 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000790
791 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000792 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000793 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000794 if (!InsMap.count((*I)->getSelector()))
795 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000796 else {
797 ObjCMethodDecl *ImpMethodDecl =
798 CatImplDecl->getInstanceMethod((*I)->getSelector());
799 ObjCMethodDecl *IntfMethodDecl =
800 CatClassDecl->getInstanceMethod((*I)->getSelector());
801 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
802 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000803
Chris Lattner4d391482007-12-12 07:09:47 +0000804 llvm::DenseSet<Selector> ClsMap;
805 // Check and see if class methods in category interface have been
806 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000807 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000808 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
809 I != E; ++I)
810 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000811
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000812 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000813 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000814 if (!ClsMap.count((*I)->getSelector()))
815 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000816 else {
817 ObjCMethodDecl *ImpMethodDecl =
818 CatImplDecl->getClassMethod((*I)->getSelector());
819 ObjCMethodDecl *IntfMethodDecl =
820 CatClassDecl->getClassMethod((*I)->getSelector());
821 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
822 }
Chris Lattner4d391482007-12-12 07:09:47 +0000823 // Check the protocol list for unimplemented methods in the @implementation
824 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000825 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
826 E = CatClassDecl->protocol_end(); PI != E; ++PI)
827 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000828 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000829}
830
831/// ActOnForwardClassDeclaration -
832Action::DeclTy *
833Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
834 IdentifierInfo **IdentList, unsigned NumElts)
835{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000836 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000837
838 for (unsigned i = 0; i != NumElts; ++i) {
839 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000840 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000841 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000842 // Maybe we will complain about the shadowed template parameter.
843 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
844 // Just pretend that we didn't see the previous declaration.
845 PrevDecl = 0;
846 }
847
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000848 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000849 // GCC apparently allows the following idiom:
850 //
851 // typedef NSObject < XCElementTogglerP > XCElementToggler;
852 // @class XCElementToggler;
853 //
854 // FIXME: Make an extension?
855 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
856 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000857 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000858 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000859 }
Chris Lattner4d391482007-12-12 07:09:47 +0000860 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000861 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000862 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattnerb752f282008-07-21 07:06:49 +0000863 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000864 SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000865 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000866
867 // Remember that this needs to be removed when the scope is popped.
868 TUScope->AddDecl(IDecl);
869 }
870
871 Interfaces.push_back(IDecl);
872 }
873
Anders Carlsson15281452008-11-04 16:57:32 +0000874 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
875 &Interfaces[0],
876 Interfaces.size());
877
878 CheckObjCDeclScope(CDecl);
879 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000880}
881
882
883/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
884/// returns true, or false, accordingly.
885/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000886bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000887 const ObjCMethodDecl *PrevMethod,
888 bool matchBasedOnSizeAndAlignment) {
889 QualType T1 = Context.getCanonicalType(Method->getResultType());
890 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
891
892 if (T1 != T2) {
893 // The result types are different.
894 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000895 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000896 // Incomplete types don't have a size and alignment.
897 if (T1->isIncompleteType() || T2->isIncompleteType())
898 return false;
899 // Check is based on size and alignment.
900 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
901 return false;
902 }
903 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
904 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
905 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
906 if (T1 != T2) {
907 // The result types are different.
908 if (!matchBasedOnSizeAndAlignment)
909 return false;
910 // Incomplete types don't have a size and alignment.
911 if (T1->isIncompleteType() || T2->isIncompleteType())
912 return false;
913 // Check is based on size and alignment.
914 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
915 return false;
916 }
Chris Lattner4d391482007-12-12 07:09:47 +0000917 }
918 return true;
919}
920
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000921void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
922 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000923 if (!FirstMethod.Method) {
924 // Haven't seen a method with this selector name yet - add it.
925 FirstMethod.Method = Method;
926 FirstMethod.Next = 0;
927 } else {
928 // We've seen a method with this name, now check the type signature(s).
929 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
930
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000931 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000932 Next = Next->Next)
933 match = MatchTwoMethodDeclarations(Method, Next->Method);
934
935 if (!match) {
936 // We have a new signature for an existing method - add it.
937 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +0000938 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +0000939 }
940 }
941}
942
Steve Naroff6f5f41c2008-10-21 10:50:19 +0000943// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +0000944ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
945 SourceRange R) {
946 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000947 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +0000948
949 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000950 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
951 // This checks if the methods differ by size & alignment.
952 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
953 issueWarning = true;
954 }
955 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000956 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +0000957 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000958 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +0000959 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +0000960 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000961 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +0000962 }
963 return MethList.Method;
964}
965
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000966void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
967 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000968 if (!FirstMethod.Method) {
969 // Haven't seen a method with this selector name yet - add it.
970 FirstMethod.Method = Method;
971 FirstMethod.Next = 0;
972 } else {
973 // We've seen a method with this name, now check the type signature(s).
974 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
975
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000976 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000977 Next = Next->Next)
978 match = MatchTwoMethodDeclarations(Method, Next->Method);
979
980 if (!match) {
981 // We have a new signature for an existing method - add it.
982 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000983 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000984 FirstMethod.Next = OMI;
985 }
986 }
987}
988
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +0000989/// diagnosePropertySetterGetterMismatch - Make sure that use-defined
990/// setter/getter methods have the property type and issue diagnostics
991/// if they don't.
992///
993void
994Sema::diagnosePropertySetterGetterMismatch(ObjCPropertyDecl *property,
995 const ObjCMethodDecl *GetterMethod,
996 const ObjCMethodDecl *SetterMethod) {
997 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +0000998 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +0000999 Diag(property->getLocation(),
1000 diag::err_accessor_property_type_mismatch)
1001 << property->getDeclName()
1002 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001003 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1004 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001005
1006 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001007 if (Context.getCanonicalType(SetterMethod->getResultType())
1008 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001009 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1010 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001011 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001012 Diag(property->getLocation(),
1013 diag::err_accessor_property_type_mismatch)
1014 << property->getDeclName()
1015 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001016 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1017 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001018 }
1019}
1020
Steve Naroffa56f6162007-12-18 01:30:32 +00001021// Note: For class/category implemenations, allMethods/allProperties is
1022// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001023void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1024 DeclTy **allMethods, unsigned allNum,
1025 DeclTy **allProperties, unsigned pNum) {
1026 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1027
Steve Naroffa56f6162007-12-18 01:30:32 +00001028 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1029 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001030 // should be true.
1031 if (!ClassDecl)
1032 return;
1033
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001034 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
1035 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +00001036
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001037 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1038 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001039
1040 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001041 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1042 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001043 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001044
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +00001045 if (pNum != 0) {
Chris Lattner55d13b42008-03-16 21:23:50 +00001046 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
1047 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +00001048 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1049 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
1050 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001051 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +00001052 else
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +00001053 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +00001054 }
Chris Lattner4d391482007-12-12 07:09:47 +00001055
1056 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001057 ObjCMethodDecl *Method =
1058 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001059
1060 if (!Method) continue; // Already issued a diagnostic.
1061 if (Method->isInstance()) {
1062 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001063 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001064 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1065 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001066 if ((isInterfaceDeclKind && PrevMethod && !match)
1067 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001068 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001069 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001070 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001071 } else {
1072 insMethods.push_back(Method);
1073 InsMap[Method->getSelector()] = Method;
1074 /// The following allows us to typecheck messages to "id".
1075 AddInstanceMethodToGlobalPool(Method);
1076 }
1077 }
1078 else {
1079 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001080 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001081 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1082 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001083 if ((isInterfaceDeclKind && PrevMethod && !match)
1084 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001085 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001086 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001087 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001088 } else {
1089 clsMethods.push_back(Method);
1090 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001091 /// The following allows us to typecheck messages to "Class".
1092 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001093 }
1094 }
1095 }
Steve Naroff5e0a74f2008-09-29 14:20:56 +00001096 // Save the size so we can detect if we've added any property methods.
1097 unsigned int insMethodsSizePriorToPropAdds = insMethods.size();
1098 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size();
Chris Lattner4d391482007-12-12 07:09:47 +00001099
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001100 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001101 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001102 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001103 ComparePropertiesInBaseAndSuper(I);
1104 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001105 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001106 e = I->classprop_end(); i != e; ++i) {
1107 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1108 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianb85cce62008-12-02 00:19:12 +00001109 I->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001110 }
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001111 I->addMethods(&insMethods[0], insMethods.size(),
1112 &clsMethods[0], clsMethods.size(), AtEndLoc);
1113
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001114 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001115 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001116 e = P->classprop_end(); i != e; ++i) {
1117 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1118 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianb85cce62008-12-02 00:19:12 +00001119 P->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001120 }
Chris Lattner4d391482007-12-12 07:09:47 +00001121 P->addMethods(&insMethods[0], insMethods.size(),
1122 &clsMethods[0], clsMethods.size(), AtEndLoc);
1123 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001124 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001125 // Categories are used to extend the class by declaring new methods.
1126 // By the same token, they are also used to add new properties. No
1127 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001128
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001129 // Merge protocol properties into category
1130 MergeProtocolPropertiesIntoClass(C, C);
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001131 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001132 e = C->classprop_end(); i != e; ++i) {
1133 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1134 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianb85cce62008-12-02 00:19:12 +00001135 C->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001136 }
Chris Lattner4d391482007-12-12 07:09:47 +00001137 C->addMethods(&insMethods[0], insMethods.size(),
1138 &clsMethods[0], clsMethods.size(), AtEndLoc);
1139 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001140 else if (ObjCImplementationDecl *IC =
1141 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001142 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001143 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +00001144 ImplMethodsVsClassMethods(IC, IDecl);
1145 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001146 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001147 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001148 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001149 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001150 // in this interface are implemented in the category @implementation.
Chris Lattner4d391482007-12-12 07:09:47 +00001151 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001152 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001153 Categories; Categories = Categories->getNextClassCategory()) {
1154 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1155 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1156 break;
1157 }
1158 }
1159 }
1160 }
Steve Naroff5e0a74f2008-09-29 14:20:56 +00001161 // Add any synthesized methods to the global pool. This allows us to
1162 // handle the following, which is supported by GCC (and part of the design).
1163 //
1164 // @interface Foo
1165 // @property double bar;
1166 // @end
1167 //
1168 // void thisIsUnfortunate() {
1169 // id foo;
1170 // double bar = [foo bar];
1171 // }
1172 //
1173 if (insMethodsSizePriorToPropAdds < insMethods.size())
1174 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++)
1175 AddInstanceMethodToGlobalPool(insMethods[i]);
1176 if (clsMethodsSizePriorToPropAdds < clsMethods.size())
1177 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++)
1178 AddFactoryMethodToGlobalPool(clsMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001179}
1180
1181
1182/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1183/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001184static Decl::ObjCDeclQualifier
1185CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1186 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1187 if (PQTVal & ObjCDeclSpec::DQ_In)
1188 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1189 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1190 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1191 if (PQTVal & ObjCDeclSpec::DQ_Out)
1192 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1193 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1194 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1195 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1196 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1197 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1198 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001199
1200 return ret;
1201}
1202
1203Sema::DeclTy *Sema::ActOnMethodDeclaration(
1204 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001205 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001206 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001207 Selector Sel,
1208 // optional arguments. The number of types/arguments is obtained
1209 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001210 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +00001211 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1212 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001213 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001214
1215 // Make sure we can establish a context for the method.
1216 if (!ClassDecl) {
1217 Diag(MethodLoc, diag::error_missing_method_context);
1218 return 0;
1219 }
Chris Lattner4d391482007-12-12 07:09:47 +00001220 QualType resultDeclType;
1221
1222 if (ReturnType)
1223 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1224 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001225 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001226
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001227 ObjCMethodDecl* ObjCMethod =
1228 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbarf6414922008-08-20 18:02:42 +00001229 ClassDecl,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001230 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001231 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001232 MethodDeclKind == tok::objc_optional ?
1233 ObjCMethodDecl::Optional :
1234 ObjCMethodDecl::Required);
1235
Chris Lattner0ed844b2008-04-04 06:12:32 +00001236 llvm::SmallVector<ParmVarDecl*, 16> Params;
1237
1238 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1239 // FIXME: arg->AttrList must be stored too!
1240 QualType argType;
1241
Steve Naroff6082c622008-12-09 19:36:17 +00001242 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001243 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001244 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1245 if (argType->isArrayType()) // (char *[]) -> (char **)
1246 argType = Context.getArrayDecayedType(argType);
1247 else if (argType->isFunctionType())
1248 argType = Context.getPointerType(argType);
1249 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001250 argType = Context.getObjCIdType();
1251 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1252 SourceLocation(/*FIXME*/),
1253 ArgNames[i], argType,
Chris Lattner04421082008-04-08 04:40:51 +00001254 VarDecl::None, 0, 0);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001255 Param->setObjCDeclQualifier(
1256 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1257 Params.push_back(Param);
1258 }
1259
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001260 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1261 ObjCMethod->setObjCDeclQualifier(
1262 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1263 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001264
1265 if (AttrList)
1266 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001267
1268 // For implementations (which can be very "coarse grain"), we add the
1269 // method now. This allows the AST to implement lookup methods that work
1270 // incrementally (without waiting until we parse the @end). It also allows
1271 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001272 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001273 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001274 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001275 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001276 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001277 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001278 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001279 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001280 }
1281 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001282 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001283 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001284 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001285 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001286 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001287 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001288 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001289 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001290 }
1291 }
1292 if (PrevMethod) {
1293 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001294 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001295 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001296 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001297 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001298 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001299}
1300
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001301void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1302 SourceLocation Loc,
1303 unsigned &Attributes) {
1304 // FIXME: Improve the reported location.
1305
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001306 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001307 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001308 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1309 ObjCDeclSpec::DQ_PR_assign |
1310 ObjCDeclSpec::DQ_PR_copy |
1311 ObjCDeclSpec::DQ_PR_retain))) {
1312 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1313 "readwrite" :
1314 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1315 "assign" :
1316 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1317 "copy" : "retain";
1318
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001319 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1320 diag::err_objc_property_attr_mutually_exclusive :
1321 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001322 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001323 }
1324
1325 // Check for copy or retain on non-object types.
1326 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1327 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001328 Diag(Loc, diag::err_objc_property_requires_object)
1329 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001330 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1331 }
1332
1333 // Check for more than one of { assign, copy, retain }.
1334 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1335 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001336 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1337 << "assign" << "copy";
1338 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001339 }
1340 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001341 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1342 << "assign" << "retain";
1343 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001344 }
1345 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1346 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001347 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1348 << "copy" << "retain";
1349 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001350 }
1351 }
1352
1353 // Warn if user supplied no assignment attribute, property is
1354 // readwrite, and this is an object type.
1355 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1356 ObjCDeclSpec::DQ_PR_retain)) &&
1357 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1358 Context.isObjCObjectPointerType(PropertyTy)) {
1359 // Skip this warning in gc-only mode.
1360 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1361 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1362
1363 // If non-gc code warn that this is likely inappropriate.
1364 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1365 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1366
1367 // FIXME: Implement warning dependent on NSCopying being
1368 // implemented. See also:
1369 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1370 // (please trim this list while you are at it).
1371 }
1372}
1373
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001374Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1375 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001376 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001377 Selector GetterSel,
1378 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001379 DeclTy *ClassCategory,
1380 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001381 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001382 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001383 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1384 // default is readwrite!
1385 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1386 // property is defaulted to 'assign' if it is readwrite and is
1387 // not retain or copy
1388 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1389 (isReadWrite &&
1390 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1391 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1392 QualType T = GetTypeForDeclarator(FD.D, S);
1393 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001394
1395 // May modify Attributes.
1396 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001397
1398 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1399 if (!CDecl->getIdentifier()) {
1400 // This is an anonymous category. property requires special
1401 // handling.
1402 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1403 if (ObjCPropertyDecl *PIDecl =
1404 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1405 // property 'PIDecl's readonly attribute will be over-ridden
1406 // with anonymous category's readwrite property attribute!
1407 unsigned PIkind = PIDecl->getPropertyAttributes();
1408 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001409 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001410 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1411 Diag(AtLoc, diag::warn_property_attr_mismatch);
1412 PIDecl->makeitReadWriteAttribute();
1413 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1414 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1415 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1416 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1417 PIDecl->setSetterName(SetterSel);
1418 // FIXME: use a common routine with addPropertyMethods.
1419 ObjCMethodDecl *SetterDecl =
1420 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1421 Context.VoidTy,
1422 ICDecl,
1423 true, false, true,
1424 ObjCMethodDecl::Required);
1425 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1426 SetterDecl,
1427 SourceLocation(),
1428 FD.D.getIdentifier(),
1429 T,
1430 VarDecl::None,
1431 0, 0);
1432 SetterDecl->setMethodParams(&Argument, 1);
1433 PIDecl->setSetterMethodDecl(SetterDecl);
1434 }
1435 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001436 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001437 *isOverridingProperty = true;
1438 return 0;
1439 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001440 // No matching property found in the main class. Just fall thru
1441 // and add property to the anonymous category. It looks like
1442 // it works as is. This category becomes just like a category
1443 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001444 } else {
1445 Diag(CDecl->getLocation(), diag::err_continuation_class);
1446 *isOverridingProperty = true;
1447 return 0;
1448 }
1449 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001450
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001451 Type *t = T.getTypePtr();
1452 if (t->isArrayType() || t->isFunctionType())
1453 Diag(AtLoc, diag::err_property_type) << T;
1454
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001455 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1456 FD.D.getIdentifier(), T);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001457 // Regardless of setter/getter attribute, we save the default getter/setter
1458 // selector names in anticipation of declaration of setter/getter methods.
1459 PDecl->setGetterName(GetterSel);
1460 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001461
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001462 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001463 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001464
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001465 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001466 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001467
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001468 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001469 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001470
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001471 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001472 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001473
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001474 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001475 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001476
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001477 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001478 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001479
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001480 if (isAssign)
1481 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1482
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001483 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001484 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001485
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001486 if (MethodImplKind == tok::objc_required)
1487 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1488 else if (MethodImplKind == tok::objc_optional)
1489 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1490
Chris Lattner4d391482007-12-12 07:09:47 +00001491 return PDecl;
1492}
1493
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001494/// ActOnPropertyImplDecl - This routine performs semantic checks and
1495/// builds the AST node for a property implementation declaration; declared
1496/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001497///
1498Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1499 SourceLocation PropertyLoc,
1500 bool Synthesize,
1501 DeclTy *ClassCatImpDecl,
1502 IdentifierInfo *PropertyId,
1503 IdentifierInfo *PropertyIvar) {
1504 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1505 // Make sure we have a context for the property implementation declaration.
1506 if (!ClassImpDecl) {
1507 Diag(AtLoc, diag::error_missing_property_context);
1508 return 0;
1509 }
1510 ObjCPropertyDecl *property = 0;
1511 ObjCInterfaceDecl* IDecl = 0;
1512 // Find the class or category class where this property must have
1513 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001514 ObjCImplementationDecl *IC = 0;
1515 ObjCCategoryImplDecl* CatImplClass = 0;
1516 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001517 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001518 // We always synthesize an interface for an implementation
1519 // without an interface decl. So, IDecl is always non-zero.
1520 assert(IDecl &&
1521 "ActOnPropertyImplDecl - @implementation without @interface");
1522
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001523 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001524 property = IDecl->FindPropertyDeclaration(PropertyId);
1525 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001526 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001527 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001528 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001529 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001530 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001531 if (Synthesize) {
1532 Diag(AtLoc, diag::error_synthesize_category_decl);
1533 return 0;
1534 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001535 IDecl = CatImplClass->getClassInterface();
1536 if (!IDecl) {
1537 Diag(AtLoc, diag::error_missing_property_interface);
1538 return 0;
1539 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001540 ObjCCategoryDecl *Category =
1541 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1542
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001543 // If category for this implementation not found, it is an error which
1544 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001545 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001546 return 0;
1547 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001548 property = Category->FindPropertyDeclaration(PropertyId);
1549 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001550 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001551 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001552 return 0;
1553 }
1554 }
1555 else {
1556 Diag(AtLoc, diag::error_bad_property_context);
1557 return 0;
1558 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001559 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001560 // Check that we have a valid, previously declared ivar for @synthesize
1561 if (Synthesize) {
1562 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001563 if (!PropertyIvar)
1564 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001565 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001566 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001567 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001568 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001569 return 0;
1570 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001571 QualType PropType = Context.getCanonicalType(property->getType());
1572 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1573
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001574 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001575 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001576 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001577 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001578 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001579 return 0;
1580 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001581 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001582 } else if (PropertyIvar) {
1583 // @dynamic
1584 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1585 return 0;
1586 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001587 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001588 ObjCPropertyImplDecl *PIDecl =
1589 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1590 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001591 ObjCPropertyImplDecl::Synthesize
1592 : ObjCPropertyImplDecl::Dynamic),
1593 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001594 if (IC) {
1595 if (Synthesize)
1596 if (ObjCPropertyImplDecl *PPIDecl =
1597 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1598 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1599 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1600 << PropertyIvar;
1601 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1602 }
1603
1604 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1605 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1606 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1607 return 0;
1608 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001609 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001610 }
1611 else {
1612 if (Synthesize)
1613 if (ObjCPropertyImplDecl *PPIDecl =
1614 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1615 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1616 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1617 << PropertyIvar;
1618 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1619 }
1620
1621 if (ObjCPropertyImplDecl *PPIDecl =
1622 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1623 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1624 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1625 return 0;
1626 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001627 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001628 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001629
1630 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001631}
Anders Carlsson15281452008-11-04 16:57:32 +00001632
1633bool Sema::CheckObjCDeclScope(Decl *D)
1634{
1635 if (isa<TranslationUnitDecl>(CurContext))
1636 return false;
1637
1638 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1639 D->setInvalidDecl();
1640
1641 return true;
1642}