blob: 95737af18ac9e32e729140371a5b9c85e862a057 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000017#include "clang/Basic/Diagnostic.h"
18#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000019
20using namespace clang;
21
Ted Kremeneka526c5c2008-01-07 19:49:32 +000022/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000023/// and user declared, in the method definition's AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000024void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000025 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff394f3f42008-07-25 17:57:26 +000026 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
27
28 // If we don't have a valid method decl, simply return.
29 if (!MDecl)
30 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000031
32 // Allow the rest of sema to find private method decl implementations.
33 if (MDecl->isInstance())
34 AddInstanceMethodToGlobalPool(MDecl);
35 else
36 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000037
38 // Allow all of Sema to see that we are entering a method definition.
Chris Lattnerb048c982008-04-06 04:47:34 +000039 PushDeclContext(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000040
41 // Create Decl objects for each parameter, entrring them in the scope for
42 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000043
44 // Insert the invisible arguments, self and _cmd!
Daniel Dunbar451318c2008-08-26 06:07:48 +000045 MDecl->createImplicitParams(Context);
Chris Lattner4d391482007-12-12 07:09:47 +000046
Daniel Dunbar451318c2008-08-26 06:07:48 +000047 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
48 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000049
Chris Lattner8123a952008-04-10 02:22:51 +000050 // Introduce all of the other parameters into this scope.
Chris Lattner58cce3b2008-03-16 01:07:14 +000051 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +000052 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner04421082008-04-08 04:40:51 +000053 IdentifierInfo *II = PDecl->getIdentifier();
Argyrios Kyrtzidis642e38b2008-04-27 13:30:35 +000054 if (II)
55 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000056 }
57}
58
Chris Lattner7caeabd2008-07-21 22:17:28 +000059Sema::DeclTy *Sema::
60ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000063 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000064 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000065 assert(ClassName && "Missing class identifier");
66
67 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +000068 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000069 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000070 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000071 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000072 }
73
Ted Kremeneka526c5c2008-01-07 19:49:32 +000074 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000075 if (IDecl) {
76 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000077 if (!IDecl->isForwardDecl()) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000078 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000079 Diag(IDecl->getLocation(), diag::note_previous_definition);
80
Steve Naroffcfe8bf32008-11-18 19:15:30 +000081 // Return the previous class interface.
82 // FIXME: don't leak the objects passed in!
83 return IDecl;
84 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000085 IDecl->setLocation(AtInterfaceLoc);
86 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000087 }
Chris Lattnerb752f282008-07-21 07:06:49 +000088 } else {
Daniel Dunbarf6414922008-08-20 18:02:42 +000089 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000090 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000091 if (AttrList)
92 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +000093
Steve Naroff31102512008-04-02 18:30:49 +000094 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +000095 // Remember that this needs to be removed when the scope is popped.
96 TUScope->AddDecl(IDecl);
97 }
98
99 if (SuperName) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000100 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000101 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000102 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000103 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000104 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000105 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000106 }
107 else {
108 // Check that super class is previously defined
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000109 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000110
111 if (!SuperClassEntry)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000112 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000113 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
114 else if (SuperClassEntry->isForwardDecl())
115 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner08631c52008-11-23 21:45:46 +0000116 << SuperClassEntry->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000117 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000118 }
119 IDecl->setSuperClass(SuperClassEntry);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000120 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000121 IDecl->setLocEnd(SuperLoc);
122 } else { // we have a root class.
123 IDecl->setLocEnd(ClassLoc);
124 }
125
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000126 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000127 if (NumProtoRefs) {
128 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000129 IDecl->setLocEnd(EndProtoLoc);
130 }
Anders Carlsson15281452008-11-04 16:57:32 +0000131
132 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000133 return IDecl;
134}
135
136/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000137/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000138Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
139 IdentifierInfo *AliasName,
140 SourceLocation AliasLocation,
141 IdentifierInfo *ClassName,
142 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000143 // Look for previous declaration of alias name
Steve Naroffb327ce02008-04-02 14:35:35 +0000144 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000145 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000146 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000147 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000148 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000149 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000150 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000151 return 0;
152 }
153 // Check for class declaration
Steve Naroffb327ce02008-04-02 14:35:35 +0000154 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000155 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
156 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000157 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000158 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000159 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000160 return 0;
161 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000162
163 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000164 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe8043c32008-04-01 23:04:06 +0000165 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
166
167 ObjCAliasDecls[AliasName] = AliasDecl;
Anders Carlsson15281452008-11-04 16:57:32 +0000168
169 if (!CheckObjCDeclScope(AliasDecl))
170 TUScope->AddDecl(AliasDecl);
171
Chris Lattner4d391482007-12-12 07:09:47 +0000172 return AliasDecl;
173}
174
Chris Lattnere13b9592008-07-26 04:03:38 +0000175Sema::DeclTy *
176Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
177 IdentifierInfo *ProtocolName,
178 SourceLocation ProtocolLoc,
179 DeclTy * const *ProtoRefs,
180 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000181 SourceLocation EndProtoLoc,
182 AttributeList *AttrList) {
183 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000184 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000185 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000186 if (PDecl) {
187 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000188 if (!PDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000189 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000190 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000191 // Just return the protocol we already had.
192 // FIXME: don't leak the objects passed in!
193 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000194 }
Steve Narofff11b5082008-08-13 16:39:22 +0000195 // Make sure the cached decl gets a valid start location.
196 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000197 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000198 } else {
Chris Lattner780f3292008-07-21 21:32:27 +0000199 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattnerc8581052008-03-16 20:19:15 +0000200 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000201 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000202 }
Chris Lattner4d391482007-12-12 07:09:47 +0000203
204 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000205 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000206 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000207 PDecl->setLocEnd(EndProtoLoc);
208 }
Anders Carlsson15281452008-11-04 16:57:32 +0000209
210 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000211 return PDecl;
212}
213
214/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000215/// issues an error if they are not declared. It returns list of
216/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000217void
Chris Lattnere13b9592008-07-26 04:03:38 +0000218Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000219 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000220 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000221 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000222 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000223 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
224 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000225 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000226 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000227 continue;
228 }
229
230 // If this is a forward declaration and we are supposed to warn in this
231 // case, do it.
232 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000233 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000234 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000235 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000236 }
237}
238
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000239/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000240/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000241///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000242void
243Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
244 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000245 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000246 ObjCPropertyDecl::PropertyAttributeKind CAttr =
247 Property->getPropertyAttributes();
248 ObjCPropertyDecl::PropertyAttributeKind SAttr =
249 SuperProperty->getPropertyAttributes();
250 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
251 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000252 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000253 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000254 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
255 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000256 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000257 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000258 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
259 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000260 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000261 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000262
263 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
264 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000265 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000266 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000267 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000268 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000269 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000270 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000271 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000272 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000273
Chris Lattner717250a2008-07-26 20:50:02 +0000274 if (Context.getCanonicalType(Property->getType()) !=
275 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000276 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000277 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000278
279}
280
281/// ComparePropertiesInBaseAndSuper - This routine compares property
282/// declarations in base and its super class, if any, and issues
283/// diagnostics in a variety of inconsistant situations.
284///
285void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000286Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000287 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
288 if (!SDecl)
289 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000290 // FIXME: O(N^2)
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000291 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
292 E = SDecl->classprop_end(); S != E; ++S) {
293 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000294 // Does property in super class has declaration in current class?
295 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
296 E = IDecl->classprop_end(); I != E; ++I) {
297 ObjCPropertyDecl *PDecl = (*I);
298 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000299 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000300 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000301 }
302 }
303}
304
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000305/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
306/// of properties declared in a protocol and adds them to the list
307/// of properties for current class if it is not there already.
308void
309Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000310 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000311 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
312 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
313 E = PDecl->classprop_end(); P != E; ++P) {
314 ObjCPropertyDecl *Pr = (*P);
315 ObjCInterfaceDecl::classprop_iterator CP, CE;
316 // Is this property already in class's list of properties?
317 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
318 CP != CE; ++CP)
319 if ((*CP)->getIdentifier() == Pr->getIdentifier())
320 break;
321 if (CP == CE)
322 // Add this property to list of properties for thie class.
323 mergeProperties.push_back(Pr);
324 else
325 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000326 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000327 }
328 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
329}
330
331/// MergeProtocolPropertiesIntoClass - This routine merges properties
332/// declared in 'MergeItsProtocols' objects (which can be a class or an
333/// inherited protocol into the list of properties for class 'IDecl'
334///
335
336void
337Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
338 DeclTy *MergeItsProtocols) {
339 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattnerb752f282008-07-21 07:06:49 +0000340 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000341 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
342 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000343 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000344 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
345
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000346 // Go thru the list of protocols for this class and recursively merge
347 // their properties into this class as well.
348 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
349 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000350 MergeProtocolPropertiesIntoClass(IDecl, *P);
351 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000352 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
353 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
354 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000355 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000356 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000357}
358
Chris Lattner4d391482007-12-12 07:09:47 +0000359/// ActOnForwardProtocolDeclaration -
360Action::DeclTy *
361Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000362 const IdentifierLocPair *IdentList,
363 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000364 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000365
366 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000367 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000368 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattner7caeabd2008-07-21 22:17:28 +0000369 if (PDecl == 0) // Not already seen?
370 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner4d391482007-12-12 07:09:47 +0000371
372 Protocols.push_back(PDecl);
373 }
Anders Carlsson15281452008-11-04 16:57:32 +0000374
375 ObjCForwardProtocolDecl *PDecl =
376 ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
377 &Protocols[0], Protocols.size());
378
379 CheckObjCDeclScope(PDecl);
380 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000381}
382
Chris Lattner7caeabd2008-07-21 22:17:28 +0000383Sema::DeclTy *Sema::
384ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
385 IdentifierInfo *ClassName, SourceLocation ClassLoc,
386 IdentifierInfo *CategoryName,
387 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000388 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000389 unsigned NumProtoRefs,
390 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000391 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000392
Chris Lattner61f9d412008-03-16 20:34:23 +0000393 ObjCCategoryDecl *CDecl =
Chris Lattner68c82cf2008-03-16 20:47:45 +0000394 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000395 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000396
397 /// Check that class of this category is already completely declared.
398 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000399 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffd100c802008-06-05 15:03:27 +0000400 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000401 /// Check for duplicate interface declaration for this category
402 ObjCCategoryDecl *CDeclChain;
403 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
404 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000405 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000406 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner3c73c412008-11-19 08:23:25 +0000407 << ClassName << CategoryName;
Chris Lattner6ff0fc32008-11-23 22:38:38 +0000408 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000409 break;
410 }
Chris Lattner4d391482007-12-12 07:09:47 +0000411 }
Steve Naroffd100c802008-06-05 15:03:27 +0000412 if (!CDeclChain)
413 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000414 }
Chris Lattner4d391482007-12-12 07:09:47 +0000415
416 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000417 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
418 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000419 }
Anders Carlsson15281452008-11-04 16:57:32 +0000420
421 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000422 return CDecl;
423}
424
425/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000426/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000427/// object.
428Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
429 SourceLocation AtCatImplLoc,
430 IdentifierInfo *ClassName, SourceLocation ClassLoc,
431 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000432 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000433 ObjCCategoryImplDecl *CDecl =
434 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000435 /// Check that class of this category is already completely declared.
436 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000437 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000438
439 /// TODO: Check that CatName, category name, is not used in another
440 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000441 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000442
443 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000444 return CDecl;
445}
446
447Sema::DeclTy *Sema::ActOnStartClassImplementation(
448 SourceLocation AtClassImplLoc,
449 IdentifierInfo *ClassName, SourceLocation ClassLoc,
450 IdentifierInfo *SuperClassname,
451 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000452 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000453 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000454 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000455 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000456 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000457 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000458 }
459 else {
460 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000461 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000462 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000463 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000464 }
465
466 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000467 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000468 if (SuperClassname) {
469 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000470 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000471 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000472 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
473 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000474 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000475 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000476 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000477 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000478 Diag(SuperClassLoc, diag::err_undef_superclass)
479 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000480 else if (IDecl && IDecl->getSuperClass() != SDecl) {
481 // This implementation and its interface do not have the same
482 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000483 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000484 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000485 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000486 }
487 }
488 }
489
490 if (!IDecl) {
491 // Legacy case of @implementation with no corresponding @interface.
492 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000493
494 // FIXME: Do we support attributes on the @implementation? If so
495 // we should copy them over.
Chris Lattnerb752f282008-07-21 07:06:49 +0000496 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000497 ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000498 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000499 IDecl->setSuperClass(SDecl);
500 IDecl->setLocEnd(ClassLoc);
501
502 // Remember that this needs to be removed when the scope is popped.
503 TUScope->AddDecl(IDecl);
504 }
505
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000506 ObjCImplementationDecl* IMPDecl =
Chris Lattner75c9cae2008-03-16 20:53:07 +0000507 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
508 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000509
Anders Carlsson15281452008-11-04 16:57:32 +0000510 if (CheckObjCDeclScope(IMPDecl))
511 return IMPDecl;
512
Chris Lattner4d391482007-12-12 07:09:47 +0000513 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000514 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000515 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000516 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000517 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000518 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000519 return IMPDecl;
520}
521
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000522void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
523 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000524 SourceLocation RBrace) {
525 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000526 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000527 if (!IDecl)
528 return;
529 /// Check case of non-existing @interface decl.
530 /// (legacy objective-c @implementation decl without an @interface decl).
531 /// Add implementations's ivar to the synthesize class's ivar list.
532 if (IDecl->ImplicitInterfaceDecl()) {
533 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
534 return;
535 }
536 // If implementation has empty ivar list, just return.
537 if (numIvars == 0)
538 return;
539
540 assert(ivars && "missing @implementation ivars");
541
542 // Check interface's Ivar list against those in the implementation.
543 // names and types must match.
544 //
Chris Lattner4d391482007-12-12 07:09:47 +0000545 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000546 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000547 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
548 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000549 ObjCIvarDecl* ImplIvar = ivars[j++];
550 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000551 assert (ImplIvar && "missing implementation ivar");
552 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000553 if (Context.getCanonicalType(ImplIvar->getType()) !=
554 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000555 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000556 << ImplIvar->getIdentifier()
557 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000558 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000559 }
560 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
561 // as error.
562 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000563 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000564 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000565 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000566 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000567 }
568 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000569 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000570
571 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000572 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000573 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000574 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000575}
576
Steve Naroff3c2eb662008-02-10 21:38:56 +0000577void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
578 bool &IncompleteImpl) {
579 if (!IncompleteImpl) {
580 Diag(ImpLoc, diag::warn_incomplete_impl);
581 IncompleteImpl = true;
582 }
Chris Lattner08631c52008-11-23 21:45:46 +0000583 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000584}
585
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000586/// FIXME: Type hierarchies in Objective-C can be deep. We could most
587/// likely improve the efficiency of selector lookups and type
588/// checking by associating with each protocol / interface / category
589/// the flattened instance tables. If we used an immutable set to keep
590/// the table then it wouldn't add significant memory cost and it
591/// would be handy for lookups.
592
Steve Naroffefe7f362008-02-08 22:06:17 +0000593/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000594/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000595void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
596 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000597 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000598 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000599 const llvm::DenseSet<Selector> &ClsMap,
600 ObjCInterfaceDecl *IDecl) {
601 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
602
603 // If a method lookup fails locally we still need to look and see if
604 // the method was implemented by a base class or an inherited
605 // protocol. This lookup is slow, but occurs rarely in correct code
606 // and otherwise would terminate in a warning.
607
Chris Lattner4d391482007-12-12 07:09:47 +0000608 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000609 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000610 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000611 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000612 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000613 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000614 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000615 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000616 }
617 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000618 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000619 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000620 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000621 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
622 !ClsMap.count(method->getSelector()) &&
623 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000624 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000625 }
Chris Lattner780f3292008-07-21 21:32:27 +0000626 // Check on this protocols's referenced protocols, recursively.
627 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
628 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000629 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000630}
631
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000632void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
633 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000634 llvm::DenseSet<Selector> InsMap;
635 // Check and see if instance methods in class interface have been
636 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000637 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000638 E = IMPDecl->instmeth_end(); I != E; ++I)
639 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000640
641 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000642 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000643 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000644 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000645 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4c525092007-12-12 17:58:05 +0000646
Chris Lattner4d391482007-12-12 07:09:47 +0000647 llvm::DenseSet<Selector> ClsMap;
648 // Check and see if class methods in class interface have been
649 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000650 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000651 E = IMPDecl->classmeth_end(); I != E; ++I)
652 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000653
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000654 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000655 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000656 if (!ClsMap.count((*I)->getSelector()))
657 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000658
659 // Check the protocol list for unimplemented methods in the @implementation
660 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000661 const ObjCList<ObjCProtocolDecl> &Protocols =
662 IDecl->getReferencedProtocols();
663 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
664 E = Protocols.end(); I != E; ++I)
665 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000666 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000667}
668
669/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000670/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000671void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
672 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000673 llvm::DenseSet<Selector> InsMap;
674 // Check and see if instance methods in category interface have been
675 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000676 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000677 E = CatImplDecl->instmeth_end(); I != E; ++I)
678 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000679
680 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000681 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000682 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000683 if (!InsMap.count((*I)->getSelector()))
684 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
685
Chris Lattner4d391482007-12-12 07:09:47 +0000686 llvm::DenseSet<Selector> ClsMap;
687 // Check and see if class methods in category interface have been
688 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000689 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000690 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
691 I != E; ++I)
692 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000693
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000694 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000695 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000696 if (!ClsMap.count((*I)->getSelector()))
697 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000698
699 // Check the protocol list for unimplemented methods in the @implementation
700 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000701 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
702 E = CatClassDecl->protocol_end(); PI != E; ++PI)
703 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000704 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000705}
706
707/// ActOnForwardClassDeclaration -
708Action::DeclTy *
709Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
710 IdentifierInfo **IdentList, unsigned NumElts)
711{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000712 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000713
714 for (unsigned i = 0; i != NumElts; ++i) {
715 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000716 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000717 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000718 // GCC apparently allows the following idiom:
719 //
720 // typedef NSObject < XCElementTogglerP > XCElementToggler;
721 // @class XCElementToggler;
722 //
723 // FIXME: Make an extension?
724 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
725 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000726 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000727 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000728 }
Chris Lattner4d391482007-12-12 07:09:47 +0000729 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000730 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000731 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattnerb752f282008-07-21 07:06:49 +0000732 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000733 SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000734 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000735
736 // Remember that this needs to be removed when the scope is popped.
737 TUScope->AddDecl(IDecl);
738 }
739
740 Interfaces.push_back(IDecl);
741 }
742
Anders Carlsson15281452008-11-04 16:57:32 +0000743 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
744 &Interfaces[0],
745 Interfaces.size());
746
747 CheckObjCDeclScope(CDecl);
748 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000749}
750
751
752/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
753/// returns true, or false, accordingly.
754/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000755bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000756 const ObjCMethodDecl *PrevMethod,
757 bool matchBasedOnSizeAndAlignment) {
758 QualType T1 = Context.getCanonicalType(Method->getResultType());
759 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
760
761 if (T1 != T2) {
762 // The result types are different.
763 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000764 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000765 // Incomplete types don't have a size and alignment.
766 if (T1->isIncompleteType() || T2->isIncompleteType())
767 return false;
768 // Check is based on size and alignment.
769 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
770 return false;
771 }
772 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
773 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
774 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
775 if (T1 != T2) {
776 // The result types are different.
777 if (!matchBasedOnSizeAndAlignment)
778 return false;
779 // Incomplete types don't have a size and alignment.
780 if (T1->isIncompleteType() || T2->isIncompleteType())
781 return false;
782 // Check is based on size and alignment.
783 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
784 return false;
785 }
Chris Lattner4d391482007-12-12 07:09:47 +0000786 }
787 return true;
788}
789
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000790void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
791 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000792 if (!FirstMethod.Method) {
793 // Haven't seen a method with this selector name yet - add it.
794 FirstMethod.Method = Method;
795 FirstMethod.Next = 0;
796 } else {
797 // We've seen a method with this name, now check the type signature(s).
798 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
799
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000800 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000801 Next = Next->Next)
802 match = MatchTwoMethodDeclarations(Method, Next->Method);
803
804 if (!match) {
805 // We have a new signature for an existing method - add it.
806 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +0000807 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +0000808 }
809 }
810}
811
Steve Naroff6f5f41c2008-10-21 10:50:19 +0000812// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +0000813ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
814 SourceRange R) {
815 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000816 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +0000817
818 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000819 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
820 // This checks if the methods differ by size & alignment.
821 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
822 issueWarning = true;
823 }
824 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000825 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +0000826 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000827 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +0000828 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +0000829 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000830 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +0000831 }
832 return MethList.Method;
833}
834
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000835void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
836 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000837 if (!FirstMethod.Method) {
838 // Haven't seen a method with this selector name yet - add it.
839 FirstMethod.Method = Method;
840 FirstMethod.Next = 0;
841 } else {
842 // We've seen a method with this name, now check the type signature(s).
843 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
844
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000845 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000846 Next = Next->Next)
847 match = MatchTwoMethodDeclarations(Method, Next->Method);
848
849 if (!match) {
850 // We have a new signature for an existing method - add it.
851 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000852 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000853 FirstMethod.Next = OMI;
854 }
855 }
856}
857
Steve Naroffa56f6162007-12-18 01:30:32 +0000858// Note: For class/category implemenations, allMethods/allProperties is
859// always null.
Chris Lattner4d391482007-12-12 07:09:47 +0000860void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
861 DeclTy **allMethods, unsigned allNum,
862 DeclTy **allProperties, unsigned pNum) {
863 Decl *ClassDecl = static_cast<Decl *>(classDecl);
864
Steve Naroffa56f6162007-12-18 01:30:32 +0000865 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
866 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +0000867 // should be true.
868 if (!ClassDecl)
869 return;
870
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000871 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
872 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +0000873
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000874 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
875 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +0000876
877 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000878 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
879 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000880 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000881
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000882 if (pNum != 0) {
Chris Lattner55d13b42008-03-16 21:23:50 +0000883 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
884 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000885 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
886 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
887 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000888 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000889 else
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000890 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000891 }
Chris Lattner4d391482007-12-12 07:09:47 +0000892
893 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000894 ObjCMethodDecl *Method =
895 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +0000896
897 if (!Method) continue; // Already issued a diagnostic.
898 if (Method->isInstance()) {
899 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000900 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000901 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
902 : false;
903 if (isInterfaceDeclKind && PrevMethod && !match
904 || checkIdenticalMethods && match) {
Chris Lattner5f4a6822008-11-23 23:12:31 +0000905 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000906 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000907 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000908 } else {
909 insMethods.push_back(Method);
910 InsMap[Method->getSelector()] = Method;
911 /// The following allows us to typecheck messages to "id".
912 AddInstanceMethodToGlobalPool(Method);
913 }
914 }
915 else {
916 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000917 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000918 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
919 : false;
920 if (isInterfaceDeclKind && PrevMethod && !match
921 || checkIdenticalMethods && match) {
Chris Lattner5f4a6822008-11-23 23:12:31 +0000922 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000923 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000924 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000925 } else {
926 clsMethods.push_back(Method);
927 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +0000928 /// The following allows us to typecheck messages to "Class".
929 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +0000930 }
931 }
932 }
Steve Naroff5e0a74f2008-09-29 14:20:56 +0000933 // Save the size so we can detect if we've added any property methods.
934 unsigned int insMethodsSizePriorToPropAdds = insMethods.size();
935 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size();
Chris Lattner4d391482007-12-12 07:09:47 +0000936
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000937 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000938 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000939 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000940 ComparePropertiesInBaseAndSuper(I);
941 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000942 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
943 e = I->classprop_end(); i != e; ++i)
944 I->addPropertyMethods(Context, *i, insMethods);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000945 I->addMethods(&insMethods[0], insMethods.size(),
946 &clsMethods[0], clsMethods.size(), AtEndLoc);
947
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000948 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000949 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
950 e = P->classprop_end(); i != e; ++i)
951 P->addPropertyMethods(Context, *i, insMethods);
Chris Lattner4d391482007-12-12 07:09:47 +0000952 P->addMethods(&insMethods[0], insMethods.size(),
953 &clsMethods[0], clsMethods.size(), AtEndLoc);
954 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000955 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000956 // FIXME: Need to compare properties to those in interface?
957
958 // FIXME: If we merge properties into class we should probably
959 // merge them into category as well?
960 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
961 e = C->classprop_end(); i != e; ++i)
962 C->addPropertyMethods(Context, *i, insMethods);
Chris Lattner4d391482007-12-12 07:09:47 +0000963 C->addMethods(&insMethods[0], insMethods.size(),
964 &clsMethods[0], clsMethods.size(), AtEndLoc);
965 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000966 else if (ObjCImplementationDecl *IC =
967 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000968 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000969 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +0000970 ImplMethodsVsClassMethods(IC, IDecl);
971 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000972 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000973 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000974 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000975 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000976 // in this interface are implemented in the category @implementation.
Chris Lattner4d391482007-12-12 07:09:47 +0000977 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000978 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +0000979 Categories; Categories = Categories->getNextClassCategory()) {
980 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
981 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
982 break;
983 }
984 }
985 }
986 }
Steve Naroff5e0a74f2008-09-29 14:20:56 +0000987 // Add any synthesized methods to the global pool. This allows us to
988 // handle the following, which is supported by GCC (and part of the design).
989 //
990 // @interface Foo
991 // @property double bar;
992 // @end
993 //
994 // void thisIsUnfortunate() {
995 // id foo;
996 // double bar = [foo bar];
997 // }
998 //
999 if (insMethodsSizePriorToPropAdds < insMethods.size())
1000 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++)
1001 AddInstanceMethodToGlobalPool(insMethods[i]);
1002 if (clsMethodsSizePriorToPropAdds < clsMethods.size())
1003 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++)
1004 AddFactoryMethodToGlobalPool(clsMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001005}
1006
1007
1008/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1009/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001010static Decl::ObjCDeclQualifier
1011CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1012 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1013 if (PQTVal & ObjCDeclSpec::DQ_In)
1014 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1015 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1016 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1017 if (PQTVal & ObjCDeclSpec::DQ_Out)
1018 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1019 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1020 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1021 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1022 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1023 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1024 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001025
1026 return ret;
1027}
1028
1029Sema::DeclTy *Sema::ActOnMethodDeclaration(
1030 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001031 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001032 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001033 Selector Sel,
1034 // optional arguments. The number of types/arguments is obtained
1035 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001036 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +00001037 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1038 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001039 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001040
1041 // Make sure we can establish a context for the method.
1042 if (!ClassDecl) {
1043 Diag(MethodLoc, diag::error_missing_method_context);
1044 return 0;
1045 }
Chris Lattner4d391482007-12-12 07:09:47 +00001046 QualType resultDeclType;
1047
1048 if (ReturnType)
1049 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1050 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001051 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001052
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001053 ObjCMethodDecl* ObjCMethod =
1054 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbarf6414922008-08-20 18:02:42 +00001055 ClassDecl,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001056 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001057 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001058 MethodDeclKind == tok::objc_optional ?
1059 ObjCMethodDecl::Optional :
1060 ObjCMethodDecl::Required);
1061
Chris Lattner0ed844b2008-04-04 06:12:32 +00001062 llvm::SmallVector<ParmVarDecl*, 16> Params;
1063
1064 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1065 // FIXME: arg->AttrList must be stored too!
1066 QualType argType;
1067
1068 if (ArgTypes[i])
1069 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1070 else
1071 argType = Context.getObjCIdType();
1072 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1073 SourceLocation(/*FIXME*/),
1074 ArgNames[i], argType,
Chris Lattner04421082008-04-08 04:40:51 +00001075 VarDecl::None, 0, 0);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001076 Param->setObjCDeclQualifier(
1077 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1078 Params.push_back(Param);
1079 }
1080
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001081 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1082 ObjCMethod->setObjCDeclQualifier(
1083 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1084 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001085
1086 if (AttrList)
1087 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001088
1089 // For implementations (which can be very "coarse grain"), we add the
1090 // method now. This allows the AST to implement lookup methods that work
1091 // incrementally (without waiting until we parse the @end). It also allows
1092 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001093 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001094 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001095 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001096 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001097 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001098 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001099 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001100 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001101 }
1102 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001103 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001104 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001105 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001106 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001107 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001108 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001109 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001110 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001111 }
1112 }
1113 if (PrevMethod) {
1114 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001115 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001116 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001117 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001118 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001119 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001120}
1121
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001122void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1123 SourceLocation Loc,
1124 unsigned &Attributes) {
1125 // FIXME: Improve the reported location.
1126
1127 // readonly and readwrite conflict.
1128 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1129 (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001130 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1131 << "readonly" << "readwrite";
1132 Attributes &= ~ObjCDeclSpec::DQ_PR_readonly;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001133 }
1134
1135 // Check for copy or retain on non-object types.
1136 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1137 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001138 Diag(Loc, diag::err_objc_property_requires_object)
1139 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001140 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1141 }
1142
1143 // Check for more than one of { assign, copy, retain }.
1144 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1145 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001146 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1147 << "assign" << "copy";
1148 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001149 }
1150 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001151 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1152 << "assign" << "retain";
1153 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001154 }
1155 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1156 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001157 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1158 << "copy" << "retain";
1159 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001160 }
1161 }
1162
1163 // Warn if user supplied no assignment attribute, property is
1164 // readwrite, and this is an object type.
1165 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1166 ObjCDeclSpec::DQ_PR_retain)) &&
1167 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1168 Context.isObjCObjectPointerType(PropertyTy)) {
1169 // Skip this warning in gc-only mode.
1170 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1171 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1172
1173 // If non-gc code warn that this is likely inappropriate.
1174 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1175 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1176
1177 // FIXME: Implement warning dependent on NSCopying being
1178 // implemented. See also:
1179 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1180 // (please trim this list while you are at it).
1181 }
1182}
1183
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001184Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1185 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001186 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001187 Selector GetterSel,
1188 Selector SetterSel,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001189 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001190 QualType T = GetTypeForDeclarator(FD.D, S);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001191 unsigned Attributes = ODS.getPropertyAttributes();
1192
1193 // May modify Attributes.
1194 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
1195
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001196 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1197 FD.D.getIdentifier(), T);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001198 // Regardless of setter/getter attribute, we save the default getter/setter
1199 // selector names in anticipation of declaration of setter/getter methods.
1200 PDecl->setGetterName(GetterSel);
1201 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001202
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001203 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001204 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001205
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001206 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001207 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001208
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001209 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001210 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001211
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001212 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001213 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner4d391482007-12-12 07:09:47 +00001214
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001215 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001216 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001217
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001218 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001219 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001220
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001221 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001222 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001223
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001224 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001225 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001226
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001227 if (MethodImplKind == tok::objc_required)
1228 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1229 else if (MethodImplKind == tok::objc_optional)
1230 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1231
Chris Lattner4d391482007-12-12 07:09:47 +00001232 return PDecl;
1233}
1234
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001235/// ActOnPropertyImplDecl - This routine performs semantic checks and
1236/// builds the AST node for a property implementation declaration; declared
1237/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001238///
1239Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1240 SourceLocation PropertyLoc,
1241 bool Synthesize,
1242 DeclTy *ClassCatImpDecl,
1243 IdentifierInfo *PropertyId,
1244 IdentifierInfo *PropertyIvar) {
1245 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1246 // Make sure we have a context for the property implementation declaration.
1247 if (!ClassImpDecl) {
1248 Diag(AtLoc, diag::error_missing_property_context);
1249 return 0;
1250 }
1251 ObjCPropertyDecl *property = 0;
1252 ObjCInterfaceDecl* IDecl = 0;
1253 // Find the class or category class where this property must have
1254 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001255 ObjCImplementationDecl *IC = 0;
1256 ObjCCategoryImplDecl* CatImplClass = 0;
1257 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001258 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001259 // We always synthesize an interface for an implementation
1260 // without an interface decl. So, IDecl is always non-zero.
1261 assert(IDecl &&
1262 "ActOnPropertyImplDecl - @implementation without @interface");
1263
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001264 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001265 property = IDecl->FindPropertyDeclaration(PropertyId);
1266 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001267 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001268 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001269 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001270 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001271 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001272 if (Synthesize) {
1273 Diag(AtLoc, diag::error_synthesize_category_decl);
1274 return 0;
1275 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001276 IDecl = CatImplClass->getClassInterface();
1277 if (!IDecl) {
1278 Diag(AtLoc, diag::error_missing_property_interface);
1279 return 0;
1280 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001281 ObjCCategoryDecl *Category =
1282 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1283
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001284 // If category for this implementation not found, it is an error which
1285 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001286 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001287 return 0;
1288 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001289 property = Category->FindPropertyDeclaration(PropertyId);
1290 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001291 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001292 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001293 return 0;
1294 }
1295 }
1296 else {
1297 Diag(AtLoc, diag::error_bad_property_context);
1298 return 0;
1299 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001300 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001301 // Check that we have a valid, previously declared ivar for @synthesize
1302 if (Synthesize) {
1303 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001304 if (!PropertyIvar)
1305 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001306 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001307 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001308 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001309 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001310 return 0;
1311 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001312 QualType PropType = Context.getCanonicalType(property->getType());
1313 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1314
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001315 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001316 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001317 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001318 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001319 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001320 return 0;
1321 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001322 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001323 } else if (PropertyIvar) {
1324 // @dynamic
1325 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1326 return 0;
1327 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001328 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001329 ObjCPropertyImplDecl *PIDecl =
1330 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1331 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001332 ObjCPropertyImplDecl::Synthesize
1333 : ObjCPropertyImplDecl::Dynamic),
1334 Ivar);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001335 if (IC)
1336 IC->addPropertyImplementation(PIDecl);
1337 else
1338 CatImplClass->addPropertyImplementation(PIDecl);
1339
1340 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001341}
Anders Carlsson15281452008-11-04 16:57:32 +00001342
1343bool Sema::CheckObjCDeclScope(Decl *D)
1344{
1345 if (isa<TranslationUnitDecl>(CurContext))
1346 return false;
1347
1348 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1349 D->setInvalidDecl();
1350
1351 return true;
1352}