blob: 64fd6c515d7fa85928332ae62c3d5958056eacaf [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 Lattnerfa25bbb2008-11-19 05:08:23 +000078 Diag(AtInterfaceLoc, diag::err_duplicate_class_def) << IDecl->getName();
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,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000245 const char *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)
253 << Property->getName() << 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)
257 << Property->getName() << "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)
261 << Property->getName() << "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)
266 << Property->getName() << "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)
269 << Property->getName() << "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)
272 << Property->getName() << "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)
277 << Property->getType().getAsString() << 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,
300 SDecl->getIdentifierName());
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,
310 ObjCProtocolDecl *PDecl)
311{
312 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
313 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
314 E = PDecl->classprop_end(); P != E; ++P) {
315 ObjCPropertyDecl *Pr = (*P);
316 ObjCInterfaceDecl::classprop_iterator CP, CE;
317 // Is this property already in class's list of properties?
318 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
319 CP != CE; ++CP)
320 if ((*CP)->getIdentifier() == Pr->getIdentifier())
321 break;
322 if (CP == CE)
323 // Add this property to list of properties for thie class.
324 mergeProperties.push_back(Pr);
325 else
326 // Property protocol already exist in class. Diagnose any mismatch.
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000327 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifierName());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000328 }
329 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
330}
331
332/// MergeProtocolPropertiesIntoClass - This routine merges properties
333/// declared in 'MergeItsProtocols' objects (which can be a class or an
334/// inherited protocol into the list of properties for class 'IDecl'
335///
336
337void
338Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
339 DeclTy *MergeItsProtocols) {
340 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattnerb752f282008-07-21 07:06:49 +0000341 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000342 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
343 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000344 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000345 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
346
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000347 // Go thru the list of protocols for this class and recursively merge
348 // their properties into this class as well.
349 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
350 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000351 MergeProtocolPropertiesIntoClass(IDecl, *P);
352 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000353 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
354 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
355 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000356 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000357 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000358}
359
Chris Lattner4d391482007-12-12 07:09:47 +0000360/// ActOnForwardProtocolDeclaration -
361Action::DeclTy *
362Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000363 const IdentifierLocPair *IdentList,
364 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000365 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000366
367 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000368 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000369 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattner7caeabd2008-07-21 22:17:28 +0000370 if (PDecl == 0) // Not already seen?
371 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner4d391482007-12-12 07:09:47 +0000372
373 Protocols.push_back(PDecl);
374 }
Anders Carlsson15281452008-11-04 16:57:32 +0000375
376 ObjCForwardProtocolDecl *PDecl =
377 ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
378 &Protocols[0], Protocols.size());
379
380 CheckObjCDeclScope(PDecl);
381 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000382}
383
Chris Lattner7caeabd2008-07-21 22:17:28 +0000384Sema::DeclTy *Sema::
385ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
386 IdentifierInfo *ClassName, SourceLocation ClassLoc,
387 IdentifierInfo *CategoryName,
388 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000389 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000390 unsigned NumProtoRefs,
391 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000392 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000393
Chris Lattner61f9d412008-03-16 20:34:23 +0000394 ObjCCategoryDecl *CDecl =
Chris Lattner68c82cf2008-03-16 20:47:45 +0000395 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000396 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000397
398 /// Check that class of this category is already completely declared.
399 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000400 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffd100c802008-06-05 15:03:27 +0000401 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000402 /// Check for duplicate interface declaration for this category
403 ObjCCategoryDecl *CDeclChain;
404 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
405 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000406 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000407 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner3c73c412008-11-19 08:23:25 +0000408 << ClassName << CategoryName;
Chris Lattner6ff0fc32008-11-23 22:38:38 +0000409 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000410 break;
411 }
Chris Lattner4d391482007-12-12 07:09:47 +0000412 }
Steve Naroffd100c802008-06-05 15:03:27 +0000413 if (!CDeclChain)
414 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000415 }
Chris Lattner4d391482007-12-12 07:09:47 +0000416
417 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000418 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
419 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000420 }
Anders Carlsson15281452008-11-04 16:57:32 +0000421
422 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000423 return CDecl;
424}
425
426/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000427/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000428/// object.
429Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
430 SourceLocation AtCatImplLoc,
431 IdentifierInfo *ClassName, SourceLocation ClassLoc,
432 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000433 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000434 ObjCCategoryImplDecl *CDecl =
435 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000436 /// Check that class of this category is already completely declared.
437 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000438 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000439
440 /// TODO: Check that CatName, category name, is not used in another
441 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000442 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000443
444 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000445 return CDecl;
446}
447
448Sema::DeclTy *Sema::ActOnStartClassImplementation(
449 SourceLocation AtClassImplLoc,
450 IdentifierInfo *ClassName, SourceLocation ClassLoc,
451 IdentifierInfo *SuperClassname,
452 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000453 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000454 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000455 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000456 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000457 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000458 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000459 }
460 else {
461 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000462 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000463 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000464 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000465 }
466
467 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000468 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000469 if (SuperClassname) {
470 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000471 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000472 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000473 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
474 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000475 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000476 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000477 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000478 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000479 Diag(SuperClassLoc, diag::err_undef_superclass)
480 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000481 else if (IDecl && IDecl->getSuperClass() != SDecl) {
482 // This implementation and its interface do not have the same
483 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000484 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000485 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000486 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000487 }
488 }
489 }
490
491 if (!IDecl) {
492 // Legacy case of @implementation with no corresponding @interface.
493 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000494
495 // FIXME: Do we support attributes on the @implementation? If so
496 // we should copy them over.
Chris Lattnerb752f282008-07-21 07:06:49 +0000497 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000498 ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000499 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000500 IDecl->setSuperClass(SDecl);
501 IDecl->setLocEnd(ClassLoc);
502
503 // Remember that this needs to be removed when the scope is popped.
504 TUScope->AddDecl(IDecl);
505 }
506
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000507 ObjCImplementationDecl* IMPDecl =
Chris Lattner75c9cae2008-03-16 20:53:07 +0000508 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
509 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000510
Anders Carlsson15281452008-11-04 16:57:32 +0000511 if (CheckObjCDeclScope(IMPDecl))
512 return IMPDecl;
513
Chris Lattner4d391482007-12-12 07:09:47 +0000514 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000515 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000516 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000517 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000518 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000519 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000520 return IMPDecl;
521}
522
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000523void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
524 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000525 SourceLocation RBrace) {
526 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000527 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000528 if (!IDecl)
529 return;
530 /// Check case of non-existing @interface decl.
531 /// (legacy objective-c @implementation decl without an @interface decl).
532 /// Add implementations's ivar to the synthesize class's ivar list.
533 if (IDecl->ImplicitInterfaceDecl()) {
534 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
535 return;
536 }
537 // If implementation has empty ivar list, just return.
538 if (numIvars == 0)
539 return;
540
541 assert(ivars && "missing @implementation ivars");
542
543 // Check interface's Ivar list against those in the implementation.
544 // names and types must match.
545 //
Chris Lattner4d391482007-12-12 07:09:47 +0000546 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000547 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000548 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
549 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000550 ObjCIvarDecl* ImplIvar = ivars[j++];
551 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000552 assert (ImplIvar && "missing implementation ivar");
553 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000554 if (Context.getCanonicalType(ImplIvar->getType()) !=
555 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000556 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000557 << ImplIvar->getIdentifier()
558 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000559 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000560 }
561 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
562 // as error.
563 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000564 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000565 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000566 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000567 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000568 }
569 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000570 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000571
572 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000573 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000574 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000575 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000576}
577
Steve Naroff3c2eb662008-02-10 21:38:56 +0000578void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
579 bool &IncompleteImpl) {
580 if (!IncompleteImpl) {
581 Diag(ImpLoc, diag::warn_incomplete_impl);
582 IncompleteImpl = true;
583 }
Chris Lattner08631c52008-11-23 21:45:46 +0000584 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000585}
586
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000587/// FIXME: Type hierarchies in Objective-C can be deep. We could most
588/// likely improve the efficiency of selector lookups and type
589/// checking by associating with each protocol / interface / category
590/// the flattened instance tables. If we used an immutable set to keep
591/// the table then it wouldn't add significant memory cost and it
592/// would be handy for lookups.
593
Steve Naroffefe7f362008-02-08 22:06:17 +0000594/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000595/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000596void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
597 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000598 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000599 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000600 const llvm::DenseSet<Selector> &ClsMap,
601 ObjCInterfaceDecl *IDecl) {
602 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
603
604 // If a method lookup fails locally we still need to look and see if
605 // the method was implemented by a base class or an inherited
606 // protocol. This lookup is slow, but occurs rarely in correct code
607 // and otherwise would terminate in a warning.
608
Chris Lattner4d391482007-12-12 07:09:47 +0000609 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000610 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000611 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000612 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000613 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
614 !InsMap.count(method->getSelector()) &&
615 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000616 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000617 }
618 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000619 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000620 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000621 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000622 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
623 !ClsMap.count(method->getSelector()) &&
624 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000625 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000626 }
Chris Lattner780f3292008-07-21 21:32:27 +0000627 // Check on this protocols's referenced protocols, recursively.
628 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
629 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000630 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000631}
632
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000633void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
634 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000635 llvm::DenseSet<Selector> InsMap;
636 // Check and see if instance methods in class interface have been
637 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000638 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000639 E = IMPDecl->instmeth_end(); I != E; ++I)
640 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000641
642 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000643 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000644 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000645 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000646 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4c525092007-12-12 17:58:05 +0000647
Chris Lattner4d391482007-12-12 07:09:47 +0000648 llvm::DenseSet<Selector> ClsMap;
649 // Check and see if class methods in class interface have been
650 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000651 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000652 E = IMPDecl->classmeth_end(); I != E; ++I)
653 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000654
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000655 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000656 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000657 if (!ClsMap.count((*I)->getSelector()))
658 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000659
660 // Check the protocol list for unimplemented methods in the @implementation
661 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000662 const ObjCList<ObjCProtocolDecl> &Protocols =
663 IDecl->getReferencedProtocols();
664 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
665 E = Protocols.end(); I != E; ++I)
666 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000667 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000668}
669
670/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000671/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000672void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
673 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000674 llvm::DenseSet<Selector> InsMap;
675 // Check and see if instance methods in category interface have been
676 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000677 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000678 E = CatImplDecl->instmeth_end(); I != E; ++I)
679 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000680
681 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000682 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000683 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000684 if (!InsMap.count((*I)->getSelector()))
685 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
686
Chris Lattner4d391482007-12-12 07:09:47 +0000687 llvm::DenseSet<Selector> ClsMap;
688 // Check and see if class methods in category interface have been
689 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000690 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000691 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
692 I != E; ++I)
693 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000694
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000695 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000696 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000697 if (!ClsMap.count((*I)->getSelector()))
698 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000699
700 // Check the protocol list for unimplemented methods in the @implementation
701 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000702 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
703 E = CatClassDecl->protocol_end(); PI != E; ++PI)
704 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000705 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000706}
707
708/// ActOnForwardClassDeclaration -
709Action::DeclTy *
710Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
711 IdentifierInfo **IdentList, unsigned NumElts)
712{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000713 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000714
715 for (unsigned i = 0; i != NumElts; ++i) {
716 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000717 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000718 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000719 // GCC apparently allows the following idiom:
720 //
721 // typedef NSObject < XCElementTogglerP > XCElementToggler;
722 // @class XCElementToggler;
723 //
724 // FIXME: Make an extension?
725 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
726 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000727 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000728 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000729 }
Chris Lattner4d391482007-12-12 07:09:47 +0000730 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000731 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000732 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattnerb752f282008-07-21 07:06:49 +0000733 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000734 SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000735 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000736
737 // Remember that this needs to be removed when the scope is popped.
738 TUScope->AddDecl(IDecl);
739 }
740
741 Interfaces.push_back(IDecl);
742 }
743
Anders Carlsson15281452008-11-04 16:57:32 +0000744 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
745 &Interfaces[0],
746 Interfaces.size());
747
748 CheckObjCDeclScope(CDecl);
749 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000750}
751
752
753/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
754/// returns true, or false, accordingly.
755/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000756bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000757 const ObjCMethodDecl *PrevMethod,
758 bool matchBasedOnSizeAndAlignment) {
759 QualType T1 = Context.getCanonicalType(Method->getResultType());
760 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
761
762 if (T1 != T2) {
763 // The result types are different.
764 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000765 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000766 // Incomplete types don't have a size and alignment.
767 if (T1->isIncompleteType() || T2->isIncompleteType())
768 return false;
769 // Check is based on size and alignment.
770 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
771 return false;
772 }
773 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
774 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
775 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
776 if (T1 != T2) {
777 // The result types are different.
778 if (!matchBasedOnSizeAndAlignment)
779 return false;
780 // Incomplete types don't have a size and alignment.
781 if (T1->isIncompleteType() || T2->isIncompleteType())
782 return false;
783 // Check is based on size and alignment.
784 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
785 return false;
786 }
Chris Lattner4d391482007-12-12 07:09:47 +0000787 }
788 return true;
789}
790
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000791void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
792 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000793 if (!FirstMethod.Method) {
794 // Haven't seen a method with this selector name yet - add it.
795 FirstMethod.Method = Method;
796 FirstMethod.Next = 0;
797 } else {
798 // We've seen a method with this name, now check the type signature(s).
799 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
800
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000801 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000802 Next = Next->Next)
803 match = MatchTwoMethodDeclarations(Method, Next->Method);
804
805 if (!match) {
806 // We have a new signature for an existing method - add it.
807 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +0000808 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +0000809 }
810 }
811}
812
Steve Naroff6f5f41c2008-10-21 10:50:19 +0000813// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +0000814ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
815 SourceRange R) {
816 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000817 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +0000818
819 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000820 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
821 // This checks if the methods differ by size & alignment.
822 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
823 issueWarning = true;
824 }
825 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000826 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +0000827 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000828 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +0000829 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +0000830 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000831 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +0000832 }
833 return MethList.Method;
834}
835
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000836void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
837 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000838 if (!FirstMethod.Method) {
839 // Haven't seen a method with this selector name yet - add it.
840 FirstMethod.Method = Method;
841 FirstMethod.Next = 0;
842 } else {
843 // We've seen a method with this name, now check the type signature(s).
844 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
845
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000846 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000847 Next = Next->Next)
848 match = MatchTwoMethodDeclarations(Method, Next->Method);
849
850 if (!match) {
851 // We have a new signature for an existing method - add it.
852 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000853 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000854 FirstMethod.Next = OMI;
855 }
856 }
857}
858
Steve Naroffa56f6162007-12-18 01:30:32 +0000859// Note: For class/category implemenations, allMethods/allProperties is
860// always null.
Chris Lattner4d391482007-12-12 07:09:47 +0000861void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
862 DeclTy **allMethods, unsigned allNum,
863 DeclTy **allProperties, unsigned pNum) {
864 Decl *ClassDecl = static_cast<Decl *>(classDecl);
865
Steve Naroffa56f6162007-12-18 01:30:32 +0000866 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
867 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +0000868 // should be true.
869 if (!ClassDecl)
870 return;
871
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000872 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
873 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +0000874
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000875 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
876 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +0000877
878 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000879 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
880 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000881 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000882
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000883 if (pNum != 0) {
Chris Lattner55d13b42008-03-16 21:23:50 +0000884 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
885 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000886 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
887 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
888 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000889 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000890 else
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000891 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000892 }
Chris Lattner4d391482007-12-12 07:09:47 +0000893
894 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000895 ObjCMethodDecl *Method =
896 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +0000897
898 if (!Method) continue; // Already issued a diagnostic.
899 if (Method->isInstance()) {
900 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000901 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000902 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
903 : false;
904 if (isInterfaceDeclKind && PrevMethod && !match
905 || checkIdenticalMethods && match) {
Chris Lattner5f4a6822008-11-23 23:12:31 +0000906 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000907 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000908 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000909 } else {
910 insMethods.push_back(Method);
911 InsMap[Method->getSelector()] = Method;
912 /// The following allows us to typecheck messages to "id".
913 AddInstanceMethodToGlobalPool(Method);
914 }
915 }
916 else {
917 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000919 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
920 : false;
921 if (isInterfaceDeclKind && PrevMethod && !match
922 || checkIdenticalMethods && match) {
Chris Lattner5f4a6822008-11-23 23:12:31 +0000923 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000924 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000925 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000926 } else {
927 clsMethods.push_back(Method);
928 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +0000929 /// The following allows us to typecheck messages to "Class".
930 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +0000931 }
932 }
933 }
Steve Naroff5e0a74f2008-09-29 14:20:56 +0000934 // Save the size so we can detect if we've added any property methods.
935 unsigned int insMethodsSizePriorToPropAdds = insMethods.size();
936 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size();
Chris Lattner4d391482007-12-12 07:09:47 +0000937
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000938 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000939 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000940 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000941 ComparePropertiesInBaseAndSuper(I);
942 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000943 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
944 e = I->classprop_end(); i != e; ++i)
945 I->addPropertyMethods(Context, *i, insMethods);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000946 I->addMethods(&insMethods[0], insMethods.size(),
947 &clsMethods[0], clsMethods.size(), AtEndLoc);
948
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000949 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000950 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
951 e = P->classprop_end(); i != e; ++i)
952 P->addPropertyMethods(Context, *i, insMethods);
Chris Lattner4d391482007-12-12 07:09:47 +0000953 P->addMethods(&insMethods[0], insMethods.size(),
954 &clsMethods[0], clsMethods.size(), AtEndLoc);
955 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000956 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000957 // FIXME: Need to compare properties to those in interface?
958
959 // FIXME: If we merge properties into class we should probably
960 // merge them into category as well?
961 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
962 e = C->classprop_end(); i != e; ++i)
963 C->addPropertyMethods(Context, *i, insMethods);
Chris Lattner4d391482007-12-12 07:09:47 +0000964 C->addMethods(&insMethods[0], insMethods.size(),
965 &clsMethods[0], clsMethods.size(), AtEndLoc);
966 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000967 else if (ObjCImplementationDecl *IC =
968 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000969 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000970 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +0000971 ImplMethodsVsClassMethods(IC, IDecl);
972 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000973 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000974 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000975 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000976 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000977 // in this interface are implemented in the category @implementation.
Chris Lattner4d391482007-12-12 07:09:47 +0000978 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000979 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +0000980 Categories; Categories = Categories->getNextClassCategory()) {
981 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
982 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
983 break;
984 }
985 }
986 }
987 }
Steve Naroff5e0a74f2008-09-29 14:20:56 +0000988 // Add any synthesized methods to the global pool. This allows us to
989 // handle the following, which is supported by GCC (and part of the design).
990 //
991 // @interface Foo
992 // @property double bar;
993 // @end
994 //
995 // void thisIsUnfortunate() {
996 // id foo;
997 // double bar = [foo bar];
998 // }
999 //
1000 if (insMethodsSizePriorToPropAdds < insMethods.size())
1001 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++)
1002 AddInstanceMethodToGlobalPool(insMethods[i]);
1003 if (clsMethodsSizePriorToPropAdds < clsMethods.size())
1004 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++)
1005 AddFactoryMethodToGlobalPool(clsMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001006}
1007
1008
1009/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1010/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001011static Decl::ObjCDeclQualifier
1012CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1013 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1014 if (PQTVal & ObjCDeclSpec::DQ_In)
1015 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1016 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1017 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1018 if (PQTVal & ObjCDeclSpec::DQ_Out)
1019 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1020 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1021 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1022 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1023 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1024 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1025 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001026
1027 return ret;
1028}
1029
1030Sema::DeclTy *Sema::ActOnMethodDeclaration(
1031 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001032 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001033 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001034 Selector Sel,
1035 // optional arguments. The number of types/arguments is obtained
1036 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001037 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +00001038 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1039 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001040 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001041
1042 // Make sure we can establish a context for the method.
1043 if (!ClassDecl) {
1044 Diag(MethodLoc, diag::error_missing_method_context);
1045 return 0;
1046 }
Chris Lattner4d391482007-12-12 07:09:47 +00001047 QualType resultDeclType;
1048
1049 if (ReturnType)
1050 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1051 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001052 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001053
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001054 ObjCMethodDecl* ObjCMethod =
1055 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbarf6414922008-08-20 18:02:42 +00001056 ClassDecl,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001057 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001058 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001059 MethodDeclKind == tok::objc_optional ?
1060 ObjCMethodDecl::Optional :
1061 ObjCMethodDecl::Required);
1062
Chris Lattner0ed844b2008-04-04 06:12:32 +00001063 llvm::SmallVector<ParmVarDecl*, 16> Params;
1064
1065 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1066 // FIXME: arg->AttrList must be stored too!
1067 QualType argType;
1068
1069 if (ArgTypes[i])
1070 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1071 else
1072 argType = Context.getObjCIdType();
1073 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1074 SourceLocation(/*FIXME*/),
1075 ArgNames[i], argType,
Chris Lattner04421082008-04-08 04:40:51 +00001076 VarDecl::None, 0, 0);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001077 Param->setObjCDeclQualifier(
1078 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1079 Params.push_back(Param);
1080 }
1081
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001082 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1083 ObjCMethod->setObjCDeclQualifier(
1084 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1085 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001086
1087 if (AttrList)
1088 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001089
1090 // For implementations (which can be very "coarse grain"), we add the
1091 // method now. This allows the AST to implement lookup methods that work
1092 // incrementally (without waiting until we parse the @end). It also allows
1093 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001094 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001095 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001096 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001097 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001098 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001099 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001100 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001101 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001102 }
1103 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001104 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001105 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001106 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001107 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001108 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001109 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001110 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001111 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001112 }
1113 }
1114 if (PrevMethod) {
1115 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001116 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001117 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001118 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001119 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001120 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001121}
1122
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001123void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1124 SourceLocation Loc,
1125 unsigned &Attributes) {
1126 // FIXME: Improve the reported location.
1127
1128 // readonly and readwrite conflict.
1129 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1130 (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001131 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1132 << "readonly" << "readwrite";
1133 Attributes &= ~ObjCDeclSpec::DQ_PR_readonly;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001134 }
1135
1136 // Check for copy or retain on non-object types.
1137 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1138 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001139 Diag(Loc, diag::err_objc_property_requires_object)
1140 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001141 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1142 }
1143
1144 // Check for more than one of { assign, copy, retain }.
1145 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1146 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001147 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1148 << "assign" << "copy";
1149 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001150 }
1151 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001152 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1153 << "assign" << "retain";
1154 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001155 }
1156 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1157 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001158 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1159 << "copy" << "retain";
1160 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001161 }
1162 }
1163
1164 // Warn if user supplied no assignment attribute, property is
1165 // readwrite, and this is an object type.
1166 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1167 ObjCDeclSpec::DQ_PR_retain)) &&
1168 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1169 Context.isObjCObjectPointerType(PropertyTy)) {
1170 // Skip this warning in gc-only mode.
1171 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1172 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1173
1174 // If non-gc code warn that this is likely inappropriate.
1175 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1176 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1177
1178 // FIXME: Implement warning dependent on NSCopying being
1179 // implemented. See also:
1180 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1181 // (please trim this list while you are at it).
1182 }
1183}
1184
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001185Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1186 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001187 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001188 Selector GetterSel,
1189 Selector SetterSel,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001190 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001191 QualType T = GetTypeForDeclarator(FD.D, S);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001192 unsigned Attributes = ODS.getPropertyAttributes();
1193
1194 // May modify Attributes.
1195 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
1196
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001197 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1198 FD.D.getIdentifier(), T);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001199 // Regardless of setter/getter attribute, we save the default getter/setter
1200 // selector names in anticipation of declaration of setter/getter methods.
1201 PDecl->setGetterName(GetterSel);
1202 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001203
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001204 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001205 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001206
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001207 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001208 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001209
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001210 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001211 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001212
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001213 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001214 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner4d391482007-12-12 07:09:47 +00001215
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001216 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001217 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001218
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001219 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001220 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001221
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001222 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001223 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001224
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001225 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001226 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001227
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001228 if (MethodImplKind == tok::objc_required)
1229 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1230 else if (MethodImplKind == tok::objc_optional)
1231 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1232
Chris Lattner4d391482007-12-12 07:09:47 +00001233 return PDecl;
1234}
1235
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001236/// ActOnPropertyImplDecl - This routine performs semantic checks and
1237/// builds the AST node for a property implementation declaration; declared
1238/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001239///
1240Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1241 SourceLocation PropertyLoc,
1242 bool Synthesize,
1243 DeclTy *ClassCatImpDecl,
1244 IdentifierInfo *PropertyId,
1245 IdentifierInfo *PropertyIvar) {
1246 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1247 // Make sure we have a context for the property implementation declaration.
1248 if (!ClassImpDecl) {
1249 Diag(AtLoc, diag::error_missing_property_context);
1250 return 0;
1251 }
1252 ObjCPropertyDecl *property = 0;
1253 ObjCInterfaceDecl* IDecl = 0;
1254 // Find the class or category class where this property must have
1255 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001256 ObjCImplementationDecl *IC = 0;
1257 ObjCCategoryImplDecl* CatImplClass = 0;
1258 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001259 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001260 // We always synthesize an interface for an implementation
1261 // without an interface decl. So, IDecl is always non-zero.
1262 assert(IDecl &&
1263 "ActOnPropertyImplDecl - @implementation without @interface");
1264
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001265 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001266 property = IDecl->FindPropertyDeclaration(PropertyId);
1267 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001268 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001269 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001270 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001271 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001272 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001273 if (Synthesize) {
1274 Diag(AtLoc, diag::error_synthesize_category_decl);
1275 return 0;
1276 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001277 IDecl = CatImplClass->getClassInterface();
1278 if (!IDecl) {
1279 Diag(AtLoc, diag::error_missing_property_interface);
1280 return 0;
1281 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001282 ObjCCategoryDecl *Category =
1283 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1284
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001285 // If category for this implementation not found, it is an error which
1286 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001287 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001288 return 0;
1289 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001290 property = Category->FindPropertyDeclaration(PropertyId);
1291 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001292 Diag(PropertyLoc, diag::error_bad_category_property_decl)
1293 << Category->getName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001294 return 0;
1295 }
1296 }
1297 else {
1298 Diag(AtLoc, diag::error_bad_property_context);
1299 return 0;
1300 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001301 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001302 // Check that we have a valid, previously declared ivar for @synthesize
1303 if (Synthesize) {
1304 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001305 if (!PropertyIvar)
1306 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001307 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001308 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001309 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001310 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001311 return 0;
1312 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001313 QualType PropType = Context.getCanonicalType(property->getType());
1314 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1315
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001316 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001317 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001318 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001319 Diag(PropertyLoc, diag::error_property_ivar_type)
1320 << property->getName() << Ivar->getName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001321 return 0;
1322 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001323 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001324 } else if (PropertyIvar) {
1325 // @dynamic
1326 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1327 return 0;
1328 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001329 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001330 ObjCPropertyImplDecl *PIDecl =
1331 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1332 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001333 ObjCPropertyImplDecl::Synthesize
1334 : ObjCPropertyImplDecl::Dynamic),
1335 Ivar);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001336 if (IC)
1337 IC->addPropertyImplementation(PIDecl);
1338 else
1339 CatImplClass->addPropertyImplementation(PIDecl);
1340
1341 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001342}
Anders Carlsson15281452008-11-04 16:57:32 +00001343
1344bool Sema::CheckObjCDeclScope(Decl *D)
1345{
1346 if (isa<TranslationUnitDecl>(CurContext))
1347 return false;
1348
1349 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1350 D->setInvalidDecl();
1351
1352 return true;
1353}