blob: cf1a5259f20c0277c67caa7a0a7540c7be93d72c [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()) {
Fariborz Jahanianfc820a42008-12-29 19:57:17 +0000238 if (attr->getKind() == Attr::Unavailable)
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000239 Diag(ProtocolId[i].second, diag::warn_unavailable) <<
240 PDecl->getDeclName();
Fariborz Jahanianfc820a42008-12-29 19:57:17 +0000241 if (attr->getKind() == Attr::Deprecated)
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000242 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);
602 return;
603 }
604 // If implementation has empty ivar list, just return.
605 if (numIvars == 0)
606 return;
607
608 assert(ivars && "missing @implementation ivars");
609
610 // Check interface's Ivar list against those in the implementation.
611 // names and types must match.
612 //
Chris Lattner4d391482007-12-12 07:09:47 +0000613 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000614 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000615 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
616 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617 ObjCIvarDecl* ImplIvar = ivars[j++];
618 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000619 assert (ImplIvar && "missing implementation ivar");
620 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000621 if (Context.getCanonicalType(ImplIvar->getType()) !=
622 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000623 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000624 << ImplIvar->getIdentifier()
625 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000626 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000627 }
628 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
629 // as error.
630 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000631 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000632 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000633 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000634 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000635 }
636 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000637 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000638
639 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000640 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000641 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000642 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000643}
644
Steve Naroff3c2eb662008-02-10 21:38:56 +0000645void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
646 bool &IncompleteImpl) {
647 if (!IncompleteImpl) {
648 Diag(ImpLoc, diag::warn_incomplete_impl);
649 IncompleteImpl = true;
650 }
Chris Lattner08631c52008-11-23 21:45:46 +0000651 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000652}
653
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000654void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
655 ObjCMethodDecl *IntfMethodDecl) {
656 bool err = false;
657 QualType ImpMethodQType =
658 Context.getCanonicalType(ImpMethodDecl->getResultType());
659 QualType IntfMethodQType =
660 Context.getCanonicalType(IntfMethodDecl->getResultType());
661 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
662 err = true;
663 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
664 IF=IntfMethodDecl->param_begin(),
665 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
666 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
667 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
668 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
669 err = true;
670 break;
671 }
672 }
673 if (err) {
674 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
675 << ImpMethodDecl->getDeclName();
676 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
677 }
678}
679
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000680/// FIXME: Type hierarchies in Objective-C can be deep. We could most
681/// likely improve the efficiency of selector lookups and type
682/// checking by associating with each protocol / interface / category
683/// the flattened instance tables. If we used an immutable set to keep
684/// the table then it wouldn't add significant memory cost and it
685/// would be handy for lookups.
686
Steve Naroffefe7f362008-02-08 22:06:17 +0000687/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000688/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000689void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
690 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000691 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000692 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000693 const llvm::DenseSet<Selector> &ClsMap,
694 ObjCInterfaceDecl *IDecl) {
695 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
696
697 // If a method lookup fails locally we still need to look and see if
698 // the method was implemented by a base class or an inherited
699 // protocol. This lookup is slow, but occurs rarely in correct code
700 // and otherwise would terminate in a warning.
701
Chris Lattner4d391482007-12-12 07:09:47 +0000702 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000703 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000704 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000705 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000706 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000707 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000708 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000709 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000710 }
711 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000712 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000713 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000714 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000715 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
716 !ClsMap.count(method->getSelector()) &&
717 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000718 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000719 }
Chris Lattner780f3292008-07-21 21:32:27 +0000720 // Check on this protocols's referenced protocols, recursively.
721 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
722 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000723 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000724}
725
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000726void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
727 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000728 llvm::DenseSet<Selector> InsMap;
729 // Check and see if instance methods in class interface have been
730 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000731 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000732 E = IMPDecl->instmeth_end(); I != E; ++I)
733 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000734
735 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000736 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000737 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000738 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000739 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000740 else {
Fariborz Jahaniande739412008-12-05 01:35:25 +0000741 ObjCMethodDecl *ImpMethodDecl =
742 IMPDecl->getInstanceMethod((*I)->getSelector());
743 ObjCMethodDecl *IntfMethodDecl =
744 IDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000745 assert(IntfMethodDecl &&
746 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
747 // ImpMethodDecl may be null as in a @dynamic property.
748 if (ImpMethodDecl)
749 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahaniande739412008-12-05 01:35:25 +0000750 }
Chris Lattner4c525092007-12-12 17:58:05 +0000751
Chris Lattner4d391482007-12-12 07:09:47 +0000752 llvm::DenseSet<Selector> ClsMap;
753 // Check and see if class methods in class interface have been
754 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000755 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000756 E = IMPDecl->classmeth_end(); I != E; ++I)
757 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000758
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000759 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000760 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000761 if (!ClsMap.count((*I)->getSelector()))
762 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000763 else {
764 ObjCMethodDecl *ImpMethodDecl =
765 IMPDecl->getClassMethod((*I)->getSelector());
766 ObjCMethodDecl *IntfMethodDecl =
767 IDecl->getClassMethod((*I)->getSelector());
768 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
769 }
770
Chris Lattner4d391482007-12-12 07:09:47 +0000771
772 // Check the protocol list for unimplemented methods in the @implementation
773 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000774 const ObjCList<ObjCProtocolDecl> &Protocols =
775 IDecl->getReferencedProtocols();
776 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
777 E = Protocols.end(); I != E; ++I)
778 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000779 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000780}
781
782/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000783/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000784void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
785 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000786 llvm::DenseSet<Selector> InsMap;
787 // Check and see if instance methods in category interface have been
788 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000789 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000790 E = CatImplDecl->instmeth_end(); I != E; ++I)
791 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000792
793 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000794 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000795 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000796 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000797 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000798 else {
799 ObjCMethodDecl *ImpMethodDecl =
800 CatImplDecl->getInstanceMethod((*I)->getSelector());
801 ObjCMethodDecl *IntfMethodDecl =
802 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000803 assert(IntfMethodDecl &&
804 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
805 // ImpMethodDecl may be null as in a @dynamic property.
806 if (ImpMethodDecl)
807 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000808 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000809
Chris Lattner4d391482007-12-12 07:09:47 +0000810 llvm::DenseSet<Selector> ClsMap;
811 // Check and see if class methods in category interface have been
812 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000813 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000814 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
815 I != E; ++I)
816 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000817
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000818 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000819 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000820 if (!ClsMap.count((*I)->getSelector()))
821 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000822 else {
823 ObjCMethodDecl *ImpMethodDecl =
824 CatImplDecl->getClassMethod((*I)->getSelector());
825 ObjCMethodDecl *IntfMethodDecl =
826 CatClassDecl->getClassMethod((*I)->getSelector());
827 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
828 }
Chris Lattner4d391482007-12-12 07:09:47 +0000829 // Check the protocol list for unimplemented methods in the @implementation
830 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000831 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
832 E = CatClassDecl->protocol_end(); PI != E; ++PI)
833 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000834 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000835}
836
837/// ActOnForwardClassDeclaration -
838Action::DeclTy *
839Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
840 IdentifierInfo **IdentList, unsigned NumElts)
841{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000842 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000843
844 for (unsigned i = 0; i != NumElts; ++i) {
845 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000846 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000847 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000848 // Maybe we will complain about the shadowed template parameter.
849 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
850 // Just pretend that we didn't see the previous declaration.
851 PrevDecl = 0;
852 }
853
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000854 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000855 // GCC apparently allows the following idiom:
856 //
857 // typedef NSObject < XCElementTogglerP > XCElementToggler;
858 // @class XCElementToggler;
859 //
860 // FIXME: Make an extension?
861 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
862 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000863 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000864 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000865 }
Chris Lattner4d391482007-12-12 07:09:47 +0000866 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000867 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000868 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattnerb752f282008-07-21 07:06:49 +0000869 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000870 SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000871 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000872
873 // Remember that this needs to be removed when the scope is popped.
874 TUScope->AddDecl(IDecl);
875 }
876
877 Interfaces.push_back(IDecl);
878 }
879
Anders Carlsson15281452008-11-04 16:57:32 +0000880 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
881 &Interfaces[0],
882 Interfaces.size());
883
884 CheckObjCDeclScope(CDecl);
885 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000886}
887
888
889/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
890/// returns true, or false, accordingly.
891/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000892bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000893 const ObjCMethodDecl *PrevMethod,
894 bool matchBasedOnSizeAndAlignment) {
895 QualType T1 = Context.getCanonicalType(Method->getResultType());
896 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
897
898 if (T1 != T2) {
899 // The result types are different.
900 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000901 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000902 // Incomplete types don't have a size and alignment.
903 if (T1->isIncompleteType() || T2->isIncompleteType())
904 return false;
905 // Check is based on size and alignment.
906 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
907 return false;
908 }
909 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
910 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
911 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
912 if (T1 != T2) {
913 // The result types are different.
914 if (!matchBasedOnSizeAndAlignment)
915 return false;
916 // Incomplete types don't have a size and alignment.
917 if (T1->isIncompleteType() || T2->isIncompleteType())
918 return false;
919 // Check is based on size and alignment.
920 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
921 return false;
922 }
Chris Lattner4d391482007-12-12 07:09:47 +0000923 }
924 return true;
925}
926
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000927void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
928 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000929 if (!FirstMethod.Method) {
930 // Haven't seen a method with this selector name yet - add it.
931 FirstMethod.Method = Method;
932 FirstMethod.Next = 0;
933 } else {
934 // We've seen a method with this name, now check the type signature(s).
935 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
936
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000937 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000938 Next = Next->Next)
939 match = MatchTwoMethodDeclarations(Method, Next->Method);
940
941 if (!match) {
942 // We have a new signature for an existing method - add it.
943 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +0000944 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +0000945 }
946 }
947}
948
Steve Naroff6f5f41c2008-10-21 10:50:19 +0000949// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +0000950ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
951 SourceRange R) {
952 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000953 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +0000954
955 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000956 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
957 // This checks if the methods differ by size & alignment.
958 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
959 issueWarning = true;
960 }
961 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000962 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +0000963 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000964 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +0000965 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +0000966 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000967 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +0000968 }
969 return MethList.Method;
970}
971
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000972void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
973 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000974 if (!FirstMethod.Method) {
975 // Haven't seen a method with this selector name yet - add it.
976 FirstMethod.Method = Method;
977 FirstMethod.Next = 0;
978 } else {
979 // We've seen a method with this name, now check the type signature(s).
980 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
981
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000982 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000983 Next = Next->Next)
984 match = MatchTwoMethodDeclarations(Method, Next->Method);
985
986 if (!match) {
987 // We have a new signature for an existing method - add it.
988 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000989 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000990 FirstMethod.Next = OMI;
991 }
992 }
993}
994
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +0000995/// diagnosePropertySetterGetterMismatch - Make sure that use-defined
996/// setter/getter methods have the property type and issue diagnostics
997/// if they don't.
998///
999void
1000Sema::diagnosePropertySetterGetterMismatch(ObjCPropertyDecl *property,
1001 const ObjCMethodDecl *GetterMethod,
1002 const ObjCMethodDecl *SetterMethod) {
1003 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001004 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001005 Diag(property->getLocation(),
1006 diag::err_accessor_property_type_mismatch)
1007 << property->getDeclName()
1008 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001009 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1010 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001011
1012 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001013 if (Context.getCanonicalType(SetterMethod->getResultType())
1014 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001015 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1016 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001017 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001018 Diag(property->getLocation(),
1019 diag::err_accessor_property_type_mismatch)
1020 << property->getDeclName()
1021 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001022 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1023 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001024 }
1025}
1026
Steve Naroffa56f6162007-12-18 01:30:32 +00001027// Note: For class/category implemenations, allMethods/allProperties is
1028// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001029void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1030 DeclTy **allMethods, unsigned allNum,
1031 DeclTy **allProperties, unsigned pNum) {
1032 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1033
Steve Naroffa56f6162007-12-18 01:30:32 +00001034 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1035 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001036 // should be true.
1037 if (!ClassDecl)
1038 return;
1039
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001040 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
1041 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +00001042
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001043 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1044 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001045
1046 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001047 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1048 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001049 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001050
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +00001051 if (pNum != 0) {
Chris Lattner55d13b42008-03-16 21:23:50 +00001052 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
1053 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +00001054 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1055 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
1056 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001057 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +00001058 else
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +00001059 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +00001060 }
Chris Lattner4d391482007-12-12 07:09:47 +00001061
1062 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001063 ObjCMethodDecl *Method =
1064 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001065
1066 if (!Method) continue; // Already issued a diagnostic.
1067 if (Method->isInstance()) {
1068 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001069 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001070 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1071 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001072 if ((isInterfaceDeclKind && PrevMethod && !match)
1073 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001074 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001075 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001076 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001077 } else {
1078 insMethods.push_back(Method);
1079 InsMap[Method->getSelector()] = Method;
1080 /// The following allows us to typecheck messages to "id".
1081 AddInstanceMethodToGlobalPool(Method);
1082 }
1083 }
1084 else {
1085 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001086 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001087 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1088 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001089 if ((isInterfaceDeclKind && PrevMethod && !match)
1090 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001091 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001092 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001093 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001094 } else {
1095 clsMethods.push_back(Method);
1096 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001097 /// The following allows us to typecheck messages to "Class".
1098 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001099 }
1100 }
1101 }
Steve Naroff5e0a74f2008-09-29 14:20:56 +00001102 // Save the size so we can detect if we've added any property methods.
1103 unsigned int insMethodsSizePriorToPropAdds = insMethods.size();
1104 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size();
Chris Lattner4d391482007-12-12 07:09:47 +00001105
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001106 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001107 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001108 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001109 ComparePropertiesInBaseAndSuper(I);
1110 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001111 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001112 e = I->classprop_end(); i != e; ++i) {
1113 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1114 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianb85cce62008-12-02 00:19:12 +00001115 I->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001116 }
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001117 I->addMethods(&insMethods[0], insMethods.size(),
1118 &clsMethods[0], clsMethods.size(), AtEndLoc);
1119
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001120 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001121 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001122 e = P->classprop_end(); i != e; ++i) {
1123 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1124 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianb85cce62008-12-02 00:19:12 +00001125 P->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001126 }
Chris Lattner4d391482007-12-12 07:09:47 +00001127 P->addMethods(&insMethods[0], insMethods.size(),
1128 &clsMethods[0], clsMethods.size(), AtEndLoc);
1129 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001130 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001131 // Categories are used to extend the class by declaring new methods.
1132 // By the same token, they are also used to add new properties. No
1133 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001134
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001135 // Merge protocol properties into category
1136 MergeProtocolPropertiesIntoClass(C, C);
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001137 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001138 e = C->classprop_end(); i != e; ++i) {
1139 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1140 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianb85cce62008-12-02 00:19:12 +00001141 C->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001142 }
Chris Lattner4d391482007-12-12 07:09:47 +00001143 C->addMethods(&insMethods[0], insMethods.size(),
1144 &clsMethods[0], clsMethods.size(), AtEndLoc);
1145 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001146 else if (ObjCImplementationDecl *IC =
1147 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001148 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001149 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +00001150 ImplMethodsVsClassMethods(IC, IDecl);
1151 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001152 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001153 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001154 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001155 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001156 // in this interface are implemented in the category @implementation.
Chris Lattner4d391482007-12-12 07:09:47 +00001157 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001158 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001159 Categories; Categories = Categories->getNextClassCategory()) {
1160 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1161 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1162 break;
1163 }
1164 }
1165 }
1166 }
Steve Naroff5e0a74f2008-09-29 14:20:56 +00001167 // Add any synthesized methods to the global pool. This allows us to
1168 // handle the following, which is supported by GCC (and part of the design).
1169 //
1170 // @interface Foo
1171 // @property double bar;
1172 // @end
1173 //
1174 // void thisIsUnfortunate() {
1175 // id foo;
1176 // double bar = [foo bar];
1177 // }
1178 //
1179 if (insMethodsSizePriorToPropAdds < insMethods.size())
1180 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++)
1181 AddInstanceMethodToGlobalPool(insMethods[i]);
1182 if (clsMethodsSizePriorToPropAdds < clsMethods.size())
1183 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++)
1184 AddFactoryMethodToGlobalPool(clsMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001185}
1186
1187
1188/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1189/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001190static Decl::ObjCDeclQualifier
1191CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1192 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1193 if (PQTVal & ObjCDeclSpec::DQ_In)
1194 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1195 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1196 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1197 if (PQTVal & ObjCDeclSpec::DQ_Out)
1198 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1199 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1200 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1201 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1202 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1203 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1204 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001205
1206 return ret;
1207}
1208
1209Sema::DeclTy *Sema::ActOnMethodDeclaration(
1210 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001211 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001212 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001213 Selector Sel,
1214 // optional arguments. The number of types/arguments is obtained
1215 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001216 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +00001217 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1218 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001219 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001220
1221 // Make sure we can establish a context for the method.
1222 if (!ClassDecl) {
1223 Diag(MethodLoc, diag::error_missing_method_context);
1224 return 0;
1225 }
Chris Lattner4d391482007-12-12 07:09:47 +00001226 QualType resultDeclType;
1227
1228 if (ReturnType)
1229 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1230 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001231 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001232
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001233 ObjCMethodDecl* ObjCMethod =
1234 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbarf6414922008-08-20 18:02:42 +00001235 ClassDecl,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001236 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001237 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001238 MethodDeclKind == tok::objc_optional ?
1239 ObjCMethodDecl::Optional :
1240 ObjCMethodDecl::Required);
1241
Chris Lattner0ed844b2008-04-04 06:12:32 +00001242 llvm::SmallVector<ParmVarDecl*, 16> Params;
1243
1244 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1245 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001246 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001247
Steve Naroff6082c622008-12-09 19:36:17 +00001248 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001249 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001250 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001251 if (argType->isArrayType()) { // (char *[]) -> (char **)
1252 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001253 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001254 }
Steve Naroff6082c622008-12-09 19:36:17 +00001255 else if (argType->isFunctionType())
1256 argType = Context.getPointerType(argType);
1257 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001258 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001259 ParmVarDecl* Param;
1260 if (originalArgType.isNull())
1261 Param = ParmVarDecl::Create(Context, ObjCMethod,
1262 SourceLocation(/*FIXME*/),
1263 ArgNames[i], argType,
1264 VarDecl::None, 0, 0);
1265 else
1266 Param = ParmVarWithOriginalTypeDecl::Create(Context, ObjCMethod,
1267 SourceLocation(/*FIXME*/),
1268 ArgNames[i], argType, originalArgType,
1269 VarDecl::None, 0, 0);
1270
Chris Lattner0ed844b2008-04-04 06:12:32 +00001271 Param->setObjCDeclQualifier(
1272 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1273 Params.push_back(Param);
1274 }
1275
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001276 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1277 ObjCMethod->setObjCDeclQualifier(
1278 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1279 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001280
1281 if (AttrList)
1282 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001283
1284 // For implementations (which can be very "coarse grain"), we add the
1285 // method now. This allows the AST to implement lookup methods that work
1286 // incrementally (without waiting until we parse the @end). It also allows
1287 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001288 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001289 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001290 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001291 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001292 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001293 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001294 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001295 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001296 }
1297 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001298 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001299 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001300 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001301 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001302 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001303 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001304 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001305 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001306 }
1307 }
1308 if (PrevMethod) {
1309 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001310 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001311 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001312 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001313 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001314 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001315}
1316
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001317void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1318 SourceLocation Loc,
1319 unsigned &Attributes) {
1320 // FIXME: Improve the reported location.
1321
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001322 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001323 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001324 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1325 ObjCDeclSpec::DQ_PR_assign |
1326 ObjCDeclSpec::DQ_PR_copy |
1327 ObjCDeclSpec::DQ_PR_retain))) {
1328 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1329 "readwrite" :
1330 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1331 "assign" :
1332 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1333 "copy" : "retain";
1334
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001335 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1336 diag::err_objc_property_attr_mutually_exclusive :
1337 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001338 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001339 }
1340
1341 // Check for copy or retain on non-object types.
1342 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1343 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001344 Diag(Loc, diag::err_objc_property_requires_object)
1345 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001346 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1347 }
1348
1349 // Check for more than one of { assign, copy, retain }.
1350 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1351 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001352 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1353 << "assign" << "copy";
1354 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001355 }
1356 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001357 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1358 << "assign" << "retain";
1359 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001360 }
1361 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1362 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001363 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1364 << "copy" << "retain";
1365 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001366 }
1367 }
1368
1369 // Warn if user supplied no assignment attribute, property is
1370 // readwrite, and this is an object type.
1371 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1372 ObjCDeclSpec::DQ_PR_retain)) &&
1373 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1374 Context.isObjCObjectPointerType(PropertyTy)) {
1375 // Skip this warning in gc-only mode.
1376 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1377 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1378
1379 // If non-gc code warn that this is likely inappropriate.
1380 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1381 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1382
1383 // FIXME: Implement warning dependent on NSCopying being
1384 // implemented. See also:
1385 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1386 // (please trim this list while you are at it).
1387 }
1388}
1389
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001390Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1391 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001392 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001393 Selector GetterSel,
1394 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001395 DeclTy *ClassCategory,
1396 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001397 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001398 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001399 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1400 // default is readwrite!
1401 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1402 // property is defaulted to 'assign' if it is readwrite and is
1403 // not retain or copy
1404 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1405 (isReadWrite &&
1406 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1407 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1408 QualType T = GetTypeForDeclarator(FD.D, S);
1409 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001410
1411 // May modify Attributes.
1412 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001413
1414 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1415 if (!CDecl->getIdentifier()) {
1416 // This is an anonymous category. property requires special
1417 // handling.
1418 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1419 if (ObjCPropertyDecl *PIDecl =
1420 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1421 // property 'PIDecl's readonly attribute will be over-ridden
1422 // with anonymous category's readwrite property attribute!
1423 unsigned PIkind = PIDecl->getPropertyAttributes();
1424 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001425 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001426 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1427 Diag(AtLoc, diag::warn_property_attr_mismatch);
1428 PIDecl->makeitReadWriteAttribute();
1429 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1430 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1431 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1432 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1433 PIDecl->setSetterName(SetterSel);
1434 // FIXME: use a common routine with addPropertyMethods.
1435 ObjCMethodDecl *SetterDecl =
1436 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1437 Context.VoidTy,
1438 ICDecl,
1439 true, false, true,
1440 ObjCMethodDecl::Required);
1441 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1442 SetterDecl,
1443 SourceLocation(),
1444 FD.D.getIdentifier(),
1445 T,
1446 VarDecl::None,
1447 0, 0);
1448 SetterDecl->setMethodParams(&Argument, 1);
1449 PIDecl->setSetterMethodDecl(SetterDecl);
1450 }
1451 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001452 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001453 *isOverridingProperty = true;
1454 return 0;
1455 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001456 // No matching property found in the main class. Just fall thru
1457 // and add property to the anonymous category. It looks like
1458 // it works as is. This category becomes just like a category
1459 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001460 } else {
1461 Diag(CDecl->getLocation(), diag::err_continuation_class);
1462 *isOverridingProperty = true;
1463 return 0;
1464 }
1465 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001466
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001467 Type *t = T.getTypePtr();
1468 if (t->isArrayType() || t->isFunctionType())
1469 Diag(AtLoc, diag::err_property_type) << T;
1470
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001471 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1472 FD.D.getIdentifier(), T);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001473 // Regardless of setter/getter attribute, we save the default getter/setter
1474 // selector names in anticipation of declaration of setter/getter methods.
1475 PDecl->setGetterName(GetterSel);
1476 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001477
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001478 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001479 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001480
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001481 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001482 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001483
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001484 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001485 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001486
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001487 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001488 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001489
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001490 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001491 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001492
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001493 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001494 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001495
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001496 if (isAssign)
1497 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1498
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001499 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001500 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001501
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001502 if (MethodImplKind == tok::objc_required)
1503 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1504 else if (MethodImplKind == tok::objc_optional)
1505 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1506
Chris Lattner4d391482007-12-12 07:09:47 +00001507 return PDecl;
1508}
1509
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001510/// ActOnPropertyImplDecl - This routine performs semantic checks and
1511/// builds the AST node for a property implementation declaration; declared
1512/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001513///
1514Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1515 SourceLocation PropertyLoc,
1516 bool Synthesize,
1517 DeclTy *ClassCatImpDecl,
1518 IdentifierInfo *PropertyId,
1519 IdentifierInfo *PropertyIvar) {
1520 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1521 // Make sure we have a context for the property implementation declaration.
1522 if (!ClassImpDecl) {
1523 Diag(AtLoc, diag::error_missing_property_context);
1524 return 0;
1525 }
1526 ObjCPropertyDecl *property = 0;
1527 ObjCInterfaceDecl* IDecl = 0;
1528 // Find the class or category class where this property must have
1529 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001530 ObjCImplementationDecl *IC = 0;
1531 ObjCCategoryImplDecl* CatImplClass = 0;
1532 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001533 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001534 // We always synthesize an interface for an implementation
1535 // without an interface decl. So, IDecl is always non-zero.
1536 assert(IDecl &&
1537 "ActOnPropertyImplDecl - @implementation without @interface");
1538
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001539 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001540 property = IDecl->FindPropertyDeclaration(PropertyId);
1541 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001542 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001543 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001544 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001545 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001546 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001547 if (Synthesize) {
1548 Diag(AtLoc, diag::error_synthesize_category_decl);
1549 return 0;
1550 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001551 IDecl = CatImplClass->getClassInterface();
1552 if (!IDecl) {
1553 Diag(AtLoc, diag::error_missing_property_interface);
1554 return 0;
1555 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001556 ObjCCategoryDecl *Category =
1557 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1558
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001559 // If category for this implementation not found, it is an error which
1560 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001561 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001562 return 0;
1563 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001564 property = Category->FindPropertyDeclaration(PropertyId);
1565 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001566 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001567 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001568 return 0;
1569 }
1570 }
1571 else {
1572 Diag(AtLoc, diag::error_bad_property_context);
1573 return 0;
1574 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001575 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001576 // Check that we have a valid, previously declared ivar for @synthesize
1577 if (Synthesize) {
1578 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001579 if (!PropertyIvar)
1580 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001581 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001582 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001583 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001584 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001585 return 0;
1586 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001587 QualType PropType = Context.getCanonicalType(property->getType());
1588 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1589
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001590 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001591 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001592 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001593 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001594 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001595 return 0;
1596 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001597 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001598 } else if (PropertyIvar) {
1599 // @dynamic
1600 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1601 return 0;
1602 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001603 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001604 ObjCPropertyImplDecl *PIDecl =
1605 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1606 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001607 ObjCPropertyImplDecl::Synthesize
1608 : ObjCPropertyImplDecl::Dynamic),
1609 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001610 if (IC) {
1611 if (Synthesize)
1612 if (ObjCPropertyImplDecl *PPIDecl =
1613 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1614 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1615 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1616 << PropertyIvar;
1617 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1618 }
1619
1620 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1621 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1622 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1623 return 0;
1624 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001625 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001626 }
1627 else {
1628 if (Synthesize)
1629 if (ObjCPropertyImplDecl *PPIDecl =
1630 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1631 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1632 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1633 << PropertyIvar;
1634 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1635 }
1636
1637 if (ObjCPropertyImplDecl *PPIDecl =
1638 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1639 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1640 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1641 return 0;
1642 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001643 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001644 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001645
1646 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001647}
Anders Carlsson15281452008-11-04 16:57:32 +00001648
Chris Lattnercc98eac2008-12-17 07:13:27 +00001649bool Sema::CheckObjCDeclScope(Decl *D) {
Anders Carlsson15281452008-11-04 16:57:32 +00001650 if (isa<TranslationUnitDecl>(CurContext))
1651 return false;
1652
1653 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1654 D->setInvalidDecl();
1655
1656 return true;
1657}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001658
1659/// Collect the instance variables declared in an Objective-C object. Used in
1660/// the creation of structures from objects using the @defs directive.
1661/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1662/// part of the AST generation logic of @defs.
1663static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1664 ASTContext& Ctx,
1665 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1666 if (Class->getSuperClass())
1667 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1668
1669 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1670 for (ObjCInterfaceDecl::ivar_iterator
1671 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1672
1673 ObjCIvarDecl* ID = *I;
1674 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1675 ID->getLocation(),
1676 ID->getIdentifier(),
1677 ID->getType(),
1678 ID->getBitWidth()));
1679 }
1680}
1681
1682/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1683/// instance variables of ClassName into Decls.
1684void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1685 IdentifierInfo *ClassName,
1686 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1687 // Check that ClassName is a valid class
1688 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1689 if (!Class) {
1690 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1691 return;
1692 }
1693 // Collect the instance variables
1694 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1695
1696 // Introduce all of these fields into the appropriate scope.
1697 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1698 D != Decls.end(); ++D) {
1699 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1700 if (getLangOptions().CPlusPlus)
1701 PushOnScopeChains(cast<FieldDecl>(FD), S);
1702 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
1703 Record->addDecl(Context, FD);
1704 }
1705}
1706